机器学习 - 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)

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

相关推荐
码路高手几秒前
Trae-Agent中的tool reflection机制
人工智能·架构
Timer@3 分钟前
LangChain 教程 01|概述:AI 应用开发的新范式
人工智能·语言模型·langchain·前端框架
qq_白羊座6 分钟前
AI 所处的发展阶段
人工智能
GoCoding9 分钟前
Triton 环境
人工智能
智塑未来13 分钟前
像素蛋糕安卓版 AI 专业修图全场景输出高清成片
android·人工智能
懒麻蛇17 分钟前
用大语言模型生成心理学范式 2.0
人工智能·语言模型·自然语言处理
人工干智能26 分钟前
用AI写Fusion 360脚本:个人版也能免费玩自动化
运维·人工智能·自动化·fusion
薛定猫AI29 分钟前
【深度解析】从 MAI Image 2 到自进化智能体:新一代 AI 系统架构与实战落地
人工智能
K姐研究社40 分钟前
智象未来「帧赞」内测 – AI原生多模态大模型终结”抽卡”时代
人工智能·aigc·ai-native
AI产品备案1 小时前
生成式人工智能(大语言模型)上线备案表【模版分享+填报指南】
人工智能·语言模型·自然语言处理