hands on keras - unq.edu.arceciliajarne.web.unq.edu.ar/.../06/hands_on_keras.pdf · hands-on keras...

12
Hands on Keras Cecilia Jarne [email protected] Twitter: @ceciliajarne Cecilia Jarne Neural Networks and Keras [email protected] 1 / 12

Upload: others

Post on 26-May-2020

29 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hands on Keras - unq.edu.arceciliajarne.web.unq.edu.ar/.../06/hands_on_keras.pdf · Hands-On Keras Keras is a high-level neural networks API, written in Python and capable of running

Hands on Keras

Cecilia Jarne

[email protected]

Twitter: @ceciliajarne

Cecilia Jarne Neural Networks and Keras [email protected] 1 / 12

Page 2: Hands on Keras - unq.edu.arceciliajarne.web.unq.edu.ar/.../06/hands_on_keras.pdf · Hands-On Keras Keras is a high-level neural networks API, written in Python and capable of running

Necessary Libraries

Available in the following sites:

https://www.anaconda.com/distribution/

https://www.scipy.org/install.html

https://www.tensorflow.org/install/

https://keras.io/

Please create a github account:https://github.com/

Cecilia Jarne Neural Networks and Keras [email protected] 2 / 12

Page 3: Hands on Keras - unq.edu.arceciliajarne.web.unq.edu.ar/.../06/hands_on_keras.pdf · Hands-On Keras Keras is a high-level neural networks API, written in Python and capable of running

Hands-On Keras

Keras is a high-level neural networks API, written in Python and capableof running on top of TensorFlow CNTK, or Theano. It was developed witha focus on enabling fast experimentation.Use Keras if you need a deep learning library that:

Allows for easy and fast prototyping (through user friendliness,modularity, and extensibility).

Supports both convolutional networks and recurrent networks, as wellas combinations of the two, )and others via complementary projects).

Runs seamlessly on CPU and GPU.

Cecilia Jarne Neural Networks and Keras [email protected] 3 / 12

Page 4: Hands on Keras - unq.edu.arceciliajarne.web.unq.edu.ar/.../06/hands_on_keras.pdf · Hands-On Keras Keras is a high-level neural networks API, written in Python and capable of running

Core Models: Sequential

The Sequential model is a linear stack of layers. 3 main Steps:

1. Specify the input shape

2. Compile()

3. Fit()

Cecilia Jarne Neural Networks and Keras [email protected] 4 / 12

Page 5: Hands on Keras - unq.edu.arceciliajarne.web.unq.edu.ar/.../06/hands_on_keras.pdf · Hands-On Keras Keras is a high-level neural networks API, written in Python and capable of running

Hands-On Keras

Model compilation It need 3 elements:

An optimizer. This could be the string identifier of an existingoptimizer (such as rmsprop or adagrad), or an instance of theOptimizer class.

A loss function. This is the objective that the model will try tominimize. It can be the string identifier of an existing loss function(such as categorical crossentropy or mse), or it can be an objectivefunction.

A list of metrics.

1

2 model.compile(loss=’mean_squared_error’, optimizer=’sgd’, metrics=[metrics.mae, metrics.

categorical_accuracy])

Cecilia Jarne Neural Networks and Keras [email protected] 5 / 12

Page 6: Hands on Keras - unq.edu.arceciliajarne.web.unq.edu.ar/.../06/hands_on_keras.pdf · Hands-On Keras Keras is a high-level neural networks API, written in Python and capable of running

Training

Keras models are trained on Numpy arrays of input data and labels. Fortraining a model, you will typically use the fit function.

Cecilia Jarne Neural Networks and Keras [email protected] 6 / 12

Page 7: Hands on Keras - unq.edu.arceciliajarne.web.unq.edu.ar/.../06/hands_on_keras.pdf · Hands-On Keras Keras is a high-level neural networks API, written in Python and capable of running

Hands-On Keras:Leyers

Layer Types: Convolutional layers

Conv1D

Conv2D

Conv3DSpecially fast Convolutional layers:

• CuDNNGRU• CuDNNLSTM

Cecilia Jarne Neural Networks and Keras [email protected] 7 / 12

Page 8: Hands on Keras - unq.edu.arceciliajarne.web.unq.edu.ar/.../06/hands_on_keras.pdf · Hands-On Keras Keras is a high-level neural networks API, written in Python and capable of running

Hands-On Keras

Layer Types: Recurrent layers

RNN

SimpleRNN

GRU

LSTM

ConvLSTM2D

StackedRNNCells

Cecilia Jarne Neural Networks and Keras [email protected] 8 / 12

Page 9: Hands on Keras - unq.edu.arceciliajarne.web.unq.edu.ar/.../06/hands_on_keras.pdf · Hands-On Keras Keras is a high-level neural networks API, written in Python and capable of running

Hands-On Keras

Losses:

mean squared error

mean squared error(y true, y pred)

mean absolute error(y true, y pred)

mean absolute percentage error(y true, y pred)

mean squared logarithmic error(y true, y pred)

categorical crossentropy(y true, y pred)

Cecilia Jarne Neural Networks and Keras [email protected] 9 / 12

Page 10: Hands on Keras - unq.edu.arceciliajarne.web.unq.edu.ar/.../06/hands_on_keras.pdf · Hands-On Keras Keras is a high-level neural networks API, written in Python and capable of running

Hands-On Keras

Metrics

accuracy

binary accuracy

binary accuracy(y true, y pred)

categorical accuracy(y true, y pred)

top k categorical accuracy(y true, y pred, k=5)

Cecilia Jarne Neural Networks and Keras [email protected] 10 / 12

Page 11: Hands on Keras - unq.edu.arceciliajarne.web.unq.edu.ar/.../06/hands_on_keras.pdf · Hands-On Keras Keras is a high-level neural networks API, written in Python and capable of running

Hands-On Keras

Optimizers

1 keras.optimizers.SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False)

2 keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)

3 keras.optimizers.Adagrad(lr=0.01, epsilon=1e-08, decay=0.0)

4 keras.optimizers.Adadelta(lr=1.0, rho=0.95, epsilon=1e-08, decay=0.0)

5 keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)

Cecilia Jarne Neural Networks and Keras [email protected] 11 / 12

Page 12: Hands on Keras - unq.edu.arceciliajarne.web.unq.edu.ar/.../06/hands_on_keras.pdf · Hands-On Keras Keras is a high-level neural networks API, written in Python and capable of running

Hands-On Keras

Activations:

Example:

1 from keras.layers import Activation, Dense

2 model.add(Dense(64))

3 model.add(Activation(’tanh’))

Cecilia Jarne Neural Networks and Keras [email protected] 12 / 12