linux(manjaro) tensorflow2.1 conda cuda10 双显卡笔记本深度学习环境搭建

本文主要是介绍linux(manjaro) tensorflow2.1 conda cuda10 双显卡笔记本深度学习环境搭建,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

linux(manjaro) tensorflow2.1 conda cuda10 双显卡笔记本深度学习环境搭建

下学期要学tensorflow,看着我可怜的1050ti,流下了贫穷的泪水,但无奈要做实验啊,学还是得学的,安装过程记录一下,仅供参考

关于manjaro

之前写过一篇怎么安装manjaro的文章来着,虽然manjaro在国内不是大众发行版,但在尝试过诸多linux后,我最终留在了manjaro.

双显卡驱动

我的驱动,直接上图
驱动

Anaconda

一开始我尝试用pacman直接安装tf cuda cudnn等,很简单

tf CPU
sudo pacman -S python-tensorflow-opt
tf GPU
sudo pacman -S python-tensorflow-opt-cuda cuda cudnn

但是GUP版装好之后运行测试会报
RuntimeError: cuda runtime error (35) : CUDA driver version is insufficient for CUDA runtime version at …
原因:CUDA驱动版本不满足CUDA运行版本。
具体显卡驱动与CUDA版本对应见下
https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html
在这里插入图片描述
我的是440xx 而软件库中提供的是cuda11

不想换驱动,那就给 cuda 和 tf 降级

conda安装

sudo pacman -S anacondaconda -h

如果有conda:命令未找到的报错,就需要修改一下环境变量

export PATH=$PATH:/opt/anaconda/bin

CUDA CUDNN

conda install cudatoolkit=10.1 cudnn=7.6 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/

tensorflow2.1

conda create -n tf2-gpu tensorflow-gpu==2.1 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/

装好后,检查环境

conda env list# conda environments:
#
tf2-gpu                  $home/.conda/envs/tf2-gpu
base                  *  /opt/anaconda
进入环境并测试

与win不同,linux进入conda环境要使用source activate,退出则是conda deactivate
要进入刚才搭建的tf2的环境只需要输入source activate tf2-gpu

source activate tf2-gpu(tf2-gpu) git clone https://hub.fastgit.org/guangfuhao/Deeplearning(tf2-gpu) cd Deeplearning(tf2-gpu) cp mnist.npz <你的测试目录>(tf2-gpu) pip install matplotlib numpy

编辑测试程序,很短就用vim test.py,注意将这个test.py与之前下载的mnist.npz放到同一目录下

测试程序
# 1.Import the neccessary libraries needed
import numpy as np
import tensorflow as tf
import matplotlib
from matplotlib import pyplot as plt######################################################################### 2.Set default parameters for plots
matplotlib.rcParams['font.size'] = 20
matplotlib.rcParams['figure.titlesize'] = 20
matplotlib.rcParams['figure.figsize'] = [9, 7]
matplotlib.rcParams['font.family'] = ['STKaiTi']
matplotlib.rcParams['axes.unicode_minus'] = False########################################################################
# 3.Initialize Parameters# Initialize learning rate
lr = 1e-3
# Initialize loss array
losses = []
# Initialize the weights layers and the bias layers
w1 = tf.Variable(tf.random.truncated_normal([784, 256], stddev=0.1))
b1 = tf.Variable(tf.zeros([256]))
w2 = tf.Variable(tf.random.truncated_normal([256, 128], stddev=0.1))
b2 = tf.Variable(tf.zeros([128]))
w3 = tf.Variable(tf.random.truncated_normal([128, 10], stddev=0.1))
b3 = tf.Variable(tf.zeros([10]))######################################################################### 4.Import the minist dataset by numpy offlinedef load_mnist():# define the directory where mnist.npz is(Please watch the '\'!)path = r'./mnist.npz'f = np.load(path)x_train, y_train = f['x_train'], f['y_train']x_test, y_test = f['x_test'], f['y_test']f.close()return (x_train, y_train), (x_test, y_test)(train_image, train_label), _ = load_mnist()
x = tf.convert_to_tensor(train_image, dtype=tf.float32) / 255.
y = tf.convert_to_tensor(train_label, dtype=tf.int32)
# Reshape x from [60k, 28, 28] to [60k, 28*28]
x = tf.reshape(x, [-1, 28*28])######################################################################### 5.Combine x and y as a tuple and batch them
train_db = tf.data.Dataset.from_tensor_slices((x, y)).batch(128)
'''
#Encapsulate train_db as an iterator object
train_iter = iter(train_db)
sample = next(train_iter)
'''######################################################################### 6.Iterate database for 20 times
for epoch in range(20):# For every batch:x:[128, 28*28],y: [128]for step, (x, y) in enumerate(train_db):with tf.GradientTape() as tape:  # tf.Variable# x: [b, 28*28]# h1 = x@w1 + b1# [b, 784]@[784, 256] + [256] => [b, 256] + [256] => [b, 256] + [b, 256]h1 = x@w1 + tf.broadcast_to(b1, [x.shape[0], 256])h1 = tf.nn.relu(h1)# [b, 256] => [b, 128]h2 = h1@w2 + b2h2 = tf.nn.relu(h2)# [b, 128] => [b, 10]out = h2@w3 + b3# y: [b] => [b, 10]y_onehot = tf.one_hot(y, depth=10)# compute loss# mse = mean(sum(y-out)^2)# [b, 10]loss = tf.square(y_onehot - out)# mean: scalarloss = tf.reduce_mean(loss)# compute gradientsgrads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3])# Update the weights and the biasw1.assign_sub(lr * grads[0])b1.assign_sub(lr * grads[1])w2.assign_sub(lr * grads[2])b2.assign_sub(lr * grads[3])w3.assign_sub(lr * grads[4])b3.assign_sub(lr * grads[5])if step % 100 == 0:print(epoch, step, 'loss:', float(loss))losses.append(float(loss))######################################################################### 7.Show the change of losses via matplotlib
plt.figure()
plt.plot(losses, color='C0', marker='s', label='训练')
plt.xlabel('Epoch')
plt.legend()
plt.ylabel('MSE')
# Save figure as '.svg' file
# plt.savefig('forward.svg')
plt.show()
python3 test.py

