简体   繁体   中英

Neural networks do not work well in pytorch

I am trying to build a neural network with two inputs and one output in pytorch.
However, I get an error and cannot get it to work.

python code is below.

import torch
import numpy as np
import os
import pandas as pd
import glob
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.linear0 = nn.Linear(2, 256)
        self.linear1 = nn.Linear(256, 128)
        self.linear2 = nn.Linear(128, 64)
        self.linear3 = nn.Linear(64, 32)
        self.linear4 = nn.Linear(32, 16)
        self.linear5 = nn.Linear(16, 8)
        self.linear6 = nn.Linear(8, 4)
        # self.linear7 = nn.Linear(4, 1)

    def forward(self, x):
        x = self.linear0(x)
        x = torch.sigmoid(x)
        x = self.linear1(x)
        x = torch.sigmoid(x)
        x = self.linear2(x)
        x = torch.sigmoid(x)
        x = self.linear3(x)
        x = torch.sigmoid(x)
        x = self.linear4(x)
        x = torch.sigmoid(x)
        x = self.linear5(x)
        x = torch.sigmoid(x)
        x = self.linear6(x)
        # x = torch.sigmoid(x)
        # x = self.linear7
        return F.log_softmax(x, dim=1)
net = Model()

x = torch.tensor(a[0].values)
y = torch.tensor(a[1].values)

def train(model, optimizer, E, iteration, x, y):
    losses = []
    for i in range(iteration):
        optimizer.zero_grad()                   # 勾配情報を0に初期化
        y_pred = model(x)                       # 予測
        loss = E(y_pred.reshape(y.shape), y)    # 損失を計算(shapeを揃える)
        loss.backward()                         # 勾配の計算
        optimizer.step()                        # 勾配の更新
        losses.append(loss.item())              # 損失値の蓄積
        print('epoch=', i+1, 'loss=', loss)
    return model, losses

optimizer = optim.RMSprop(net.parameters(), lr=0.01)                # 最適化にRMSpropを設定
E = nn.MSELoss()   

net, losses = train(model=net, optimizer=optimizer, E=E, iteration=5000, x=x, y=y)
y_pred = test(net, X_test)

input data is 2Dimention. like this ↓

在此处输入图像描述

output data is 1Dimention.

The error is as follows.

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias) 1846 if has_torch_function_variadic(input, weight, bias): 1847 return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias) -> 1848 return torch._C._nn.linear(input, weight, bias) 1849 1850

RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x100 and 2x256)

What should I do?

You're getting an error at the first layer of your neural network because there is a dimension mismatch. The weights are shape (2,256), so it expects an input of shape (N,2).

It looks like you provide 100 training examples, so N=100, but your input is shape (100,1) instead of (100,2). In your code, it looks like a is (100,2), but x = a[0] is (100,1).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM