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

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

相关推荐
weixin_507929912 分钟前
第G7周:Semi-Supervised GAN 理论与实战
人工智能·pytorch·深度学习
一叶飘零_sweeeet20 分钟前
IDEA 插件 Trae AI 全攻略
java·人工智能·intellij-idea
SEO_juper44 分钟前
AI 搜索时代:引领变革,重塑您的 SEO 战略
人工智能·搜索引擎·seo·数字营销·seo优化
shengyicanmou1 小时前
深度解码格行无缝切网引擎:40%延迟降低背后的多网智能切换架构
人工智能·物联网·智能硬件
Hello123网站2 小时前
GLM-4-Flash:智谱AI推出的首个免费API服务,支持128K上下文
人工智能·ai工具
试剂界的爱马仕2 小时前
胶质母细胞瘤对化疗的敏感性由磷脂酰肌醇3-激酶β选择性调控
人工智能·科技·算法·机器学习·ai写作
青岛佰优联创新科技有限公司2 小时前
移动板房的网络化建设
服务器·人工智能·云计算·智慧城市
双向332 小时前
私有化部署全攻略:开源模型本地化改造的性能与安全评测
人工智能
189228048612 小时前
NY243NY253美光固态闪存NY257NY260
大数据·网络·人工智能·缓存
AI波克布林2 小时前
发文暴论!线性注意力is all you need!
人工智能·深度学习·神经网络·机器学习·注意力机制·线性注意力