YDOOK AI : Pytorch : pytorch常用的张量生成与张量定义函数 tensor 生成与定义函数方法
1. 源代码展示:
import torch
a = [[1, 2, 3], [4, 5, 6]]
print('a = ', a)
# YDOOK JYLin : 普通的数组不能使用 tensor.dytpe 获取数据类型,需要使用 type(valuable) 获取
print('type(a) = ', type(a))
print()
t1 = torch.tensor(a)
print('t1 = ', t1)
print('t1.type = ', t1.dtype)
print()
t2_1 = torch.zeros(2, 3)
print('t2_1 = ', t2_1)
print('t2_1.type = ', t2_1.dtype)
print()
t2_2 = torch.zeros(3, 2)
print('t2_2 = ', t2_2)
print('t2_2.type = ', t2_2.dtype)
print()
t3 = torch.zeros_like(t1)
print('t3 = ', t3)
print('t3.type = ', t3.dtype)
print()
t4_1 = torch.ones(2, 3)
print('t4_1 = ', t4_1)
print('t4_1.type = ', t4_1.dtype)
print()
t4_2 = torch.ones(2, 3)
print('t4_2 = ', t4_2)
print('t4_2.type = ', t4_2.dtype)
print()
t5 = torch.ones_like(t1)
print('t5 = ', t5)
print('t5.type = ', t5.dtype)
print()
t6 = torch.full(t1.size(), 3.14)
print('t6 = ', t6)
print('t6.type = ', t6.dtype)
print()
t6_2 = torch.full(t1.size(), 3.14, dtype=torch.float64)
print('t6_2 = ', t6_2)
print('t6_2.type = ', t6_2.dtype)
print()
t7 = torch.full_like(t1, 6.28)
print('t7 = ', t7)
print('t7.type = ', t7.dtype)
print()
t7_2 = torch.full_like(t1, 6.28, dtype=torch.float64)
print('t7_2 = ', t7_2)
print('t7_2.type = ', t7_2.dtype)
print()
t8 = torch.empty(t1.size())
print('t8 = ', t8)
print('t8.type = ', t8.dtype)
print()
t8_2 = torch.empty(t1.size(), out=t1)
print('t8_2 = ', t8_2)
print('t8_2.type = ', t8_2.dtype)
print()
t8_3 = torch.empty_like(t1)
print('t8_3 = ', t8_3)
print('t8_3.type = ', t8_3.dtype)
print()
t9 = torch.eye(2, 3)
print('t9 = ', t9)
print('t9.type = ', t9.dtype)
print()
t9_2 = torch.eye(3, 3)
print('t9_2 = ', t9_2)
print('t9_2.type = ', t9_2.dtype)
print()
t10 = torch.arange(1, 10)
print('t10 = ', t10)
print('t10.type = ', t10.dtype)
print()
# YDOOK JYLin : 很有用的 torch.linspace(start, end, steps)
t11 = torch.linspace(1, 10, steps=10)
print('t11 = ', t11)
print('t11.type = ', t11.dtype)
print()
t11_2 = torch.linspace(1, 10, steps=9)
print('t11_2 = ', t11_2)
print('t11_2.type = ', t11_2.dtype)
print()
t11_3 = torch.linspace(1, 10, steps=8)
print('t11_3 = ', t11_3)
print('t11_3.type = ', t11_3.dtype)
print()
2. 输出:
a = [[1, 2, 3], [4, 5, 6]]
type(a) =
t1 = tensor([[1, 2, 3],
[4, 5, 6]])
t1.type = torch.int64
t2_1 = tensor([[0., 0., 0.],
[0., 0., 0.]])
t2_1.type = torch.float32
t2_2 = tensor([[0., 0.],
[0., 0.],
[0., 0.]])
t2_2.type = torch.float32
t3 = tensor([[0, 0, 0],
[0, 0, 0]])
t3.type = torch.int64
t4_1 = tensor([[1., 1., 1.],
[1., 1., 1.]])
t4_1.type = torch.float32
t4_2 = tensor([[1., 1., 1.],
[1., 1., 1.]])
t4_2.type = torch.float32
t5 = tensor([[1, 1, 1],
[1, 1, 1]])
t5.type = torch.int64
t6 = tensor([[3.1400, 3.1400, 3.1400],
[3.1400, 3.1400, 3.1400]])
t6.type = torch.float32
t6_2 = tensor([[3.1400, 3.1400, 3.1400],
[3.1400, 3.1400, 3.1400]], dtype=torch.float64)
t6_2.type = torch.float64
t7 = tensor([[6, 6, 6],
[6, 6, 6]])
t7.type = torch.int64
t7_2 = tensor([[6.2800, 6.2800, 6.2800],
[6.2800, 6.2800, 6.2800]], dtype=torch.float64)
t7_2.type = torch.float64
t8 = tensor([[3.1400, 3.1400, 3.1400],
[3.1400, 3.1400, 3.1400]])
t8.type = torch.float32
t8_2 = tensor([[1, 2, 3],
[4, 5, 6]])
t8_2.type = torch.int64
t8_3 = tensor([[4614253070214989087, 4614253070214989087, 4614253070214989087],
[4614253070214989087, 4614253070214989087, 4614253070214989087]])
t8_3.type = torch.int64
t9 = tensor([[1., 0., 0.],
[0., 1., 0.]])
t9.type = torch.float32
t9_2 = tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
t9_2.type = torch.float32
t10 = tensor([1, 2, 3, 4, 5, 6, 7, 8, 9])
t10.type = torch.int64
t11 = tensor([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
t11.type = torch.float32
t11_2 = tensor([ 1.0000, 2.1250, 3.2500, 4.3750, 5.5000, 6.6250, 7.7500, 8.8750,
10.0000])
t11_2.type = torch.float32
t11_3 = tensor([ 1.0000, 2.2857, 3.5714, 4.8571, 6.1429, 7.4286, 8.7143, 10.0000])
t11_3.type = torch.float32