Python 3.x RuntimeError:四维权重X应为四维输入,但得到的是大小为Y的三维输入
我正在建立一个CNN,在EMNIST数据集上进行图像分类 为此,我有以下数据集:Python 3.x RuntimeError:四维权重X应为四维输入,但得到的是大小为Y的三维输入,python-3.x,runtime-error,pytorch,cnn,Python 3.x,Runtime Error,Pytorch,Cnn,我正在建立一个CNN,在EMNIST数据集上进行图像分类 为此,我有以下数据集: import scipy .io emnist = scipy.io.loadmat(DIRECTORY + '/emnist-letters.mat') data = emnist ['dataset'] X_train = data ['train'][0, 0]['images'][0, 0] X_train = X_train.reshape((-1,28,28), order='F') y_train
import scipy .io
emnist = scipy.io.loadmat(DIRECTORY + '/emnist-letters.mat')
data = emnist ['dataset']
X_train = data ['train'][0, 0]['images'][0, 0]
X_train = X_train.reshape((-1,28,28), order='F')
y_train = data ['train'][0, 0]['labels'][0, 0]
X_test = data ['test'][0, 0]['images'][0, 0]
X_test = X_test.reshape((-1,28,28), order = 'F')
y_test = data ['test'][0, 0]['labels'][0, 0]
形状:
train_dataset = torch.utils.data.TensorDataset(torch.from_numpy(X_train), torch.from_numpy(y_train))
test_dataset = torch.utils.data.TensorDataset(torch.from_numpy(X_test), torch.from_numpy(y_test))
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=False)
class CNNModel(nn.Module):
def __init__(self):
super(CNNModel, self).__init__()
self.cnn_layers = Sequential(
# Defining a 2D convolution layer
Conv2d(1, 4, kernel_size=3, stride=1, padding=1),
BatchNorm2d(4),
ReLU(inplace=True),
MaxPool2d(kernel_size=2, stride=2),
# Defining another 2D convolution layer
Conv2d(4, 4, kernel_size=3, stride=1, padding=1),
BatchNorm2d(4),
ReLU(inplace=True),
MaxPool2d(kernel_size=2, stride=2),
)
self.linear_layers = Sequential(
Linear(4 * 7 * 7, 10)
)
# Defining the forward pass
def forward(self, x):
x = self.cnn_layers(x)
x = x.view(x.size(0), -1)
x = self.linear_layers(x)
return x
model = CNNModel()
我的模型如下所示:
train_dataset = torch.utils.data.TensorDataset(torch.from_numpy(X_train), torch.from_numpy(y_train))
test_dataset = torch.utils.data.TensorDataset(torch.from_numpy(X_test), torch.from_numpy(y_test))
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=False)
class CNNModel(nn.Module):
def __init__(self):
super(CNNModel, self).__init__()
self.cnn_layers = Sequential(
# Defining a 2D convolution layer
Conv2d(1, 4, kernel_size=3, stride=1, padding=1),
BatchNorm2d(4),
ReLU(inplace=True),
MaxPool2d(kernel_size=2, stride=2),
# Defining another 2D convolution layer
Conv2d(4, 4, kernel_size=3, stride=1, padding=1),
BatchNorm2d(4),
ReLU(inplace=True),
MaxPool2d(kernel_size=2, stride=2),
)
self.linear_layers = Sequential(
Linear(4 * 7 * 7, 10)
)
# Defining the forward pass
def forward(self, x):
x = self.cnn_layers(x)
x = x.view(x.size(0), -1)
x = self.linear_layers(x)
return x
model = CNNModel()
下面的代码是我用来训练我的模型的代码的一部分:
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = Variable(images)
labels = Variable(labels)
# Forward pass to get output/logits
outputs = model(images)
但是,通过执行我的代码,我得到以下错误:
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [4, 1, 3, 3], but got 3-dimensional input of size [100, 28, 28] instead
因此,当我的输入是3D时,需要4D输入。我该怎么做才能使3D模型比4D模型更符合要求
有人问了一个类似的问题,但我不知道如何将其转换为我的代码。卷积期望输入的大小为[batch\u size,channel,height,width],但图像的大小为[batch\u size,height,width],通道尺寸缺失。灰度用单个通道表示,您已将第一次卷积的
in_channels
正确设置为1,但图像没有匹配维度
您可以轻松地使用添加单个维度
另外,请不要使用,它在两年前发布的PyTorch 0.4.0中被弃用,它的所有功能都已合并到张量中
枚举(列车装载机)中的i(图像、标签):
#添加单个通道标注
#发件人:[批次大小、高度、宽度]
#收件人:[批量大小,1,高度,宽度]
图像=图像。取消查询(1)
#向前传递以获取输出/登录
输出=模型(图像)
卷积预计输入的大小为[batch\u size,channel,height,width],但图像的大小为[batch\u size,height,width],通道尺寸缺失。灰度用单个通道表示,您已将第一次卷积的
in_channels
正确设置为1,但图像没有匹配维度
您可以轻松地使用添加单个维度
另外,请不要使用,它在两年前发布的PyTorch 0.4.0中被弃用,它的所有功能都已合并到张量中
枚举(列车装载机)中的i(图像、标签):
#添加单个通道标注
#发件人:[批次大小、高度、宽度]
#收件人:[批量大小,1,高度,宽度]
图像=图像。取消查询(1)
#向前传递以获取输出/登录
输出=模型(图像)
谢谢你的回答,Michael。你所说的三行评论是什么意思?你有如何做到这一点的示例代码吗?注释描述了下面这一行代码的功能。谢谢你的回答。你所说的三行评论是什么意思?你有如何做的示例代码吗?注释描述了下面这行代码的作用。