- 收藏
- 点赞
- 分享
- 举报
PyTorch---感知机 损失函数计算的数值一直没变化
在训练过程中,损失函数的值一直没有变化,这是为啥啊?恳请大佬指点一下问题出在哪儿
pyTorch —-> 感知机
激活函数为 sign函数
import torch
import pandas
from torch import nn, optim
from torch.autograd import Variable
def get_data(path): # 将 csv 中的 每一条数据,放进一个列表中 —>每个列表为一个样本点 , y单独 放进一个列表中
data_x1 = pandas.read_csv(path)[‘x1’] # 特征 x1
data_x2 = pandas.read_csv(path)[‘x2’] # 特征 x2
target = pandas.read_csv(path)[‘y’] # 所属分类
target = list(target)
data_train = []
for i in range(len(target)):
if target[i] == 0:
target[i] = -1
for i in range(len(data_x1)):
temp_data = []
temp_data.append(data_x1[i])
temp_data.append(data_x2[i])
data_train.append(temp_data)
return data_train, target
def trans_list_variable(data_train, target): # list 转成 Tensor 再转成 Variable
# 列表转成Tensor
data_train = torch.Tensor(data_train)
target = [target]
target = torch.Tensor(target)
target =target.T
# Tensor 转成 Variable
data_train = Variable(data_train, requires_grad=True)
target = Variable(target, requires_grad=True)
return data_train, target
class LineRegression(nn.Module):
def init(self):
super(LineRegression, self).init()
self.linear_function = nn.Linear(2, 1)
def forward(self, data_train):
result = []
for i in data_train:
out = self.linear_function(i)
out = torch.sign(out)
result.append(out)
# 将列表转换成 Variable
result = [result]
result = torch.Tensor(result)
result = result.T
result = Variable(result)
return result
def train(data_train, target):
model = LineRegression()
loss_Model = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.1)
epoch = 40000
for i in range(epoch): # 训练40000次
result = model(data_train)
loss_function = loss_Model(result, target)
optimizer.zero_grad()
loss_function.backward()
optimizer.step()
if (i+1) % 200 == 0:
out = loss_function.item()
print(out)
if name == ‘main‘:
path = r’C:\Users\sel\Desktop\ML_data.csv’
data_train, target = get_data(path) # 获得数据
data_train, target = trans_list_variable(data_train, target)
train(data_train, target)
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片![alt](url)
-
2020-11-21 17:18:24
-
2017-01-16 21:28:27
-
2017-05-02 15:22:27
-
2019-10-31 20:30:23
-
2016-06-23 15:39:50
-
2020-09-30 10:54:39
-
2020-11-06 10:18:11
-
2018-11-15 10:15:37
-
2020-11-18 10:16:01
-
2020-10-07 17:59:14
-
2020-03-11 10:20:37
-
2018-03-08 12:28:43
-
2014-12-16 21:27:47
-
2023-08-28 17:05:04
-
2020-08-18 13:39:57
-
2020-10-05 19:12:15
-
2018-11-05 09:30:26
-
2019-01-03 13:50:00
-
2018-10-29 15:50:59
-
50如何获取vpss chn的图像修改后发送至vo
-
5FPGA通过Bt1120传YUV422数据过来,vi接收不到数据——3516dv500
-
50SS928 运行PQtools 拼接 推到设备里有一半画面会异常
-
53536AV100的sample_vdec输出到CVBS显示
-
10海思板子mpp怎么在vi阶段改变视频数据尺寸
-
10HI3559AV100 多摄像头同步模式
-
9海思ss928单路摄像头vio中加入opencv处理并显示
-
10EB-RV1126-BC-191板子运行自己编码的程序
-
10求HI3519DV500_SDK_V2.0.1.1
-
5有偿求HI3516DV500 + OV5647驱动
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明