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

相关推荐
yzx9910133 分钟前
图像去雾:从暗通道先验到可学习融合——一份可跑的 PyTorch 教程
人工智能·pytorch·学习
AI Echoes31 分钟前
别再手工缝合API了!开源LLMOps神器LMForge,让你像搭积木一样玩转AI智能体!
人工智能·python·langchain·开源·agent
AI Echoes35 分钟前
从零构建企业级LLMOps平台:LMForge——支持多模型、可视化编排、知识库与安全审核的全栈解决方案
人工智能·python·langchain·开源·agent
啦啦啦在冲冲冲1 小时前
解释一下roberta,bert-chinese和bert-case有啥区别还有bert-large这些
人工智能·深度学习·bert
2401_897930061 小时前
PyTorch 中训练语言模型过程
人工智能·pytorch·语言模型
beijingliushao1 小时前
58-正则表达式
数据库·python·mysql·正则表达式
陈敬雷-充电了么-CEO兼CTO1 小时前
具身智能多模态感知与场景理解:融合语言模型的多模态大模型
人工智能·python·gpt·语言模型·自然语言处理·chatgpt·多模态
荔枝吻1 小时前
【AI总结】Python BERT 向量化入门指南
人工智能·python·bert
张子夜 iiii2 小时前
传统神经网络实现-----手写数字识别(MNIST)项目
人工智能·pytorch·python·深度学习·算法
Rhys..2 小时前
python + Flask模块学习 1 基础用法
python·学习·前端框架·flask