解决TypeError: conv2d() received an invalid combination of arguments

1.      

        看b站视频用卷积进行手写数字识别源代码如下:

#输入为28*28*1 经过第一次卷积计算后为 28*28*16   池化变为14*14*16    第二次卷积14*14*32    池化7*7*32即全连接(10为10分类)
#前向传播:经过卷积1、2     再把结果转化为向量格式   结果×全连接层(w*x+b)

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets,transforms


#定义超参数
input_size = 28     #图像的总尺寸28*28
num_classes = 10    #标签的种类数
num_epochs = 3      #训练的总循环周期
batch_size = 64     #一个撮(批次)的大小,64张图片

# 数据集
train_dataset = datasets.MNIST("./data",train=True,transform=transforms.ToTensor(),download=True)
test_dataset = datasets.MNIST("./data",train=False,transform=transforms.ToTensor())
#构建batch数据
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,batch_size=batch_size,shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,batch_size=batch_size,shuffle=True)


class CNN(nn.Module):
    def __init__(self):
        super(CNN,self).__init__()
        self.conv1=nn.Sequential(            #输入大小(1, 28,28)
            nn.Conv2d(
                in_channels=1,               #灰度图
                out_channels=16,             #要得到几多少个特征图
                kernel_size=5,               #卷积核大小
                stride=1,                    #步长
                padding=2,                   #如果希望卷积后大小跟原来一样, 需要设置padding=(kerneI_size-1)/2 if stride=1
            ),                               #输出的特征图为(16, 28, 28)
            nn.ReLU(),  # relu层
            nn.MaxPool2d(kernel_size=2),     #进行池化操作(2x2区域),输出结果为: (16, 14, 14)
        )

        self.conv2 = nn.Sequential(          #下一个套餐的输入(16, 14, 14)
            nn.Conv2d(16,32,5,1,2),          #输出(32, 14,14)此处的16为上面的到的16个特征图
            nn.ReLU(),                       # relu层
            nn.MaxPool2d(2),                 #输出(32,7,7)
        )

        self.out = nn.Linear(32*7*7,10)      # 全连接层得到的结果(输入32*37*7 输出10)

    def forward(self,x):                     #前向传播
        x = self.conv1(x),
        x = self.conv2(x)
        x = x.view(x.size(0),-1)           #flatten操作,结果为: (batch_ size, 32*7*7)
        output = self.out(x)                 #上步向量乘全连接层
        return output


def accuracy(predictions, labels):
    pred = torch.max(predictions.data,1)[1]
    rights = pred.eq(labels.data.view_as(pred)).sum()
    return rights, len(labels)


#实例化
net = CNN()
#损失函数
criterion = nn.CrossEntropyLoss()
#优化器
optimizer = optim.Adam(net.parameters(), lr=0.001)  #定义优化器,普通的随机梯度下降算法


#开始训练循环
for epoch in range(num_epochs):
    #当前epoch的结果保存下来
    train_rights = []

    for batch_idx,(data,target) in enumerate(train_loader): #针对容器中的每一个批进行循环
        #对于一个可迭代的(iterable)/可遍历的对象(如列表、字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值
        net.train()
        output=net(data)
        loss = criterion(output,target)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        right = accuracy(output,target)
        train_rights.append(right)

        if batch_idx % 100 == 0:

            net.eval()
            val_rights = []             #保存精度

            for (data,target) in test_loader:
                output = net(data)
                right = accuracy(output,target)
                val_rights.append(right)

            #准确率计算
            train_r = (sum([tup[0] for tup in train_rights]),sum([tup[1] for tup in train_rights]))
            val_r = (sum([tup[0] for tup in val_rights]),sum([tup[1] for tup in val_rights]))
            print('当前epoch:{}      [{}/{} ({:.0f}%)]\t       损失: {:.6f}\t      训练集准确率: {:.2f}%\t    测试集正确率: {:.2f}%'.format(
                epoch, batch_idx * batch_size, len(train_loader.dataset),
                100.*batch_idx / len(train_loader),
                loss.data,
                100. * train_r[0].numpy() / train_r[1],
                100. * val_r[0].numpy() / val_r[1]))

        运行代码报如下错误:

        看了一下大概意思就是往conv2传的参数格式不对,检查一下,发现太粗心,在54行多打了个“,”修改后正常运行了

 2.

        改完后搜了一下,看看有没有同学跟我一样粗心的,发现一个同学也报同样的错误,但不是多了一个逗号,他的源代码如下:

import torch
import torch.nn as nn
from torchvision import datasets, transforms
from torchvision.datasets import MNIST
import torch.optim as optim
import numpy as np
import torch.nn.functional as F
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader
 
# -- 定义超参数
input_size = 28  # -- 图片尺寸28*28
num_class = 10  # -- 最后分类情况 10分类0-9
num_epochs = 3  # -- 循环3个周期
batch_size = 64  # -- batch大小64
 
# -- 训练集
train_dataset = MNIST(root="./data/", train=True, transform=transforms.ToTensor(), download=True)
# -- 测试集
test_dataset = MNIST(root="./data/", train=False, transform=transforms.ToTensor())
 
# -- 构建batch数据
train_loader = DataLoader(dataset=train_dataset,
                          batch_size=batch_size,
                          shuffle=True)
