当前位置:网站首页>Pytoch foundation - (1) initialization of tensors
Pytoch foundation - (1) initialization of tensors
2022-07-06 03:27:00 【Up and down black】
Import pytorch library
import torch
Initialization tensor
DEVICE = "cuda" if torch.cuda.is_available() else "cpu" # Set up the running equipment
x = torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.float32, device=DEVICE, requires_grad=True)
Initialize other methods
1. produce 0-1 Between Uniform distribution Of 2x3 Tensor
x = torch.rand((2, 3))
print(x)
2. produce 0-1 Between Normal distribution Of 2x3 Tensor
x = torch.randn((2, 3))
print(x)
3. produce 3-10 Between Random integers Of 2x3 Tensor
x = torch.randint(3, 10, (2, 3))
print(x)
4. Produce and input Tensor with the same format
input = torch.rand((3, 3))
x = torch.rand_like(input)
5. The initial value generated is 0, In steps of 1, Sequences that do not contain termination values
x = torch.arange(start=0, end=10, step=1)
6. The initial value generated is 0, The end value is 9, The total number of steps is 11 Sequence
x = torch.linspace(start=0, end=9, steps=11)
7. The diagonal is 0-1 Uniformly distributed tensor
x = torch.diag(torch.rand(5))
Convert data type
We often use numpy data type , But it can't be directly in torch Middle operation , Therefore, you need to convert the data type .
import numpy as np
x = np.zeros((5, 5))
print(x)
print(x.dtype)
x_torch = torch.from_numpy(x) # from numpy become torch Tensor type
print(x_torch)
print(x_torch.dtype)
x_back = x_torch.numpy() # from torch Tensor reversion numpy type
print(x_back)
print(x_back.dtype)
Be careful :
appear Numpy is not available When reporting a mistake , yes numpy and pytorch The version of does not match .
边栏推荐
- Getting started with applet cloud development - getting user search content
- Item 10: Prefer scoped enums to unscoped enums.
- Erreur de la carte SD "erreur - 110 whilst initialisation de la carte SD
- 2.2 fonctionnement stm32 GPIO
- JS音乐在线播放插件vsPlayAudio.js
- Safety science to | travel, you must read a guide
- 关于非虚函数的假派生
- Pytorch基础——(2)张量(tensor)的数学运算
- 蓝色样式商城网站页脚代码
- Record the process of reverse task manager
猜你喜欢
【概念】Web 基础概念认知
【SLAM】ORB-SLAM3解析——跟踪Track()(3)
Performance test method of bank core business system
[slam] orb-slam3 parsing - track () (3)
pytorch加载数据
Derivation of anti Park transform and anti Clarke transform formulas for motor control
1、工程新建
Pointer for in-depth analysis (problem solution)
2.2 STM32 GPIO操作
2.2 STM32 GPIO操作
随机推荐
[padding] an error is reported in the prediction after loading the model weight attributeerror: 'model' object has no attribute '_ place‘
. Net 6 and Net core learning notes: Important issues of net core
Linear regression and logistic regression
Crazy, thousands of netizens are exploding the company's salary
Daily question brushing plan-2-13 fingertip life
ASU & OSU | model based regularized off-line meta reinforcement learning
施努卡:什么是视觉定位系统 视觉系统如何定位
MADDPG的pythorch实现——(1)OpenAI MADDPG环境配置
适合程序员学习的国外网站推荐
Cubemx 移植正点原子LCD显示例程
jsscript
下一个行业风口:NFT 数字藏品,是机遇还是泡沫?
Analyze 菜单分析
Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art
Quartz misfire missed and compensated execution
Pelosi: Congress will soon have legislation against members' stock speculation
Who is the winner of PTA
JS音乐在线播放插件vsPlayAudio.js
How to write compile scripts compatible with arm and x86 (Makefile, cmakelists.txt, shell script)
OCR文字識別方法綜述