我试图用CNN编码一个自动编码器,我必须使用我自己的数据。数据以列表的形式出现,我已将其转换(或试图转换)为pytorch张量
tensor_x = torch.Tensor([np.array(v) for v in testPulses])
print(tensor_x)
my_dataset = TensorDataset(tensor_x) # create your datset
data_loader = DataLoader(my_dataset) # create your dataloader
#HERE PROCESS THE DATA
tensor_x = torch.Tensor([np.array(v) for v in testPulses])
print(tensor_x)
my_dataset = TensorDataset(tensor_x) # create your datset
data_loader = DataLoader(my_dataset) # create your dataloader
tensor([[ 0.0009, -0.0010, -0.0003, ..., 0.0033, 0.0079, 0.0030],
[-0.0020, 0.0071, 0.0093, ..., 0.0401, 0.0352, 0.0300],
[ 0.0002, -0.0031, 0.0042, ..., 0.0341, 0.0338, 0.0326],
...,
[ 0.0060, 0.0084, 0.0047, ..., 0.0380, 0.0365, 0.0334],
[-0.0047, 0.0017, -0.0065, ..., 0.0038, 0.0090, 0.0051],
[-0.0061, -0.0009, -0.0030, ..., 0.0327, 0.0336, 0.0333]])
我的自动编码器是
class Autoencoder(nn.Module):
def __init__(self):
# N, 1, 28*28
super().__init__()
self.encoder = nn.Sequential(
nn.Conv1d(1, 16, 5, stride = 10, padding = 1), # N, 16, 1639
nn.ReLU(),
nn.Conv1d(16, 32, 9, stride = 10, padding = 0), # N, 32, 164
nn.ReLU(),
nn.Conv1d(32, 64, 5, stride = 10, padding = 1), # N, 64, 17
nn.ReLU(),
nn.Conv1d(64, 128, 9, stride = 10, padding = 2), # N, 128, 2
)
# N, 64, 1, 1
self.decoder = nn.Sequential(
nn.ConvTranspose1d(128, 64, 9, stride = 10, padding = 2), # N, 64, 17
nn.ReLU(),
nn.ConvTranspose1d(64, 32, 5, stride = 10, padding = 1), # N, 32, 164
nn.ReLU(),
nn.ConvTranspose1d(32, 16, 9, stride = 10, padding = 0), # N, 16, 1639
nn.ReLU(),
nn.ConvTranspose1d(16, 1, 5, stride = 10, padding = 1), # N, 1, 16384
nn.ReLU()
)
def forward(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
在这一点上,我创建了实际的模型
model = Autoencoder()
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr = 1e-3, weight_decay = 1e-5)
然后我对其进行训练并收到以下错误
num_epochs = 10
outputs = []
dataTemp = [img for img in data_loader]
halfData = dataTemp[0:len(dataTemp)//2]
for epoch in range(num_epochs):
for img in halfData:
recon = model(img)
loss = criterion(recon, img)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(f'Epoch:{epoch+1}, Loss:{loss.item():.4f}')
outputs.append((epoch, img, recon))
for img in dataTemp:
recon = model(img)
loss = criterion(recon, img)
print(f'Final Loss:{loss.item():.4f}')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-68-78e6fd204df1> in <module>()
5 for epoch in range(num_epochs):
6 for img in halfData:
----> 7 recon = model(img)
8 loss = criterion(recon, img)
9
6 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
293 _single(0), self.dilation, self.groups)
294 return F.conv1d(input, weight, bias, self.stride,
--> 295 self.padding, self.dilation, self.groups)
296
297 def forward(self, input: Tensor) -> Tensor:
TypeError: conv1d() received an invalid combination of arguments - got (list, Parameter, Parameter, tuple, tuple, tuple, int), but expected one of:
* (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)
didn't match because some of the arguments have invalid types: (!list!, !Parameter!, !Parameter!, !tuple!, !tuple!, !tuple!, int)
* (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups)
didn't match because some of the arguments have invalid types: (!list!, !Parameter!, !Parameter!, !tuple!, !tuple!, !tuple!, int)
我完全不知道如何解决这个问题。我是pytorch的新手,所以任何帮助都会很好:)
目前没有回答
相关问题 更多 >
编程相关推荐