当前位置:网站首页>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 .
边栏推荐
- An article about liquid template engine
- Buuctf question brushing notes - [geek challenge 2019] easysql 1
- Performance analysis of user login TPS low and CPU full
- 如何做好功能测试
- Teach you to build your own simple BP neural network with pytoch (take iris data set as an example)
- pytorch加载数据
- Image super-resolution using deep convolutional networks(SRCNN)解读与实现
- resulttype和resultmap的区别和应用场景
- 【RISC-V】外部中断
- three. JS page background animation liquid JS special effect
猜你喜欢
随机推荐
出现Permission denied的解决办法(750权限谨慎使用)
Pytorch基础——(1)张量(tensor)的初始化
[slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board
resulttype和resultmap的区别和应用场景
教你用Pytorch搭建一个自己的简单的BP神经网络( 以iris数据集为例 )
SAP ALV单元格级别设置颜色
Precautions for single chip microcomputer anti reverse connection circuit
3.2 rtthread 串口设备(V2)详解
js凡客banner轮播图js特效
2、GPIO相关操作
继承day01
Polymorphic day02
[rust notes] 18 macro
Pointer written test questions ~ approaching Dachang
Safety science to | travel, you must read a guide
Leetcode problem solving -- 108 Convert an ordered array into a binary search tree
Svg drag point crop image JS effect
Image super-resolution using deep convolutional networks(SRCNN)解读与实现
【Rust 笔记】18-宏
Introduction to DeNO









