例程1:
import torch
t2=torch.tensor([[0,1,2],[3,4,5]])
print(t2)
print('数据={}'.format(t2))
print(t2.reshape(3,2))
print(t2+1)
print('大小={}'.format(t2.size()))
print('维度={}'.format(t2.dim()))
print('元素的个数={}'.format(t2.numel()))
print('元素的类型={}'.format(t2.dtype))
运行结果:
tensor([[0, 1, 2],
[3, 4, 5]])
数据=tensor([[0, 1, 2],
[3, 4, 5]])
tensor([[0, 1],
[2, 3],
[4, 5]])
tensor([[1, 2, 3],
[4, 5, 6]])
大小=torch.Size([2, 3])
维度=2
元素的个数=6
元素的类型=torch.int64
构造随机变量
torch.bernoulli() 可以生成元素值为1或者0的张量
例程2.1
probs=torch.full((3,4),0.5)
print(probs)
z1=torch.bernoulli(probs)
print(z1)
运行结果