Python 3.x pytorch中的类型不匹配

Python 3.x pytorch中的类型不匹配,python-3.x,types,pytorch,Python 3.x,Types,Pytorch,我在试我的手电筒。 我得到这个错误: RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #2 'weight' in call to _thnn_conv2d_forward 这是我的代码(无耻地抄袭自在线教程): 班级网络(模块): 定义初始化(自): 超级(网络,自我)。\uuuu初始化 self.cnn_层=顺序( Conv2d(1,4,内核大小=3,步幅=

我在试我的手电筒。 我得到这个错误:

RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #2 'weight' in call to _thnn_conv2d_forward
这是我的代码(无耻地抄袭自在线教程):

班级网络(模块):
定义初始化(自):
超级(网络,自我)。\uuuu初始化
self.cnn_层=顺序(
Conv2d(1,4,内核大小=3,步幅=1,填充=1),
第2D(4)节,
ReLU(就地=真),
MaxPool2d(内核大小=2,步幅=2),
Conv2d(4,4,内核大小=3,步幅=1,填充=1),
第2D(4)节,
ReLU(就地=真),
MaxPool2d(内核大小=2,步幅=2)
)
self.linear\u层=顺序(
线性(900,10)
)
def前进(自身,x):
#self.weights=self.weights.double()
x=自.cnn_层(x)
x=x.view(x.size(0),-1)
x=自线性_层(x)
返回x
tdata=dt.数据(“列车”)
train_x=火炬从(tdata.get_train()[0]。重塑(925,1300300))
train_y=torch.from_numpy(tdata.get_train()[1].astype(int))
val_x=torch.from_numpy(tdata.get_test()[0]。重塑(102,1300300))
val_y=torch.from_numpy(tdata.get_test()[1].astype(int))
打印(值y.形状)
plt.imshow(tdata.get_train()[0][100],cmap='gray')
plt.show()
model=Net()
#定义优化器
optimizer=Adam(model.parameters(),lr=0.07)
#定义损失函数
标准=交叉熵()
#检查GPU是否可用
如果torch.cuda.U可用():
model=model.cuda()
criteria=criteria.cuda()
打印(模型)
def系列(EPOC):
模型列车()
tr_损失=0
#获取训练集
x\u列,y\u列=变量(列x.double()),变量(列y.double())
#获取验证集
x_val,y_val=变量(val_x),变量(val_y)
#将数据转换为GPU格式
如果torch.cuda.U可用():
x_train=x_train.cuda()
y_train=y_train.cuda()
x_val=x_val.cuda()
y_val=y_val.cuda()
#清除模型参数的梯度
optimizer.zero_grad()
#训练和验证集的预测
输出列车=型号(x列车.double())
输出值=模型(x值)
#计算训练和验证损失
损失列车=标准(输出列车,y列车)
损耗值=标准(输出值,y值)
列车损失。追加(列车损失)
val_loss.append(loss_val)
#计算所有模型参数的更新权重
列车后退时的损失()
optimizer.step()
tr_损失=损失_列车项目()
如果历元%2==0:
#打印验证丢失
打印('历元:',历元+1',\t',损失:',损失值)
n_时代=25
#存储培训损失的空列表
列车损失=[]
#用于存储验证损失的空列表
val_损失=[]
#训练模型
对于范围内的历元(n_历元):
列车(新纪元)
tdata.get\u train()和tdata.get\u test()返回一个元组(numpy(dtype='double'),numpy(dtype='int'))
我认为权重是一种内部数据结构。因此,其类型应由PyTorch自身进行调整。这里有什么问题?

你可以把
.to(torch.float32)
加到你的
train\ux
val\ux

张量中
你可以把
.to(torch.float32)
加到你的
train\ux
val\ux
张量中我不完全同意另一个答案。它部分地解决了这个问题。 好的,写:

# converting the target into torch format
train_y = train_y.astype(int);
train_y = torch.from_numpy(train_y).to(torch.float32)

# converting validation images into torch format
val_x = val_x.reshape(6000, 1, 28, 28)
val_x  = torch.from_numpy(val_x).to(torch.float32)

# converting the target into torch format
val_y = val_y.astype(int);
val_y = torch.from_numpy(val_y).to(torch.float32)
这很重要,但也很重要:

# computing the training and validation loss
y_train = y_train.long() #we convert the results because they aren't in the good format
y_train = y_train.squeeze_()
y_val = y_val.long() #we convert the results because they aren't in the good format
y_val = y_val.squeeze_()
loss_train = criterion(output_train, y_train)
loss_val = criterion(output_val, y_val)
train_losses.append(loss_train)
val_losses.append(loss_val)

就我而言,这解决了我的问题。

我不完全同意另一个答案。它部分地解决了这个问题。 好的,写:

# converting the target into torch format
train_y = train_y.astype(int);
train_y = torch.from_numpy(train_y).to(torch.float32)

# converting validation images into torch format
val_x = val_x.reshape(6000, 1, 28, 28)
val_x  = torch.from_numpy(val_x).to(torch.float32)

# converting the target into torch format
val_y = val_y.astype(int);
val_y = torch.from_numpy(val_y).to(torch.float32)
这很重要,但也很重要:

# computing the training and validation loss
y_train = y_train.long() #we convert the results because they aren't in the good format
y_train = y_train.squeeze_()
y_val = y_val.long() #we convert the results because they aren't in the good format
y_val = y_val.squeeze_()
loss_train = criterion(output_train, y_train)
loss_val = criterion(output_val, y_val)
train_losses.append(loss_train)
val_losses.append(loss_val)
就我而言,这解决了我的问题