pytorch错误尺寸划分batch_size使用不同批次的培训

发布于 2025-01-27 16:41:19 字数 6893 浏览 4 评论 0原文

我正在尝试解决这个问题。 It is a 1D VAE

when I used summary(vae.encoder,(1,28)), it works but when I train using a batch size (32) I have the following error:

RuntimeError: Given groups=1, weight of size [16,1,4],预期输入[1,32,28]有1个频道,但有32个频道,

我的代码希望您可以帮助我

class Encoder(nn.Module):
    def __init__(self, z_dim):
        super(Encoder, self).__init__()
        self.conv1 = nn.Conv1d(1, 16, 4)
        self.conv2 = nn.Conv1d(16, 16, 4)
        self.conv3 = nn.Conv1d(16, 32, 4)
        self.conv4 = nn.Conv1d(32, 32, 4)
        self.fc1 = nn.Linear(32*16, 64)
        self.fc2 = nn.Linear(64, 16)
        self.fc21 = nn.Linear(16, z_dim)
        self.fc22 = nn.Linear(16, z_dim)
        self.bn1 = nn.BatchNorm1d(16)
        self.bn2 = nn.BatchNorm1d(16)
        self.bn3 = nn.BatchNorm1d(32)
        self.bn4 = nn.BatchNorm1d(32)
        self.bn5 = nn.BatchNorm1d(64)
        self.relu = nn.ReLU()

    def forward(self, x):
        print('HERE0',x.shape)
        x = self.relu(self.conv1(x))
        #x=torch.movedim(x, 1, 0)
        print('HERE1',x.shape)
        x = self.bn1(x)
        print('HERE2',x.shape)
        x = F.dropout(x, 0.3)
        #x=torch.movedim(x, 1, 0)
        #print('HERE3',x.shape)
        x = self.relu(self.conv2(x))
        print('HERE4',x.shape)
        #x=torch.movedim(x, 1, 0)
        x = self.bn2(x)
        print('HERE5',x.shape)
        x = F.dropout(x, 0.3)
        print('HERE6',x.shape)
        #x=torch.movedim(x, 1, 0)
        x = self.relu(self.conv3(x))
        print('HERE7',x.shape)
        #x=torch.movedim(x, 1, 0)
        x = self.bn3(x)
        print('HERE8',x.shape)
        x = F.dropout(x, 0.3)
        print('HERE9',x.shape)
        #x=torch.movedim(x, 1, 0)
        x = self.relu(self.conv4(x))
        print('HERE10',x.shape)
        #x=torch.movedim(x, 1, 0)
        x = self.bn4(x)
        print('HERE11',x.shape)
        x = F.dropout(x, 0.3)
        print('HERE12',x.shape)
        x = x.contiguous().view(-1,32*16)
        print('HERE13',x.shape)
        x = self.relu(self.fc1(x))
        print('HERE14',x.shape)
        x = self.bn5(x)
        print('HERE15',x.shape)
        #x = F.dropout(x, 0.5)
        print('HERE16',x.shape)
        x = self.relu(self.fc2(x))
        print('HERE17',x.shape)
        z_loc = self.fc21(x)
        print('HERE18',x.shape)
        z_scale = self.fc22(x)
        print('HERE19',x.shape)
        return z_loc, z_scale



class Decoder(nn.Module):
    def __init__(self, z_dim):
        super(Decoder, self).__init__()
        self.fc1 = nn.Linear(z_dim, 32*16)
        self.conv1 = nn.ConvTranspose1d(32, 32, 3)
        self.conv2 = nn.ConvTranspose1d(32, 32, 3)
        self.conv3 = nn.ConvTranspose1d(32, 16, 3)
        self.conv4 = nn.ConvTranspose1d(16, 16, 4)
        self.conv5 = nn.ConvTranspose1d(16, 1, 4)
        self.bn1 = nn.BatchNorm1d(32)
        self.bn2 = nn.BatchNorm1d(32)
        self.bn3 = nn.BatchNorm1d(16)
        self.bn4 = nn.BatchNorm1d(16)
        self.relu = nn.ReLU()

    def forward(self, z):
        z = self.relu(self.fc1(z))
        print('HERE20',z.shape)
        #z = F.dropout(z, 0.3)
        z = z.view(-1, 32, 16)
        print('HERE21',z.shape)
        z = self.relu(self.conv1(z))
        print('HERE22',z.shape)
        z = self.bn1(z)
        print('HERE23',z.shape)
        #z = F.dropout(z, 0.3)
        z = self.relu(self.conv2(z))
        print('HERE24',z.shape)
        z = self.bn2(z)
        print('HERE25',z.shape)
        #z = F.dropout(z, 0.3)
        z = self.relu(self.conv3(z))
        print('HERE26',z.shape)
        z = self.bn3(z)
        print('HERE27',z.shape)
        #z = F.dropout(z, 0.3)
        z = self.relu(self.conv4(z))
        print('HERE28',z.shape)
        z = self.bn4(z)
        print('HERE29',z.shape)
        #z = F.dropout(z, 0.3)
        z = self.conv5(z)
        print('HERE30',z.shape)
        recon = torch.sigmoid(z)
        return recon


