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

本文详细指导如何在Linux服务器上创建Python 3.8的PyTorch虚拟环境,包括环境创建、指定版本安装、检查安装及运行示例代码。遇到的matplotlib缺失问题也一并解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建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环境,再运行代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

electrochemjy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值