当前位置:网站首页>pytorch环境配置及基础知识
pytorch环境配置及基础知识
2022-07-25 11:37:00 【Alexa2077】
一、配置环境
提前准备:本文默认已安装Anaconda,以下操作在Anaconda Prompt上进行。
1,创建虚拟环境:conda create -n env_name python==version
2,激活虚拟环境:conda activate env_name
3,查看显卡驱动: nvidia-smi
4,到PyTorch官网安装:建议用CUDA版本
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
5,检验是否安装成功:
输入python后输入:
import torch
torch.cuda.is_available()
CPU版本返回False,GPU版本返回True
6,使用PyCharm连接到该虚拟环境进行使用
二、基础知识
1,张量
张量:基于向量和矩阵的推广,从标量,向量,矩阵,向更高维度进行扩展。
比如说:
0D:标量,数字
1D:向量
2D:矩阵
3D:时间序列,单张彩色图片
4D:图像
5D:视频
在PyTorch中,torch.Tensor 是存储和变换数据的主要工具。与NumPy颇有相似之处。然而,Tensor 提供GPU计算和自动求梯度等更多功能,这些使 Tensor 这一数据类型更加适合深度学习。
2,常见操作
1,随机初始化矩阵:torch.rand()
2,全0矩阵:torch.zeros()
3,直接构建:torch.tensor()
4,加法操作:torch.add(x,y)
5,索引操作(类似于numpy):索引出来的结果与原数据共享内存,修改一个,另一个会跟着修改。如果不想修改,可以考虑使用copy()等方法
6,维度变换:
torch.view():返回的新tensor与源tensor共享内存(其实是同一个tensor),更改其中的一个,另外一个也会跟着改变。(顾名思义,view()仅仅是改变了对这个张量的观察角度)
torch.reshape(): 同样可以改变张量的形状,但是此函数并不能保证返回的是其拷贝值,所以官方不推荐使用。
推荐方法:先用 clone() 创造一个张量副本然后再使用 torch.view()进行函数维度变换 。使用 clone() 还有一个好处是会被记录在计算图中,即梯度回传到副本时也会传到源 Tensor 。
3,广播机制
当对两个形状不同的 Tensor 按元素运算时,可能会触发广播(broadcasting)机制:先适当复制元素使这两个 Tensor 形状相同后再按元素运算。
边栏推荐
- Multi label image classification
- Zero shot image retrieval (zero sample cross modal retrieval)
- Intelligent information retrieval (overview of intelligent information retrieval)
- Zuul网关使用
- R语言使用wilcox.test函数执行wilcox符号秩检验获取总体中位数(median)的置信区间(默认输出结果包括95%置信水平的置信区间)
- R语言使用ggpubr包的ggarrange函数将多幅图像组合起来、使用ggexport函数将可视化图像保存为jpeg格式(width参数指定宽度、height参数指定高度、res参数指定分辨率)
- Sword finger offer 22. the penultimate node in the linked list
- 【图攻防】《Backdoor Attacks to Graph Neural Networks 》(SACMAT‘21)
- 【GCN-RS】Learning Explicit User Interest Boundary for Recommendation (WWW‘22)
- NLP knowledge - pytorch, back propagation, some small pieces of notes for predictive tasks
猜你喜欢

【AI4Code】《CoSQA: 20,000+ Web Queries for Code Search and Question Answering》 ACL 2021

【AI4Code】CodeX:《Evaluating Large Language Models Trained on Code》(OpenAI)

【GCN-RS】Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for RS (SIGIR‘22)

【微服务~Sentinel】Sentinel降级、限流、熔断

【Debias】Model-Agnostic Counterfactual Reasoning for Eliminating Popularity Bias in RS(KDD‘21)

搭建Vision Transformer系列实践,终于见面了,Timm库!

NLP knowledge - pytorch, back propagation, some small pieces of notes for predictive tasks

给生活加点惊喜,做创意生活的原型设计师丨编程挑战赛 x 选手分享

记录一次线上死锁的定位分析

OSPF comprehensive experiment
随机推荐
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the dot strip chart, set the palette parameter to configure the color of data points at different levels,
Data transmission under the same LAN based on tcp/ip
R language ggplot2 visualization: use the ggviolin function of ggpubr package to visualize the violin graph, set the add parameter to add jitter data points and mean standard deviation vertical bars (
Learning to pre train graph neural networks
如何从远程访问 DMS数据库?IP地址是啥?用户名是啥?
[high concurrency] a lock faster than read-write lock in high concurrency scenarios. I'm completely convinced after reading it!! (recommended Collection)
WPF项目入门1-简单登录页面的设计和开发
[high concurrency] Why is the simpledateformat class thread safe? (six solutions are attached, which are recommended for collection)
R language ggpubr package ggarrange function combines multiple images and annotates_ Figure function adds annotation, annotation and annotation information for the combined image, adds image labels fo
From cloud native to intelligent, in-depth interpretation of the industry's first "best practice map of live video technology"
NLP的基本概念1
keepalived实现mysql的高可用
Power Bi -- these skills make the report more "compelling"“
3.2.1 what is machine learning?
R language uses LM function to build multiple linear regression model, step function to build forward stepwise regression model to screen the best subset of prediction variables, and scope parameter t
Start with the development of wechat official account
Video caption (cross modal video summary / subtitle generation)
OSPF comprehensive experiment
Brpc source code analysis (V) -- detailed explanation of basic resource pool
Feign use