python打卡day48

python 复制代码
import torch
python 复制代码
# 生成一个3x3的标准正态分布随机张量
random_tensor = torch.randn(3, 3)
print("随机张量:\n", random_tensor)

随机张量:

tensor(\[-0.9343, -0.3254, 0.6991,

-1.7157, 1.7171, -0.4322,

0.6004, -1.1050, -0.2178])

python 复制代码
# 生成一个形状为(2, 4)的随机张量
random_tensor_2 = torch.randn(2, 4)
print("\n2x4随机张量:\n", random_tensor_2)

2x4随机张量:

tensor(\[-0.0638, -0.6070, 0.0341, -0.5346,

-2.1379, -0.5141, 0.0484, 0.0098])

python 复制代码
# 标量与张量相加(广播)
tensor_a = torch.tensor([[1, 2], [3, 4]])
scalar = 5
result = tensor_a + scalar  # 标量5会被广播成[[5,5],[5,5]]
print("\n标量广播加法:\n", result)

标量广播加法:

tensor(\[6, 7,

8, 9])

python 复制代码
# 不同形状张量相加
tensor_b = torch.tensor([[10], [20]])  # 形状(2,1)
result = tensor_a + tensor_b  # tensor_b会被广播成[[10,10],[20,20]]
print("\n不同形状张量加法:\n", result)

不同形状张量加法:

tensor(\[11, 12,

23, 24])

python 复制代码
# 标量与张量相乘(广播)
result = tensor_a * 2  # 标量2会被广播成[[2,2],[2,2]]
print("\n标量广播乘法:\n", result)

标量广播乘法:

tensor(\[2, 4,

6, 8])

python 复制代码
# 不同形状张量相乘
tensor_c = torch.tensor([100, 200])  # 形状(2,)
result = tensor_a * tensor_c  # tensor_c会被广播成[[100,200],[100,200]]
print("\n不同形状张量乘法:\n", result)

不同形状张量乘法:

tensor(\[100, 400,

300, 800])

@浙大疏锦行

相关推荐
Wellijohn4 小时前
我把公司内部的 12 个 CRUD 接口交给了 AI Agent,然后省出了两周
人工智能·python·mcp
2601_967980116 小时前
python pytest怎样测试代码
python·pytest·测试·文档·断言
伞伞悦读6 小时前
【第92期】逻辑回归的分数概率与阈值的实验与排查
人工智能·python·深度学习·机器学习
ch6237 小时前
🚀 从零构建本地文档智能审查 Agent:Vibe Coding 实战指南
python
摇滚侠7 小时前
《On Java 中文版 基础卷》阅读笔记 接口 18
java·笔记·python
Thanks_ks8 小时前
AI Agent 开发入门:用 Function Calling 让大模型学会 “动手干活”,附订单查询机器人实战
python·ai·大模型·openai·agent·通义千问·mcp
专注仿真8 小时前
智能体训练之模仿学习:GAIL 算法详解与实战
python·算法·仿真·多智能体强化学习·afsim智能体训练
databook9 小时前
处理一批 PDF,怎么算真的处理完了
后端·python
承渊政道11 小时前
【Python编程快速上手—让繁琐工作自动化】(列表:把数据排成队,从下标取值到深拷贝)
开发语言·python·pycharm·自动化·列表
大胖爱学习_MJ11 小时前
Agent 如何安全地拿数据库密码?基于 HashiCorp Vault 的动态凭证下发
经验分享·python