调用PyTorch神经网络的forward()函数时出现错误“mat1和mat2的形状无法相乘”

我有以下代码,定义了一个简单的多层感知机神经网络:

class MlpNN(nn.Module):    def __init__(self, in_dim=301, hidden_dims=[500,500,500,500,500,30]):        super(MlpNN, self).__init__()        self.net = nn.Sequential()        self.net.add_module("lin_0", nn.Linear(in_dim, hidden_dims[0]))        self.net.add_module("relu_0", nn.ReLU())        layer_id = 1        for hidden_dim in hidden_dims[1:]:            self.net.add_module("lin_"+str(layer_id), nn.Linear(hidden_dim, hidden_dim))            self.net.add_module("relu_"+str(layer_id), nn.ReLU())            layer_id += 1        def forward(self, x):        return self.net(x)

形成的网络结构如下:

MlpNN(  (net): Sequential(    (lin_0): Linear(in_features=301, out_features=500, bias=True)    (relu_0): ReLU()    (lin_1): Linear(in_features=500, out_features=500, bias=True)    (relu_1): ReLU()    (lin_2): Linear(in_features=500, out_features=500, bias=True)    (relu_2): ReLU()    (lin_3): Linear(in_features=500, out_features=500, bias=True)    (relu_3): ReLU()    (lin_4): Linear(in_features=500, out_features=500, bias=True)    (relu_4): ReLU()    (lin_5): Linear(in_features=30, out_features=30, bias=True)    (relu_5): ReLU()  ))

在执行forward()函数时,出现了以下错误:

mat1 and mat2 shapes cannot be multiplied (1x500 and 30x30)

我在这儿做错了什么?我知道这可能是个很基本的问题,因为我刚开始学习机器学习。


回答:

在你的网络中,lin_4层的输出特征数为500,而lin_5层的输入特征数为30。这导致了形状不匹配。要解决这个问题,你需要确保一层的输出特征数与下一层的输入特征数相同。在你的代码中,可以这样修改:

class MlpNN(nn.Module):    def __init__(self, in_dim=301, hidden_dims=[500,500,500,500,500,30]):        super(MlpNN, self).__init__()        self.net = nn.Sequential()        self.net.add_module("lin_0", nn.Linear(in_dim, hidden_dims[0]))        self.net.add_module("relu_0", nn.ReLU())        for layer_id in range(1, len(hidden_dims)):            self.net.add_module("lin_"+str(layer_id), nn.Linear(hidden_dims[layer_id-1], hidden_dims[layer_id]))            self.net.add_module("relu_"+str(layer_id), nn.ReLU())        def forward(self, x):        return self.net(x)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注