首页 > 其他分享 >【笔记】动手学深度学习-预备知识

【笔记】动手学深度学习-预备知识

时间:2024-04-25 19:00:11浏览次数:19  
标签:tensor 预备 sum torch 笔记 动手 shape arange print

预备知识
2.1 数据操作

import torch

x = torch.arange(12)

print(x.shape)

print(torch.Size(x))

print(x.numel())

X = x.reshape(3, 4)

print(X)

print(torch.ones((2, 3, 4)))

print(torch.randn(3, 4))

print(torch.tensor([[2, 1, 3, 4], [1, 2, 3, 4], [3, 4, 5, 6]]))


x = torch.tensor([1.0, 2, 4, 8])
y = torch.tensor([2, 2, 2, 2])
print(x + y, x - y, x * y, x / y, x ** y)  # **运算符是求幂运算

print(torch.exp(x))


X = torch.arange(12, dtype=torch.float32).reshape((3,4))
Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
print(torch.cat((X, Y), dim=0))
print(torch.cat((X, Y), dim=1))

print(X == Y)

print(X.sum())

a = torch.arange(3).reshape((3, 1))
b = torch.arange(2).reshape((1, 2))
print(a)
print(b)
print(a+b)

print(X[-1])
print(X[1:3])

X[1, 2] = 9
print(X)

X[0:2, :] = 12
print(X)

before = id(Y)
Y = Y + X
print(id(Y) == before)

Z = torch.zeros_like(Y)
print('id(Z):', id(Z))
Z[:] = X + Y
print('id(Z):', id(Z))

A = X.numpy()
B = torch.tensor(A)
print(type(A))
print(type(B))

a = torch.tensor([3.5])
print(a)
print(a.item())
print(float(a))
print(int(a))

2.2 数据预处理

import pandas as pd

data = pd.read_csv(data_file)
inputs, outputs = data.iloc[:, 0:2], data.iloc[:, 2]
inputs = inputs.fillna(inputs.mean())
print(inputs)

import torch

X = torch.tensor(inputs.to_numpy(dtype=float))
y = torch.tensor(outputs.to_numpy(dtype=float))
print(x)
print(y)

2.3 线性代数

import torch

x = torch.tensor(3.0)
y = torch.tensor(2.0)

print(x + y)
print(x * y)
print(x / y)
print(x**y)

x = torch.arange(4)
print(x)

print(x[3])

print(len(x))

print(x.shape)

A = torch.arange(20).reshape(5, 4)
print(A)

print(A.T)

B = torch.tensor([[1, 2, 3], [2, 0, 4], [3, 4, 5]])
print(B)

print(B == B.T)

print(X = torch.arange(24).reshape(2, 3, 4))
print(X)


A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
B = A.clone()  # 通过分配新内存,将A的一个副本分配给B
print(A)
print(A + B)

print(A * B)

a = 2
X = torch.arange(24).reshape(2, 3, 4)
print(a + X)
print((a * X).shape)

x = torch.arange(4, dtype=torch.float32)
print(x)
print(x.sum())

A_sum_axis0 = A.sum(axis=0)
print(A_sum_axis0)
print(A_sum_axis0.shape)

A_sum_axis1 = A.sum(axis=1)
print(A_sum_axis1)
print(A_sum_axis1.shape)

print(A.sum(axis=[0, 1]))

print(A.mean())
print(A.sum())
print(A.numel())

print(A.mean(axis=0), A.sum(axis=0) / A.shape[0])

sum_A = A.sum(axis=1, keepdims=True)
print(sum_A)

print(A / sum_A)

print(A.cumsum(axis=0))

y = torch.ones(4, dtype = torch.float32)
print(x)
print(y)
print(torch.dot(x, y))
print(torch.sum(x * y))

print(A.shape)
print(x.shape)
print(torch.mv(A, x))

B = torch.ones(4, 3)
print(torch.mm(A, B))

u = torch.tensor([3.0, -4.0])
print(torch.norm(u))

print(torch.abs(u).sum())

print(torch.norm(torch.ones((4, 9))))

标签:tensor,预备,sum,torch,笔记,动手,shape,arange,print
From: https://www.cnblogs.com/everfight/p/18158369

