class TestMNIST: def __init__(self): # 该模型运行在CUP上,CUP的数量为2 paddle.init(use_gpu=False, trainer_count=2)
获取训练器`def get_trainer(self):
# 获取分类器
out = convolutional_neural_network()
# 定义标签
label = paddle.layer.data(name="label",
type=paddle.data_type.integer_value(10))
# 获取损失函数
cost = paddle.layer.classification_cost(input=out, label=label)
# 获取参数
parameters = paddle.parameters.create(layers=cost)
"""
定义优化方法
learning_rate 迭代的速度
momentum 跟前面动量优化的比例
regularzation 正则化,防止过拟合
:leng re
"""
optimizer = paddle.optimizer.Momentum(learning_rate=0.1 / 128.0,
momentum=0.9,
regularization=paddle.optimizer.L2Regularization(rate=0.0005 * 128))
'''
创建训练器
cost 损失函数
parameters 训练参数,可以通过创建,也可以使用之前训练好的参数
update_equation 优化方法
'''
trainer = paddle.trainer.SGD(cost=cost,
parameters=parameters,
update_equation=optimizer)
return trainer
读取+预测
def to_prediction(self, out, parameters, test_data):
# 开始预测
probs = paddle.infer(output_layer=out,
parameters=parameters,
input=test_data)
# 处理预测结果并打印
lab = np.argsort(-probs)
print "预测结果为: %d" % lab[0][0]
```python
if __name__ == "__main__":
testMNIST = TestMNIST()
out = convolutional_neural_network()
parameters = testMNIST.get_parameters()
test_data = testMNIST.get_TestData()
# 开始预测
testMNIST.to_prediction(out=out, parameters=parameters, test_data=test_data)
物联191邢成彬1908070127



