Keras Feedforward Tutorial-经管之家官网!

人大经济论坛-经管之家 收藏本站
您当前的位置> 会计>>

会计库

>>

Keras Feedforward Tutorial

Keras Feedforward Tutorial

发布:Nicolle | 分类:会计库

关于本站

人大经济论坛-经管之家:分享大学、考研、论文、会计、留学、数据、经济学、金融学、管理学、统计学、博弈论、统计年鉴、行业分析包括等相关资源。
经管之家是国内活跃的在线教育咨询平台!

经管之家新媒体交易平台

提供"微信号、微博、抖音、快手、头条、小红书、百家号、企鹅号、UC号、一点资讯"等虚拟账号交易,真正实现买卖双方的共赢。【请点击这里访问】

提供微信号、微博、抖音、快手、头条、小红书、百家号、企鹅号、UC号、一点资讯等虚拟账号交易,真正实现买卖双方的共赢。【请点击这里访问】

KerasFeedforwardTutorialThissectionwillwalkyouthroughthecodeoffeedforward_keras_mnist.py,whichIsuggestyouhaveopenwhilereading.ThistutorialisbasedonseveralKerasexamplesandfromit'sdocumentation:mnist_ml ...
免费学术公开课,扫码加入



  1. Keras Feedforward Tutorial

  2. This section will walk you through the code of feedforward_keras_mnist.py, which I suggest you have open while reading. This tutorial is based on several Keras examples and from it's documentation :

  3. mnist_mlp.py example
  4. mnist dataset in Keras
  5. Loss history callback
  6. If you are not yet familiar with what mnist is, please spend a couple minutes there. It is basically a set of hadwritten digit images of size 28*28 (= 784) in greyscale (0-255). There are 60,000 training examples and 10,000 testing examples. The training examples could be also split into 50,000 training examples and 10,000 validation examples.

  7. By the way, Keras's documentation is better and better (and it's already good) and the community answers fast to questions or implementation problems.

  8. ####Keras Documentation ####Keras's Github

  9. Recognizing handwritten digits with Keras

  10. Table of Contents

  11. General Organization

  12. Imports

  13. Callbacks

  14. Loading the Data

  15. Creating the Model

  16. Running the Network

  17. Plot

  18. Usage

  19. General orginization

  20. We start with importing everything we'll need (no *****...). Then we define the callback class that will be used to store the loss history. Lastly we define functions to load the data, compile the model, train it and plot the losses.

  21. The overall philosophy is modularity. We use default parameters in the run_network function so that you can feed it with already loaded data (and not re-load it each time you train a network) or a pre-trained network model.

  22. Also, don't forget the Python's reload(package) function, very useful to run updates from your code without quitting (I)python.

  23. Imports

  24. import time
  25. import numpy as np
  26. from matplotlib import pyplot as plt
  27. from keras.utils import np_utils
  28. import keras.callbacks as cb
  29. from keras.models import Sequential
  30. from keras.layers.core import Dense, Dropout, Activation
  31. from keras.optimizers import RMSprop
  32. from keras.datasets import mnist
  33. time, numpy and matplotlib I'll assume you already know.

  34. np_utils is a set of helper functions, we will only use to_categorical which i'll describe later on.
  35. callbacks is quite transparent, it is a customizable class that triggers functions on events.
  36. models is the core of Keras's neural networks implementation. It is the object that represents the network : it will have layers, activations and so on. It is the object that will be 'trained' and 'tested'. Sequetial means we will use a 'layered' model, not a graphical one.
  37. layers are the objects we stack on the model. There are a couple ways of using them, either include the dropout and activation parameters in the Dense layer, or treat them as layers that will apply to the model's last 'real' layer.
  38. optimizers are the optimization algorithms such as the classic Stochastic Gradient Descent. We will use RMSprop (see here G. Hinton's explanatory video and there the slides)
  39. datasets (in our case) will download the mnist dataset if it is not already in ~/.keras/datasets/ and load it into numpy arrays.
  40. Callback

  41. class LossHistory(cb.Callback):
  42. def on_train_begin(self, logs={}):
  43. self.losses = []

  44. def on_batch_end(self, batch, logs={}):
  45. batch_loss = logs.get('loss')
  46. self.losses.append(batch_loss)
  47. The new class LossHistory extends Keras's Callbackclass. It basically relies on two events:

  48. on_train_begin -> the event is clear : when the training begins, the callback initiates a list self.losses that will store the training losses.
  49. on_batch_end -> when a batch is done propagating forward in the network : we get its loss and append it to self.losses.
  50. This callback is pretty straight forward. But you could want to make it more complicated! Remember that callbacks are simply functions : you could do anything else within these. More on callbacks and available events there.

  51. Loading the Data

  52. def load_data():
  53. print 'Loading data...'
  54. (X_train, y_train), (X_test, y_test) = mnist.load_data()

  55. X_train = X_train.astype('float32')
  56. X_test = X_test.astype('float32')

  57. X_train /= 255
  58. X_test /= 255

  59. y_train = np_utils.to_categorical(y_train, 10)
  60. y_test = np_utils.to_categorical(y_test, 10)

  61. X_train = np.reshape(X_train, (60000, 784))
  62. X_test = np.reshape(X_test, (10000, 784))

  63. print 'Data loaded.'
  64. return [X_train, X_test, y_train, y_test]
  65. Keras makes it very easy to load the Mnist data. It is split between train and test data, between examples and targets.

  66. Images in mnist are greyscale so values are int between 0 and 255. We are going to rescale the inputs between 0 and 1 so we first need to change types from int to float32 or we'll get 0 when dividing by 255.

  67. Then we need to change the targets. y_train and y_test have shapes (60000,) and (10000,) with values from 0 to 9. We do not expect our network to output a value from 0 to 9, rather we will have 10 output neurons with softmax activations, attibuting the class to the best firing neuron (argmax of activations). np_utils.to_categorical returns vectors of dimensions (1,10) with 0s and one 1 at the index of the transformed number : [3] -> [0, 0, 0, 1, 0, 0, 0, 0, 0, 0].

  68. Lastly we reshape the examples so that they are shape (60000,784), (10000, 784) and not (60000, 28, 28), (10000, 28, 28).

  69. Creating the model

  70. def init_model():
  71. start_time = time.time()
  72. print 'Compiling Model ... '
  73. model = Sequential()
  74. model.add(Dense(500, input_dim=784))
  75. model.add(Activation('relu'))
  76. model.add(Dropout(0.4))
  77. model.add(Dense(300))
  78. model.add(Activation('relu'))
  79. model.add(Dropout(0.4))
  80. model.add(Dense(10))
  81. model.add(Activation('softmax'))

  82. rms = RMSprop()
  83. model.compile(loss='categorical_crossentropy', optimizer=rms, metrics=['accuracy'])
  84. print 'Model compield in {0} seconds'.format(time.time() - start_time)
  85. return model
  86. Here is the core of what makes your neural network : the model.
  87. We begin with creating an instance of the Sequential model. Then we add a couple hidden layers and an output layer. After that we instanciate the rms optimizer that will update the network's parameters according to the RMSProp algorithm. Lastly we compile the model with the categorical_crossentropy cost / loss / objective function and the optimizer. We also state we want to see the accuracy during fitting and testing.

  88. Let's get into the model's details :

  89. The first hidden layer is has 500 units, rectified linear unit activation function and 40% of dropout. Also, it needs the input dimension : by specifying input_dim = 784 we tell this first layer that the virtual input layer will be of size 784.
  90. The second hidden layer has 300 units, rectified linear unit activation function and 40% of dropout.
  91. The output layer has 10 units (because we have 10 categories / labels in mnist), no dropout (of course...) and a softmax activation function to output a probability. softmax output + categorical_crossentropy is standard for multiclass classification.
  92. This structure 500-300-10 comes from Y. LeCun's website citing G. Hinton's unpublished work
  93. Here I have kept the default initialization of weights and biases but you can find here the list of possible initializations. Also, here are the possible activations.
  94. Remember I mentioned that Keras used Theano? well, you just went through it. Creating the modeland optimizer instances as well as adding layers is all about creating Theano variables and explaining how they depend on each other. Then the compilation time is simply about declaring an undercover Theano function. This is why this step can be a little long. The more complex your model, the longer (captain here).

  95. And yes, that's it about Theano. Told you you did not need much!

  96. Running the network

  97. def run_network(data=None, model=None, epochs=20, batch=256):
  98. try:
  99. start_time = time.time()
  100. if data is None:
  101. X_train, X_test, y_train, y_test = load_data()
  102. else:
  103. X_train, X_test, y_train, y_test = data

  104. if model is None:
  105. model = init_model()

  106. history = LossHistory()

  107. print 'Training model...'
  108. model.fit(X_train, y_train, nb_epoch=epochs, batch_size=batch,
  109. callbacks=[history],
  110. validation_data=(X_test, y_test), verbose=2)

  111. print "Training duration : {0}".format(time.time() - start_time)
  112. score = model.evaluate(X_test, y_test, batch_size=16)

  113. print "Network's test score [loss, accuracy]: {0}".format(score)
  114. return model, history.losses
  115. except KeyboardInterrupt:
  116. print ' KeyboardInterrupt'
  117. return model, history.losses
  118. The try/except is there so that you can stop the network's training without losing it.

  119. With Keras, training your network is a piece of cake: all you have to do is call fit on your model and provide the data.

  120. So first we load the data, create the model and start the loss history. All there is to do then is fit the network to the data. Here are fit's arguments:

  121. X_train, y_train are the training data
  122. nb_epoch is perfecty transparent and epochs is defined when calling the run_networkfunction.
  123. batch_sizeidem as nb_epoch. Keras does all the work for you regarding epochs and batch training.
  124. callbacks is a list of callbacks. Here we only provide history but you could provide any number of callbacks.
  125. validation_data is, well, the validation data. Here we use the test data but it could be different. Also you could specify a validation_split float between 0 and 1 instead, spliting the training data for validation.
  126. verbose = 2 so that Keras displays both the training and validation loss and accuracy.
  127. ##Plot

  128. def plot_losses(losses):
  129. fig = plt.figure()
  130. ax = fig.add_subplot(111)
  131. ax.plot(losses)
  132. ax.set_title('Loss per batch')
  133. fig.show()
  134. Nothing much here, just that it is helpful to monitor the loss during training but you could provide any list here of course.

  135. Usage

  136. import feedforward_keras_mnist as fkm

  137. model, losses = fkm.run_network()

  138. fkm.plot_losses(losses)
  139. if you do not want to reload the data every time:

  140. import feedforward_keras_mnist as fkm

  141. data = fkm.load_data()
  142. model, losses = fkm.run_network(data=data)

  143. # change some parameters in your code

  144. reload(fkm)
  145. model, losses = fkm.run_network(data=data)
  146. Using an Intel i7 CPU at 3.5GHz and an NVidia GTX 970 GPU, we achieve 0.9847 accuracy (1.53% error) in 56.6 seconds of training using this implementation (including loading and compilation).
复制代码
「经管之家」APP:经管人学习、答疑、交友,就上经管之家!
免流量费下载资料----在经管之家app可以下载论坛上的所有资源,并且不额外收取下载高峰期的论坛币。
涵盖所有经管领域的优秀内容----覆盖经济、管理、金融投资、计量统计、数据分析、国贸、财会等专业的学习宝库,各类资料应有尽有。
来自五湖四海的经管达人----已经有上千万的经管人来到这里,你可以找到任何学科方向、有共同话题的朋友。
经管之家(原人大经济论坛),跨越高校的围墙,带你走进经管知识的新世界。
扫描下方二维码下载并注册APP
本文关键词:

本文论坛网址:https://bbs.pinggu.org/thread-6205871-1-1.html

人气文章

1.凡人大经济论坛-经管之家转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
经管之家 人大经济论坛 大学 专业 手机版