04 Pytorch tensor

一:老版本的 variable

二:新版 tensor

曾经:求导相关

如今:数据相关

--dtype: 张量的数据类型,三大类,共9种。torch.FloatTensor, torch.cuda.FloatTensor

--shape: 张量的形状。如:(64,3,224,224)

--decive: 所在设备

三:tensor创建方法

Tensor创建一:直接创建 (共享内存:out)

1)

复制代码
torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False)

2) 从numpy创建tensor : torch.from_numpy(ndarray)

注意:共享内容

python 复制代码
import torch
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
t = torch.from_numpy(arr)

t[0, 0] = -1
print(arr)
print(t)
Tensor创建二:依据数值创建(等差,均分,单位矩阵)

4.2 依据数值创建

1)torch.zeros():按照size创建全0张量

复制代码
torch.zeros(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
  1. 21torch.zeros_like()

    torch.zeros_like(input, dtype=None, layout=None, device=None, requires_grad=False)

  2. torch.ones()

  3. torch.ones_like()

  4. torch.full()

  5. torch.full_like()

  6. torch.arange(),创建等差数列,区间:[start, end,数值) 等差创建

6.torch.linspace(),创建均分数列,区间:[start, end]

注意:step是步长;steps是长度

t = torch.linspace(start=0, end=100, steps=5, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)

Tensor创建三:依据概率创建(正太,标准正太,伯努利分布)

第二章:Tensor 操作与线性回归

2.1:张量的操作:拼接、切分、索引和变换
2.1.1拼接:
  • torch.cat(): 将张量按维度dim进行拼接
python 复制代码
t_0 = torch.cat([t, t], dim=0)
t_1 = torch.cat([t, t], dim=1)

torch.Size([4, 3])

torch.Size([2, 6])

  • torch.stack():在新建的维度dim上进行拼接
python 复制代码
t = torch.ones((2, 3))

t_0 = torch.cat([t, t], dim=0)
t_1 = torch.stack([t, t], dim=0)

print(t_0)
print(t_0.shape)
print(t_1)
print(t_1.shape)
2.1.2 切分

按维度dim进行平均切分: torch.chunk(input, chunks, dim)

python 复制代码
t = torch.ones((2, 7))
print(t)

list_of_tensor = torch.chunk(t, dim=1, chunks=3)
print(list_of_tensor)

指定切分:torch.split() [2,2,3]切分三个,每个都有特定的te'zheng'sh

python 复制代码
t = torch.ones((2, 7))
print(t)
list_of_tensor_2 = torch.split(t, 3, dim=1)
print(list_of_tensor_2)

list_of_tensor_3 = torch.split(t, [2, 2, 3], dim=1)
print(list_of_tensor_3)
2.1.3 reshape

torch.reshape: 变换张量形状

notice: 注意事项:当张量在内存中是连续时,新张 量与input共享数据内存

python 复制代码
# torch.reshape
t = torch.randperm(8)
print(t)
t_reshape = torch.reshape(t, (2, 4))  # -1代表不关心
print(t_reshape)
2.2张量的数学运算

2.3线性回归

用wx+b 去拟合谁?

逻辑回归:

Deepshare-Pytorch/作业/第二周作业1/第二周作业1.md at master · 799609164/Deepshare-Pytorch · GitHub

相关推荐
九章云极AladdinEdu23 分钟前
临床数据挖掘与分析:利用GPU加速Pandas和Scikit-learn处理大规模数据集
人工智能·pytorch·数据挖掘·pandas·scikit-learn·paddlepaddle·gpu算力
上海锝秉工控24 分钟前
超声波风向传感器:以科技之翼,捕捉风的每一次呼吸
大数据·人工智能·科技
说私域25 分钟前
基于开源AI智能名片、链动2+1模式与S2B2C商城小程序的流量运营与个人IP构建研究
人工智能·小程序·流量运营
海天一色y1 小时前
Pycharm(二十一)递归删除文件夹
ide·python·pycharm
xiaoxiaoxiaolll2 小时前
期刊速递 | 《Light Sci. Appl.》超宽带光热电机理研究,推动碳纳米管传感器在制药质控中的实际应用
人工智能·学习
练习两年半的工程师2 小时前
AWS TechFest 2025: 风险模型的转变、流程设计的转型、生成式 AI 从实验走向实施的三大关键要素、评估生成式 AI 用例的适配度
人工智能·科技·金融·aws
Elastic 中国社区官方博客5 小时前
Elasticsearch:智能搜索的 MCP
大数据·人工智能·elasticsearch·搜索引擎·全文检索
stbomei5 小时前
从“能说话”到“会做事”:AI Agent如何重构日常工作流?
人工智能
yzx9910135 小时前
生活在数字世界:一份人人都能看懂的网络安全生存指南
运维·开发语言·网络·人工智能·自动化
许泽宇的技术分享6 小时前
LangGraph深度解析:构建下一代智能Agent的架构革命——从Pregel到现代AI工作流的技术飞跃
人工智能·架构