运行时错误:mat1 和 mat2 形状无法相乘(200x16 和 32x1)

发布于 2025-01-15 02:00:09 字数 2457 浏览 0 评论 0原文

我想连接两个全连接层。然后,在连接它们之后,我想构建另一个具有全连接层的神经网络。
我可以看到该错误是由于未正确设置 cat_x = torch.cat([x, x1]) 引起的。但是,我不知道如何解决这个问题。

import torch
from torch import nn, optim
import numpy as np
from matplotlib import pyplot as plt
 
class Regression(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear1 = nn.Linear(2, 32)
        self.linear2 = nn.Linear(32, 16)
        self.linear3 = nn.Linear(32, 1)
 
    def forward(self, input):
        x = nn.functional.relu(self.linear1(input))
        x = nn.functional.relu(self.linear2(x))

        x1 = nn.functional.elu(self.linear1(input))
        x1 = nn.functional.elu(self.linear2(x1))

        cat_x = torch.cat([x, x1])

        cat_x = self.linear3(cat_x)

        return cat_x
 
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
 
def test(model, x):
    y_pred = model(x).data.numpy().T[0]  # 予測
    return y_pred

x = np.random.uniform(0, 10, 100)                                   # x軸をランダムで作成
y = np.random.uniform(0.9, 1.1, 100) * np.sin(2 * np.pi * 0.1 * x)  # 正弦波を作成
x = torch.from_numpy(x.astype(np.float32)).float()                  # xをテンソルに変換
y = torch.from_numpy(y.astype(np.float32)).float()                  # yをテンソルに変換
X = torch.stack([torch.ones(100), x], 1)                            # xに切片用の定数1配列を結合
 
net = Regression()
 
 
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)

错误信息

/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 (200x16 and 32x1)

I would like to connect two Fully Connected layers. Then, after connecting them, I want to build another neural net with the Fully Connected layer.
I can see that the error is caused by not setting cat_x = torch.cat([x, x1]) properly. However, I do not know how to solve this problem.

import torch
from torch import nn, optim
import numpy as np
from matplotlib import pyplot as plt
 
class Regression(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear1 = nn.Linear(2, 32)
        self.linear2 = nn.Linear(32, 16)
        self.linear3 = nn.Linear(32, 1)
 
    def forward(self, input):
        x = nn.functional.relu(self.linear1(input))
        x = nn.functional.relu(self.linear2(x))

        x1 = nn.functional.elu(self.linear1(input))
        x1 = nn.functional.elu(self.linear2(x1))

        cat_x = torch.cat([x, x1])

        cat_x = self.linear3(cat_x)

        return cat_x
 
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
 
def test(model, x):
    y_pred = model(x).data.numpy().T[0]  # 予測
    return y_pred

x = np.random.uniform(0, 10, 100)                                   # x軸をランダムで作成
y = np.random.uniform(0.9, 1.1, 100) * np.sin(2 * np.pi * 0.1 * x)  # 正弦波を作成
x = torch.from_numpy(x.astype(np.float32)).float()                  # xをテンソルに変換
y = torch.from_numpy(y.astype(np.float32)).float()                  # yをテンソルに変換
X = torch.stack([torch.ones(100), x], 1)                            # xに切片用の定数1配列を結合
 
net = Regression()
 
 
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)

error message

/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 (200x16 and 32x1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巨坚强 2025-01-22 02:00:09

由于 xx1 的维度均为 (100,16),因此 torch.cat 运算符在第一维度(因为它们在该方向上的大小相似)。为了使您的代码正常工作,请将 cat_x = torch.cat([x, x1]) 更改为 cat_x = torch.cat([x, x1], dim=1)

since both x and x1 have dimensions of (100,16), the torch.cat operator concatenates in the 1st dimension (since they are of similar size in that direction). For your code to work change the cat_x = torch.cat([x, x1]) to cat_x = torch.cat([x, x1], dim=1)

烙印 2025-01-22 02:00:09

尝试:

cat_x = torch.cat([x, x1], dim=1)

Try:

cat_x = torch.cat([x, x1], dim=1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文