简体   繁体   English

RuntimeError: size mismatch, m1: [4 x 784], m2: [4 x 784] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:136

[英]RuntimeError: size mismatch, m1: [4 x 784], m2: [4 x 784] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:136

I have executed the following code我已经执行了以下代码

   import matplotlib.pyplot as plt
   import torch
   import torch.nn as nn
   import torch.optim as optim
   from torch.autograd import Variable
   from torch.utils import data as t_data
   import torchvision.datasets as datasets
   from torchvision import transforms
   data_transforms = transforms.Compose([transforms.ToTensor()])
  mnist_trainset = datasets.MNIST(root='./data', train=True,    
                           download=True, transform=data_transforms)
batch_size=4
dataloader_mnist_train = t_data.DataLoader(mnist_trainset, 
                                           batch_size=batch_size,
                                           shuffle=True
                                           )

def make_some_noise():
    return torch.rand(batch_size,100)


class generator(nn.Module):

    def __init__(self, inp, out):

        super(generator, self).__init__()

        self.net = nn.Sequential(
                                 nn.Linear(inp,784),
                                 nn.ReLU(inplace=True),
                                 nn.Linear(784,1000),
                                 nn.ReLU(inplace=True),
                                 nn.Linear(1000,800),
                                 nn.ReLU(inplace=True),
                                 nn.Linear(800,out)
                                    )

    def forward(self, x):
        x = self.net(x)
        return x

class discriminator(nn.Module):

    def __init__(self, inp, out):

        super(discriminator, self).__init__()

        self.net = nn.Sequential(
                                 nn.Linear(inp,784),
                                 nn.ReLU(inplace=True),
                                 nn.Linear(784,784),
                                 nn.ReLU(inplace=True),
                                 nn.Linear(784,200),
                                 nn.ReLU(inplace=True),
                                 nn.Linear(200,out),
                                 nn.Sigmoid()
                                    )

    def forward(self, x):
        x = self.net(x)
        return x

def plot_img(array,number=None):
    array = array.detach()
    array = array.reshape(28,28)

    plt.imshow(array,cmap='binary')
    plt.xticks([])
    plt.yticks([])
    if number:
        plt.xlabel(number,fontsize='x-large')
    plt.show()

d_steps = 100
g_steps = 100

gen=generator(4,4)
dis=discriminator(4,4)

criteriond1 = nn.BCELoss()
optimizerd1 = optim.SGD(dis.parameters(), lr=0.001, momentum=0.9)

criteriond2 = nn.BCELoss()
optimizerd2 = optim.SGD(gen.parameters(), lr=0.001, momentum=0.9)

printing_steps = 20

epochs = 5

for epoch in range(epochs):

    print (epoch)

    # training discriminator
    for d_step in range(d_steps):
        dis.zero_grad()

        # training discriminator on real data
        for inp_real,_ in dataloader_mnist_train:
            inp_real_x = inp_real
            break

        inp_real_x = inp_real_x.reshape(batch_size,784)
        dis_real_out = dis(inp_real_x)
        dis_real_loss = criteriond1(dis_real_out,
                              Variable(torch.ones(batch_size,1)))
        dis_real_loss.backward()

        # training discriminator on data produced by generator
        inp_fake_x_gen = make_some_noise()
        #output from generator is generated        
        dis_inp_fake_x = gen(inp_fake_x_gen).detach()
        dis_fake_out = dis(dis_inp_fake_x)
        dis_fake_loss = criteriond1(dis_fake_out,
                                Variable(torch.zeros(batch_size,1)))
        dis_fake_loss.backward()

        optimizerd1.step()



    # training generator
    for g_step in range(g_steps):
        gen.zero_grad()

        #generating data for input for generator
        gen_inp = make_some_noise()

        gen_out = gen(gen_inp)
        dis_out_gen_training = dis(gen_out)
        gen_loss = criteriond2(dis_out_gen_training,
                               Variable(torch.ones(batch_size,1)))
        gen_loss.backward()

        optimizerd2.step()

    if epoch%printing_steps==0:
        plot_img(gen_out[0])
        plot_img(gen_out[1])
        plot_img(gen_out[2])
        plot_img(gen_out[3])
        print("\n\n")

On running the code,following error is shown在运行代码时,显示以下错误

 File "mygan.py", line 105, in <module>
    dis_real_out = dis(inp_real_x)
    RuntimeError: size mismatch, m1: [4 x 784], m2: [4 x 784] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:136

How can I resolve this?我该如何解决这个问题?

I got the code from https://blog.usejournal.com/train-your-first-gan-model-from-scratch-using-pytorch-9b72987fd2c0我从https://blog.usejournal.com/train-your-first-gan-model-from-scratch-using-pytorch-9b72987fd2c0得到了代码

