当前位置:网站首页>Pytorch基础——(1)张量(tensor)的初始化
Pytorch基础——(1)张量(tensor)的初始化
2022-07-06 03:18:00 【七上八下的黑】
导入pytorch库
import torch
初始化张量
DEVICE = "cuda" if torch.cuda.is_available() else "cpu" # 设置运行的设备
x = torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.float32, device=DEVICE, requires_grad=True)
其他初始化方法
1.产生0-1之间均匀分布的2x3的张量
x = torch.rand((2, 3))
print(x)
2.产生0-1之间正态分布的2x3的张量
x = torch.randn((2, 3))
print(x)
3. 产生3-10之间随机整数的2x3的张量
x = torch.randint(3, 10, (2, 3))
print(x)
4.产生和input格式一样的张量
input = torch.rand((3, 3))
x = torch.rand_like(input)
5.产生初始值为0,步长为1,不包含终止值的序列
x = torch.arange(start=0, end=10, step=1)
6.产生初始值为0,终止值为9,总步数为11的序列
x = torch.linspace(start=0, end=9, steps=11)
7.产生对角线是0-1均匀分布的张量
x = torch.diag(torch.rand(5))
转换数据类型
我们常用numpy数据类型,但是它不能直接在torch中运算,因此需要转换数据类型。
import numpy as np
x = np.zeros((5, 5))
print(x)
print(x.dtype)
x_torch = torch.from_numpy(x) # 从numpy变成torch张量类型
print(x_torch)
print(x_torch.dtype)
x_back = x_torch.numpy() # 从torch张量变回numpy类型
print(x_back)
print(x_back.dtype)
注意:
出现 Numpy is not available 报错时,是numpy和pytorch的版本不匹配。
边栏推荐
- Leetcode problem solving -- 108 Convert an ordered array into a binary search tree
- 下一个行业风口:NFT 数字藏品,是机遇还是泡沫?
- What are the principles of software design (OCP)
- #PAT#day10
- Era5 reanalysis data download strategy
- 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
- Eight super classic pointer interview questions (3000 words in detail)
- jsscript
- NR modulation 1
- Explore pointers and pointer types in depth
猜你喜欢
Performance analysis of user login TPS low and CPU full
Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
适合程序员学习的国外网站推荐
Tidb ecological tools (backup, migration, import / export) collation
1.16 - 校验码
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1
JS音乐在线播放插件vsPlayAudio.js
Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]
SAP ALV颜色代码对应颜色(整理)
[ruoyi] enable Mini navigation bar
随机推荐
js凡客banner轮播图js特效
Advanced learning of MySQL -- Fundamentals -- isolation level of transactions
深度解析指针与数组笔试题
Who is the winner of PTA
My C language learning record (blue bridge) -- on the pointer
Overview of OCR character recognition methods
适合程序员学习的国外网站推荐
Polymorphic day02
【SLAM】lidar-camera外参标定(港大MarsLab)无需二维码标定板
Map sorts according to the key value (ascending plus descending)
Performance test method of bank core business system
1.16 - 校验码
出现Permission denied的解决办法(750权限谨慎使用)
Self made CA certificate and SSL certificate using OpenSSL
Mysqldump data backup
Sign SSL certificate as Ca
Custom attribute access__ getattribute__/ Settings__ setattr__/ Delete__ delattr__ method
My C language learning record (blue bridge) -- under the pointer
How to write compile scripts compatible with arm and x86 (Makefile, cmakelists.txt, shell script)
JS regular filtering and adding image prefixes in rich text