0%

docker file 包含一系列命令行,docker通过该文件组织生成镜像,一个docker file文件主要包括四部分:

  1. 基础镜像信息
  2. 维护者信息
  3. 镜像操作指令
  4. 容器启动时执行指令

以一个dockerfile为例:

Dockerfile 的指令每执行一次都会在 docker 上新建一层。所以过多无意义的层,会造成镜像膨胀过大

阅读全文 »

1. 动量梯度下降

为了解决随机梯度梯度下降存在的震荡问题,通过添加动量,平滑动量变化。

什么是指数加权平均

当前步的函数值不仅取决于当前的输入,还取决于之前的函数值,公式如下

通过不断带入展开,可得只包含 的表达式为

由于 在n趋向于无穷时等于 。当 趋向于1时, 同样等于 ,如果将 看作一个可以忽略的数字,则展开式中含有阶数大于等于 的相可以忽略。

  • ​时,​ ,所以所有系数包含​ n>=20的均省略,原式子等价于前20个​的加权平均和,即当前梯度等于前20个梯度的加权平均和
    阅读全文 »

什么是树状DP

建立在 ”树“ 这一数据结构上的DP问题,难度介于线性dp和图dp之间,当前节点的状态可能取决于父亲节点或者孩子节点,即存在两种状态转移方向,树可能包括二叉树和多叉树。

难点还是如何找到状态转移方程

主要题目类型

  1. 最大独立子集合问题

    给一无向图,找出一个点集,使得任意两点之间都没有连边,这个点集就是独立集。而点最多的独立集,就是最大独立集,针对不问题,选取点的条件可能发生变化,但总体还是在限定条件下,选择最优的点集

  2. 最小点覆盖

  3. 最小支配集

    阅读全文 »

The sklearn.preprocessing package provides several common utility functions and transformer classes to change raw feature vectors into a representation that is more suitable for the downstream estimators.

sklearn的preprocessing模块提供了一系列包括标准化、数据最大最小缩放处理、正则化、特征二值化和数据缺失值处理在内的数据预处理模块。

基本操作流程为:

1
2
3
4
5
6
7
8
# 1.创建预处理器 transform
test_scaler = StandardScaler()
# 2. 调用fit函数 计算预处理所需要的相关数据(如StandardScaler会计算mean、var等)
test_scaler.fit(input)
# 3. 调用transform函数对数据进行预处理
test_scaler.transform(input)
# 或者直接合并fit和transform两部操作
test_scaler.fit_transform(input)
阅读全文 »

Transformers are usually combined with classifiers, regressors or other estimators to build a composite estimator

将一系列的数据预处理和模型封装在一起,固定处理数据的一系列步骤,调用一次fit和predict完成整个流程,并可使用grid search对pipeline内参数进行统一调试。

构成规则如下(前面处理数据,最后一步输入模型或者完全处理数据):

  • 所有流水线中的estimator必须为transformer,除了最后一个
  • 最后一个estimator可以是任何类型(trainsformer,classifier)

主要函数:

  • union为只包含transformer的pipeline
阅读全文 »

为了避免模型过拟合,通过添加验证集,模型训练完成后使用验证集验证模型的效果后,再对测试集进行测试。

训练集和测试集划分

使用train_test_split函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>> import numpy as np
>> from sklearn.model_selection import train_test_split
>> from sklearn import datasets

# 加载鸢尾花数据集
>> x_data, y_data = datasets.load_iris(return_X_y=True)
# 划分测试集和训练集
>> x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size = 0.2, random_state = 42)
>> print(x_train.shape)
>> print(x_test.shape)
(120, 4)
(30, 4)
>> from sklearn import svm
>> clf = svm.SVC(kernel='linear', C=1).fit(X_train, y_train)
>> clf.score(X_test, y_test)
1.0
阅读全文 »

主要包括六个模块,其中四个模块为分类,回归,聚类,降维的算法模块,两个其他模型,模型选择评估模块和预处理模块。

阅读全文 »

Scatter函数

scatter_(dim, index, src, reduce=None) → Tensor

Writes all values from the tensor src into self at the indices specified in the index tensor. For each value in src, its output index is specified by its index in src for dimension != dim and by the corresponding value in index for dimension = dim.

简单理解就是将 src 张量中的元素散落到 self 张量中,具体选择哪个元素,选择的元素散落到哪个位置由index张量决定,具体的映射规则为:

三维张量为例
1
2
3
4
# 其中 i,j,k 为index张量中元素坐标。
self[index[i][j][k]][j][k] = src[i][j][k] # if dim == 0
self[i][index[i][j][k]][k] = src[i][j][k] # if dim == 1
self[i][j][index[i][j][k]] = src[i][j][k] # if dim == 2
阅读全文 »

如何对梯度追踪张量进行inplace操作

别忘了梯度追踪张量必须为float

Only Tensors of floating point and complex dtype can require gradients

问题来源

当对设置了requires_grad=True的张量进行原地操作时,pytorch会抛出运行错误:

1
2
3
>> X = torch.rand((3,4),requires_grad = True)
>> X.fill_(0)
RuntimeError: a leaf Variable that requires grad is being used in an in-place operation.
阅读全文 »

DataLoaders和DataSets使用总结

通过 torch.utils.data.Dataset类定义数据集,通过torch.utils.data.DataLoaderDataset配合定义数据加载方式

torch.utils.data.Dataset主要参数:

  1. dataset 指定构造的数据集,一般是数据+label的元组
  2. shuffle 指定是否打乱
  3. sampler 指定采样器,采取某种方式遍历数据(顺序,随机等)
  4. collate_fn (callable, optional):将数据形成一个batch的tensor(在某些时候需要自定义)
    阅读全文 »