机器学习 - 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 1*1, 2*2, 3\*3 = 1, 4, 9 tensor * tensor
Matrix multiplication 1*1 + 2*2 + 3\*3 = 14 tensor.matmul(tensor)

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

相关推荐
艾莉丝努力练剑19 分钟前
【AI大模型接入SDK】ChatGPT API
网络·c++·人工智能·websocket·网络协议·学习·chatgpt
枫叶林FYL1 小时前
【群体智能集群控制工程实践】第8章 无人机蜂群测试与部署
人工智能·无人机
2601_949950635 小时前
练题簿,把培训从“走过场”变成“真落地”
人工智能·学习·小程序·刷题·小程序推荐
诸神缄默不语6 小时前
备战 AI 出海 DAY 0:海外数据采集
大数据·人工智能
Ysn07197 小时前
YOLO-Master工程:experts.py 专家结构详解与目标检测专家设计启发
人工智能·yolo·目标检测
BingoGo7 小时前
使用 GPT-6 Astra 模型 请立刻更新你的 Skill 与提示词
人工智能
香菜+7 小时前
端侧视频分析 · 架构笔记
人工智能
s1ckrain7 小时前
【论文阅读】A Survey on Vision–Language–Action Models for Embodied AI
论文阅读·人工智能·多模态·具身智能
深圳雨林凯AI8 小时前
图案裂变的“风格归一化“设计思路:主体保得住,画风才拉得齐|雨林凯AI技术笔记
人工智能·笔记
DevNo8 小时前
英文会议转中文纪要,三款AI工具实测体验
人工智能