第一次接触pytorch,本贴仅记录学习过程,侵删
在B站看完了视频的P7 07.处理多维特征的输入。 附上视频地址:《PyTorch深度学习实践》完结合集_07. 处理多维特征的输入
先记录一些笔记。
Multiple Dimension Logistic Regression Model: Mini-Batch (N samples):
import torch
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear = torch.nn.Linear(8, 1)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
x = self.sigmoid(self.linear(x))
return x
model = Model()
矩阵是一种空间变换的函数,是从N维空间映射到M维空间的线性变换。
Example:Diabetes Prediction
import numpy as np
import torch
xy = np.loadtxt('diabetes.csv.gz', delimiter=',', dtype=np.float32)
x_data = torch.from_numpy((xy[:, :-1]))
y_data = torch.from_numpy(xy[:, [-1]])
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear1 = torch.nn.Linear(8, 6)
self.linear2 = torch.nn.Linear(6, 4)
self.linear3 = torch.nn.Linear(4, 1)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
x = self.sigmoid(self.linear1(x))
x = self.sigmoid(self.linear2(x))
x = self.sigmoid(self.linear3(x))
return x
model = Model()
criterion = torch.nn.BCELoss(size_average=True)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
for epoch in range(1000):
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(epoch, loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
使用不同的激活函数: 其中,ReLU并不是连续的。
可以在此链接出看到相应激活函数的具体内容:Visualising Activation Functions in Neural Networks
Pytorch激活函数文档:Non-linear Activations (weighted sum, nonlinearity)
当我们尝试不同的激活函数时,我们只需修改一小部分内容: 特别的,如果我们设置的激活函数是ReLU,由于它的取值范围是在(0,1),最后一层输出的值如果是小于0的话,那么在ReLU作用后,输出会为0,如果后面我们需要算ln0的话,就会出现问题。这个时候我们就可以把最后一层的激活函数改成sigmoid,这样结果就会得到0-1之间比较光滑的概率输出。
import numpy as np
import torch
xy = np.loadtxt('diabetes.csv.gz', delimiter=',', dtype=np.float32)
x_data = torch.from_numpy((xy[:, :-1]))
y_data = torch.from_numpy(xy[:, [-1]])
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear1 = torch.nn.Linear(8, 6)
self.linear2 = torch.nn.Linear(6, 4)
self.linear3 = torch.nn.Linear(4, 1)
self.activate = torch.nn.ReLU()
def forward(self, x):
x = self.activate(self.linear1(x))
x = self.activate(self.linear2(x))
x = torch.sigmoid(self.linear3(x))
return x
model = Model()
criterion = torch.nn.BCELoss(size_average=True)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
for epoch in range(1000):
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(epoch, loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
|