【神经网络】-在linux环境下进行pytorch安装

在linux环境下进行pytorch安装

  • 创建pytorch环境
  • 在pytorch环境下安装pytorch
  • 检查是否安装成功
  • 在pytorch环境下运行python代码
  • 创建pytorch环境

    首先创建与自己服务器中python版本相匹配的pytorch虚拟环境

    conda create -n pytorch python=3.8
    

    在pytorch环境下安装pytorch

    接着进入所创建的pytorch环境中

    conda activate pytorch #
    

    然后去pytorch官网找到适合自己电脑配置的pytorch版本进行安装,在这里由于我是安装在服务器终端,所以选择了以下版本

    安装命令在官网上选好配置后会显示出来

    conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
    

    检查是否安装成功

    在pytorch环境下输入逐次输入以下命令

    python #检查python版本
    import torch
    torch.cuda.is_available()
    

    显示结果见下:(则表明已正确安装pytorch)

    在pytorch环境下运行python代码

    比如我想运行以下代码,代码来自视频博主莫烦

    """
    View more, visit my tutorial page: https://mofanpy.com/tutorials/
    My Youtube Channel: https://www.youtube.com/user/MorvanZhou
    Dependencies:
    torch: 0.4
    matplotlib
    """
    import os
    import torch
    import torch.nn.functional as F
    from torch.autograd import Variable
    import matplotlib.pyplot as plt
    
    # fake data
    x = torch.linspace(-5, 5, 200)  # x data (tensor), shape=(100, 1)
    x = Variable(x)
    x_np = x.data.numpy()   # numpy array for plotting
    
    # following are popular activation functions
    y_relu = torch.relu(x).data.numpy()
    y_sigmoid = torch.sigmoid(x).data.numpy()
    y_tanh = torch.tanh(x).data.numpy()
    y_softplus = F.softplus(x).data.numpy() # there's no softplus in torch
    # y_softmax = torch.softmax(x, dim=0).data.numpy() softmax is a special kind of activation function, it is about probability
    
    # plt to visualize these activation function
    if not os.path.exists("./example"):
    	os.makedirs("./example")
    plt.figure(1, figsize=(8, 6))
    plt.subplot(221)
    plt.plot(x_np, y_relu, c='red', label='relu')
    plt.ylim((-1, 5))
    plt.legend(loc='best')
    plt.savefig(os.path.join("./example","relu"+'.png'),bbox_inches = 'tight')#分别创建文件夹,分别储存命名图片
    plt.clf()
    
    plt.subplot(222)
    plt.plot(x_np, y_sigmoid, c='red', label='sigmoid')
    plt.ylim((-0.2, 1.2))
    plt.legend(loc='best')
    plt.savefig(os.path.join("./example","sigmoid"+'.png'),bbox_inches = 'tight')#分别创建文件夹,分别储存命名图片
    plt.clf()
    
    plt.subplot(223)
    plt.plot(x_np, y_tanh, c='red', label='tanh')
    plt.ylim((-1.2, 1.2))
    plt.legend(loc='best')
    plt.savefig(os.path.join("./example","tanh"+'.png'),bbox_inches = 'tight')#分别创建文件夹,分别储存命名图片
    plt.clf()
    
    plt.subplot(224)
    plt.plot(x_np, y_softplus, c='red', label='softplus')
    plt.ylim((-0.2, 6))
    plt.legend(loc='best')
    plt.savefig(os.path.join("./example","softplus"+'.png'),bbox_inches = 'tight')#分别创建文件夹,分别储存命名图片
    plt.clf()
    

    在终端上运行该份代码的命令为:
    (首先需要进入该份代码所在的文件夹,并激活pytorch环境)

    python torch_and_numpy.py
    

    注:torch_and_numpy.py为该份代码的文件名
    运行时,系统报错:
    说明在该环境下需要安装python的matplotlib包,相似的,后面运行程序时,如果还是出现ModuleNotFoundError,是同样的解决方法,即在该环境下安装所需要的包即可
    例如:

    conda install matplotlib
    

    注:在运行时,特别注意要先进入pytorch环境,再运行代码

    来源:electrochemjy

    物联沃分享整理
    物联沃-IOTWORD物联网 » 【神经网络】-在linux环境下进行pytorch安装

    发表评论