Lösungen

Aufgabe: Eigene Experimente mit Keras

import numpy
import tensorflow.keras
import matplotlib.pyplot

data = numpy.loadtxt('diabetes.csv', delimiter=',')
inputs = data[:,:-1]
targets = data[:,-1]

network = tensorflow.keras.models.Sequential()
network.add(tensorflow.keras.layers.Dense(12, input_dim=8, activation='relu'))
network.add(tensorflow.keras.layers.Dense(8, activation='relu'))
network.add(tensorflow.keras.layers.Dense(1, activation='sigmoid'))

network.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])

stop = tensorflow.keras.callbacks.EarlyStopping(monitor='val_loss', patience=500)
best = tensorflow.keras.callbacks.ModelCheckpoint(
    'best.h5', monitor='val_loss', save_best_only=True)
training = network.fit(
    inputs, targets, epochs=5000, batch_size=10,
    validation_split=0.3, callbacks=[stop,best]
)

fig = matplotlib.pyplot.figure()
pic = fig.add_subplot(1,1,1)
pic.plot(training.history['loss'][10:])
pic.plot(training.history['acc'][10:])
pic.plot(training.history['val_loss'][10:])
pic.plot(training.history['val_acc'][10:])
pic.set_title('diabetes training')
pic.legend(['loss','acc','val_loss','val_acc'], loc='upper left')
pic.set_xlabel('epoch')
pic.set_ylabel('metric')
fig.savefig('diabetes_training.png')