class VAE(nn.Module):
    def __init__(self, z_dim=2):
        super(VAE, self).__init__()
        self.encoder = Encoder(z_dim)
        self.decoder = Decoder(z_dim)
        self.cuda()
        self.z_dim = z_dim

    def reparameterize(self, z_loc, z_scale):
        std = z_scale.mul(0.5).exp_()
        epsilon = torch.randn(*z_loc.size()).to(device)
        z = z_loc + std * epsilon
        return z

device = torch.device("cuda:0")

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.1)

n_train = len(x_train)
n_test = len(x_test)

device=torch.device('cuda:0')

batch_size = 32

x_train=torch.Tensor(x_train)
y_train=torch.Tensor(y_train)
x_test=torch.Tensor(x_test)
y_test=torch.Tensor(y_test)

print(x_train.shape)

train_ds = TensorDataset(x_train, y_train)
train_dl = DataLoader(train_ds, batch_size=batch_size, shuffle=True)
test_ds = TensorDataset(x_test, y_test)
test_dl = DataLoader(test_ds, batch_size=batch_size, shuffle=True)

vae = VAE()

summary(vae.encoder,(1,28))
summary(vae.decoder,(1,2))

optimizer = torch.optim.Adam(vae.parameters(), lr=0.001)
#optimizer = torch.optim.RMSprop(vae.parameters(), lr=0.001, alpha=0.9)

def loss_fn(recon_x, x, z_loc, z_scale):
    BCE = F.mse_loss(recon_x, x, size_average=False)*100
    KLD = -0.5 * torch.sum(1 + z_scale - z_loc.pow(2) - z_scale.exp())
    return BCE + KLD


for epoch in range(1000):
    for x, _ in train_dl:
        x = x.cuda()
        print(x.shape)
       
        z_loc, z_scale = vae.encoder(x)
        z = vae.reparameterize(z_loc, z_scale)
        recon = vae.decoder(z)
        loss = loss_fn(recon, x, z_loc, z_scale)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    vae.eval()
    with torch.no_grad():
        for i, (x, _) in enumerate(test_dl):
            x = x.cuda()
            z_loc, z_scale = vae.encoder(x)
            z = vae.reparameterize(z_loc, z_scale)
            recon = vae.decoder(z)
            test_loss = loss_fn(recon, x, z_loc, z_scale)
    normalizer_test = len(test_dl.dataset)
    total_epoch_loss_test = test_loss / normalizer_test
    if epoch == 0:
        loss_test_history = total_epoch_loss_test.item()
        patience = 0
    else:
        loss_test_history = np.append(loss_test_history, total_epoch_loss_test.item())

    if total_epoch_loss_test.item() < 0.000001+np.min(loss_test_history):
        patience = 0
        torch.save(vae.decoder.state_dict(), "best_decoder_model.pt")
        torch.save(vae.encoder.state_dict(), "best_encoder_model.pt")
    else:
        patience +=1

    print(epoch, patience, total_epoch_loss_test.item(), np.min(loss_test_history))

    if patience == 3: #32
        break

#This is just to visualize the outputs for myself
X_enc, _ = vae.encoder(x_test)
recon = vae.decoder(X_enc)
X_enc = X_enc.cpu().detach().numpy()
y_enc = y_test.cpu().detach().numpy()
#y_enc = np.array(([np.argmax(l) for l in y_enc]))


