采用miniconda3安装Tensorflow和PyTorch GPU及Pycharm的配置

一、Tensorflow介绍

tenroflow是Google开源软件库,为机器学习工程中的问题提供了一整套解决方案。

二、Pycharm 介绍

PyTorch是一个由Facebook开发的深度学习框架,使用动态计算图来进行张量计算和自动微分。这个特性使得模型的构建更灵活,更容易调试,并且能够支持更复杂的模型。

三、安装miniconda3

3.1、下载miniconda3

3.2、直接按提示安装miniconda3

四、安装Tensorflow和PyTorch

4.0、配置.condarc

channels:
  - defaults
show_channel_urls: true
ssl_verify: false
default_channels:
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

4.1、创建dlGPU虚拟环境

conda create -n dlGPU python=3.7

4.2、激活环境

conda activate dlGPU

C:\Users\主机名>conda activate dlGPU

(dlGPU) C:\Users\主机名>

4.4、下在CUDA

developer.nvidia.com

选择一个适合自己的版本安装

4.5、安装Tensorflow-gpu

pip install tensorflow-gpu==2.6.0 --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple

4.6、验证Tensorflow

在当前虚拟环境下,启动python后,输入import tensorflow as tf 验证tensorflow是否安装成功

Python 3.7.1 (default, Oct 28 2018, 08:39:03) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.__version__
'2.6.0'

4.7、安装scikit-learn

conda install SciPy

conda install NumPy


conda install scikit-learn

4.8、安装pytorch,我们选择cuda 11.8,这个根据自己机器来选定,我们这里只是测试。

conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

 

4.9、验证pyTorch,输入python

(dlCPU) C:\Users\主机名>python
Python 3.7.16 (default, Jan 17 2023, 16:06:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.cuda.is_available()
False
>>> torch.is_available()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'torch' has no attribute 'is_available'
>>> torch.__version__
'1.13.1'

 我们这里没有安装CUDA,如果安装了,就会显示True

4.10、安装CUDA 11.8

4.11、安装步骤安装

C:\Users\主机名>nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Sep_21_10:41:10_Pacific_Daylight_Time_2022
Cuda compilation tools, release 11.8, V11.8.89
Build cuda_11.8.r11.8/compiler.31833905_0
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\extras\demo_suite>bandwidthTest.exe
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\extras\demo_suite>bandwidthTest.exe
[CUDA Bandwidth Test] - Starting...
Running on...

 Device 0: NVIDIA GeForce RTX 3060 Laptop GPU
 Quick Mode

 Host to Device Bandwidth, 1 Device(s)
 PINNED Memory Transfers
   Transfer Size (Bytes)        Bandwidth(MB/s)
   33554432                     12366.2

 Device to Host Bandwidth, 1 Device(s)
 PINNED Memory Transfers
   Transfer Size (Bytes)        Bandwidth(MB/s)
   33554432                     12792.3

 Device to Device Bandwidth, 1 Device(s)
 PINNED Memory Transfers
   Transfer Size (Bytes)        Bandwidth(MB/s)
   33554432                     259424.4

Result = PASS

NOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.

说明安装成功了。

五、配置Pycharm

import tensorflow as tf;
import torch;
import numpy as np;
arr=np.ones((12,12));
print("arr的数据类型为:"+str(arr.dtype))
t=torch.tensor(arr)
print(t)
print(torch.__version__);
print(torch.cuda.is_available())
print(tf.__version__);

 

六、采用conda安装显卡驱动方式(彩蛋)

6.1、安装cudatoolkit

conda install cudatoolkit=11.3.1 

6.2、安装cudnn

conda install cudnn=8.2.1

6.3、安装tensorflow

pip install tensorflow-gpu==2.10.0 --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple

6.4、安装protobuf 

pip install protobuf --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple

6.5、安装 numpy

pip install numpy==1.19.5 --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple

6.6、matplotlib

pip install matplotlib==3.3.4  --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple

6.7、进入python

>>> import tensorflow as tf
>>> tf.__version__
'2.6.0'
>>> tf.test.is_gpu_available()
WARNING:tensorflow:From <stdin>:1: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
2023-11-23 23:54:19.866115: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-11-23 23:54:21.562121: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /device:GPU:0 with 3475 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3060 Laptop GPU, pci bus id: 0000:01:00.0, compute capability: 8.6
True

 说明tensorflow的GPU方式安装成功

七、安装pytorch的1.12.1版本

7.1、查找对应的版本

https://pytorch.org/get-started/previous-versions/

7.2、安装pytorch

conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch

7.3、验证pyTorch,输入python

(dlGPU) C:\Users\主机名>python
Python 3.7.16 (default, Jan 17 2023, 16:06:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.__version__
'1.12.1'
>>> torch.cuda.is_available()
True

这样pyTorch也支持了GPU了

#安装一些实用的python包
pip install gradio --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install flask --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install redis --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install nltk --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install opencv-python --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install mysql.connector --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install requests --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install requests --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pillow --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install jsonify --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install keras --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install microconventions --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install moviepy --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pandas --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pyinstaller --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install python-docx --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pyttsx3 --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install tensorboard --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install scipy --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install ffmpy --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install fpdf --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install gtts --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install numpy --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pygame --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install qrcode --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install translate --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install microprediction --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple


#导出环境配置
conda env export > d:/test/environment.yaml	

conda list -e > d:/test/requirements.txt
或
conda list --explicit > d:/test/requirements.txt

# 导出当前环境中的配置信息至txt文件
pip freeze > d:/test/requirements.txt

#导入环境配置
# 将txt文件中配置安装至当前环境中
pip install -r d:/test/requirements.txt

conda env create -f d:/test/environment.yaml

conda install --yes --file d:/test/requirements.txt

# 将txt文件中配置安装至指定的环境中
conda install --name dlGPU38 --file d:/test/requirements.txt	

# 将txt文件中配置安装至创建的环境中
conda create --name dlGPU38 --file d:/test/filename.txt	

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值