import torch
print(dir(torch.distributions))
print('1.张量的创建')
# ones 函数创建一个具有指定形状的新张量,并将所有元素值设置为 1
t = torch.ones(4)
print('t:', t)
x = torch.arange(12)
print('x:', x)
print('x shape:', x.shape)
print('x.numel():', x.numel())
y = x.reshape(3, 4) # 改变一个张量的形状而不改变元素数量和元素值
print('y:', y)
print('y.numel():', y.numel()) # 返回张量中元素的总个数
z = torch.zeros(2, 3, 4) # 创建一个张量,其中所有元素都设置为0
print('z:', z)
w = torch.randn(2, 3, 4) # 每个元素都从均值为0、标准差为1的标准高斯(正态)分布中随机采样。
print('w:', w)
q = torch.tensor([[[1, 2, 3], [4, 3, 2], [7, 4, 3]]]) # 通过提供包含数值的 Python 列表(或嵌套列表)来为所需张量中的每个元素赋予确定值
print('q:', q)
print('q shape:', q.shape)#三维
print('2.张量的运算')
x = torch.tensor([1.0, 2, 4, 8])#1.0浮点数
y = torch.tensor([2, 2, 2, 2])#整数
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x ** y) # **运算符是求幂运算
print(torch.exp(x))#指数运算
X = torch.arange(12, dtype=torch.float32).reshape(3, 4)#12个数 格式float32 形状三行四列
Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
print('c
李沐动手学深度学习pytorch版 pycharm代码实现
最新推荐文章于 2025-06-11 11:29:15 发布