[3]深度学习和Keras----Keras深度学习框架入门例子

只要是程序员都知道,学习一门新的语言或者框架的时候,第一个自己敲入且运行的程序,都一个HelloWorld的程序、笔者也不例外,当笔者把Keras在Unbuntu系统安装好之后,早已掩盖不住激动的心,然后想在Keras上面运行一个HelloWorld的例子。后面笔者参考了莫愁同学的Keras的学习视频,上面提到了一个用Keras的API去反推出一个线性函数的例子,感觉非常的有趣,所有就模拟敲入了一把,现在让我给大家来逐一解释一下,解释不到位的地方,还请海涵,毕竟也是楼主第一次接触Keras框架。所有的代码全部黏贴出来,文件名为helloworld.py,如下,具体解释请看代码中的注释。

# Import package
import numpy as np
np.random.seed(1337)
from keras.models import Sequential
from keras.layers import Dense
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

# 1.Build the trainning data
X=np.linspace(-1,1,200)
np.random.shuffle(X)
np.random.normal()
Y=0.5*X+2+np.random.normal(0,0.05,(200,))
plt.scatter(X,Y)
plt.show()
X_train,Y_train=X[:160],Y[:160]
X_test,Y_test=X[160:],Y[160:]
print(X)
print("*******************************************")
print(Y)

# 2.Build a neural network from the 1st layer to the last layer
model=Sequential()
model.add(Dense(output_dim=1,input_dim=1))

#3. Choose loss function and optimzing method
model.compile(loss='mse',optimizer='sgd')

#4. Trainning
print("Training......")
for step in range(1400):
        cost=model.train_on_batch(X_train,Y_train)
        if step % 100 ==0:
                print('train cost',cost)
#5.Test
print("\n Testing...........")
cost=model.evaluate(X_test,Y_test,batch_size=40)
print("Test cost:",cost)
W,b=model.layers[0].get_weights()
print('Weight=',W,"\nbiases=",b)

#6.Plotting the prediction
Y_pred=model.predict(X_test)
plt.scatter(X_test,Y_test)
plt.plot(X_test,Y_pred)
plt.show()


运行后的结果如下:

ubuntu@keras:~/keraslearn$ python3 helloworld.py
Using TensorFlow backend.
[-0.70854271  0.1758794  -0.30653266  0.74874372 -0.02512563  0.33668342
 -0.85929648  0.01507538 -0.13567839  0.72864322  0.24623116 -0.74874372
 -0.78894472  0.50753769  0.03517588  0.35678392 -0.55778894  0.2361809
 -0.25628141 -0.44723618  0.2160804  -0.43718593 -0.64824121  0.69849246
 -0.03517588 -0.45728643  0.86934673  0.73869347  0.53768844 -0.67839196
 -0.75879397  0.55778894  0.28643216 -0.05527638 -0.86934673  0.1959799
 -0.57788945 -0.9798995  -0.6080402  -0.63819095  0.84924623  0.41708543
  0.13567839  0.79899497 -0.47738693  0.46733668  0.59798995 -0.80904523
 -0.98994975 -0.36683417 -0.5678392  -0.00502513 -0.53768844 -0.37688442
 -0.65829146 -0.1959799   0.06532663  0.44723618 -0.01507538 -0.6281407
  0.02512563 -0.71859296 -0.14572864 -0.46733668  0.07537688  0.85929648
  0.76884422  0.40703518 -0.68844221  0.68844221 -0.29648241  0.66834171
 -0.95979899 -0.33668342  0.26633166 -0.82914573  1.         -0.5879397
 -0.69849246 -0.20603015  0.63819095 -0.88944724 -0.40703518 -0.32663317
  0.15577889 -0.41708543  0.10552764  0.20603015 -0.04522613  0.00502513
 -0.31658291  0.43718593  0.42713568  0.45728643 -0.59798995 -0.66834171
  0.83919598  0.75879397 -0.24623116  0.71859296 -0.92964824  0.39698492
  0.61809045 -0.84924623 -0.87939698 -0.96984925  0.87939698  0.6281407
  0.25628141  0.27638191  0.12562814  0.09547739 -0.89949749  0.80904523
 -0.16582915 -0.12562814  0.30653266  0.49748744  0.5879397  -0.51758794
 -0.10552764  0.54773869 -0.94974874  0.92964824  0.16582915 -0.83919598
 -0.35678392 -0.48743719  0.08542714 -0.61809045  0.18592965  0.57788945
  0.65829146  0.38693467  0.91959799 -0.26633166 -0.50753769 -1.
 -0.54773869  0.6080402  -0.49748744 -0.22613065  0.9798995   0.98994975
  0.5678392   0.32663317  0.64824121 -0.52763819  0.36683417  0.81909548
 -0.11557789  0.31658291 -0.2160804   0.95979899  0.77889447 -0.73869347
 -0.81909548 -0.79899497  0.78894472  0.88944724 -0.2361809   0.37688442
  0.70854271  0.22613065 -0.28643216 -0.38693467  0.90954774 -0.91959799
  0.48743719 -0.42713568 -0.08542714  0.11557789 -0.18592965  0.47738693
 -0.39698492 -0.34673367  0.04522613  0.05527638  0.93969849 -0.77889447
 -0.93969849 -0.06532663 -0.72864322  0.29648241  0.52763819 -0.76884422
  0.94974874  0.82914573  0.34673367 -0.90954774 -0.27638191 -0.15577889
 -0.1758794   0.14572864 -0.09547739  0.96984925  0.67839196 -0.07537688
  0.89949749  0.51758794]