I am trying to solve this problem. It is a 1D VAE

when I used summary(vae.encoder,(1,28)), it works but when I train using a batch size (32) I have the following error:

RuntimeError: Given groups=1, weight of size [16, 1, 4], expected input[1, 32, 28] to have 1 channels, but got 32 channels instead

Here is my code hope you can help me

class Encoder(nn.Module):
    def __init__(self, z_dim):
        super(Encoder, self).__init__()
        self.conv1 = nn.Conv1d(1, 16, 4)
        self.conv2 = nn.Conv1d(16, 16, 4)
        self.conv3 = nn.Conv1d(16, 32, 4)
        self.conv4 = nn.Conv1d(32, 32, 4)
        self.fc1 = nn.Linear(32*16, 64)
        self.fc2 = nn.Linear(64, 16)
        self.fc21 = nn.Linear(16, z_dim)
        self.fc22 = nn.Linear(16, z_dim)
        self.bn1 = nn.BatchNorm1d(16)
        self.bn2 = nn.BatchNorm1d(16)
        self.bn3 = nn.BatchNorm1d(32)
        self.bn4 = nn.BatchNorm1d(32)
        self.bn5 = nn.BatchNorm1d(64)
        self.relu = nn.ReLU()

    def forward(self, x):
        print('HERE0',x.shape)
        x = self.relu(self.conv1(x))
        #x=torch.movedim(x, 1, 0)
        print('HERE1',x.shape)
        x = self.bn1(x)
        print('HERE2',x.shape)
        x = F.dropout(x, 0.3)
        #x=torch.movedim(x, 1, 0)
        #print('HERE3',x.shape)
        x = self.relu(self.conv2(x))
        print('HERE4',x.shape)
        #x=torch.movedim(x, 1, 0)
        x = self.bn2(x)
        print('HERE5',x.shape)
        x = F.dropout(x, 0.3)
        print('HERE6',x.shape)
        #x=torch.movedim(x, 1, 0)
        x = self.relu(self.conv3(x))
        print('HERE7',x.shape)
        #x=torch.movedim(x, 1, 0)
        x = self.bn3(x)
        print('HERE8',x.shape)
        x = F.dropout(x, 0.3)
        print('HERE9',x.shape)
        #x=torch.movedim(x, 1, 0)
        x = self.relu(self.conv4(x))
        print('HERE10',x.shape)
        #x=torch.movedim(x, 1, 0)
        x = self.bn4(x)
        print('HERE11',x.shape)
        x = F.dropout(x, 0.3)
        print('HERE12',x.shape)
        x = x.contiguous().view(-1,32*16)
        print('HERE13',x.shape)
        x = self.relu(self.fc1(x))
        print('HERE14',x.shape)
        x = self.bn5(x)
        print('HERE15',x.shape)
        #x = F.dropout(x, 0.5)
        print('HERE16',x.shape)
        x = self.relu(self.fc2(x))
        print('HERE17',x.shape)
        z_loc = self.fc21(x)
        print('HERE18',x.shape)
        z_scale = self.fc22(x)
        print('HERE19',x.shape)
        return z_loc, z_scale



class Decoder(nn.Module):
    def __init__(self, z_dim):
        super(Decoder, self).__init__()
        self.fc1 = nn.Linear(z_dim, 32*16)
        self.conv1 = nn.ConvTranspose1d(32, 32, 3)
        self.conv2 = nn.ConvTranspose1d(32, 32, 3)
        self.conv3 = nn.ConvTranspose1d(32, 16, 3)
        self.conv4 = nn.ConvTranspose1d(16, 16, 4)
        self.conv5 = nn.ConvTranspose1d(16, 1, 4)
        self.bn1 = nn.BatchNorm1d(32)
        self.bn2 = nn.BatchNorm1d(32)
        self.bn3 = nn.BatchNorm1d(16)
        self.bn4 = nn.BatchNorm1d(16)
        self.relu = nn.ReLU()

    def forward(self, z):
        z = self.relu(self.fc1(z))
        print('HERE20',z.shape)
        #z = F.dropout(z, 0.3)
        z = z.view(-1, 32, 16)
        print('HERE21',z.shape)
        z = self.relu(self.conv1(z))
        print('HERE22',z.shape)
        z = self.bn1(z)
        print('HERE23',z.shape)
        #z = F.dropout(z, 0.3)
        z = self.relu(self.conv2(z))
        print('HERE24',z.shape)
        z = self.bn2(z)
        print('HERE25',z.shape)
        #z = F.dropout(z, 0.3)
        z = self.relu(self.conv3(z))
        print('HERE26',z.shape)
        z = self.bn3(z)
        print('HERE27',z.shape)
        #z = F.dropout(z, 0.3)
        z = self.relu(self.conv4(z))
        print('HERE28',z.shape)
        z = self.bn4(z)
        print('HERE29',z.shape)
        #z = F.dropout(z, 0.3)
        z = self.conv5(z)
        print('HERE30',z.shape)
        recon = torch.sigmoid(z)
        return recon


