【问题标题】:Deep Smote error : RuntimeError: mat1 and mat2 shapes cannot be multiplied (51200x1 and 512x300)Deep Smote 错误:RuntimeError:mat1 和 mat2 形状不能相乘(51200x1 和 512x300)
【发布时间】:2022-08-15 15:53:27
【问题描述】:

我正在尝试在 cifar10 上运行深度 Smote,并且在 tensorflow 中编码时对 pytorch 没有太多经验。当我在 MNIST 和 FMNIST 上运行它时它工作正常,保持 channles = 1 然而,当我在 cifar10 上试用它的那一刻,我的表现并不好。 论文中给出的代码说它也适用于 Cifar10, 感谢所有帮助 这是论文源代码的链接 https://github.com/dd1github/DeepSMOTE 源代码在 Tensorflow 中,有人可以在这里帮助我吗

RuntimeError                              Traceback (most recent call last)
C:\\Users\\RESEAR~1\\AppData\\Local\\Temp/ipykernel_24844/1514724550.py in <module>
     93 
     94                 # run images
---> 95                 z_hat = encoder(images)
     96 
     97                 x_hat = decoder(z_hat) #decoder outputs tanh

~\\.conda\\envs\\pytorch\\lib\\site-packages\\torch\\nn\\modules\\module.py in _call_impl(self, *input, **kwargs)
   1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1101                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102             return forward_call(*input, **kwargs)
   1103         # Do not call functions when jit is used
   1104         full_backward_hooks, non_full_backward_hooks = [], []



~\\.conda\\envs\\pytorch\\lib\\site-packages\\torch\\nn\\functional.py in linear(input, weight, bias)
   1846     if has_torch_function_variadic(input, weight, bias):
   1847         return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)
-> 1848     return torch._C._nn.linear(input, weight, bias)
   1849 
   1850 

RuntimeError: mat1 and mat2 shapes cannot be multiplied (51200x1 and 512x300)

这是代码:

