当前位置:网站首页>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 .
边栏推荐
- Redo file corruption repair
- ASU & OSU | model based regularized off-line meta reinforcement learning
- svg拖动点裁剪图片js特效
- SD card reports an error "error -110 whilst initializing SD card
- Pytorch基础——(2)张量(tensor)的数学运算
- 【RISC-V】外部中断
- Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art
- Item 10: Prefer scoped enums to unscoped enums.
- Pointer for in-depth analysis (problem solution)
- Arabellacpc 2019 (supplementary question)
猜你喜欢
银行核心业务系统性能测试方法
Who is the winner of PTA
遥感图像超分辨重建综述
js凡客banner轮播图js特效
施努卡:3d视觉检测应用行业 机器视觉3d检测
C language judgment, ternary operation and switch statement usage
Princeton University, Peking University & UIUC | offline reinforcement learning with realizability and single strategy concentration
2.1 rtthread pin设备详解
Map sorts according to the key value (ascending plus descending)
three.js网页背景动画液态js特效
随机推荐
Problems encountered in 2022 work IV
Microsoft Research, UIUC & Google research | antagonistic training actor critic based on offline training reinforcement learning
施努卡:视觉定位系统 视觉定位系统的工作原理
OCR文字識別方法綜述
Research on cooperative control of industrial robots
Selenium share
Daily question brushing plan-2-13 fingertip life
教你用Pytorch搭建一个自己的简单的BP神经网络( 以iris数据集为例 )
Inherit day01
The real machine cannot access the shooting range of the virtual machine, and the real machine cannot Ping the virtual machine
Map sorts according to the key value (ascending plus descending)
【Rust 笔记】18-宏
数据分析——seaborn可视化(笔记自用)
Exness foreign exchange: the governor of the Bank of Canada said that the interest rate hike would be more moderate, and the United States and Canada fell slightly to maintain range volatility
Teach you to build your own simple BP neural network with pytoch (take iris data set as an example)
1.16 - 校验码
Mysqldump data backup
BUAA喜鹊筑巢
. Net 6 and Net core learning notes: Important issues of net core
Shell 传递参数