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\]\]) [@浙大疏锦行](https://blog.csdn.net/weixin_45655710 "@浙大疏锦行")

相关推荐
Rolei_zl5 分钟前
AIGC(生成式AI)试用 48 -- AI与软件开发过程3
python·aigc
qq_416018728 分钟前
持续集成/持续部署(CI/CD) for Python
jvm·数据库·python
qq_4160187211 分钟前
使用Python处理计算机图形学(PIL/Pillow)
jvm·数据库·python
测试199814 分钟前
功能测试、自动化测试、性能测试的区别?
自动化测试·软件测试·python·功能测试·测试工具·性能测试·安全性测试
MyY_DO30 分钟前
序列模型说人话
python
AC赳赳老秦31 分钟前
使用OpenClaw tavily-search技能高效撰写工作报告:以人工智能在医疗行业的应用为例
运维·人工智能·python·flask·自动化·deepseek·openclaw
未知鱼1 小时前
Python安全开发之简易whois查询
java·python·安全
阿钱真强道1 小时前
26 Python 分类:一个模型不够稳怎么办?一文认识组合分类
python·随机森林·分类·提升·组合分类·装袋·投票分类器
左左右右左右摇晃1 小时前
Java笔记——多态
java·笔记·python
qyzm1 小时前
天梯赛练习题
数据结构·python·算法·贪心算法