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])

@浙大疏锦行

相关推荐
迅猛龙办公室5 分钟前
python实现倒序输出数字
开发语言·python
Joshua-a2 小时前
工程数学-理解卷积的含义_线性时不变系统的冲激响应与卷积
人工智能·深度学习
song5012 小时前
React Native for OpenHarmony 实战:三方库 react-native-css-transformer 的鸿蒙化适配指南
css·人工智能·深度学习·react native·transformer
全栈练习生9 小时前
AI Agent 沙箱
python·ai
SEO_juper10 小时前
用 Python 写一个 GEO 可见性检查脚本:你的网站现在能被 AI 引用吗
开发语言·人工智能·爬虫·python·seo·外贸独立站
数智工坊11 小时前
视觉SLAM第12讲|地图构建:单目稠密重建、RGB-D点云与八叉树地图全解析
人工智能·深度学习·矩阵·机器人
GPU实战笔记11 小时前
云端 GPU 训练的账户余额提醒:它能说明什么,不能说明什么?
深度学习·算法·成本管理·gpu云计算·云端训练
用户83562907805112 小时前
使用 Python 对 Excel 工作表进行排序
后端·python
for_ever_love__12 小时前
MySQL 全文索引实战:FULLTEXT、ngram 中文分词与 MATCH AGAINST 到底该怎么用
java·python·mysql·全文检索·分词·索引·ngram
用户83562907805112 小时前
使用 Python 合并 PowerPoint 演示文稿
后端·python