I am trying to construct a Convolutional Autoencoder
model for time series data, Where embed_size = 32
and time_step = 400
, the model constructor is like this:
class CNNEncoder(nn.Module):
def __init__(self, embedding_size, timestep) -> None:
super().__init__()
self.conv = nn.Sequential(
nn.Conv1d(in_channels=timestep, out_channels=128, kernel_size=3, padding=1),
nn.AvgPool1d(3),
nn.ReLU(True),
nn.Conv1d(in_channels=128, out_channels=64, kernel_size=3, padding=1),
nn.AvgPool1d(3),
nn.ReLU(True),
nn.Conv1d(in_channels=64, out_channels=32, kernel_size=3, padding=1),
nn.ReLU(True),
nn.Conv1d(in_channels=32, out_channels=16, kernel_size=3, padding=1),
#nn.ReLU(True),
)
self.fc = nn.Sequential(
nn.Linear(192, 256),
nn.BatchNorm1d(256),
nn.LeakyReLU(0.2),
nn.Linear(256, embedding_size),
)
def forward(self, x):
conv = self.conv(x)
conv = torch.flatten(conv, start_dim=1)
return self.fc(conv)
class CNNDecoder(nn.Module):
def __init__(self, embedding_size, timestep) -> None:
super().__init__()
self.fc = nn.Sequential(
nn.Linear(embedding_size, 256),
nn.BatchNorm1d(256),
nn.ReLU(True),
nn.Linear(256, 576),
nn.ReLU(True),
nn.Linear(576, 1024),
#nn.ReLU(True),
)
self.deconv = nn.Sequential(
nn.ConvTranspose1d(1024, 576, 3, dilation=2),
nn.BatchNorm1d(576),
nn.ReLU(True),
nn.ConvTranspose1d(576, 384, 4, padding=3, stride=3),
nn.BatchNorm1d(384),
nn.ReLU(True),
nn.ConvTranspose1d(384, timestep, 3, 3, dilation=2),
nn.Sigmoid(),
)
def forward(self, x):
fc = self.fc(x)
fc = fc.view(-1, 1024, 1)
return self.deconv(fc)
class AutoEncoder(nn.Module):
def __init__(self, embed_size, time_step) -> None:
super().__init__()
self.encoder = CNNEncoder(embed_size, time_step)
self.decoder = CNNDecoder(embed_size, time_step)
def encode(self, x):
return self.encoder(x)
def decode(self, x):
return self.decoder(x)
def forward(self, x):
z = self.encode(x)
return self.decode(z)
But I’m getting the error:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (4x48 and 192x256)
I understand that there is a mismatch between the dimensions of the matrices I’m trying to multiply in one of the linear layers. And most probably the error occurs in CNNEncoder
class, in the first linear layer of the self.fc
sequential block. But I’m not sure regarding this. What would be the solution?