Cookbook for Practice 6.1
Cong Li 李聪
实践6.1攻略
Suppress Output 减少输出
We will run a large number of training & testing passes. Therefore we need to suppress the output in ‘tester.py’.
我们会运行很多轮训练和评估。因此我们需要减少“tester.py”中的输出。
We will also return the accuracy after each round of training & testing for upper level processing
每一轮训练和评估结束之后,我们需要将准确率返回至上层进行处理
A New Start 重新开始
Start a new file ‘test.py’ & import the relevant components
开始一个新的“test.py”文件,并导入相关组件
Then define some constants: the maximum dimension allowed, relevant attribute number, total rounds, the test size in each round 然后定义常数:最大允许维度、相关属性数、总轮数、每轮测试数据量
Generate Hyperplane �生成超平面 (1)
Write a new function ‘generate_parameters’, starting from randomly determining the attribute importance from a normal distribution 写一个新的“generate_parameters”函数,从一个正态分布中确定属性的重要性
概率密度
Generate Hyperplane �生成超平面 (2)
Sort the attributes w.r.t. the importance, & then randomly determine the sign 根据重要性对属性排序,并随机确定其符号
Generate a random bias 随机生成偏向
Return the weights & the bias 返回权重和偏向
Generate Data 生成数据 (1)
Write a new function ‘generate_data’ to take inputs of the data dimension & the hyperplane, initializing the attributes array & the summation to get the ground truth class label
写一个“generate_data”的新函数,接受数据维度和超平面的输入,并初始化属性数组和总和变量以获取真实类别标记
Generate Data 生成数据 (2)
Randomly generate the values till the maximum dimension, but only use them as the attributes based on the dimension specified, & also do the calculation based on the hyperplane
依据最大维度生成随机数,但仅依据指定维度用作属性,同时根据超平面进行计算
Only use the values as the attributes based on the dimension specified�仅依据指定维度用作属性
Generate Data 生成数据 (2)
Randomly generate the values till the maximum dimension, but only use them as the attributes based on the dimension specified, & also do the calculation based on the hyperplane
依据最大维度生成随机数,但仅依据指定维度用作属性,同时根据超平面进行计算
Do the calculation based on the hyperplane�根据超平面进行计算
Generate Data 生成数据 (3)
Determine the ground truth class label 确定真实类别标记
Finally return the attributes & the ground truth class label 最后,返回属性和真实类别标记
Main Section 主程序 (1)
Now write the main section, starting w/ parsing the argument for training data size
现在写主程序,从命令行参数解析、获取训练数据量开始
Loop the dimension over 3 to 16, & use a variable to track the average accuracy �从3到16作维度循环,用一个变量来记录平均准确率
Main Section 主程序 (2)
Try numerous rounds. In each round, start w/ a different random seed & generate the hyperplane
尝试很多轮,每一轮都用一个不同的随机种子,并生成超平面
Generate the test data first 先生成测试数据
Main Section 主程序 (3)
Next, generate training data 然后生成训练数据
Now use the perceptron for learning & classification
现在,用感知器进行学习和分类
Main Section 主程序 (4)
Finally, after the rounds for a dimension number, print the average accuracy
最后,当一个维度的所有轮次都结束之后,输出平均准确率
Run it w/ the training size of 20, 50, & 100
尝试不同的训练数据量(20、50和100)运行该程序
The End