机器学习 - PyTorch 常见的操作

可以用PyTorch做加减乘除操作

python 复制代码
import torch

tensor_operation = torch.tensor([1,2,3])
print(tensor_operation)

print(tensor_operation + 10)
print(torch.add(tensor_operation, 10))

print(tensor_operation * 10) 
print(torch.multiply(tensor_operation, 10))

print(tensor_operation - 10)
print(tensor_operation / 10)

print(tensor_operation * tensor_operation)

# 输出

0s
tensor_operation = torch.tensor([1,2,3])
print(tensor_operation)

print(tensor_operation + 10)
print(torch.add(tensor_operation, 10))

print(tensor_operation * 10) 
print(torch.multiply(tensor_operation, 10))

print(tensor_operation - 10)

tensor([1, 2, 3])
tensor([11, 12, 13])
tensor([11, 12, 13])
tensor([10, 20, 30])
tensor([10, 20, 30])
tensor([-9, -8, -7])
tensor([0.1000, 0.2000, 0.3000])
tensor([1, 4, 9])

矩阵相乘

One of the most common operations in machine learning and deep learning algorithms (like neural networks) is matrix multiplication.

做矩阵相乘的规则:

python 复制代码
(3,2) * (3,2) => 不符合条件

(2,3) * (3,2) = (2,2)

(3,2) * (2,3) = (3,3)

在 PyTorch 里,可以使用 torch.matmul() 方法。

python 复制代码
tensor_matrix = torch.tensor([1,2,3])
print(tensor_matrix * tensor_matrix)
print(torch.matmul(tensor_matrix, tensor_matrix))
print(tensor_matrix.matmul(tensor_matrix))

# 结果
tensor([1, 4, 9])
tensor(14)
tensor(14)
Operation Calculation Code
Element-wise multiplication [11, 22, 3*3] = [1, 4, 9] tensor * tensor
Matrix multiplication [11 + 22 + 3*3] = [14] tensor.matmul(tensor)

都看到这里了,给个赞咯~

相关推荐
数据的世界017 分钟前
重构智慧书-第7条:恰当彰显价值,勿越职场分寸
人工智能
xwill*8 分钟前
VOTE: Vision-Language-Action Optimization with Trajectory Ensemble Voting
人工智能·pytorch·深度学习
wxdlfkj8 分钟前
光谱共焦传感器 LTC2400/LTC4000F 对手机镜头镜片的圆角倒角厚度测量检测
人工智能
mys551812 分钟前
从SEO到GEO:AI搜索如何重塑企业流量新路径?
人工智能·aigc·geo·ai搜索优化·ai引擎优化
用户17178327988114 分钟前
AI大模型爆火Agent(打造专属LLM智能体)
人工智能
大千AI助手22 分钟前
多维空间的高效导航者:KD树算法深度解析
数据结构·人工智能·算法·机器学习·大千ai助手·kd tree·kd树
Coding茶水间22 分钟前
基于深度学习的西红柿成熟度检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·深度学习·yolo·目标检测·计算机视觉
roman_日积跬步-终至千里23 分钟前
【模式识别与机器学习(11)】数据预处理(第三部分):高级技术与质量保证
人工智能·机器学习·支持向量机
HX43625 分钟前
Swift - Sendable (not just Sendable)
人工智能·ios·全栈
大白的编程笔记26 分钟前
大语言模型(Large Language Model, LLM)系统详解
人工智能·语言模型·自然语言处理