torch 设置gpu

本文档记录了CUDA和cuDNN的安装过程,以及验证CUDA是否可用和TensorFlow-GPU的配置与运行。通过安装相应版本的CUDA、cuDNN和TensorFlow-GPU,成功在Python环境中运行了GPU加速的矩阵乘法操作,显示出设备已分配到GPU并返回了正确的计算结果。
部署运行你感兴趣的模型镜像

为了解决 torch.cuda.is_avalibale() 

安装

472.12-desktop-win8-win7-64bit-international-whql.exe

cuda_11.5.0_496.13_win10.exe

torch-1.11.0.dev20211115+cu113-cp37-cp37m-win_amd64.whl

莫名的能用了

C:\Users\Administrator>nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Mon_Sep_13_20:11:50_Pacific_Daylight_Time_2021
Cuda compilation tools, release 11.5, V11.5.50
Build cuda_11.5.r11.5/compiler.30411180_0

C:\Users\Administrator>
(tf26) C:\Users\Administrator>pip list |grep cu
pycuda                           2021.1
torch                            1.11.0.dev20211115+cu113

​
(tf26) C:\Users\Administrator>pip list |grep tensor
tensorboard                      2.7.0
tensorboard-data-server          0.6.1
tensorboard-plugin-wit           1.8.0
tensorflow                       2.7.0
tensorflow-gpu                   2.7.0
tensorflow-io-gcs-filesystem     0.21.0

​
Python 3.7.11 (default, Jul 27 2021, 09:42:29) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.29.0 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 7.29.0
Python 3.7.11 (default, Jul 27 2021, 09:42:29) [MSC v.1916 64 bit (AMD64)] on win32
import torch
import torch
torch.cuda.is_available()
Out[4]: True
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
# Creates a graph.
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))
WARNING:tensorflow:From C:\Users\Administrator\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\compat\v2_compat.py:111: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: NVIDIA GeForce GTX 1060 6GB, pci bus id: 0000:07:00.0, compute capability: 6.1

[[22. 28.]
 [49. 64.]]

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

在使用方面,一开始trae推荐的torch版本为pip install torch==2.3.0 torchvision==0.18.0 intel - extension - for - pytorch==2.3.0 memory - profiler==0.61,可通过此命令安装相关版本用于CPU或GPU计算[^2]。对于NVIDIA GPU,有入门教程指导在其上运行Python脚本,可对比CPU与GPU性能,还能检测A100性能,帮助用户了解如何在NVIDIA GPU上运行Python脚本并掌握性能测试方法[^3]。 在性能方面,torch.cosine_smilarity()计算中出现了CPU和GPU精度问题。在CPU上计算出的5种相似度结果惊人一致,而在GPU上计算出的5种相似度结果中,中间三种基于torch函数调用的方式计算结果均为0,第一种将特征搬运到CPU上然后使用scipy.spatial.distance.cosine()函数的计算结果和第五种直接在GPU上使用基于余弦相似度公式的torch代码的计算结果又与CPU上计算出的结果各有不同,这体现出CPU和GPU在计算精度和性能表现上存在差异[^1]。 在差异影响因素上,对于torch.utils.data.DataLoader方法,如果未设置共享内存pin_memory或者设置成False,数据需从CPU传入到缓存RAM里面,再传输到GPU上;若内存富裕,设置pin_memory=True,可直接将数据映射到GPU的相关内存块上,省掉一点数据传输时间,这说明数据传输方式会对CPU和GPU使用性能产生影响[^4]。 ```python # 示例代码:设置pin_memory import torch from torch.utils.data import DataLoader, Dataset class MyDataset(Dataset): def __len__(self): return 100 def __getitem__(self, idx): return torch.randn(10) dataset = MyDataset() # 设置pin_memory为True dataloader = DataLoader(dataset, batch_size=10, pin_memory=True) for data in dataloader: # 对数据进行处理 pass ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值