问题描述
def train(epoch):
model.train()
train_loss = 0
for batch_idx,(data,_) in enumerate(train_loader):
data = data[None,:,:]
print(data.size()) # something seems to change between here
data = data.to(device)
optimizer.zero_grad()
recon_batch,mu,logvar = model(data) # and here???
loss = loss_function(recon_batch,data,logvar)
loss.backward()
train_loss += loss.item()
optimizer.step()
if batch_idx % 1000 == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch,batch_idx * len(data),len(train_loader.dataset),100. * batch_idx / len(train_loader),loss.item() / len(data)))
print('====> Epoch: {} Average loss: {:.4f}'.format(epoch,train_loss / len(train_loader.dataset)))
for epoch in range(1,4):
train(epoch)
从训练循环来看,这很奇怪,它确实知道大小为[1,1,1998]
,但是在将其发送到设备后发生了变化吗?
torch.Size([1,1998])
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-138-70cca679f91a> in <module>()
27
28 for epoch in range(1,4):
---> 29 train(epoch)
5 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in forward(self,input)
255 _single(0),self.dilation,self.groups)
256 return F.conv1d(input,self.weight,self.bias,self.stride,--> 257 self.padding,self.groups)
258
259
RuntimeError: Expected 3-dimensional input for 3-dimensional weight [12,1],but got 2-dimensional input of size [1,1998] instead
这也是我的模型(我认识到这里可能还有其他一些问题,但我在询问张量大小未注册)
class VAE(nn.Module):
def __init__(self):
super(VAE,self).__init__()
self.conv1 = nn.Conv1d( 1,12,kernel_size=1,stride=5,padding=0)
self.conv1_drop = nn.Dropout2d()
self.pool1 = nn.MaxPool1d(kernel_size=3,stride=2)
self.fc21 = nn.Linear(198,1)
self.fc22 = nn.Linear(198,1)
self.fc3 = nn.Linear(1,198)
self.fc4 = nn.Linear(198,1998)
def encode(self,x):
h1 = self.conv1(x)
h1 = self.conv1_drop(h1)
h1 = self.pool1(h1)
h1 = F.relu(h1)
h1 = h1.view(1,-1) # 1 is the batch size
return self.fc21(h1),self.fc22(h1)
def reparameterize(self,logvar):
std = torch.exp(0.5*logvar)
eps = torch.rand_like(std)
return mu + eps*std
def decode(self,z):
h3 = F.relu(self.fc3(z))
return torch.sigmoid(self.fc4(h3))
def forward(self,x):
mu,logvar = self.encode(x.view(-1,1998))
z = self.reparameterize(mu,logvar)
return self.decode(z),logvar
那么为什么Pytorch在重塑后不保留尺寸,如果是的话,那将是正确的张量大小吗?
解决方法
当我打电话给forward()
时,我刚刚发现自己的错误,self.encode(x.view(-1,1998))
正在重整张量。