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

相关推荐
墨尘游子2 分钟前
目标导向的强化学习:问题定义与 HER 算法详解—强化学习(19)
人工智能·python·算法
贝塔西塔1 小时前
PytorchLightning最佳实践基础篇
pytorch·深度学习·lightning·编程框架
小白学大数据1 小时前
基于Python的新闻爬虫:实时追踪行业动态
开发语言·爬虫·python
freed_Day1 小时前
python面向对象编程详解
开发语言·python
普郎特2 小时前
张三:从泥水匠到包工头的故事 *—— 深入浅出讲解 `run_in_executor()` 的工作原理*
python
ReinaXue2 小时前
大模型【进阶】(五):低秩适配矩阵LORA的深度认识
人工智能·深度学习·神经网络·语言模型·自然语言处理·transformer
小猪和纸箱2 小时前
通过Python交互式控制台理解Conv1d的输入输出
pytorch
DisonTangor2 小时前
商汤InternLM发布最先进的开源多模态推理模型——Intern-S1
人工智能·深度学习·开源·aigc
我要学习别拦我~2 小时前
kaggle分析项目:steam付费游戏数据分析
python·游戏·数据分析
大模型真好玩2 小时前
深入浅出LangChain AI Agent智能体开发教程(四)—LangChain记忆存储与多轮对话机器人搭建
前端·人工智能·python