如何修复此运行时错误:大小不匹配,m1:[64 x 103],m2:[550 x 50]

2024-04-26 06:28:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行一个GAN模型,但是它看起来像是在发电机前部区域,我得到了一个尺寸不匹配。我不确定如何解决这个问题

整个错误消息如下所示

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-25-5498a3d56294> in <module>
    190         print('{}. {}: {:.4f}'.format(idx+1, ccle_features.index[top], pval[top]))
    191 
--> 192 run_test_ccle(X_drug[:,ccle_selected],y_drug)

<ipython-input-25-5498a3d56294> in run_test_ccle(X, Y)
    182         # now run test
    183         #pval.append(GCIT(x,Y,z))
--> 184         pval.append(EBGCIT(x,Y,z))
    185 
    186     ccle_features = features[ccle_selected]

<ipython-input-24-d7ac34907be1> in EBGCIT(x, y, z, n_iter)
     66 
     67             G_loss=0
---> 68             d_input=torch.cat((G[j](g_input),z),axis=1)
     69             Gen_Dis=D(d_input)
     70             G_loss=loss_criteria(Gen_Dis.squeeze(),real)

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

<ipython-input-23-6f461c71bb16> in forward(self, x)
     32         # Linear function
     33         #print(x.shape)
---> 34         out = self.fc1(x)
     35         out = self.tanh(out)
     36         # out = self.fc2(out)

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/linear.py in forward(self, input)
     89 
     90     def forward(self, input: Tensor) -> Tensor:
---> 91         return F.linear(input, self.weight, self.bias)
     92 
     93     def extra_repr(self) -> str:

~/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py in linear(input, weight, bias)
   1672     if input.dim() == 2 and bias is not None:
   1673         # fused op is marginally faster
-> 1674         ret = torch.addmm(bias, input, weight.t())
   1675     else:
   1676         output = input.matmul(weight.t())

RuntimeError: size mismatch, m1: [64 x 103], m2: [550 x 50] at /Users/distiller/project/conda/conda-bld/pytorch_1595629430416/work/aten/src/TH/generic/THTensorMath.cpp:41

这是我的EBGCIT函数代码的一部分

def EBGCIT(x, y, z, n_iter=1000):
    J_g=10
    J_d=1

    # Generator and Discriminator 
    G=[None]*J_g
    lr_g=0.005

    noise_G=[None]*J_g

    G_optimizer=[None]*J_g

    for i in range(J_g):
        G[i]=Generator(z_dim+v_dim,h_dim, x_dim)
        G_optimizer[i]=torch.optim.SGD(G[i].parameters(),lr=lr_g)

    D=Discriminator(x_dim+z_dim,h_dim, x_dim)
    D_optimizer=torch.optim.SGD(D.parameters(),lr=lr_g)


    m=0
    for p in G[0].parameters():
        m=m+1
      
    M=[]
    for j in range(J_g):
        M.append([None]*m)

    for j in range(J_g):
        m=0
        for par in G[0].parameters():
            M[j][m]=torch.zeros(par.size())
            m=m+1 

    n = len(z[:, 0])
    # define training and testing subsets, training for learning the sampler and 
    # testing for computing test statistic. Set 2/3 and 1/3 as default
    x_train, y_train, z_train = x[:int(2*n/3),], y[:int(2*n/3),], z[:int(2*n/3),] 
    x_test, y_test, z_test = x[int(2*n/3):,], y[int(2*n/3):,], z[int(2*n/3):,] 

    n = len(z_train[:, 0])

    # 2. # of confounders

    z_train=torch.Tensor(z_train)
    x_train=torch.Tensor(x_train)

    z_test=torch.Tensor(z_test)
    check=x_test
    x_test=torch.Tensor(x_test)

    for iter in range(1000):
    
        # Train Generator
        for j in range(J_g):
          
            v=np.sqrt(1./3)*torch.randn(mini_batch,v_dim)
            perm = torch.randperm(n)
            idx=perm[:mini_batch]
            z=z_train[idx]
            g_input=torch.cat((z,v),axis=1)
          
            G_loss=0
            d_input=torch.cat((G[j](g_input),z),axis=1)
            Gen_Dis=D(d_input)
            G_loss=loss_criteria(Gen_Dis.squeeze(),real)

            l2=0.0
            for p in G[j].parameters():
                l2=l2+(p**2).sum()/200
          
            G_loss+=l2/n
            
            G_optimizer[j].zero_grad()
          
            G_loss.backward()
            G_optimizer[j].step()
      
            m=0
            for par in G[j].parameters():
                par.data.sub_(a*M[j][m]*lr_g/n)
                m=m+1
          
            with torch.no_grad():
                for param in G[j].parameters():
                    param.add_(torch.randn(param.size()) * np.sqrt(2*lr_g/n))

            m=0
            for par in G[j].parameters():
                M[j][m]*=beta_1
                M[j][m]+=(1-beta_1)*par.grad.data*n            
                m=m+1     
               

这是我的生成器和鉴别器类的代码

J_g=10
np.random.seed(1)
torch.manual_seed(1)


class Generator(nn.Module):
    def __init__(self, input_dim, hidden_dim, output_dim):
        super(Generator, self).__init__()
        # Linear function
        self.fc1 = nn.Linear(input_dim, hidden_dim) 
        nn.init.xavier_normal_(self.fc1.weight)
        self.tanh = nn.Tanh()

        self.fc3 = nn.Linear(hidden_dim, output_dim)  
        nn.init.xavier_normal_(self.fc3.weight)

    def forward(self, x):
        # Linear function
        #print(x.shape)
        out = self.fc1(x)
        out = self.tanh(out)
        out = self.fc3(out)
        return out
    

class Discriminator(nn.Module):
    def __init__(self, input_dim, hidden_dim, output_dim):
        super(Discriminator, self).__init__()
        # Linear function
        self.fc1 = nn.Linear(input_dim, hidden_dim) 
        nn.init.xavier_normal_(self.fc1.weight)
        self.relu = nn.ReLU()

        self.fc3 = nn.Linear(hidden_dim, output_dim)  
        nn.init.xavier_normal_(self.fc3.weight)
        

    def forward(self, x):
        # Linear function
        out = self.fc1(x)
        out = self.relu(out)
        out = self.fc3(out)
        return out

mini_batch=64    
loss_criteria=nn.BCEWithLogitsLoss()

real=torch.ones(mini_batch)
fake=torch.zeros(mini_batch)

    
a=1
beta_1=0.9

A=1
B=1000
sig=nn.Sigmoid()

任何帮助都将不胜感激。谢谢


1条回答
网友
1楼 · 发布于 2024-04-26 06:28:20

我们假设m1:[a x b],m2:[c x d]\

[a x b] = [batch size x in_channels]
[c x d] = [in_channels x out_channels]

因此,我们需要做的就是:

  [b = c]

相关问题 更多 >