*******************************************
[ 1.67999883  2.15732476  1.86215827  2.35166736  1.93572236  2.18016038
  1.61608065  2.03423062  1.99563479  2.37665292  2.18481192  1.68394294
  1.62204409  2.26767077  1.98888272  2.22171139  1.71104094  2.17892658
  1.91484027  1.82024419  2.16604711  1.81951961  1.54483872  2.34601517
  2.00887174  1.77313928  2.37743864  2.31814604  2.32716849  1.68305339
  1.60808704  2.24648868  2.26423579  1.96845047  1.66389213  2.1151824
  1.76544914  1.5408011   1.68818555  1.65445629  2.34232127  2.21861488
  2.07349814  2.44520484  1.68813908  2.23841182  2.28008276  1.61527365
  1.45239671  1.86837714  1.73063808  2.01510789  1.78710972  1.81579434
  1.7282564   1.88204563  2.12479282  2.27787801  2.04415445  1.64586279
  2.02443768  1.69792605  1.90274178  1.78137239  2.02497528  2.42132526
  2.42777448  2.16564271  1.68763444  2.31923599  1.81271447  2.34163208
  1.48248635  1.78163246  2.0686663   1.56576829  2.57615655  1.65904659
  1.57722512  1.81629077  2.26556137  1.61007302  1.88352331  1.71313059
  2.10104146  1.7038275   1.97249884  2.10127479  2.07381404  2.11472407
  1.83211308  2.26576731  2.23235442  2.28700014  1.72663184  1.71425067
  2.44958621  2.48620331  1.89267965  2.35438093  1.57974268  2.22254286
  2.23280715  1.50250543  1.58598131  1.40667751  2.50025819  2.37980426
  2.19965578  2.12256837  2.05612666  1.98159549  1.49287023  2.43805289
  1.87463195  2.04988238  2.12882858  2.2546237   2.34223092  1.65392499
  1.99025412  2.28291716  1.45807616  2.51044748  1.97988068  1.66059626
  1.7961923   1.75129171  2.0241939   1.69914728  2.07346622  2.33384759
  2.35610171  2.22847144  2.39450148  1.84129314  1.69452659  1.55065089
  1.66503047  2.28833885  1.65681898  1.9227888   2.46308462  2.55316759
  2.3420487   2.1586477   2.31608103  1.72595194  2.20947867  2.43358179
  1.92711345  2.09783848  1.90011119  2.3742013   2.39525115  1.61921788
  1.60036827  1.61487721  2.32314506  2.47010224  1.78412147  2.20253779
  2.29520865  2.13392445  1.88572837  1.80240843  2.37782058  1.53947432
  2.21682792  1.80241496  1.9832941   2.19691473  1.98446141  2.25189264
  1.79373247  1.91751681  2.04975192  1.95401037  2.47210723  1.64954777
  1.53229024  1.89811048  1.56746323  2.10295781  2.18903255  1.5645517
  2.47228584  2.29823263  2.17956294  1.51738366  1.86618515  1.92440005
  1.77639738  2.04854509  1.89432423  2.44443885  2.40954465  1.93669521
  2.4738211   2.26099266]
