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

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

linux hostname设置全过程

《linuxhostname设置全过程》:本文主要介绍linuxhostname设置全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录查询hostname设置步骤其它相关点hostid/etc/hostsEDChina编程A工具license破解注意事项总结以RHE

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

SQLite3 在嵌入式C环境中存储音频/视频文件的最优方案

《SQLite3在嵌入式C环境中存储音频/视频文件的最优方案》本文探讨了SQLite3在嵌入式C环境中存储音视频文件的优化方案,推荐采用文件路径存储结合元数据管理,兼顾效率与资源限制,小文件可使用B... 目录SQLite3 在嵌入式C环境中存储音频/视频文件的专业方案一、存储策略选择1. 直接存储 vs

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

在Linux终端中统计非二进制文件行数的实现方法

《在Linux终端中统计非二进制文件行数的实现方法》在Linux系统中,有时需要统计非二进制文件(如CSV、TXT文件)的行数,而不希望手动打开文件进行查看,例如,在处理大型日志文件、数据文件时,了解... 目录在linux终端中统计非二进制文件的行数技术背景实现步骤1. 使用wc命令2. 使用grep命令