当前位置:网站首页>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的版本不匹配。
边栏推荐
- Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]
- The next industry outlet: NFT digital collection, is it an opportunity or a foam?
- tcpdump: no suitable device found
- ASU & OSU | model based regularized off-line meta reinforcement learning
- Research on cooperative control of industrial robots
- [kubernetes series] learn the exposed application of kubernetes service security
- 【 kubernets series】 a Literature Study on the Safe exposure Applications of kubernets Service
- SD卡报错“error -110 whilst initialising SD card
- js凡客banner轮播图js特效
- Polymorphic day02
猜你喜欢

C # create self host webservice

【SLAM】lidar-camera外参标定(港大MarsLab)无需二维码标定板

I sorted out a classic interview question for my job hopping friends

SAP ALV颜色代码对应颜色(整理)

ASU & OSU | model based regularized off-line meta reinforcement learning

记录一下逆向任务管理器的过程
![[pointer training - eight questions]](/img/fd/1aa3937548a04078c4d7e08198c3a8.png)
[pointer training - eight questions]

Software design principles

Research on cooperative control of industrial robots

JS音乐在线播放插件vsPlayAudio.js
随机推荐
Microsoft Research, UIUC & Google research | antagonistic training actor critic based on offline training reinforcement learning
【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
Recommended foreign websites for programmers to learn
[ruoyi] enable Mini navigation bar
Analyze menu analysis
出现Permission denied的解决办法(750权限谨慎使用)
MySQL advanced notes
Tomb. Weekly update of Finance (February 7 - February 13)
Mysql database operation
MySQL advanced notes
Add one to non negative integers in the array
An article about liquid template engine
这些不太会
Sign SSL certificate as Ca
Mysqldump data backup
NR modulation 1
Derivation of anti Park transform and anti Clarke transform formulas for motor control
codeforces每日5题(均1700)-第六天
蓝色样式商城网站页脚代码
Lua uses require to load the shared library successfully, but the return is Boolean (always true)