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]]
x1_gpu = x1.cuda()
print(x1)
tensor([3., 4.])
x1_cpu = x1_gpu.cpu()
print(x1_cpu)
tensor([3., 4.])
x1_array = x1_cpu.numpy()
print(x1_array)
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)
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])
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]])
print(x)
y = torch.unsqueeze(x,dim=0)
print(y)
print(y.shape)
z = y.unsqueeze(3)
print(z)
print(z.size())
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])
sum_tensor = torch.sum(p,dim=1)
print(sum_tensor)
tensor([4., 4., 4.])
开个新坑, 回头有空了继续 其实是一年前的草稿拿来氵[doge]
|