Pytorch RuntimeError:大小不匹配

时间:2019-10-09 20:40:28

标签: python pytorch gan

当我尝试运行以下代码来训练GAN进行预测时,出现以下错误:

  

RuntimeError:大小不匹配,m1:[128 x 1],m2:[1392 x 2784],位于C:\ w \ 1 \ s \ tmp_conda_3.7_021303 \ conda \ conda-bld \ pytorch_1565316900252 \ work \ aten \ src \ TH / generic / THTensorMath.cpp:752

如果您在代码中发现任何其他错误或有一些一般提示,请发表评论。

# Sample indices
def sample_idx(m, n):
    A = np.random.permutation(m)
    idx = A[:n]
    return idx

class Discriminator(nn.Module):
    def __init__(self):
        super(Discriminator, self).__init__()
        self.fc1 = nn.Linear(in_features = dim, out_features = dim*2)
        self.fc2 = nn.Linear(in_features = dim*2, out_features = dim)
        self.fc3 = nn.Linear(in_features = dim, out_features = int(dim/2))
        self.fc4 = nn.Linear(in_features = int(dim/2), out_features = 1)
        self.relu = nn.LeakyReLU(0.2)
        self.sigmoid = nn.Sigmoid()
        self.init_weight()

    def init_weight(self):
        layers = [self.fc1, self.fc2, self.fc3]
        [nn.init.xavier_normal_(layer.weight) for layer in layers]

    def forward(self, x):
        x = self.relu(self.fc1(x))
        x = self.relu(self.fc2(x))
        x = self.relu(self.fc3(x))
        x = self.sigmoid(self.fc4(x))
        return x



class Generator(nn.Module):
    def __init__(self):
        super(Generator, self).__init__()
        self.fc1 = nn.Linear(in_features = dim, out_features = dim*2)
        self.fc2 = nn.Linear(in_features = dim*2, out_features = dim)
        self.fc3 = nn.Linear(in_features = dim, out_features = int(dim/2))
        self.fc4 = nn.Linear(in_features = int(dim/2), out_features = 1)
        self.relu = nn.LeakyReLU(0.2)
        self.init_weight()

    def init_weight(self):
        layers = [self.fc1, self.fc2, self.fc3]
        [nn.init.xavier_normal_(layer.weight) for layer in layers]

    def forward(self, x):
        x = self.relu(self.fc1(x))
        x = self.relu(self.fc2(x))
        x = self.relu(self.fc3(x))
        x = self.fc4(x)
        return x

# 1. Mini batch size
mb_size = 128

# 4. Loss Hyperparameters
alpha = 10

# 5. Input Dim (Fixed)
dim = data.shape[1] - 1

# 6. Train Test Split
train_n = int(199476 * 0.8)
test_n = 199476 - int(199476 * 0.8)

X_train = data[:train_n,:-1]
y_train = data[:train_n, -1]

X_test = data[train_n:,:-1]
y_test = data[train_n:, -1]

# Nets
D = Discriminator()
G = Generator()

# Optimizers
optimD = torch.optim.Adam(D.parameters(), lr = 0.001)
optimG = torch.optim.Adam(G.parameters(), lr = 0.001)

# Loss
mse_loss = nn.MSELoss(reduction = 'elementwise_mean')

i = 1
for it in tqdm(range(10000)):
    mb_idx = sample_idx(train_n, mb_size)
    X_mb = X_train[mb_idx,:]
    X_mb = torch.tensor(X_mb).float()
    y_mb = y_train[mb_idx]        
    y_mb = torch.tensor(y_mb).float()

    # Train D
    G_sample = G(X_mb)
    D_loss1 = ((D(X_mb) + 1e-8).log()).mean() + ((1 - D(G_sample) + 1e-8).log()).mean()
    D_loss = - D_loss1
    D_loss.backward()
    optimD.step()
    optimD.zero_grad()

    # Train G
    G_sample = G(X_mb)
    D_prob.detach()
    G_loss1 = ((1 - D(G_sample) + 1e-8).log()).mean()
    G_mse_loss = mse_loss(G_sample, y_mb)
    G_loss = G_loss1 + alpha * G_mse_loss
    G_loss.backward()
    optimG.step()
    optimG.zero_grad()

    if it % 100 == 0:
        print("Iter: {}".format(it))
        print("D_loss: {:.4}".format(D_loss))
        print("Train loss: {:.4}".format(G_mse_loss))
        print()

1 个答案:

答案 0 :(得分:0)

如前所述,您在下一行中得到了错误。

CREATE PROJECTION table_data_date_sorted
(
  a_date,
  ...
)
AS
  SELECT
    a_date,
    ...
  FROM table_data
  ORDER BY
    a_date,
    ...
SEGMENTED BY HASH(a_date) ALL NODES;
  

我怀疑有问题的部分是:D_loss1 = ((D(X_mb) + 1e-8).log()).mean() + ((1 - D(G_sample) + 1e-8).log()).mean() 。为什么?

由于D(G_sample)的形状为G_sample = G(X_mb),因此不能作为鉴别符[batch_size, 1]的输入,因为它需要形状为D的张量作为输入。

这就是为什么出现错误的原因:

[batch_size, dim]

如您所见,输入的形状为RuntimeError: size mismatch, m1: [128 x 1], m2: [1392 x 2784] ,其中[128 x 1]。但是鉴别器batch_size = 128期望输入形状为D。这里,[batch_size x 1392]是鉴别器中m2层的权重矩阵的形状。