栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

手写数字识别

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

手写数字识别

初始化paddle

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/339596.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号