我正在尝试指定动态数量的层,我似乎做错了。我的问题是,当我在这里定义 100 层时,我会在前进步骤中出错。但是当我正确定义图层时它会起作用吗?下面的简化示例
class PredictFromEmbeddParaSmall(LightningModule):
def __init__(self, hyperparams={'lr': 0.0001}):
super(PredictFromEmbeddParaSmall, self).__init__()
#Input is something like tensor.size=[768*100]
self.TO_ILLUSTRATE = nn.Linear(768, 5)
self.enc_ref=[]
for i in range(100):
self.enc_red.append(nn.Linear(768, 5))
# gather the layers output sth
self.dense_simple1 = nn.Linear(5*100, 2)
self.output = nn.Sigmoid()
def forward(self, x):
# first input to enc_red
x_vecs = []
for i in range(self.para_count):
layer = self.enc_red[i]
# The first dim is the batch size here, output is correct
processed_slice = x[:, i * 768:(i + 1) * 768]
# This works and give the out of size 5
rand = self.TO_ILLUSTRATE(processed_slice)
#This will fail? Error below
ret = layer(processed_slice)
#more things happening we can ignore right now since we fail earlier
执行“ret = layer.forward(processed_slice)”时出现此错误
RuntimeError:设备类型为 cuda 的预期对象,但在调用 _th_addmm 时获得了参数 #1 'self' 的设备类型 cpu
有没有更聪明的方法来编程?或解决错误?