经过7天的充实的学习,有两点比较深的感触
1.自认为自己在CV 领域技术还行,然而 现实很残酷,发现自己差了好几条街
2.飞桨这个框架非常好用 是国产框架的骄傲
刚开始车牌号识别和手体识别相对来说难度不大,但是便于我们入门,对神经网络有了更深的认识,自己有手动推到了一遍,
对了 值得一提的是百度这次不能有课程讲解,最重要的是还免费提供了GPU的算力卡,给计算带来了很大的帮助,
记得之前自己参加过腾讯的一个算法比赛,就是死在计算的GPU 上,所以老铁们 硬件还是很重要的哦
最后说几点
A.特别所人工智能(新技术),一定要多参加社区,比赛等等
B.有事没事 看看官网
C.有些算法光调用是不够的,必须推倒公式 深入理解
以下就是paddlepaddle的第一讲,利用房价讲解线性回归。
模型训练:
复制代码
#-*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import os
import paddle.v2 as paddle
import paddle.v2.dataset.uci_housing as uci_housing
with_gpu = os.getenv('WITH_GPU', '0') != '0'
def main():
# 初始化PaddlePaddle
paddle.init(use_gpu=with_gpu, trainer_count=1)
# 模型配置
x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13))#利用前13因数来预测房价
y_predict = paddle.layer.fc(input=x, size=1, act=paddle.activation.Linear())#预测的房价值,线性激活函数
y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1))#实际的房价值
cost = paddle.layer.square_error_cost(input=y_predict, label=y)#损失函数
# 保存网络拓扑
inference_topology = paddle.topology.Topology(layers=y_predict)
with open("inference_topology.pkl", 'wb') as f:
inference_topology.serialize_for_inference(f)
# 创建参数
parameters = paddle.parameters.create(cost)
# 创建trainer
optimizer = paddle.optimizer.Momentum(momentum=0)#learning_rate=0.0001 学习率
trainer = paddle.trainer.SGD(
cost=cost, parameters=parameters, update_equation=optimizer)#随机梯度下降算法
feeding = {'x': 0, 'y': 1}
# 读取数据且打印训练的中间信息
def event_handler(event):
if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 100 == 0:
print "Pass %d, Batch %d, Cost %f" % (
event.pass_id, event.batch_id, event.cost)
if isinstance(event, paddle.event.EndPass):
if event.pass_id % 10 == 0:
with open('params_pass_%d.tar' % event.pass_id, 'w') as f:
trainer.save_parameter_to_tar(f)
result = trainer.test(
reader=paddle.batch(uci_housing.test(), batch_size=2),
feeding=feeding)#读取房价数据,将数据打乱,每次取出2条
print "Test %d, Cost %f" % (event.pass_id, result.cost)
# 开始训练
trainer.train(
reader=paddle.batch(
paddle.reader.shuffle(uci_housing.train(), buf_size=500),
batch_size=2),
feeding=feeding,
event_handler=event_handler,#提供一个 event_handler,来打印训练的进度:
num_passes=30)
# 生成测试数据
test_data_creator = paddle.dataset.uci_housing.test()
test_data = []
test_label = []
#取出测试集中5条数据用于最后的预测
for item in test_data_creator():
test_data.append((item[0], ))
test_label.append(item[1])
if len(test_data) == 5:
break
#推测inference
probs = paddle.infer(
output_layer=y_predict, parameters=parameters, input=test_data)
for i in xrange(len(probs)):
print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0])
if __name__ == '__main__':
main()
复制代码
运行结果:
复制代码
Pass 0, Batch 0, Cost 886.077026
Pass 0, Batch 100, Cost 236.768433
Pass 0, Batch 200, Cost 555.669922
Test 0, Cost 56.372781
Pass 1, Batch 0, Cost 558.157104
Pass 1, Batch 100, Cost 17.486526
Pass 1, Batch 200, Cost 49.110359
Test 1, Cost 22.666769
Pass 2, Batch 0, Cost 2.017142
Pass 2, Batch 100, Cost 5.376208
Pass 2, Batch 200, Cost 1.576212
Test 2, Cost 18.296844
Pass 3, Batch 0, Cost 103.864586
Pass 3, Batch 100, Cost 84.158134
Pass 3, Batch 200, Cost 5.564497
Test 3, Cost 17.668033
Pass 4, Batch 0, Cost 2.316584
Pass 4, Batch 100, Cost 9.555552
Pass 4, Batch 200, Cost 74.418373
Test 4, Cost 17.311696
Pass 5, Batch 0, Cost 9.540855
Pass 5, Batch 100, Cost 22.676167
Pass 5, Batch 200, Cost 123.998085
Test 5, Cost 16.799527
Pass 6, Batch 0, Cost 56.558044
Pass 6, Batch 100, Cost 33.035114
Pass 6, Batch 200, Cost 58.189980
Test 6, Cost 16.333503
Pass 7, Batch 0, Cost 7.590010
Pass 7, Batch 100, Cost 34.771137
Pass 7, Batch 200, Cost 44.883244
Test 7, Cost 16.017060
Pass 8, Batch 0, Cost 42.311310
Pass 8, Batch 100, Cost 24.567163
Pass 8, Batch 200, Cost 33.340485
Test 8, Cost 15.520346
Pass 9, Batch 0, Cost 178.452744
Pass 9, Batch 100, Cost 10.791793
Pass 9, Batch 200, Cost 0.137641
Test 9, Cost 15.214742
Pass 10, Batch 0, Cost 10.072014
Pass 10, Batch 100, Cost 11.594021
Pass 10, Batch 200, Cost 24.404564
Test 10, Cost 14.916112
Pass 11, Batch 0, Cost 5.649694
Pass 11, Batch 100, Cost 31.902603
Pass 11, Batch 200, Cost 11.218608
Test 11, Cost 14.600422
Pass 12, Batch 0, Cost 87.761772
Pass 12, Batch 100, Cost 53.684475
Pass 12, Batch 200, Cost 37.861378
Test 12, Cost 14.326864
Pass 13, Batch 0, Cost 5.141076
Pass 13, Batch 100, Cost 0.324465
Pass 13, Batch 200, Cost 2.333709
Test 13, Cost 14.124264
Pass 14, Batch 0, Cost 9.482045
Pass 14, Batch 100, Cost 22.704296
Pass 14, Batch 200, Cost 12.826228
Test 14, Cost 13.945640
Pass 15, Batch 0, Cost 41.819580
Pass 15, Batch 100, Cost 10.353182
Pass 15, Batch 200, Cost 13.374403
Test 15, Cost 13.767083
Pass 16, Batch 0, Cost 83.044785
Pass 16, Batch 100, Cost 27.363625
Pass 16, Batch 200, Cost 5.347357
Test 16, Cost 13.665516
Pass 17, Batch 0, Cost 0.994224
Pass 17, Batch 100, Cost 0.298174
Pass 17, Batch 200, Cost 140.061615
Test 17, Cost 13.568394
Pass 18, Batch 0, Cost 11.832894
Pass 18, Batch 100, Cost 8.340067
Pass 18, Batch 200, Cost 30.967430
Test 18, Cost 13.465723
Pass 19, Batch 0, Cost 15.379287
Pass 19, Batch 100, Cost 123.313614
Pass 19, Batch 200, Cost 36.328705
Test 19, Cost 13.377999
Pass 20, Batch 0, Cost 12.842525
Pass 20, Batch 100, Cost 54.218903
Pass 20, Batch 200, Cost 18.377592
Test 20, Cost 13.266518
Pass 21, Batch 0, Cost 49.386784
Pass 21, Batch 100, Cost 215.253906
Pass 21, Batch 200, Cost 0.260682
Test 21, Cost 13.237288
Pass 22, Batch 0, Cost 469.974213
Pass 22, Batch 100, Cost 8.073731
Pass 22, Batch 200, Cost 0.810365
Test 22, Cost 13.192008
Pass 23, Batch 0, Cost 145.341141
Pass 23, Batch 100, Cost 15.787022
Pass 23, Batch 200, Cost 4.965213
Test 23, Cost 13.133022
Pass 24, Batch 0, Cost 10.377566
Pass 24, Batch 100, Cost 3.863908
Pass 24, Batch 200, Cost 15.857657
Test 24, Cost 13.113067
Pass 25, Batch 0, Cost 6.239013
Pass 25, Batch 100, Cost 15.914387
Pass 25, Batch 200, Cost 48.752701
Test 25, Cost 13.137239
Pass 26, Batch 0, Cost 57.843086
Pass 26, Batch 100, Cost 0.732344
Pass 26, Batch 200, Cost 48.501846
Test 26, Cost 13.141359
Pass 27, Batch 0, Cost 443.271545
Pass 27, Batch 100, Cost 227.696655
Pass 27, Batch 200, Cost 1.482114
Test 27, Cost 13.094058
Pass 28, Batch 0, Cost 11.784382
Pass 28, Batch 100, Cost 1.334578
Pass 28, Batch 200, Cost 16.487831
Test 28, Cost 13.122105
Pass 29, Batch 0, Cost 10.043719
Pass 29, Batch 100, Cost 26.890572
Pass 29, Batch 200, Cost 11.034937
Test 29, Cost 13.203439
label=8.5, predict=11.7476
label=5.0, predict=13.6822
label=11.9, predict=10.7325
label=27.9, predict=18.0696
label=17.2, predict=13.0193
不错,