helloworld.py:25: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(units=1, input_dim=1)`
  model.add(Dense(output_dim=1,input_dim=1))
Training......
2017-05-30 13:52:34.569118: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-30 13:52:34.569158: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-30 13:52:34.569165: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-05-30 13:52:34.569169: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-30 13:52:34.569176: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
train cost 4.02812
train cost 0.0807406
train cost 0.00588549
train cost 0.00316872
train cost 0.00276465
train cost 0.00266697
train cost 0.00264223
train cost 0.00263594
train cost 0.00263434
train cost 0.00263394
train cost 0.00263383
train cost 0.00263381
train cost 0.0026338
train cost 0.0026338

 Testing...........
40/40 [==============================] - 0s
Test cost: 0.00337669742294
Weight= [[ 0.5063296]]
biases= [ 2.00427961]

@ 导入相关的Python和Keras的模块(module)

import numpy as np
np.random.seed(1337)
from keras.models import Sequential
from keras.layers import Dense
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
需要特别注意的是,如果在Ubuntu下,如果我们用的远程命令行方式,因为在远程命令行的环境下显示不了图形界面,所以需要加入下面的两行代码,否则会运行报错。

import matplotlib
matplotlib.use('Agg')

但是在Ubuntu的图形化界面下(比如,远程桌面)不需要。

@ 随机生成200个数字并模拟一个线性函数

随机生成200个范围在-1到1之间一个浮点数。并模拟一个线性函数的公式,0.5*X+2 并加上一些随机的干扰,生成200个函数结果Y

然后从中抽选出160组数据作为训练数据,40组作为测试训练的结果的数据。

# 1.Build the trainning data
X=np.linspace(-1,1,200)
np.random.shuffle(X)
np.random.normal()
Y=0.5*X+2+np.random.normal(0,0.05,(200,))
plt.scatter(X,Y)
plt.show()
X_train,Y_train=X[:160],Y[:160]
X_test,Y_test=X[160:],Y[160:]
print(X)
print("*******************************************")
print(Y)
把下面的两行注释掉,并在Ubuntu的图形界面模式下,显示出来的图形效果如下:

import matplotlib
matplotlib.use('Agg')

@ 用Keras的API建立一个神经网络模型

这个模型是总共有只要一层,1个输入和一个输出,建立好神经网络后,选择损失函数和优化器。从下面的代码来看,其实还是非常的简洁的,如果用TensorFlow或者Theano实现的话,代码会多不少,但是用Keras实现非常的简洁和简单。

# 2.Build a neural network from the 1st layer to the last layer
model=Sequential()
model.add(Dense(output_dim=1,input_dim=1))

#3. Choose loss function and optimzing method
model.compile(loss='mse',optimizer='sgd')
损失函数只要有下面的几种:

  • mean_squared_error
  • mean_absolute_error
  • mean_absolute_percentage_error
  • mean_squared_logarithmic_error
  • squared_hinge
  • hinge
  • logcosh
  • categorical_crossentropy
  • sparse_categorical_crossentropy
  • binary_crossentropy
  • kullback_leibler_divergence
  • poisson
  • cosine_proximity

具体的意思,请参考:

https://keras.io/losses/

https://github.com/fchollet/keras/blob/master/keras/losses.py

我们当前选择的是:sgd,其支持,随机梯度下降法,支持动量参数,支持学习衰减率,支持Nesterov动量。

此外还有优化器:

  • RMSprop
  • Adagrad
  • Adadelta
  • Adam
  • Adamax
  • Nadam
  • TFOptimizer

不同的优化器有有不同的适用场景,限于篇幅,请读者自己补脑和查找相关资料。


@ 分批次训练

批次的次数不是越多越好,在当前的例子中,批次的训练次数达到1300次左右基本上已经达到损失函数能够达到的最好的结果了,在增加次数也增加了不了精度。具体请见
日志的输出。

#4. Trainning
print("Training......")
for step in range(1400):
        cost=model.train_on_batch(X_train,Y_train)
        if step % 100 ==0:
                print('train cost',cost)


@ 测试数据测试训练结果。

在上面的部分,经过1400次的分批次训练,神经网络已经完全模拟出了上面的线性函数的模型。这个时候代入剩下的40组测试数据进行测试。
我们会发现0.5*X+2 这个线性函数完全被建立起来了。从输出的weight和biases的值其实就是上面的0.5和2; weight和0.5越接近,说明效果越好;
biases和2越接近说明效果越好。
#5.Test
print("\n Testing...........")
cost=model.evaluate(X_test,Y_test,batch_size=40)
print("Test cost:",cost)
W,b=model.layers[0].get_weights()
print('Weight=',W,"\nbiases=",b)

@ Keras模型结果 VS 原始测试数据结果

把通过神经网络得出的结果与原始的测试结果得出的结果进行比较,并显示其对比图像。
#6.Plotting the prediction
Y_pred=model.predict(X_test)
plt.scatter(X_test,Y_test)
plt.plot(X_test,Y_pred)
plt.show()


addition_rnn.py 执行序列学习以执行两个数字(作为字符串)的添加。 antirectifier.py 演示如何为Keras编写自定义图层。 babi_memnn.py 在bAbI数据集上训练一个内存网络以进行阅读理解。 babi_rnn.py 在bAbI数据集上训练一个双支循环网络,以便阅读理解。 cifar10_cnn.py 在CIFAR10小图像数据集上训练一个简单的深CNN。 conv_filter_visualization.py 通过输入空间中的渐变上升可视化VGG16的过滤器。 conv_lstm.py 演示使用卷积LSTM网络。 deep_dream.py 深深的梦想在克拉斯。 image_ocr.py 训练一个卷积堆叠,后跟一个循环堆栈和一个CTC logloss函数来执行光学字符识别(OCR)。 imdb_bidirectional_lstm.py 在IMDB情绪分类任务上训练双向LSTM。 imdb_cnn.py 演示使用Convolution1D进行文本分类。 imdb_cnn_lstm.py 在IMDB情绪分类任务上训练一个卷积堆栈,后跟一个循环堆栈网络。 imdb_fasttext.py 在IMDB情绪分类任务上训练一个FastText模型。 imdb_lstm.py 在IMDB情绪分类任务上训练一个LSTM。 lstm_benchmark.py 比较IMDB情绪分类任务上不同的LSTM实现。 lstm_text_generation.py 生成尼采文字的文字。 mnist_acgan.py 在MNIST数据集上实现AC-GAN(辅助分类器GAN) mnist_cnn.py 在MNIST数据集上训练一个简单的convnet。 mnist_hierarchical_rnn.py 训练一个分级RNN(HRNN)来分类MNIST数字。 mnist_irnn.py Le等人在“以简单的方式初始化整流线性单元的反复网络”中再现具有逐像素连续MNIST的IRNN实验。 mnist_mlp.py 在MNIST数据集上训练一个简单的深层多层感知器。 mnist_net2net.py 在“Net2Net:通过知识转移加速学习”中再现带有MNIST的Net2Net实验。 mnist_siamese_graph.py 从MNIST数据集中的一对数字上训练暹罗多层感知器。 mnist_sklearn_wrapper.py 演示如何使用sklearn包装器。 mnist_swwae.py 列出了一个堆栈,其中AutoEncoder在MNIST数据集上的剩余块上构建。 mnist_transfer_cnn.py 转移学习玩具的例子。 neural_doodle.py 神经涂鸦。 neural_style_transfer.py 神经样式转移。 pretrained_word_embeddings.py 将预训练的词嵌入(GloVe embeddings)加载到冻结的Keras嵌入层中,并使用它在20个新闻组数据集上训练文本分类模型。 reuters_mlp.py 在路透社newswire主题分类任务上训练并评估一个简单的MLP。 stateful_lstm.py 演示如何使用有状态的RNN有效地建模长序列。 variational_autoencoder.py 演示如何构建变体自动编码器。 variational_autoencoder_deconv.py 演示如何使用反褶积层使用Keras构建变体自动编码器。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值