当前位置:网站首页>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 torchInitialization 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 .
边栏推荐
- BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1
- 2.2 STM32 GPIO操作
- Image super-resolution using deep convolutional networks(SRCNN)解读与实现
- 出现Permission denied的解决办法(750权限谨慎使用)
- SAP ALV color code corresponding color (finishing)
- canvas切积木小游戏代码
- 2. GPIO related operations
- [slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board
- jsscript
- . Net 6 and Net core learning notes: Important issues of net core
猜你喜欢

Who is the winner of PTA

OCR文字識別方法綜述

Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project

Eight super classic pointer interview questions (3000 words in detail)

给新人工程师组员的建议

Pointer for in-depth analysis (problem solution)

MADDPG的pythorch实现——(1)OpenAI MADDPG环境配置

Mysqldump data backup

ESBuild & SWC浅谈: 新一代构建工具

Canvas cut blocks game code
随机推荐
教你用Pytorch搭建一个自己的简单的BP神经网络( 以iris数据集为例 )
【Rust 笔记】18-宏
Microsoft Research, UIUC & Google research | antagonistic training actor critic based on offline training reinforcement learning
These are not very good
resulttype和resultmap的区别和应用场景
深入刨析的指针(题解)
js凡客banner轮播图js特效
EDCircles: A real-time circle detector with a false detection control 翻译
Performance test method of bank core business system
canvas切积木小游戏代码
NR modulation 1
SAP ALV单元格级别设置颜色
Performance analysis of user login TPS low and CPU full
Cross origin cross domain request
[slam] orb-slam3 parsing - track () (3)
MPLS experiment
C language judgment, ternary operation and switch statement usage
11. Container with the most water
蓝色样式商城网站页脚代码
The solution of permission denied (750 permissions should be used with caution)