当前位置:网站首页>Pytorch学习笔记(1)检查torch、cuda,张量的创建
Pytorch学习笔记(1)检查torch、cuda,张量的创建
2022-04-21 10:23:00 【小帅吖】
lesson1 检查torch、cuda
import torch
# 打印torch版本
print("Hello World, Hello PyTorch {}".format(torch.__version__))
# 打印cuda是否可用,打印cuda版本
print("\nCUDA is available:{}, version is {}".format(torch.cuda.is_available(), torch.version.cuda))
# 打印cuda名称
print("\ndevice_name: {}".format(torch.cuda.get_device_name(0)))
Hello World, Hello PyTorch 1.11.0
CUDA is available:True, version is 11.3
device_name: GeForce GTX 1050
lesson2 张量的创建
import torch
import numpy as np
torch.manual_seed(1)
- 通过torch.tensor创建张量
arr = np.ones((3, 3))
print("ndarray的数据类型:", arr.dtype)
t = torch.tensor(arr)
print(t)
ndarray的数据类型: float64
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
- 通过torch.from_numpy创建张量
arr = np.array([[1, 2, 3], [4, 5, 6]])
t = torch.from_numpy(arr)
print("numpy array: ", arr)
print("tensor : ", t)
print("\n修改tensor")
t[0, 0] = -1
print("numpy array: ", arr)
print("tensor : ", t)
numpy array: [[1 2 3]
[4 5 6]]
tensor : tensor([[1, 2, 3],
[4, 5, 6]], dtype=torch.int32)
修改tensor
numpy array: [[-1 2 3]
[ 4 5 6]]
tensor : tensor([[-1, 2, 3],
[ 4, 5, 6]], dtype=torch.int32)
- 通过torch.zeros创建张量
out_t = torch.tensor([1])
t = torch.zeros((3, 3), out=out_t)
print(t, '\n', out_t)
print(id(t), id(out_t), id(t) == id(out_t))
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
2717724706592 2717724706592 True
- 通过torch.full创建全1张量
t = torch.full((3, 3), 1.) # 1.6之后若不指定dtype,就需要传入浮点数
print(t)
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
- 通过torch.arange创建等差数列张量
t = torch.arange(2, 10, 2)
print(t)
tensor([2, 4, 6, 8])
- 通过torch.linspace创建均分数列张量
t = torch.linspace(2, 10, 6)
print(t)
tensor([ 2.0000, 3.6000, 5.2000, 6.8000, 8.4000, 10.0000])
- 通过torch.normal创建正态分布张量
(1)通过张量,张量创建
# mean:张量 std: 张量
mean = torch.arange(1, 5, dtype=torch.float)
std = torch.arange(1, 5, dtype=torch.float)
t_normal = torch.normal(mean, std)
print("mean:{}\nstd:{}".format(mean, std))
print(t_normal)
mean:tensor([1., 2., 3., 4.])
std:tensor([1., 2., 3., 4.])
tensor([1.6614, 2.5338, 3.1850, 6.4853])
(2)通过标量,标量创建
# mean:标量 std: 标量
t_normal = torch.normal(0., 1., size=(4,))
print(t_normal)
tensor([0.6614, 0.2669, 0.0617, 0.6213])
(3)通过张量,标量创建
# mean:张量 std: 标量
mean = torch.arange(1, 5, dtype=torch.float)
std = 1
t_normal = torch.normal(mean, std)
print("mean:{}\nstd:{}".format(mean, std))
print(t_normal)
mean:tensor([1., 2., 3., 4.])
std:1
tensor([1.6614, 2.2669, 3.0617, 4.6213])
- 通过torch.ones创建张量
a = torch.ones((2,5))
print(a)
tensor([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
- 通过torch.rand创建张量
a = torch.rand(4, 1) # 返回一个形状为(4,1)的张量,从0-1的均匀分布中抽取的一组随机数
print(a)
tensor([[0.5007],
[0.9894],
[0.7115],
[0.0602]])
- 通过torch.randn创建张量
a = torch.randn(4, 1) # 返回一个形状为(4,1)的,从标准正态分布(均值为0,方差为1)中抽取的一组随机数
print(a)
tensor([[ 0.9353],
[-0.2985],
[-0.4224],
[ 0.4048]])
版权声明
本文为[小帅吖]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_47997583/article/details/124307038
边栏推荐
猜你喜欢

Daily question (2022-04-20) - the longest absolute path of the file
Transaction isolation level and mvcc

SAP ABAP FOR ALL ENTRIES 的用法

背包问题小结(0-1,完全,多重背包问题)

openCV——模板匹配

Release announcement of HMS core version 6.4.0

SQL:树形三层职业分类表的SQL文件

摩尔线程与Ampere Computing达成合作

MKL and vs2019 configuration method

计量模型、实证stata代码合集,附顶刊示例
随机推荐
“空气洗”再迎迭代,模仿者又有了新目标
趣丸集团招股书“失效”,旗下TT语音已下架,如何实现稳定增长?
Opencv - contour detection
CommDGI: Community detection oriented deep graph infomax 2020 CIKM
C#入门-并行编程
Canoe: what is the vector tool platform
ConvNeXt
【软件测试系列九】《压力测试申请需提供事项说明》
openCV——模板匹配
The most easy to understand dependency injection and control inversion
[excel function] count function | countif function | countifs function
ConvNeXt
SAP ABAP FOR ALL ENTRIES 的用法
运行npm install命令的时候会发生什么?
L1-047 装睡 (10 分)
MySQL8. 0 learning record 07 - JSON of data type
L1-050 倒数第N个字符串 (15 分)
DNS域名系统-因特网的目录服务
[pycharm plugins] download online to install the translation plug-in
MKL与VS2019配置方法