neural-network - Pytorch Forward Pass 每次都改变?

标签 neural-network pytorch

我正在学习 pytorch 并运行一个玩具回归问题。令我困惑的是,每次我在模型中运行张量时,预测都会发生变化。显然情况并非如此,但我错过了什么?

Pytorch 版本:0.4.0

我在没有 GPU 的情况下在这里运行以消除这个潜在问题。

代码:

from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import torch
import torch.utils.data as utils_data
from torch.autograd import Variable
from torch import optim, nn
from torch.utils.data import Dataset 
import torch.nn.functional as F
from torch.nn.init import xavier_uniform_, xavier_normal_,uniform_
from sklearn.datasets import load_boston
from sklearn.metrics import mean_squared_error

cuda=False #set to true uses GPU


#load boston data from scikit
boston = load_boston()
x=boston.data
y=boston.target
y=y.reshape(y.shape[0],1)

#change to tensors
x = torch.from_numpy(x)
y = torch.from_numpy(y)

#create dataset and use data loader
training_samples = utils_data.TensorDataset(x, y)
data_loader = utils_data.DataLoader(training_samples, batch_size=64)

#simple model
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()

        #all the layers
        self.fc1   = nn.Linear(x.shape[1], 20)
        xavier_uniform_(self.fc1.weight.data) #this is how you can change the weight init
        self.drop = nn.Dropout(p=0.5)
        self.fc2   = nn.Linear(20, 1)


    def forward(self, x):
        x = F.relu(self.fc1(x))
        x=  self.drop(x)
        x = self.fc2(x)
        return x

net=Net()

if cuda:
    net.cuda()

# create a stochastic gradient descent optimizer
optimizer = optim.Adam(net.parameters())
# create a loss function (mse)
loss = nn.MSELoss(size_average=True)

# run the main training loop
epochs =50
hold_loss=[]

for epoch in range(epochs):
    cum_loss=0.
    for batch_idx, (data, target) in enumerate(data_loader):
        tr_x, tr_y = data.float(), target.float()
        if cuda:
            tr_x, tr_y = tr_x.cuda(), tr_y.cuda() 

        # Reset gradient
        optimizer.zero_grad()

        # Forward pass
        fx = net(tr_x)
        output = loss(fx, tr_y) #loss for this batch

        cum_loss += output.item() #accumulate the loss

        # Backward 
        output.backward()

        # Update parameters based on backprop
        optimizer.step()
    hold_loss.append(cum_loss/len(training_samples))  

#training loss
plt.plot(np.array(hold_loss))

这部分,如果re-ran每次都会返回不同的预测,实际不会改变所以数据的顺序不会改变!

#score the training set
for batch_idx, (data, target) in enumerate(data_loader):
        tr_x, tr_y = data.float(), target.float()
        if batch_idx ==0:
           hold_pred=net(tr_x).data.numpy()
           hold_actual=tr_y.data.numpy().reshape(tr_y.data.numpy().shape[0],1)
        else:
            hold_pred =np.row_stack([hold_pred,net(tr_x).data.numpy()])
hold_actual=np.row_stack([hold_actual,tr_y.data.numpy().reshape(tr_y.data.numpy().shape[0],1)])

#view the first few predictions      
print(hold_pred[0:10]) 
print(hold_actual[0:10]) 

最佳答案

您的网络有一个 Dropout 层,它的目的是随机采样(此处以 p=0.5 的概率)它在训练期间接收到的数据( net.train() 在推理之前设置)。见 doc了解更多信息(用途、目的)。

这个层可以在测试期间短路(net.eval()在推理之前设置)。

关于neural-network - Pytorch Forward Pass 每次都改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50233272/

相关文章:

machine-learning - 每个神经元的神经网络偏差

python - 使用 nnet_ts 模块中的 TimeSeriesNnet() 方法会抛出 NameError

machine-learning - Keras:为什么观察到的批量大小与指定的批量大小不匹配?

pytorch - 使用位掩码将数据从一个张量复制到另一个张量

python - 计算D维空间中圆上2点之间的欧式距离

python - torch.argmax() 无法在包含数据的张量中找到最大值

python-3.x - Bert 预训练模型每次都会给出随机输出

python - 使用 Keras 进行实时训练和预测

pytorch - 如何在训练 CNN 时辨别哪个图像生成了特定的特征图?

machine-learning - Tflearn 使用没有 softmax 输出层的神经网络对文档进行排名