class VAE(nn.Module):
    def __init__(self, z_dim=2):
        super(VAE, self).__init__()
        self.encoder = Encoder(z_dim)
        self.decoder = Decoder(z_dim)
        self.cuda()
        self.z_dim = z_dim

    def reparameterize(self, z_loc, z_scale):
        std = z_scale.mul(0.5).exp_()
        epsilon = torch.randn(*z_loc.size()).to(device)
        z = z_loc + std * epsilon
        return z

device = torch.device("cuda:0")

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.1)

n_train = len(x_train)
n_test = len(x_test)

device=torch.device('cuda:0')

batch_size = 32

x_train=torch.Tensor(x_train)
y_train=torch.Tensor(y_train)
x_test=torch.Tensor(x_test)
y_test=torch.Tensor(y_test)

print(x_train.shape)

train_ds = TensorDataset(x_train, y_train)
train_dl = DataLoader(train_ds, batch_size=batch_size, shuffle=True)
test_ds = TensorDataset(x_test, y_test)
test_dl = DataLoader(test_ds, batch_size=batch_size, shuffle=True)

vae = VAE()

summary(vae.encoder,(1,28))
summary(vae.decoder,(1,2))

optimizer = torch.optim.Adam(vae.parameters(), lr=0.001)
#optimizer = torch.optim.RMSprop(vae.parameters(), lr=0.001, alpha=0.9)

def loss_fn(recon_x, x, z_loc, z_scale):
    BCE = F.mse_loss(recon_x, x, size_average=False)*100
    KLD = -0.5 * torch.sum(1 + z_scale - z_loc.pow(2) - z_scale.exp())
    return BCE + KLD


for epoch in range(1000):
    for x, _ in train_dl:
        x = x.cuda()
        print(x.shape)
       
        z_loc, z_scale = vae.encoder(x)
        z = vae.reparameterize(z_loc, z_scale)
        recon = vae.decoder(z)
        loss = loss_fn(recon, x, z_loc, z_scale)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    vae.eval()
    with torch.no_grad():
        for i, (x, _) in enumerate(test_dl):
            x = x.cuda()
            z_loc, z_scale = vae.encoder(x)
            z = vae.reparameterize(z_loc, z_scale)
            recon = vae.decoder(z)
            test_loss = loss_fn(recon, x, z_loc, z_scale)
    normalizer_test = len(test_dl.dataset)
    total_epoch_loss_test = test_loss / normalizer_test
    if epoch == 0:
        loss_test_history = total_epoch_loss_test.item()
        patience = 0
    else:
        loss_test_history = np.append(loss_test_history, total_epoch_loss_test.item())

    if total_epoch_loss_test.item() < 0.000001+np.min(loss_test_history):
        patience = 0
        torch.save(vae.decoder.state_dict(), "best_decoder_model.pt")
        torch.save(vae.encoder.state_dict(), "best_encoder_model.pt")
    else:
        patience +=1

    print(epoch, patience, total_epoch_loss_test.item(), np.min(loss_test_history))

    if patience == 3: #32
        break

#This is just to visualize the outputs for myself
X_enc, _ = vae.encoder(x_test)
recon = vae.decoder(X_enc)
X_enc = X_enc.cpu().detach().numpy()
y_enc = y_test.cpu().detach().numpy()
#y_enc = np.array(([np.argmax(l) for l in y_enc]))


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文