1.张量基本操作简介与创建
1.1 torch.zeros
import torch
out_t =torch.tensor([1])
t = torch.zeros((4,4),out=out_t)
print('t :', t, '\n','out_t :', out_t)
print(id(t),id(out_t))
output
t : tensor([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) out_t : tensor([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) 2777186850912 2777186850912
out_t 与t 共用内存
1.2 torch.ones
import torch
out_t =torch.tensor([1])
t = torch.ones((3,3),out=out_t)
print('t :', t, '\n', out_t)
print(id(t),id(out_t))
output t : tensor([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) tensor([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) 2777187916752 2777187916752
1.3 torch.full
import torch
out_t =torch.tensor([1])
t = torch.full((3,3),4,out=out_t)
print('t :', t, '\n', out_t)
print(id(t),id(out_t))
output
t : tensor([[4, 4, 4], [4, 4, 4], [4, 4, 4]]) tensor([[4, 4, 4], [4, 4, 4], [4, 4, 4]]) 2776837717504 2776837717504