我已经执行了以下代码
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")
运行代码时,显示以下错误
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
我该如何解决?
我从https://blog.usejournal.com/train-your-first-gan-model-from-scratch-using-pytorch-9b72987fd2c0那里获得了代码
答案 0 :(得分:1)
该错误提示您输入到鉴别器中的张量形状不正确。现在,让我们尝试找出张量的形状以及期望的形状。
由于上面的整形操作,张量本身的形状为[batch_size x 784]
。另一方面,鉴别器网络期望最后张量为4
的张量。这是因为鉴别器网络的第一层是nn.Linear(inp, 784)
,其中inp = 4
。
线性层nn.Linear(input_size, output_size)
,期望输入张量的最终尺寸等于input_size
,并生成最终尺寸投影为output_size
的输出。在这种情况下,它期望输入形状为[batch_size x 4]
的张量,并输出形状为[batch_size x 784]
的张量。
现在到了真正的问题:您定义的生成器和鉴别器的大小不正确。您似乎已将300
的尺寸大小从博客文章更改为784
,我认为这是图像的大小(MNIST为28 x 28)。但是,300
不是输入大小,而是“隐藏状态大小”,该模型使用300维向量对输入图像进行编码。
您应该在此处执行的操作是将输入大小设置为784
,将输出大小设置为1
,因为判别器对伪造(0)或实数(1)进行二进制判断。对于生成器,输入大小应等于您随机生成的“输入噪声”,在这种情况下为100
。输出大小也应为784
,因为其输出是生成的图像,该图像的大小应与真实数据的大小相同。
因此,您只需要对代码进行以下更改,即可顺利运行:
gen = generator(100, 784)
dis = discriminator(784, 1)