## create encoder model and decoder model
class Encoder(nn.Module):
    def __init__(self, args):
        super(Encoder, self).__init__()

        self.n_channel = args[\'n_channel\']
        self.dim_h = args[\'dim_h\']
        self.n_z = args[\'n_z\']
        
        # convolutional filters, work excellent with image data
        self.conv = nn.Sequential(
            nn.Conv2d(self.n_channel, self.dim_h, 4, 2, 1, bias=False),
            #nn.ReLU(True),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(self.dim_h, self.dim_h * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.dim_h * 2),
            #nn.ReLU(True),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(self.dim_h * 2, self.dim_h * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(self.dim_h * 4),
            #nn.ReLU(True),
            nn.LeakyReLU(0.2, inplace=True),
            
            
#             nn.Conv2d(self.dim_h * 4, self.dim_h * 8, 4, 2, 1, bias=False),
            
            #3d and 32 by 32
            nn.Conv2d(self.dim_h * 4, self.dim_h * 8, 4, 1, 0, bias=False),
            
            nn.BatchNorm2d(self.dim_h * 8), # 40 X 8 = 320
            #nn.ReLU(True),
            nn.LeakyReLU(0.2, inplace=True) )#,
            #nn.Conv2d(self.dim_h * 8, 1, 2, 1, 0, bias=False))
            #nn.Conv2d(self.dim_h * 8, 1, 4, 1, 0, bias=False))
        # final layer is fully connected
        print(\"linearer >>>>>>>> \",self.dim_h * (2 ** 3), self.n_z)
        self.fc = nn.Linear(self.dim_h * (2 ** 3), self.n_z)
        print(\"leeeeeeeeeee \")
        

    def forward(self, x):
        #print(\'enc\')
        #print(\'input \',x.size()) #torch.Size([100, 3,32,32])
        x = self.conv(x)
        
#         x = x.squeeze()
#         print(\'aft squeeze \',x.size()) #torch.Size([128, 320])
        #aft squeeze  torch.Size([100, 320])
        x = self.fc(x)
        #print(\'out \',x.size()) #torch.Size([128, 20])
        #out  torch.Size([100, 300])
        return x


class Decoder(nn.Module):
    def __init__(self, args):
        super(Decoder, self).__init__()

        self.n_channel = args[\'n_channel\']
        self.dim_h = args[\'dim_h\']
        self.n_z = args[\'n_z\']

        # first layer is fully connected
        self.fc = nn.Sequential(
            nn.Linear(self.n_z, self.dim_h * 8 * 7 * 7),
            nn.ReLU())

        # deconvolutional filters, essentially inverse of convolutional filters
        self.deconv = nn.Sequential(
            nn.ConvTranspose2d(self.dim_h * 8, self.dim_h * 4, 4),
            nn.BatchNorm2d(self.dim_h * 4),
            nn.ReLU(True),
            nn.ConvTranspose2d(self.dim_h * 4, self.dim_h * 2, 4),
            nn.BatchNorm2d(self.dim_h * 2),
            nn.ReLU(True),
            nn.ConvTranspose2d(self.dim_h * 2, 1, 4, stride=2),
            #nn.Sigmoid())
            nn.Tanh())

    def forward(self, x):
        #print(\'dec\')
        #print(\'input \',x.size())
        x = self.fc(x)
        x = x.view(-1, self.dim_h * 8, 7, 7)
        x = self.deconv(x)
        return x

…………

#NOTE: Download the training (\'.../0_trn_img.txt\') and label files 
# (\'.../0_trn_lab.txt\').  Place the files in directories (e.g., ../MNIST/trn_img/
# and /MNIST/trn_lab/).  Originally, when the code was written, it was for 5 fold
#cross validation and hence there were 5 files in each of the 
#directories.  Here, for illustration, we use only 1 training and 1 label
#file (e.g., \'.../0_trn_img.txt\' and \'.../0_trn_lab.txt\').
path = \"C:/Users/antpc/Documents/saqib_smote/fmnist/\"
path = \"C:/Users/Research6/Desktop/smote experimentation/mnist/\"
dtrnimg = (path+\'/CBL_images\')
dtrnlab = (path+\'/CBL_labels\')

ids = os.listdir(dtrnimg)
idtri_f = [os.path.join(dtrnimg, image_id) for image_id in ids]
print(idtri_f)

ids = os.listdir(dtrnlab)
idtrl_f = [os.path.join(dtrnlab, image_id) for image_id in ids]
print(idtrl_f)

#for i in range(5):
for i in range(len(ids)):
    print()
    print(i)
    encoder = Encoder(args)
    decoder = Decoder(args)

    device = \'cuda\' if torch.cuda.is_available() else \'cpu\'
    print(device)
    decoder = decoder.to(device)
    encoder = encoder.to(device)

    train_on_gpu = torch.cuda.is_available()

    #decoder loss function
    criterion = nn.MSELoss()
    criterion = criterion.to(device)
    
    trnimgfile = idtri_f[i]
    trnlabfile = idtrl_f[i]
    
    print(trnimgfile)
    print(trnlabfile)
    dec_x = np.loadtxt(trnimgfile) 
    dec_y = np.loadtxt(trnlabfile)

    print(\'train imgs before reshape \',dec_x.shape) 
    print(\'train labels \',dec_y.shape) 
    print(collections.Counter(dec_y))
#     dec_x = dec_x.reshape(shape)
#     dec_x = dec_x.permute(0, 4 1, 2, 3)
#     dec_x = dec_x.reshape(shape[0],shape[3],shape[1],shape[2]) 
    print(\"shape >>>>>>>>>>>>>> \",)
    dec_x = dec_x.reshape(shape[0],3,32,32)
    print(\'train imgs after reshape \',dec_x.shape) 

    batch_size = 100
    num_workers = 0

    #torch.Tensor returns float so if want long then use torch.tensor
    tensor_x = torch.Tensor(dec_x)
    tensor_y = torch.tensor(dec_y,dtype=torch.long)
    mnist_bal = TensorDataset(tensor_x,tensor_y) 
    train_loader = torch.utils.data.DataLoader(mnist_bal, 
        batch_size=batch_size,shuffle=True,num_workers=num_workers)
    
    

    best_loss = np.inf

    t0 = time.time()
    if args[\'train\']:
        enc_optim = torch.optim.Adam(encoder.parameters(), lr = args[\'lr\'])
        dec_optim = torch.optim.Adam(decoder.parameters(), lr = args[\'lr\'])
    
        for epoch in range(args[\'epochs\']):
            train_loss = 0.0
            tmse_loss = 0.0
            tdiscr_loss = 0.0
            # train for one epoch -- set nets to train mode
            encoder.train()
            decoder.train()
        
            for images,labs in train_loader:
            
                # zero gradients for each batch
                encoder.zero_grad()
                decoder.zero_grad()
                #print(images)
                images, labs = images.to(device), labs.to(device)
                #print(\'images \',images.size()) 
                labsn = labs.detach().cpu().numpy()
                #print(\'labsn \',labsn.shape, labsn)
            
                # run images
                z_hat = encoder(images)
            
                x_hat = decoder(z_hat) #decoder outputs tanh
                #print(\'xhat \', x_hat.size())
                #print(x_hat)
                mse = criterion(x_hat,images)
                #print(\'mse \',mse)
                
                       
                resx = []
                resy = []
            
                tc = np.random.choice(10,1)
                #tc = 9
                xbeg = dec_x[dec_y == tc]
                ybeg = dec_y[dec_y == tc] 
                xlen = len(xbeg)
                nsamp = min(xlen, 100)
                ind = np.random.choice(list(range(len(xbeg))),nsamp,replace=False)
                xclass = xbeg[ind]
                yclass = ybeg[ind]
            
                xclen = len(xclass)
                #print(\'xclen \',xclen)
                xcminus = np.arange(1,xclen)
                #print(\'minus \',xcminus.shape,xcminus)
                
                xcplus = np.append(xcminus,0)
                #print(\'xcplus \',xcplus)
                xcnew = (xclass[[xcplus],:])
                #xcnew = np.squeeze(xcnew)
                xcnew = xcnew.reshape(xcnew.shape[1],xcnew.shape[2],xcnew.shape[3],xcnew.shape[4])
                #print(\'xcnew \',xcnew.shape)
            
                xcnew = torch.Tensor(xcnew)
                xcnew = xcnew.to(device)
            
                #encode xclass to feature space
                xclass = torch.Tensor(xclass)
                xclass = xclass.to(device)
                xclass = encoder(xclass)
                #print(\'xclass \',xclass.shape) 
            
                xclass = xclass.detach().cpu().numpy()
            
                xc_enc = (xclass[[xcplus],:])
                xc_enc = np.squeeze(xc_enc)
                #print(\'xc enc \',xc_enc.shape)
            
                xc_enc = torch.Tensor(xc_enc)
                xc_enc = xc_enc.to(device)
                
                ximg = decoder(xc_enc)
                
                mse2 = criterion(ximg,xcnew)
            
                comb_loss = mse2 + mse
                comb_loss.backward()
            
                enc_optim.step()
                dec_optim.step()
            
                train_loss += comb_loss.item()*images.size(0)
                tmse_loss += mse.item()*images.size(0)
                tdiscr_loss += mse2.item()*images.size(0)
            
                 
            # print avg training statistics 
            train_loss = train_loss/len(train_loader)
            tmse_loss = tmse_loss/len(train_loader)
            tdiscr_loss = tdiscr_loss/len(train_loader)
            print(\'Epoch: {} \\tTrain Loss: {:.6f} \\tmse loss: {:.6f} \\tmse2 loss: {:.6f}\'.format(epoch,
                    train_loss,tmse_loss,tdiscr_loss))
            
        
        
            #store the best encoder and decoder models
            #here, /crs5 is a reference to 5 way cross validation, but is not
            #necessary for illustration purposes
            if train_loss < best_loss:
                print(\'Saving..\')
#                 path_enc = \"C:\\\\Users\\\\Research6\\\\Desktop\\\\smote\"  + \'\\\\bst_enc.pth\'
#                 path_dec = \"C:\\\\Users\\\\Research6\\\\Desktop\\\\smote\"  + \'\\\\bst_dec.pth\'
                path_enc = path + \'\\\\bst_enc.pth\'
                path_dec = path + \'\\\\bst_dec.pth\'
                #    path_enc = \'/content/gdrive/My Drive/smote/\' \\
                #     + str(i) + \'/bst_enc.pth\'
                # path_dec = \'/content/gdrive/My Drive/smote/\' \\
                #     + str(i) + \'/bst_dec.pth\'
             
                torch.save(encoder.state_dict(), path_enc)
                torch.save(decoder.state_dict(), path_dec)
        
                best_loss = train_loss
        
        
        #in addition, store the final model (may not be the best) for
        #informational purposes
        path_enc = path + \'\\\\f_enc.pth\'
        path_dec = path  + \'\\\\f_dec.pth\'
        print(path_enc)
        print(path_dec)
        torch.save(encoder.state_dict(), path_enc)
        torch.save(decoder.state_dict(), path_dec)
        print()
              
    t1 = time.time()
    print(\'total time(min): {:.2f}\'.format((t1 - t0)/60))             
 
t4 = time.time()
print(\'final time(min): {:.2f}\'.format((t4 - t3)/60))

    标签: pytorch smote


    【解决方案1】:

    @Sadaf Shafi,我在 pytorch 上也遇到了同样的问题,您可以尝试在解码器的 nn.sequential() 中取消注释以下行。 #3d 和 32 乘 32。

    nn.Conv2d(self.dim_h * 4, self.dim_h * 8, 4, 1, 0, 偏差=假),

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 2022-01-19
      • 1970-01-01
      相关资源
      最近更新 更多