如何简单地生成大量全连接层?

我想创建640个全连接层。

(在def init中)

self.fc0 = nn.Linear(120, M)self.fc1 = nn.Linear(120, M).....self.fc638 = nn.Linear(120, M)self.fc639 = nn.Linear(120, M)

(在def forward中)

x[:,:,0,:] = self.fc0(x[:,:,0,:])x[:,:,1,:] = self.fc0(x[:,:,1,:]).......x[:,:,639,:] = self.fc639(x[:,:,639,:])

如何以更简单的方式执行上述代码?


回答:

使用容器

class MultipleFC(nn.Module):  def __init__(self, num_layers, M):    self.layers = nn.ModuleList([nn.Linear(120, M) for _ in range(num_layers)])  def forward(self, x):    y = torch.empty_like(x)  # up to third dim should be M    for i, fc in enumerate(self.layers):      y[..., i, :] = fc(x[..., i, :])    return y

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中创建了一个多类分类项目。该项目可以对…

发表回复

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