相关文章

  • 深入理解 FFmpeg 书籍笔记
    知识点太多,目前只记录遇到的错误1.在Ubuntu22.0464位上编译FFmpeg-0.6.3时,使用./configure配置时遇到如下错误ffserver.c:Infunction‘rtsp_cmd_describe’:ffserver.c:2987:5:error:implicitdeclarationoffunction‘ff_url_split’[-Werror=implicit-fun......
  • 【笔记】拓扑图工具调研
    一、在线拓扑图编辑工具零代码三维地图开发http://www.emapgis.com/数字孪生https://www.hightopo.com/demos/index.htmlvue-antvx6-demo推荐。https://gitee.com/yanggengzhen/vue-antvx6-demo/tree/masterhttps://qunee.com/数百个HTML5例子学习HT图形组件–WebGL3D......
  • 《A Discriminative Feature Learning Approach for Deep Face Recognition》阅读笔记
    论文标题《ADiscriminativeFeatureLearningApproachforDeepFaceRecognition》一种用于深度人脸识别的判别性特征学习方法作者YandongWen、KaipengZhang、ZhifengLi和YuQiao来自深圳市计算机视觉与专利重点实验室、中国科学院深圳先进技术研究院和香港中文大学......
  • 多项式乘法(FFT)学习笔记
    Reference:@自为风月马前卒亦@attack的博客这里为了帮助我自己理解,先手推(抄)一遍这位dalao给出的快速傅里叶逆变换的证明。\((y_0,y_1,\dots,y_{n-1})\)为多项式\((a_0,a_1,\dots,a_{n-1})\)在\(x\)取\((\omega^0_n,\omega^1_n,\dots,\omega^{n-1}_n)\)时......
  • 机组笔记
    哈工大刘宏伟老师《计算机组成原理》课程笔记-z5onk0-博客园哈工大刘宏伟老师《计算机组成原理》课程笔记目录1.0课程简介1.1计算机系统简介1.2计算机的基本组成1.2.1冯诺依曼计算机1.2.2现代计算机硬件框图1.2.3存储器1.2.4运算器1.2.5控制器1.3计算机硬......
  • HCDG天津站精彩回顾 | AI高效开发, ModelArts技术动手工作坊
    4月20日,HCDG城市行·天津站——“极快至简灵活部署”我想象中的AI高效开发ModelArts技术工作坊圆满举办。活动特邀来自华为云DTSE、华为云云享专家、以及天津软件技术协会的伙伴企业专家们为与会开发者带来一场别开生面的技术交流及动手实践活动。活动伊始,天津市软件行业协会技......
  • windows系统修复笔记
    windows修复笔记 1.检查修复硬盘chkdskC:/R/f2. 修复引导Bootrec.exe/fixmbrBootrec/fixbootbootrec/RebuildBcdbcdbootc:\windows/sc:3.系统文件修复sfc/SCANNOW 参考连接:还原或修复运行WindowsServerEssentials的服务器|MicrosoftLear......
  • 【学习笔记】Python 使用 matplotlib 画图
    目录安装中文显示折线图、点线图柱状图、堆积柱状图坐标轴断点参考资料本文将介绍如何使用Python的matplotlib库画图,记录一些常用的画图demo代码安装#建议先切换到虚拟环境中pipinstallmatplotlib中文显示新版的matplotlib已经支持字体回退功能,因此可以直接设置......
  • 学习 Avalonia 框架笔记 如何创建一个全屏置顶的 X11 应用窗口
    本文记录我从Avalonia框架里面学到如何创建一个全屏置顶的X11应用窗口的方法开始之前,先从Avalonia或CPF里面拷贝足够的代码,这部分代码可以从本文末尾找到下载方法设置全屏的核心代码是以下三行ChangeWMAtoms(false,XLib.XInternAtom(display,"_NET_WM_STATE_HIDDEN",......
  • 普通有限多项式笔记
    普通多项式笔记\(\textrm{Newton'sMethod}\)(牛顿迭代)应用于解决已知\(g(x)\)的情况下,求出\(g(f(x))\equiv0\modx^n\)。首先通过列出方程显然,\(f(x)\modx^n\)在此时是唯一的。那么我们假设已知\(g(f_0(x))\equiv0\modx^{n/2}\),显然此时\(f_0(x)\modx^{n/2}\)也......