The error hints that the tensor you fed into the discriminator has incorrect shape.该错误提示您输入鉴别器的张量形状不正确。 Now let's try to find out what the shape of the tensor is, and what shape is expected.现在让我们试着找出张量的形状是什么,以及预期的形状。

The tensor itself has a shape of [batch_size x 784] because of the reshape operation above.由于上面的重塑操作,张量本身的形状为[batch_size x 784] The discriminator network, on the other hand, expects a tensor with a last dimension of 4 .另一方面,鉴别器网络期望最后一个维度为4的张量。 This is because the first layer in the discriminator network is nn.Linear(inp, 784) , where inp = 4 .这是因为鉴别器网络中的第一层是nn.Linear(inp, 784) ,其中inp = 4

A linear layer nn.Linear(input_size, output_size) , expects the final dimension of the input tensor to be equal to input_size , and generates output with the final dimension projected to output_size .线性层nn.Linear(input_size, output_size)期望输入张量的最终维度等于input_size ,并生成最终维度投影到output_size In this case, it expects an input tensor of shape [batch_size x 4] , and outputs a tensor of shape [batch_size x 784] .在这种情况下,它需要一个形状为[batch_size x 4]的输入张量,并输出一个形状为[batch_size x 784]的张量。


And now to the real issue: the generator and discriminator that you defined has incorrect size.现在是真正的问题:您定义的生成器和鉴别器的大小不正确。 You seem to have changed the 300 dimension size from the blog post to 784 , which I assume is the size of your image (28 x 28 for MNIST).您似乎已将博客文章中的300维度大小更改为784 ,我认为这是您的图像大小(MNIST 为 28 x 28)。 However, the 300 is not the input size, but rather a "hidden state size" -- the model uses a 300-dimensional vector to encode your input image.但是, 300不是输入大小,而是“隐藏状态大小”——该模型使用 300 维向量来编码您的输入图像。

What you should do here is to set the input size to 784 , and the output size to 1 , because the discriminator makes a binary judgment of fake (0) or real (1).这里你应该做的是将输入大小设置为784 ,输出大小设置为1 ,因为鉴别器对假(0)或真(1)进行二元判断。 For the generator, the input size should be equal to the "input noise" that you randomly generate, in this case 100 .对于生成器,输入大小应等于您随机生成的“输入噪声”,在本例中为100 The output size should also be 784 , because its output is the generated image, which should be the same size as the real data.输出大小也应该是784 ,因为它的输出是生成的图像,应该和真实数据一样大小。

So, you only need to make the following changes to your code, and it should run smoothly:因此,您只需要对代码进行以下更改,它应该可以顺利运行:

gen = generator(100, 784)
dis = discriminator(784, 1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RuntimeError:大小不匹配,m1:[5 x 10],m2:[5 x 32] 在 /pytorch/aten/src/TH/generic/THTensorMath.cpp - RuntimeError: size mismatch, m1: [5 x 10], m2: [5 x 32] at /pytorch/aten/src/TH/generic/THTensorMath.cpp RuntimeError:大小不匹配,m1:[28 x 28],m2:[784 x 128] - RuntimeError: size mismatch, m1: [28 x 28], m2: [784 x 128] 初学者 PyTorch:运行时错误:大小不匹配,m1:[16 x 2304000],m2:[600 x 120] - Beginner PyTorch : RuntimeError: size mismatch, m1: [16 x 2304000], m2: [600 x 120] Pytorch RuntimeError:大小不匹配,m1:[1 x 7744],m2:[400 x 120] - Pytorch RuntimeError: size mismatch, m1: [1 x 7744], m2: [400 x 120] Pytorch GRU 错误 RuntimeError:尺寸不匹配,m1:[1600 x 3],m2:[50 x 20] - Pytorch GRU error RuntimeError : size mismatch, m1: [1600 x 3], m2: [50 x 20] RuntimeError:尺寸不匹配,m1:[32 x 1],m2:[32 x 9] - RuntimeError: size mismatch, m1: [32 x 1], m2: [32 x 9] 如何修复此 RuntimeError:大小不匹配,m1:[64 x 103],m2:[550 x 50] - How do I fix this RuntimeError: size mismatch, m1: [64 x 103], m2: [550 x 50] Pytorch:尺寸不匹配错误,尽管矩阵的尺寸匹配(m1:[256 x 200],m2:[256 x 200]) - Pytorch: size mismatch error although the sizes of the matrices do match (m1: [256 x 200], m2: [256 x 200]) python 中的 CNN 模块给出错误大小不匹配,m1:[12288 x 26],m2:[12288 x 26] - CNN module in python gives error size mismatch, m1: [12288 x 26], m2: [12288 x 26] Pytorch vsion 大小不匹配,m1 - Pytorch vsion size mismatch, m1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM