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


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

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

关于manjaro

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

双显卡驱动

我的驱动,直接上图
linux(manjaro) tensorflow2.1 conda cuda10  双显卡笔记本深度学习环境搭建

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
linux(manjaro) tensorflow2.1 conda cuda10  双显卡笔记本深度学习环境搭建
我的是440xx 而软件库中提供的是cuda11

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

conda安装

sudo pacman -S anaconda

conda -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 offline

def 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 + b2
h2 = 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: scalar
loss = tf.reduce_mean(loss)

# compute gradients
grads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3])
# Update the weights and the bias
w1.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

不出意外会有类似的输出
linux(manjaro) tensorflow2.1 conda cuda10  双显卡笔记本深度学习环境搭建
最后画出一张图
linux(manjaro) tensorflow2.1 conda cuda10  双显卡笔记本深度学习环境搭建

ps: 如何优雅的监控GPU

watch -n 1 nvidia-smi

linux(manjaro) tensorflow2.1 conda cuda10  双显卡笔记本深度学习环境搭建
好了,环境搭建大功告成
在我的机器上这个过程是成立的,如果有什么疑问欢迎在评论区留言

原创:https://www.panoramacn.com
源码网提供WordPress源码,帝国CMS源码discuz源码,微信小程序,小说源码,杰奇源码,thinkphp源码,ecshop模板源码,微擎模板源码,dede源码,织梦源码等。

专业搭建小说网站,小说程序,杰奇系列,微信小说系列,app系列小说

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

免责声明,若由于商用引起版权纠纷,一切责任均由使用者承担。

您必须遵守我们的协议,如果您下载了该资源行为将被视为对《免责声明》全部内容的认可-> 联系客服 投诉资源
www.panoramacn.com资源全部来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。 敬请谅解! 侵权删帖/违法举报/投稿等事物联系邮箱:2640602276@qq.com
未经允许不得转载:书荒源码源码网每日更新网站源码模板! » linux(manjaro) tensorflow2.1 conda cuda10 双显卡笔记本深度学习环境搭建
关注我们小说电影免费看
关注我们,获取更多的全网素材资源,有趣有料!
120000+人已关注
分享到:
赞(0) 打赏

评论抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

您的打赏就是我分享的动力!

支付宝扫一扫打赏

微信扫一扫打赏