当前位置:网站首页>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 .
边栏推荐
- Analyze 菜单分析
- C language judgment, ternary operation and switch statement usage
- [Li Kou] the second set of the 280 Li Kou weekly match
- canvas切积木小游戏代码
- Handwriting database client
- 给新人工程师组员的建议
- Explore pointers and pointer types in depth
- Audio audiorecord binder communication mechanism
- Crazy, thousands of netizens are exploding the company's salary
- StrError & PERROR use yyds dry inventory
猜你喜欢
Precautions for single chip microcomputer anti reverse connection circuit
2.2 fonctionnement stm32 GPIO
Record the process of reverse task manager
Four logs of MySQL server layer
多项目编程极简用例
SWC introduction
Eight super classic pointer interview questions (3000 words in detail)
数据分析——seaborn可视化(笔记自用)
Python implementation of maddpg - (1) openai maddpg environment configuration
How to choose PLC and MCU?
随机推荐
Audio audiorecord binder communication mechanism
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1
JS音乐在线播放插件vsPlayAudio.js
这些不太会
Getting started with applet cloud development - getting user search content
Precautions for single chip microcomputer anti reverse connection circuit
3.1 rtthread 串口设备(V1)详解
Arabellacpc 2019 (supplementary question)
Pelosi: Congress will soon have legislation against members' stock speculation
MySQL Server层四个日志
[slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board
Teach you to build your own simple BP neural network with pytoch (take iris data set as an example)
Cubemx 移植正点原子LCD显示例程
施努卡:3d视觉检测应用行业 机器视觉3d检测
Svg drag point crop image JS effect
Pytorch load data
[padding] an error is reported in the prediction after loading the model weight attributeerror: 'model' object has no attribute '_ place‘
Lua uses require to load the shared library successfully, but the return is Boolean (always true)
BUAA喜鹊筑巢
Pytorch基础——(1)张量(tensor)的初始化