赞
踩
创建张量:
import torch
# 直接从列表或数组创建张量
x = torch.tensor([1, 2, 3])
创建特定值的张量:
# 全零张量
zeros = torch.zeros(3, 3)
# 全一张量
ones = torch.ones(3, 3)
# 单位张量(对角线为1,其余为0)
eye = torch.eye(3)
# 随机张量
rand = torch.rand(3, 3)
# 从均值为0,标准差为1的正态分布中抽取的随机张量
randn = torch.randn(3, 3)
创建等差数列张量:
# 从0到10(不包括10)的等差数列,步长为2
arange = torch.arange(0, 10, 2)
创建特定间隔的数列张量:
# 从0到10均匀分布的5个数
linspace = torch.linspace(0, 10, 5)
加法和减法:
# 加法
z = x + y # torch.add(x, y)
print(z) # 输出: tensor([5, 7, 9])
# 减法
z = x - y # torch.sub(x, y)
print(z) # 输出: tensor([-3, -3, -3])
乘法和除法:
# 元素乘法
z = x * y # torch.mul(x, y)
print(z) # 输出: tensor([ 4, 10, 18])
# 元素除法
z = x / y # torch.div(x, y)
print(z) # 输出: tensor([0.2500, 0.4000, 0.5000])
矩阵乘法:
a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[5, 6], [7, 8]])
# 矩阵乘法
z = torch.matmul(a, b) # 或者 a @ b
print(z) # 输出: tensor([[19, 22], [43, 50]])
改变形状:view()
a = torch.tensor([[1, 2, 3], [4, 5, 6]])
# 改变形状
b = a.view(3, 2)
print(b)
# 输出:
# tensor([[1, 2],
# [3, 4],
# [5, 6]])
view中一个参数指定为-1,代表自动调整这个维度上的元素个数,以保证元素的总数不变。
import torch x1 = torch.arange(0,16) print(x1) #a1: tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) ------------------------------------------------------------------------------------------------------ x2 = x1.view(-1, 16) x3 = x1.view(-1, 8) x4 = x1.view(-1, 4) x5 = x1.view(-1, 2) print(x2) print(x3) print(x4) print(x5) x2: tensor([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]) x3: tensor([[ 0, 1, 2, 3, 4, 5, 6, 7], [ 8, 9, 10, 11, 12, 13, 14, 15]]) x4: tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) x5: tensor([[ 0, 1], [ 2, 3], [ 4, 5], [ 6, 7], [ 8, 9], [10, 11], [12, 13], [14, 15]])
拼接:
x = torch.tensor([[1, 2], [3, 4]]) y = torch.tensor([[5, 6], [7, 8]]) # 沿着0轴拼接(垂直拼接) z = torch.cat((x, y), dim=0) print(z) # 输出: # tensor([[1, 2], # [3, 4], # [5, 6], # [7, 8]]) # 沿着1轴拼接(水平拼接) z = torch.cat((x, y), dim=1) print(z) # 输出: # tensor([[1, 2, 5, 6], # [3, 4, 7, 8]])
切片:
a = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# 切片
b = a[:, 1] # 第二列
print(b) # 输出: tensor([2, 5, 8])
求和:sum()
x = torch.tensor([1, 2, 3, 4])
# 求和
sum_x = torch.sum(x)
print(sum_x) # 输出: tensor(10)
均值:mean()
# 均值
mean_x = torch.mean(x.float()) # 转换为浮点数类型
print(mean_x) # 输出: tensor(2.5000)
最大值和最小值:max(),min()
# 最大值
max_x = torch.max(x)
print(max_x) # 输出: tensor(4)
# 最小值
min_x = torch.min(x)
print(min_x) # 输出: tensor(1)
比较运算:
x = torch.tensor([1, 2, 3])
y = torch.tensor([2, 2, 2])
# 大于
print(x > y) # 输出: tensor([False, False, True])
# 小于
print(x < y) # 输出: tensor([ True, False, False])
逻辑与、或:
a = torch.tensor([True, False, True])
b = torch.tensor([False, False, True])
# 逻辑与
c = torch.logical_and(a, b)
print(c) # 输出: tensor([False, False, True])
# 逻辑或
c = torch.logical_or(a, b)
print(c) # 输出: tensor([ True, False, True])
广播机制:
a = torch.tensor([1, 2, 3])
b = torch.tensor([[1], [2], [3]])
# 广播机制
c = a + b
print(c)
# 输出:
# tensor([[2, 3, 4],
# [3, 4, 5],
# [4, 5, 6]])
自动梯度计算:
x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
# 前向传播
y = x + 2
z = y * y * 2
out = z.mean()
# 反向传播
out.backward()
print(x.grad) # 输出: tensor([4.6667, 6.0000, 7.3333])
从标准正态分布中生成随机张量:
randn_tensor = torch.randn(3, 3) # 生成一个形状为 (3, 3) 的随机张量,服从标准正态分布
print(randn_tensor)
#tensor([[ 1.2335, -0.3941, 0.8990],
# [ 0.0470, -1.2671, 0.3248],
# [-0.4062, -0.6862, 0.1314]])
生成随机排列:
randperm_tensor = torch.randperm(10) # 生成一个从 0 到 9 的随机排列
print(randperm_tensor)
#tensor([2, 0, 5, 1, 8, 6, 3, 4, 7, 9])
生成等差数列张量:
arange_tensor = torch.arange(0, 10, 2) # 生成从 0 到 10(不包括 10)的等差数列,步长为 2
print(arange_tensor)
#tensor([0, 2, 4, 6, 8])
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。