不出意外会有类似的输出
在这里插入图片描述
最后画出一张图
在这里插入图片描述

ps: 如何优雅的监控GPU
watch -n 1 nvidia-smi

在这里插入图片描述
好了,环境搭建大功告成
在我的机器上这个过程是成立的,如果有什么疑问欢迎在评论区留言

这篇关于linux(manjaro) tensorflow2.1 conda cuda10 双显卡笔记本深度学习环境搭建的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/252970

相关文章

jdk21下载、安装详细教程(Windows、Linux、macOS)

《jdk21下载、安装详细教程(Windows、Linux、macOS)》本文介绍了OpenJDK21的下载地址和安装步骤,包括Windows、Linux和macOS平台,下载后解压并设置环境变量,最... 目录1、官网2、下载openjdk3、安装4、验证1、官网官网地址:OpenJDK下载地址:Ar

linux本机进程间通信之UDS详解

《linux本机进程间通信之UDS详解》文章介绍了Unix域套接字(UDS)的使用方法,这是一种在同一台主机上不同进程间通信的方式,UDS支持三种套接字类型:SOCK_STREAM、SOCK_DGRA... 目录基础概念本机进程间通信socket实现AF_INET数据收发示意图AF_Unix数据收发流程图A

linux环境openssl、openssh升级流程

《linux环境openssl、openssh升级流程》该文章详细介绍了在Ubuntu22.04系统上升级OpenSSL和OpenSSH的方法,首先,升级OpenSSL的步骤包括下载最新版本、安装编译... 目录一.升级openssl1.官网下载最新版openssl2.安装编译环境3.下载后解压安装4.备份

Python中conda虚拟环境创建及使用小结

《Python中conda虚拟环境创建及使用小结》本文主要介绍了Python中conda虚拟环境创建及使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录0.前言1.Miniconda安装2.conda本地基本操作3.创建conda虚拟环境4.激活c

Python与DeepSeek的深度融合实战

《Python与DeepSeek的深度融合实战》Python作为最受欢迎的编程语言之一,以其简洁易读的语法、丰富的库和广泛的应用场景,成为了无数开发者的首选,而DeepSeek,作为人工智能领域的新星... 目录一、python与DeepSeek的结合优势二、模型训练1. 数据准备2. 模型架构与参数设置3

linux打包解压命令方式

《linux打包解压命令方式》文章介绍了Linux系统中常用的打包和解压命令,包括tar和zip,使用tar命令可以创建和解压tar格式的归档文件,使用zip命令可以创建和解压zip格式的压缩文件,每... 目录Lijavascriptnux 打包和解压命令打包命令解压命令总结linux 打包和解压命令打

linux如何复制文件夹并重命名

《linux如何复制文件夹并重命名》在Linux系统中,复制文件夹并重命名可以通过使用“cp”和“mv”命令来实现,使用“cp-r”命令可以递归复制整个文件夹及其子文件夹和文件,而使用“mv”命令可以... 目录linux复制文件夹并重命名我们需要使用“cp”命令来复制文件夹我们还可以结合使用“mv”命令总

Linux使用cut进行文本提取的操作方法

《Linux使用cut进行文本提取的操作方法》Linux中的cut命令是一个命令行实用程序,用于从文件或标准输入中提取文本行的部分,本文给大家介绍了Linux使用cut进行文本提取的操作方法,文中有详... 目录简介基础语法常用选项范围选择示例用法-f:字段选择-d:分隔符-c:字符选择-b:字节选择--c

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法

《ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法》本文介绍了Elasticsearch的基本概念,包括文档和字段、索引和映射,还详细描述了如何通过Docker... 目录1、ElasticSearch概念2、ElasticSearch、Kibana和IK分词器部署