Keras

from Wikipedia, the free encyclopedia
Keras

Keras logo.svg
Basic data

Maintainer Community project initiated by François Chollet
developer François Chollet
Current  version 2.4.0
( June 17, 2020 )
operating system Platform independence
programming language python
category Neural Networks
License MIT license
Keras.io

Keras is an open source deep learning library written in Python . It was initiated by François Chollet and first published on March 28, 2015. Keras offers a uniform interface for various backends , including TensorFlow , Microsoft Cognitive Toolkit (formerly CNTK) and Theano . The goal of Keras is to make the use of these libraries as beginner and user-friendly as possible.

Since the release of TensorFlow 1.4, Keras has been part of the Tensorflow Core API, but Keras will continue to be used as an independent library because, according to François Chollet, it is not intended as an interface for Tensorflow, but as an interface for many libraries. With the release of Keras 2.4, multi-backend support was discontinued. Since then, Keras has been referring directly to the implementation of Tensorflow 2.

example

The following example is intended to illustrate the basic functionality. In it, a neural network is taught the function of an exclusive-or gate with the help of Keras :

 1 # Über TensorFlow laden
 2 #
 3 # from tensorflow.keras.layers import Dense
 4 # from tensorflow.keras.models import Sequential
 5 #
 6 # oder
 7 #
 8 # Aus Keras direkt laden
 9 from keras.layers import Dense
10 from keras.models import Sequential
11 
12 # Numpy laden und festlegen des Zufalls-Startwertes
13 import numpy as np
14 np.random.seed(1337)
15 
16 # Matplotlib zur grafischen Darstellung laden
17 import matplotlib.pyplot as plt
18 
19 # Daten in Arrays speichern
20 eingangswerte = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
21 ausgangswerte = np.array([[0], [1], [1], [0]])
22 
23 # Erstellt das Model mit 2 Eingangsnodes, 2 Mittelnodes und einer Ausgangsnode
24 num_inner = 2
25 
26 model = Sequential()
27 model.add(Dense(num_inner, input_dim=2, activation='sigmoid'))
28 model.add(Dense(1))
29 
30 # Kompiliert das Model, damit es spaeter verwendet werden kann
31 model.compile(loss='mean_squared_error',
32               optimizer='adam',
33               metrics=['accuracy'])
34 
35 # Trainiert das Model mit den Eingangs-
36 # und den entsprechenden Ausgangswerten fuer 10000 Epochen
37 model.fit(x=eingangswerte, y=ausgangswerte, epochs=10000, verbose=0)
38 
39 # Testet die Eingangsdaten und schreibt die Ergebnisse in die Konsole
40 print(model.predict(eingangswerte))

The output is as follows:

1 [[  8.34465027e-07]  # Eingang 0 und 0, zu erwartender Ausgang 0
2  [  9.99996364e-01]  # Eingang 0 und 1, zu erwartender Ausgang 1
3  [  9.99996185e-01]  # Eingang 1 und 0, zu erwartender Ausgang 1
4  [  5.48362732e-06]] # Eingang 1 und 1, zu erwartender Ausgang 0

The model is very simple with two input nodes, two middle nodes, and one output node. However, it takes 10,000 epochs to get good results. Another activation function, here tanh can reduce the number of epochs to 1000:

1 # Erstellt das Model mit 2 Eingangsnodes, 32 Mittelnodes und einer Ausgangsnode
2 model = Sequential()
3 model.add(Dense(16, input_dim=2, activation='tanh'))
4 model.add(Dense(1, activation='tanh'))

The output is as follows:

1 [[0.00540294]
2  [0.94025123]
3  [0.93624824]
4  [0.00820918]]

The model analysis can be done directly with numpy and matplotlib. In this case, rasterized input data is transferred to the model and the output is graphically displayed in two dimensions in a contour plot. The limits of the classification are also shown as lines and the discrete input values ​​as points:

 1 # Bereitet die grafische Ausgabe mittels contourf vor
 2 # und rastert die Eingabewerte fuer das Modell
 3 x = np.linspace(-0.25, 1.25, 100)
 4 (X1_raster, X2_raster) = np.meshgrid(x, x)
 5 X1_vektor = X1_raster.flatten()
 6 X2_vektor = X2_raster.flatten()
 7 
 8 # Nutzt die gerasterten Eingabewerte und erzeugt Ausgabewerte
 9 eingangswerte_grafik = np.vstack((X1_vektor, X2_vektor)).T
10 ausgangswerte_grafik = model.predict(eingangswerte_grafik).reshape(X1_raster.shape)
11 
12 # Fragt die Gewichte der Verbindungen und die Bias-Daten ab
13 (gewichte, bias) = model.layers[0].get_weights()
14 
15 # Contourplot der gerasterten Ausgangswerte in leicht vergroessertem
16 # Bereich und Legende
17 plt.contourf(X1_raster, X2_raster, ausgangswerte_grafik, 100)
18 plt.xlim(-0.25, 1.25)
19 plt.ylim(-0.25, 1.25)
20 plt.xlabel("Eingabewert $x_1$")
21 plt.ylabel("Eingabewert $x_2$")
22 plt.colorbar()
23 
24 # Eintragen der Eingangsdaten in die Grafik
25 plt.scatter(np.array([0, 0, 1, 1]), np.array([0, 1, 0, 1]), color="red")
26 
27 # Plot der Klassifizierungs-"Begrenzungslinien" der Aktivierungsfunktionen
28 for i in range(num_inner):
29     plt.plot(x,
30              -gewichte[0, i]/gewichte[1, i]*x
31              - bias[i]/gewichte[1, i], color="black")
32 plt.show()

The representations for both variants of the model are as follows (left first variant, 10,000 epochs; right second variant, 500 epochs):

Keras model inner2 epochs10000.svg Keras model inner32 epochs500.svg

literature

  • François Chollet: Deep Learning with Python and Keras: The practical manual from the developer of the Keras library . mitp, 2018, ISBN 978-3-95845-838-3 .

Web links

Individual evidence

  1. Release 2.4.0 . June 17, 2020 (accessed June 18, 2020).
  2. ^ Add initial public version of Keras. March 28, 2015, accessed July 9, 2018 .
  3. Keras backends. January 4, 2018, accessed July 9, 2018 .
  4. Release TensorFlow 1.4.0. November 2, 2017, accessed July 9, 2018 .
  5. Good news, Tensorflow chooses Keras! # 5050. January 16, 2017, accessed July 9, 2018 .
  6. keras-team / keras. Accessed June 30, 2020 (English).