赞
踩
说明:使用pip install torch或pip3 install torch命令安装torch库,下完了安装时提示的错误
解决:使用官方提供安装命令安装,地址如下:
https://pytorch.org/
选择合适配置复制命令到cmd上运行即可(windows)
说明:import torch后使用torch方法时报的错误
解决:torch版本与Python版本兼容问题,使用Python3.5环境运行代码,亲测3.6不行。
说明:使用Data.TensorDataset方法时传入参数有误报的错误
解决:新版Data.TensorDataset中data_tensor 和target_tensor去掉,输入变成了可变参数。即直接输入参数,不需要指代,例如:
train_dataset = Data.TensorDataset(data_tensor=x, target_tensor=y)
#改为
train_dataset = Data.TensorDataset(x,y)
说明:使用Data.DataLoader方法时缺少相关语句报的错误
解决:根据报错提示,添加缺少语句即可。
缺少语句为if name == ‘main’:
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
举例:
import torch import torch.utils.data as Data torch.manual_seed(1) BATCH_SIZE = 5 x = torch.linspace(1, 10, 10) y = torch.linspace(10, 1, 10) #先转换成torch能识别的Dataset torch_dataset = Data.TensorDataset(x, y) #把dataset放入DataLoader loader = Data.DataLoader( dataset=torch_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2 ) if __name__ == '__main__': for epoch in range(3): #训练所有数据,一共3次 for step,(batch_x, batch_y) in enumerate(loader): #每一步loader释放一小批数据用来学习 #训练的地方 #打印数据 print('Epoch:', epoch, '|Step:',step,'|batch x:', batch_x.numpy(), '|batch y', batch_y.numpy())
说明:获取方法的输出值报的错误,我这里是CNN网络
解决:保证方法输出与声明输出的变量一致即可,比如方法返回2个输出,声明两个变量来赋值,例如:
test_output, last_layer = cnn(test_x)
说明:Pytorch版本过低加载模型报错,Can’t get attribute ‘_rebuild_parameter’ on <module ‘torch._utils’ from ‘/opt/conda/lib/python3.6/site-packages/torch/_utils.py’>
解决:升级Pytorch版本,使用合适cpu或gpu版本的torch
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。