pytorch训练模型无法创建成功

The pytorch training model cannot be created successfully

我想基于this site使用optuna做一个回归分析的神经网络。 我想在批量学习中创建一个具有两个一维数据作为输入和一个一维数据作为输出的模型。

x是训练数据,y是教师数据。

class Model(nn.Module):
    # コンストラクタ(インスタンス生成時の初期化)
    def __init__(self,trial, mid_units1, mid_units2):
        super(Model, self).__init__()
        self.linear1 = nn.Linear(2, mid_units1)
        self.bn1 = nn.BatchNorm1d(mid_units1)
        self.linear2 = nn.Linear(mid_units1, mid_units2)
        self.bn2 = nn.BatchNorm1d(mid_units2)
        self.linear3 = nn.Linear(mid_units2, 1)
        self.activation = trial_activation(trial)

    def forward(self, x):
        x = self.linear1(x)
        x = self.bn1(x)
        x = self.activation(x)
        x = self.linear2(x)

device = "cuda" if torch.cuda.is_available() else "cpu"

EPOCH = 100
x = torch.from_numpy(a[0].astype(np.float32)).to(device)
y = torch.from_numpy(a[1].astype(np.float32)).to(device)

def train_epoch(model, optimizer, criterion):
    model.train()
    optimizer.zero_grad()    # 勾配情報を0に初期化
    y_pred = model(x)                                               # 予測
    loss = criterion(y_pred.reshape(y.shape), y)          # 損失を計算(shapeを揃える)
    loss.backward()                                                       # 勾配の計算
    optimizer.step()                                                      # 勾配の更新
    return loss.item()

def trial_activation(trial):
    activation_names = ['ReLU','logsigmoid']
    activation_name = trial.suggest_categorical('activation', activation_names)
    if activation_name == activation_names[0]:
        activation = F.relu
    else:
        activation = F.logsigmoid
    return activation

def objective(trial):
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # 中間層のユニット数の試行
    mid_units1 = int(trial.suggest_discrete_uniform("mid_units1", 1024*2,1024*4, 64*2))
    mid_units2 = int(trial.suggest_discrete_uniform("mid_units2", 1024, 1024*2, 64*2))

    net = Model(trial, mid_units1, mid_units2).to(device)

    criterion = nn.MSELoss() 
    # 最適化手法の試行
    optimizer = trial_optimizer(trial, net)
    train_loss = 0
    for epoch in range(EPOCH):
        train_loss = train_epoch(net, optimizer, criterion, device)
    torch.save(net.state_dict(), str(trial.number) + "new1.pth")
    return train_loss

strage_name = "a.sql"
study_name = 'a'

study = optuna.create_study(
    study_name = study_name,
    storage='sqlite:///'  + strage_name, 
    load_if_exists=True,
    direction='minimize')
TRIAL_SIZE = 100

study.optimize(objective, n_trials=TRIAL_SIZE)

错误信息

---> 28     loss = criterion(y_pred.reshape(y.shape), y)          # 損失を計算(shapeを揃える)
     29     loss.backward()                                                       # 勾配の計算
     30     optimizer.step()                                                      # 勾配の更新

AttributeError: 'NoneType' object has no attribute 'reshape'

因为上面的错误,我查看了y_pred的值,发现是None.

    model.train()
    optimizer.zero_grad()

我在想这两行可能是错误的,但我不知道如何解决这个问题。

使用 PyTorch,当您调用 y_pred = model(x) 时,它将调用 forward 中定义的函数 Model class.

因此,y_pred 将获得 forward 函数的结果,在您的情况下,它 returns 什么都没有,这就是您获得 None 值的原因。您可以如下更改 forward 函数:

    def forward(self, x):
        x = self.linear1(x)
        x = self.bn1(x)
        x = self.activation(x)
        x = self.linear2(x)
        return x