test_loader = DataLoader(dataset=test_dataset,
                         batch_size=batch_size,
                         shuffle=True)
 
 
# -- 定义模型
class CNN_Model(nn.Module):
    def __init__(self):
        super(CNN_Model, self).__init__()
        # -- 第一个卷积单元  卷积 relu 池化        输入图为28*28*1 输出结果为 16 *14 *14
        self.conv1 = nn.Sequential(
            nn.Conv2d(in_channels=1,  # -- 输入的灰度图  1通道 所以是1
                      out_channels=16,  # -- 想得到多少个特征图  输出的特征图16个
                      kernel_size=5,  # -- 卷积核的大小 5*5 提取成1个点
                      stride=1,  # -- 步长为1
                      padding=2  # -- 填充0
                      ),
            nn.ReLU(),  # -- relu层
            nn.MaxPool2d(kernel_size=2)  # -- 池化操作 输出结果为 16 *14 *14
        )
        # -- 第二个卷积单元 输入为16*14*14  输出10分类
        self.conv2 = nn.Sequential(
            nn.Conv2d(16, 32, 5, 1, 2),  # -- 输入为16*14*14
            nn.ReLU(),
            nn.MaxPool2d(2)  # -- 输出为32*7*7
        )
        self.out = nn.Linear(32 * 7 * 7, 10)  # -- 全连接层 输出10分类
 
    # -- 前向传播
    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = x.view(x.size(0), -1)  # -- flatten操作  拉成一维数组形式  才能进行全连接输出
        output = self.out(x)
        return output
 
 
# -- 定义一个评估函数
def pinggu(predictions, labels):
    pred = torch.max(predictions.data, 1)[1]
    right = pred.eq(labels.data.view_as(pred)).sum()
    return right, len(labels)
 
 
# -- 训练网络模型
# -- 实例化
mymodel = CNN_Model()
# -- 损失函数
loss_fn = nn.CrossEntropyLoss()
# -- 优化器    # 普通的随机梯度下降
optimizer = optim.Adam(mymodel.parameters(), lr=0.001)
 
# -- 开始训练
for epoch in range(num_epochs):
    # -- 当前epoch结果保存起来
    train_right = []
    for batch_idx, (data, target) in enumerate(train_loader):
        mymodel.train()
        output = mymodel(data)
        loss = loss_fn(output, target)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        right = pinggu(output, target)
        train_right.append(right)
 
        if batch_idx % 100 == 0:
            mymodel.eval()
            val_right = []
            for (data, target) in enumerate(test_loader):
                output = mymodel(data)
                right = pinggu(output, target)
                val_right.append(right)
 
            # -- 准确率计算
            train_r = (sum([tup[0] for tup in train_right]), sum([tup[1] for tup in train_right]))
            val_r = (sum([tup[0] for tup in val_right]), sum([tup[1] for tup in val_right]))
 
            print('当前epoch:{} [{}/{} ({:.0f}%)]\t 损失:{:.6f}\t 训练集准确率: {:.2f}%\t 测试集准确率:{:.2f}%'.format(
                epoch, batch_idx * batch_size, len(train_loader.dataset),
                       100. * batch_idx / len(train_loader),
                       loss.data,
                       100. * train_r[0].numpy() / train_r[1],
                       100. * val_r[0].numpy() / val_r[1]
            ))

        他报的错误如下:

        看了一下也是传入的参数格式不对,只需要把95行修改一下

for (data, target) in enumerate(test_loader):
改为
for (data, target) in  test_loader:
修改前:
for (data, target) in enumerate(test_loader):
#输出data结果为
0
1 
2
.
. 
data格式为int


修改后:
for (data, target) in  test_loader:
输出结果为:
tensor([[[[0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
格式为Tensor

        再看一下报错:

         原因:人家要的是Tensor,你传的是int

3.总结

        该问题就是传入的参数格式不对,出现问题可以检查一下问题处的数据传入

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
The `conv2d()` function is a convolutional layer in deep learning frameworks like TensorFlow and PyTorch. It takes a few arguments, including the input tensor, the filter (kernel) tensor, the stride, padding, and activation function. If you received an error message saying that `conv2d()` received an invalid combination of arguments, it means that you passed in one or more arguments that are not compatible with each other. Here are a few common reasons why this might happen: - Incorrect input shape: The input tensor must have a certain shape that depends on the framework and the specific convolutional layer you're using. For example, in TensorFlow, the input tensor should have shape `(batch_size, height, width, channels)`. If you pass in an input tensor with a different shape, you may get an error. - Incompatible filter shape: The filter tensor must have a certain shape that depends on the number of filters, filter size, and number of input channels. If you pass in a filter tensor with an incompatible shape, you may get an error. - Incorrect stride or padding: The stride and padding parameters must be chosen carefully to ensure that the output tensor has the desired shape. If you choose stride or padding values that are incompatible with the input or filter shape, you may get an error. - Incorrect activation function: Some convolutional layers require an activation function to be specified, while others do not. If you specify an activation function that is not compatible with the layer, you may get an error. To fix the error, you should carefully check the input, filter, stride, padding, and activation function parameters to ensure that they are compatible with each other and with the specific convolutional layer you're using.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值