Trainen met Keras

Introductie tot TensorFlow in Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Overzicht trainen en evalueren

  1. Data laden en opschonen
  2. Model definiëren
  3. Model trainen en valideren
  4. Model evalueren
Introductie tot TensorFlow in Python

Een model trainen

# Import tensorflow
import tensorflow as tf

# Define a sequential model
model = tf.keras.Sequential()
# Define the hidden layer
model.add(tf.keras.layers.Dense(16, activation='relu', input_shape=(784,)))
# Define the output layer
model.add(tf.keras.layers.Dense(4, activation='softmax'))
Introductie tot TensorFlow in Python

Een model trainen

# Compile model
model.compile('adam', loss='categorical_crossentropy')
# Train model
model.fit(image_features, image_labels)
Introductie tot TensorFlow in Python

De fit()-bewerking

  • Vereiste argumenten
    • features
    • labels
  • Veel optionele argumenten
    • batch_size
    • epochs
    • validation_split
Introductie tot TensorFlow in Python

Batchgrootte en epochs

Het diagram toont hoe een dataset in batches wordt verdeeld en dat de combinatie van die batches één epoch vormt.

Introductie tot TensorFlow in Python

Validatie uitvoeren

De afbeelding toont hoe een dataset wordt gesplitst in een trainings- en validatiesteekproef.

Introductie tot TensorFlow in Python

Validatie uitvoeren

# Train model with validation split
model.fit(features, labels, epochs=10, validation_split=0.20)
Introductie tot TensorFlow in Python

Validatie uitvoeren

De afbeelding toont 10 epochs met trainings- en validatieresultaten.

Introductie tot TensorFlow in Python

De metriek wijzigen

# Recomile the model with the accuracy metric
model.compile('adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Train model with validation split
model.fit(features, labels, epochs=10, validation_split=0.20)
Introductie tot TensorFlow in Python

De metriek wijzigen

De afbeelding toont 10 epochs met trainings- en validatieresultaten.

Introductie tot TensorFlow in Python

De evaluate()-bewerking

De afbeelding toont hoe een dataset wordt gesplitst in train-, validatie- en teststeekproeven.

# Evaluate the test set
model.evaluate(test)
Introductie tot TensorFlow in Python

Laten we oefenen!

Introductie tot TensorFlow in Python

Preparing Video For Download...