简体   繁体   English

如何修复此 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]

I am running a GAN model however it looks like in the generator forward area, I am getting a size mismatch.我正在运行一个 GAN model 但是它看起来像在发电机前向区域,我的尺寸不匹配。 I am unsure on how to fix this.我不确定如何解决这个问题。

The entire error message is below.整个错误消息如下。

---------------------------------------------------------------------------
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

This is a portion of my code for my EBGCIT function.这是我的 EBGCIT function 代码的一部分。

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     
               

And this is the code for my generator and discriminator classes.这是我的生成器和鉴别器类的代码。

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()

Any help would be much appreciated.任何帮助将非常感激。 Thanks!!谢谢!!

we assume m1: [axb], m2: [cxd]\我们假设 m1: [axb], m2: [cxd]\

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

So all we need to do is to make:所以我们需要做的就是:

  [b = c]

暂无
暂无

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

相关问题 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] Pytorch RuntimeError:大小不匹配,m1:[1 x 7744],m2:[400 x 120] - Pytorch RuntimeError: size mismatch, m1: [1 x 7744], m2: [400 x 120] RuntimeError:尺寸不匹配,m1:[32 x 1],m2:[32 x 9] - RuntimeError: size mismatch, m1: [32 x 1], m2: [32 x 9] 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] 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 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 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] 如何在时间h1:m1和h2:m2之间用x_lims绘制图形 - How do I plot a graph with x_lims between time h1:m1 and h2:m2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM