文章目录
pdb
pdb开始调试python
python -m pdb 源程序.py
pdb设置断点
- linenumber是断点所在行数
break linenumber
b linenumber
- 取消断点
clear linenumber
重新运行
run
注意分别:r是return的意思,就是执行到函数结束
单步执行
n(ext)
进入到函数的内部
s(tep)
执行到下一个断点或程序结束
c(ontinue)
调用栈查看命令
w(here)
查看当前函数调用堆栈
up
向上一层函数查看调用堆栈
down
查看源代码
ll 查看当前函数的源代码
import
import 用法
-
导入整个模块。基本的import语句用于导入整个模块,可以使用模块名直接访问模块中的内容。例如,import math后可以使用math.sqrt()或math.fabs()等函数。
-
为模块指定别名。使用as关键字可以为导入的模块指定别名,例如import math as m,然后可以使用m.sqrt()代替math.sqrt()。
-
导入模块中的特定成员。使用from…import…语句可以导入模块中的特定函数、变量或类,例如from math import sqrt,这样可以直接使用sqrt()函数,而不需要通过模块名前缀。
-
导入模块中的所有内容。from…import *语句用于导入模块中的所有函数、类和变量,这样可以直接使用这些内容,而无需通过模块名前缀。但这种方法可能导致命名冲突,因此不推荐在大型项目中使用。
+ 导入包中的模块。可以使用点模块名来导入包中的模块,例如import os或import math
+
python格式化字符串
print(f"epoches:{epoch},accurate={acc}")
lambda 匿名函数
用法
numpy
导入numpy模块
import numpy as np #导入numpy并设置别名
numpy常用函数
shape和ndim
科学计数法
1e-6 = 1乘10的负6次方
reshape
reshape()函数用于在不更改数据的情况下为数组赋予新形状
>>> c=np.array([[2,3,4],[5,6,7]])
>>> c
array([[2, 3, 4],
[5, 6, 7]])
>>> c=c.reshape(3,2)
>>> c
array([[2, 3],
[4, 5],
[6, 7]])
np.argmax
import numpy as np
a = np.arange(6).reshape(2,3)
print('a数组:\n{}'.format(a))
print('a数组中最大值的索引:{}'.format(np.argmax(a)))
print('a数组沿列方向最大值的索引:{}'.format(np.argmax(a, axis=0)))
print('a数组沿行方向最大值的索引:{}'.format(np.argmax(a, axis=1)))
b = np.arange(6)
b[1] = 5
print('b数组:\n{}'.format(b))
print('b数组最大值的索引:{}'.format(np.argmax(b))) # 仅仅返回5第一次出现的位置
a数组:
[[0 1 2]
[3 4 5]]
a数组中最大值的索引:5
a数组沿列方向最大值的索引:[1 1 1]
a数组沿行方向最大值的索引:[2 2]
b数组:
[0 5 2 3 4 5]
b数组最大值的索引:1
np.sum
- 不传参数是对所有元素求和
传axis(看懂了一点,以后要用的时候再看看)
传送門
range
生成连续序列
for i in range(start,end): #start 到 end-1间隔为1 的序列
生成不连续序列
for i in range(start,end,interval): #start 到 end-1间隔为interval 的序列
matplotlib.pylab
导入
import matplotlib.pylab as plt
xlabel ylabel
- x轴,y轴的介绍
plt.xlabel("x")
plt.ylabel("f(x)")
plot
linestyle:
plt.plot(x,y,linestyle='--')
plt.legend
plt.show
- 用来显示图标
print 格式化输出
name = 'Alice'
age = 25
print("My name is %s and I am %d years old." % (name,age))
格式化浮点数
打印格式化浮点数
print('%10.3f' % 3.1415926)
字典
定义
- 和C++的map差不多,都是保存键值对
初始化
- 空字典
network = {}
- 赋值
#法一
network = {'W1':np.array([[0.1,0.3,0.5],[0.2,0.4,0.6] ]) }
#法二
network['W1'] = np.array([[0.1,0.3,0.5],[0.2,0.4,0.6]])
torch
矩阵乘法
"""
1.a*b 按位相乘
2.torch.matmul(a,b) == a @ b,支持broadcast
- 2.1.torch.dot(),1d,点积,不支持broadcast
- 2.2.torch.mm(),2d,不支持broadcast
- 2.3.torch.bmm(),3d,不支持broadcast
"""
a*b 按位相乘
torch.dot(),一维,向量内积
torch.mm 二维矩阵乘法 (n,s) *(s,t) -> (n,t)
torch.matmul 高维矩阵乘法,具有广播机制(torch.mm的进阶版)
torch.bmm 三维矩阵乘法 (b,n,s) *(b,s,t) -> (b,n,t)
看标题就已经懂了
torch.utils
torch.utils.data.Dataset
- 传入DataLoader需要是这个类
- 可以自定义类继承Dataset类,也可以用torch.utils.data.TensorDataset转换
torch.utils.data.DataLoader
DataLoader(TensorDataset对象,batch_size每批大小,shuffle是否打乱顺序,num_workers加载线程数)
torch.nn
Layer们
torch.nn.Linear 线性层/全连接层
y = xw + b # 输入x [num_samples, in_features]
# 训练网络w [in_features, out_features]
# 训练偏置 b[num_samples, out_features]
torch.nn.Sequential
- 按照特定的顺序排列一系列的网络层
import torch.nn as nn
model = nn.Sequential(
nn.Linear(in_features=10, out_features=20), #第一个全连接层
nn.ReLU(), #激活函数
nn.Linear(in_features=20, out_features=1) #第二个全连接层,输入特征为20,输出特征为1
)
torchvision
torchvision.datasets
- 下载数据示例:fashion-mnist
import torchvision
from torchvision import transforms
trans = transforms.ToTensor()
mnist_train = torchvision.datasets.FashionMNIST(root="../data",train=True, transform=trans, download=True)
mnist_test = torchvision.datasets.FashionMNIST(root="../data",train=False, transform=trans, download=True)
len(mnist_train), len(mnist_test) #60000, 10000
mnist_train[0][0].shape #[1,28,28]