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 "@浙大疏锦行")

相关推荐
故林丶5 分钟前
【Django】Django笔记
python·django
IT北辰32 分钟前
Python实现居民供暖中暖气能耗数据可视化分析(文中含源码)
开发语言·python·信息可视化
FreeCode32 分钟前
LangChain1.0智能体开发:长期记忆
python·langchain·agent
PixelMind1 小时前
【IQA技术专题】 基于多模态大模型的IQA Benchmark:Q-BENCH
图像处理·深度学习·lmm·iqa
Predestination王瀞潞1 小时前
Python __name__ 与 __main__
开发语言·python
萧曵 丶1 小时前
Python 字符串、列表、元组、字典、集合常用函数
开发语言·前端·python
梦想的初衷~2 小时前
Plaxis自动化建模与Python应用全解:从环境搭建到高级案例实战
python·自动化·工程设计·工程软件
Q_Q5110082852 小时前
python+uniapp基于微信小程序的垃圾分类信息系统
spring boot·python·微信小程序·django·flask·uni-app·node.js
HackerTom2 小时前
vs code jupyter连gpu结点kernel
python·jupyter·gpu·vs code·远程
cyyt2 小时前
深度学习周报(11.3~11.9)
人工智能·深度学习