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

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

linux-基础知识3

打包和压缩 zip 安装zip软件包 yum -y install zip unzip 压缩打包命令: zip -q -r -d -u 压缩包文件名 目录和文件名列表 -q:不显示命令执行过程-r:递归处理,打包各级子目录和文件-u:把文件增加/替换到压缩包中-d:从压缩包中删除指定的文件 解压:unzip 压缩包名 打包文件 把压缩包从服务器下载到本地 把压缩包上传到服务器(zip

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

阿里开源语音识别SenseVoiceWindows环境部署

SenseVoice介绍 SenseVoice 专注于高精度多语言语音识别、情感辨识和音频事件检测多语言识别: 采用超过 40 万小时数据训练,支持超过 50 种语言,识别效果上优于 Whisper 模型。富文本识别:具备优秀的情感识别,能够在测试数据上达到和超过目前最佳情感识别模型的效果。支持声音事件检测能力,支持音乐、掌声、笑声、哭声、咳嗽、喷嚏等多种常见人机交互事件进行检测。高效推

Linux 网络编程 --- 应用层

一、自定义协议和序列化反序列化 代码: 序列化反序列化实现网络版本计算器 二、HTTP协议 1、谈两个简单的预备知识 https://www.baidu.com/ --- 域名 --- 域名解析 --- IP地址 http的端口号为80端口,https的端口号为443 url为统一资源定位符。CSDNhttps://mp.csdn.net/mp_blog/creation/editor

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]