import torch
x1 = torch.Tensor([3,4])
print(x1)
tensor([3., 4.])
import numpy as np
numpy_tensor = np.random.randn(1,3)
print("numpy1 : ",numpy_tensor)
pytorch_tensor = torch.from_numpy(numpy_tensor)
print("torch : ",pytorch_tensor)
new_numpy_tensor = pytorch_tensor.numpy()
print("numpy2 : ",new_numpy_tensor)
numpy1 : [[ 0.6326885 0.36646224 -1.62013748]]
torch : tensor([[ 0.6327, 0.3665, -1.6201]], dtype=torch.float64)
numpy2 : [[ 0.6326885 0.36646224 -1.62013748]]
# 将变量放置在gpu上
x1_gpu = x1.cuda()
print(x1)
tensor([3., 4.])
# 将变量再放回cpu上
x1_cpu = x1_gpu.cpu()
print(x1_cpu)
tensor([3., 4.])
# 转换回np数组
x1_array = x1_cpu.numpy()
print(x1_array)
# 需要先将gpu上的变量转换回cpu上才能转换回np数组,否则会报错
x1_array1 = x1_gpu.cpu().numpy()
print(x1_array1)
[3. 4.]
[3. 4.]
x = torch.ones(3,4)
y = torch.ones(3,4)
z = torch.ones(4,3)
print(x)
print(y)
print(z)
# 矩阵和标量的运算
k = x + 3
print(k)
# 矩阵和矩阵运算
k1 = torch.add(x,3)
print(k1)
k2 = x + y
print("x + y = ")
print(k2)
k3 = torch.add(x,y)
print("torch.add(x,y) = ")
print(k3)
# 矩阵乘法
k4 = torch.mm(x,z)
print("x * z = ")
print(k4)
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
tensor([[4., 4., 4., 4.],
[4., 4., 4., 4.],
[4., 4., 4., 4.]])
tensor([[4., 4., 4., 4.],
[4., 4., 4., 4.],
[4., 4., 4., 4.]])
x + y =
tensor([[2., 2., 2., 2.],
[2., 2., 2., 2.],
[2., 2., 2., 2.]])
torch.add(x,y) =
tensor([[2., 2., 2., 2.],
[2., 2., 2., 2.],
[2., 2., 2., 2.]])
x * z =
tensor([[4., 4., 4.],
[4., 4., 4.],
[4., 4., 4.]])
a = torch.randn(4,3)
print(a)
# .size()和.shape最后返回的都是Tensor的形状
print(a.size())
print(a.shape)
tensor([[ 0.4913, -1.5053, 0.3151],
[ 0.4006, -1.4361, 0.4149],
[-0.9687, 0.5952, -1.8559],
[ 0.4900, -0.1506, -0.1390]])
torch.Size([4, 3])
torch.Size([4, 3])
# 可以使用.view()将矩阵重新排列, 但是要注意应和矩阵原有的元素个数保持一致
b = a.view(3,4)
print(b)
c = a.view(12)
print(c)
# 并不会改变原有矩阵
print(a)
tensor([[ 0.4913, -1.5053, 0.3151, 0.4006],
[-1.4361, 0.4149, -0.9687, 0.5952],
[-1.8559, 0.4900, -0.1506, -0.1390]])
tensor([ 0.4913, -1.5053, 0.3151, 0.4006, -1.4361, 0.4149, -0.9687, 0.5952,
-1.8559, 0.4900, -0.1506, -0.1390])
tensor([[ 0.4913, -1.5053, 0.3151],
[ 0.4006, -1.4361, 0.4149],
[-0.9687, 0.5952, -1.8559],
[ 0.4900, -0.1506, -0.1390]])
# 对矩阵进行升维(unsqueeze)/降维(squeeze)
print(x)
# x为输入的tensor, dim为插入维度的位置
y = torch.unsqueeze(x,dim=0)
print(y)
print(y.shape)
# unsqueeze()内只有唯一参数时默认为dim
z = y.unsqueeze(3)
print(z)
print(z.size())
# 降维同unsqueeze
o = z.squeeze(0)
print(o)
print(o.shape)
p = o.squeeze(2)
print(p)
print(p.shape)
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
tensor([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]])
torch.Size([1, 3, 4])
tensor([[[[1.],
[1.],
[1.],
[1.]],
[[1.],
[1.],
[1.],
[1.]],
[[1.],
[1.],
[1.],
[1.]]]])
torch.Size([1, 3, 4, 1])
tensor([[[1.],
[1.],
[1.],
[1.]],
[[1.],
[1.],
[1.],
[1.]],
[[1.],
[1.],
[1.],
[1.]]])
torch.Size([3, 4, 1])
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
torch.Size([3, 4])
# 求和
# dim=0对第一个维度(3行)求和
sum_tensor = torch.sum(p,dim=1)
print(sum_tensor)
tensor([4., 4., 4.])
开个新坑, 回头有空了继续
其实是一年前的草稿拿来氵[doge]