Kerasでの学習

Introduction to TensorFlow in Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

学習と評価の概要

  1. データの読み込みと整形
  2. モデル定義
  3. 学習と検証
  4. 評価
Introduction to TensorFlow in Python

モデルの学習方法

# 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'))
Introduction to TensorFlow in Python

モデルの学習方法

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

fit() 操作

  • 必須引数
    • features
    • labels
  • 任意引数(多数)
    • batch_size
    • epochs
    • validation_split
Introduction to TensorFlow in Python

バッチサイズとエポック

データセットをバッチに分割し,それらの合計が1エポックになることを示す図。

Introduction to TensorFlow in Python

検証の実行

データセットを学習用と検証用に分割する図。

Introduction to TensorFlow in Python

検証の実行

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

検証の実行

10エポック分の学習と検証結果の図。

Introduction to TensorFlow in Python

評価指標の変更

# 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)
Introduction to TensorFlow in Python

評価指標の変更

10エポック分の学習と検証結果の図。

Introduction to TensorFlow in Python

evaluation() 操作

データセットを学習・検証・テストに分割する図。

# Evaluate the test set
model.evaluate(test)
Introduction to TensorFlow in Python

実践してみましょう!

Introduction to TensorFlow in Python

Preparing Video For Download...