Cookbook for Practice 10.6
Cong Li 李聪
实践10.6攻略
RBF Kernel 径向基核计算
In ‘kernels.py’, add another class ‘RBFKernel’ w/ the initialization function taking either of the 2 inputs
在“kernels.py”中加入新类型“RBFKernel”,其初始化函数接受两个参数之一
RBF Kernel 径向基核计算
If gamma is 0, use sigma instead
如果gamma为0,就用sigma
In ‘kernels.py’, add another class ‘RBFKernel’ w/ the initialization function taking either of the 2 inputs
在“kernels.py”中加入新类型“RBFKernel”,其初始化函数接受两个参数之一
RBF Kernel 径向基核计算
Then the kernel calculation
然后是核计算
In ‘kernels.py’, add another class ‘RBFKernel’ w/ the initialization function taking either of the 2 inputs
在“kernels.py”中加入新类型“RBFKernel”,其初始化函数接受两个参数之一
Gram Matrix 格拉姆矩阵 (1)
Gram Matrix 格拉姆矩阵 (2)
Add a class ‘GramMatrix’ w/ the initialization function taking the input data & the kernel
加入新类型“GramMatrix”,其初始化函数接受训练数据和核计算作为输入
Gram Matrix 格拉姆矩阵 (3)
We then calculate the matrix
然后我们计算这个矩阵
Gram Matrix 格拉姆矩阵 (4)
We only need to provide a row in the matrix
我们只需要提供矩阵中的一行
Gram Matrix 格拉姆矩阵 (5)
We also know the maximum norm from the matrix
我们也从矩阵中知道了最大长度
Accelerate Kernel Perceptron�加速核计算感知器 (1)
In class ‘BinaryKernelPerceptron’, add a new member function to calculate the score from a row of calculated kernel results
在“perceptron.py”的“BinaryKernelPerceptron”类型中,加入一个新的成员函数,用已经计算好的一行核计算结果来计算分数
In ‘perceptron.py’, import ‘kernels’
在“perceptron.py”中导入“kernels”
Accelerate Kernel Perceptron�加速核计算感知器 (2)
Now use the matrix to get the maximum norm & calculate the confidence threshold (removing the old calculation)
现在从这个矩阵中获得最大长度并计算置信度阈值(删除原先的计算)
In member function ‘train’, accept an optional parameter of the Gram matrix. If the matrix is not provided, then calculate the matrix
在成员函数“train”中接受一个可选的格拉姆矩阵参数,如果矩阵未被传入,则计算之
Accelerate Kernel Perceptron�加速核计算感知器 (3)
In the member function of scoring for classification, also accept an optional parameter of a kernel result row (which can be calculated only once in multi-class classification) for fast calculation. If it is not available, follow the original code path
在分类时计算分数的成员函数中,也接受可选的一行核计算结果(在多类分类时只需要计算一次)用于快速计算。如果未被传入,则遵循原先的流程
Use the matrix to do scoring in training
在训练时用矩阵计算分数
Multi-class Perceptron�多类感知器 (1)
If a kernel is used, calculate the Gram matrix & store the training data in a member variable
如果用了核计算,则计算格拉姆矩阵并用成员函数存储训练数据
In the initialization function of class ‘Perceptron’, accept an optional parameter of kernel
在“Perceptron”类的初始化函数中,接受一个可选的核计算参数
Multi-class Perceptron�多类感知器 (2)
When creating binary classifier instances, choose the right class depending on whether a kernel is used
当创建两类分类器实例时,根据是否用了核计算选择正确的类型
Multi-class Perceptron�多类感知器 (3)
When training the binary classifier instances, pass in the matrix depending on whether a kernel is used
当训练两类分类器实例时,根据是否用了核计算决定是否传入矩阵
Multi-class Perceptron�多类感知器 (4)
For classification, write another member function to calculate the row of kernel results
在分类时,另外写一个成员函数计算一行核计算的结果
Multi-class Perceptron�多类感知器 (5)
In member function ‘classify’, calculate the kernel result row only once if a kernel is used
在“classify”成员函数中,如果用了核计算,一次性计算核计算结果行
In scoring, pass in the kernel result row
在计算得分时,传入核计算结果行
Run It 运行之 (1)
Create a new file ‘test.py’ & import the required components 创建新文件“test.py”并导入所需组件
Read the data from the files
从文件中读取数据
Run It 运行之 (2)
Create an instance of the RBF kernel, derive a perceptron classifier from it, & evaluate its accuracy
生成一个径向基核计算的实例,导出一个感知器分类器,并评价其准确率
Add a pause before going forward
继续运行前加入一个等待
Run It 运行之 (3)
Run it 运行之
Create an instance of the polynomial kernel, derive a perceptron classifier from it, & evaluate its accuracy
生成一个多项式核计算的实例,导出一个感知器分类器,并评价其准确率
The End