4.5 weight reduction
4.5 weight reduction12345#我们为了减小过拟合,也就是死记硬背不能达到学习的目的,我们加入惩罚项,使其不能死记硬背#将原来的训练目标:最小化训练标签上的预测损失,调整为最小化预测损失和惩罚项之和。#we take L2 norm as the penalty term. If the weight vector increases largely, our learning algorithm will more #concentrate on minimizing weight norm ||w||^2.#more details we will make up in the future.
4.5.2 high-dimension linear regression1234%matplotlib inlineimport torchfrom torch import nnfrom d2l import torch as d2l
1234567891011#firstly we generate some data like previously, with ...
KNN algorithm
算法原理KNN算法(k-near-neighbours algorithm)可用来将样本分类,也可以用来预测样本走向。我们本次先说归类。
借鉴一个图片,
通俗地讲,图中绿色的是未知点,红色和蓝色是两个标签,已经分好类了,我们要判断绿色属于红色类还是蓝色类。于是我们选择了参数K=3,从绿色的点旁边找三个距离最近的点,然后计算红色点和蓝色点的占比,哪个占比大,就属于哪一类。
K的选取很重要,因为K很小容易出现过拟合,K很大,在回归问题中,如果样本点呈现二次函数走势,取很多很远的样本纳入考虑,会使得走势不准,也就是欠拟合。
所以我们的算法流程如下
123计算待分类点与已知类别点之间的距离将所有点按照距离递增次序排序,选择参数K,取距离待定点最近的K个点计算K个点中不同类别的占比,占比最大的类,即为结果。
伪代码1234567891011121314151617181920212223#我们假设dataset是二维数组,每个向量之中两个分量,第一个表示类别比如红色,第二个表示值。int a#待定点dataset.reshape(-1, 2)#也就是修改成列向量的形式。for i in ran ...
cryptography
我们平时的key, cer等等密钥文件,打开时并不是我们喜欢的n,p,q的公私钥形式,而是已经编码好的形式,而编码的规则就是基于X.690标准中的ASN.1的编码格式:Basic Encoding Rules(BER), Canonical Encoding Rules(CER), Distinguished Encoding Rules(DER).
The BER were origin rules laid out by the ASN.1 standard for encoding data into a binary format.The rules used octets(8bit bytes) to encode data.
X.680 defines a syntax for declaring data types, for example: booleans, numbers, strings and compound structures. Each type definition also includes an identifying number.
X.68 ...
4.4 model choosing, underfitting and overfitting
4.4.1 polynomial regression12345import mathimport numpy as npimport torchfrom torch import nnfrom d2l import torch as d2l
4.4.4多项式回归1. generate datasets1234#中间内容补充:#泛化误差:我们通过将模型应用于一个独立的测试集来估计泛化误差,随机抽取该测试集,测试集不能在训练样本中出现过,避免过拟合(overfitting)。#所以泛化误差也就是模型应用在同样从原始样本的分布中抽取的无限多数据样本上,误差的期望。#训练误差:模型在训练数据集上得到的误差。
1234567#给定x,我们将使用以下三阶多项式来生成训练数据和测试数据的标签:#y = 5 + 1.2x - 3.4x^2 / 2! + 5.6x^3 / 3! + epsilon其中epsilon满足正态分布N(0,0.1^2)max_degree = 20 #多项式最大阶数n_train, n_test = 100, 100true_w = np.zeros(max_degree) ...
4.1 multilayer percepton多层感知机
4.1 multilayer percepton基础知识先略过,以后补充。123456#Activation function calculate weight and add bias to determine whether neuron should be activated.#They transform input signals into output differentiable operations.#Most activation functions are nonlinear%matplotlib inlineimport torchfrom d2l import torch as d2l
1.Relu函数1234#rectified linear unit, ReLU = max(x, 0) namely discard all the minus elements.x = torch.arange(-8.0, 8.0, 0.1, requires_grad=True)y = torch.relu(x)d2l.plot(x.detach(), y.detach() ...
3.7 Softmax regression brief achievement
123import torchfrom torch import nnfrom d2l import torch as d2l
12batch_size = 256train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)#先加载数据集并且分类为训练集和测试集
3.7.1 Initialize modelling parameters1234567891011#Softmax regression output layer is a fully connection layer. So to realize the model, we only#need to add a 10-output fully connected layer in Sequential#pytorch will not adjust shape of input implicitly#so we define flatten layer before Linear layer to adjust the shape of network in ...
3.6 Start softmax regression from zero
12345#We should know about the detail of softmax, so we use Fashion-Mnist dataset in 3.5 chapter, and we set the#batch_size 256import torchfrom IPython import displayfrom d2l import torch as d2l
12batch_size = 256train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=None)#用到了我们3.5节定义的函数
1234567891011121314#we know each image is 28x28, so we can regard them as vectors with length 784.#we recall y_j is the probability of class j with positive number and normalized by 1.#Because we ...
线性表
定义线性表注意
123对数组进行封装,在存储空间起始位置即为数组data,设置最大存储容量,一般不会变当前长度length,会变化
代码如下
12345typedef struct{ int data[MAXSIZE]; int length;}SqList;
变化的集合我们用*L,不变的集合我们参数才用L。下面是A = A并B的操作,所以我们知道A是要改变的,传入地址。
123456789101112131415void unionL(List *La, list Lb){ int La_len, Lb_len, i; ElemType e; La_len = ListLength(*La); Lb_len = ListLength(Lb); for (i = 1; i <= Lb_len; i++)//也就是判断b中每一个元素是否在La中 { GetElem(Lb, i, &e); if(!LocateElem(*La, e)) { ListInsert(La, ++La_len, e); ...
3.4 softmax regression and 3.5 The image classification dataset
3.4 softmax regression12345#硬性类别:属于某一类别#软性类别:属于某一类别的概率#我们用one-hot encoding来分类,one-hot encoding是一个向量,维数等于类别数#比如我们有猫狗鸡三类,向量为(1,0,0)或(0,1,0)或(0,0,1),当一个动物被分类为鸡时,可以看成(0,0,1)其他两个以此类推#所以y属于{(1,0,0),{0,1,0},{0,0,1}}
3.4.2 network architecture1#下面是公式推导部分,详见现有网址,后续这里补充推导。
3.5 image classification dataset1#MNIST数据集是图像分类中最广泛的数据集之一,我们使用更复杂的Fashion-MNIST数据集
1234567%matplotlib inlineimport torchimport torchvisionfrom torch.utils import datafrom torchvision import transformsfrom d ...
K-means算法python实现
1.我们后期补充数学推导我们算法的原理如下:
1首先对于一批样本点,我们并没有头绪,首先随机找到几个已知点作为初始质心,然后分别对样本所有点,计算其到质心的距离,为每个样本点选择最近的质心,这样就形成了一个初始的归类了,然后我们对于每个类,重新计算质心,得到新的质心后,对所有样本,再执行到质心距离的计算和判别,重新分类,多次迭代实现分类。
我们来实现一下伪代码:
12345678910111213141516171819init_center[] = select_random_smaple_point(k)#所以为k-means算法for i in range(dataset): for j in range(init_center): sample_to_center_distances[i][j] = calculate_distance(dataset(i), init_center(j)) sample_belongs_to_center[i] = min(sample_to_center_distances[i])#取距离最小的那个int class[][]k=0#将每个 ...