![]() | ![]() | ![]() | ![]() |
セットアップ
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
前書き
Kerasは、デフォルトのトレーニングおよび評価ループ、 fit()
およびevaluate()
提供します。それらの使用法は、組み込みメソッドを使用したトレーニングと評価のガイドで説明されています。
fit()
利便性を活用しながらモデルの学習アルゴリズムをカスタマイズしたい場合(たとえば、 fit()
を使用してGANをトレーニングする場合)、 Model
クラスをサブクラス化し、独自のtrain_step()
メソッドを実装できます。 fit()
中fit()
繰り返し呼び出されます。これについては、 fit()
で何が起こるかをカスタマイズするガイドで説明されています。
ここで、トレーニングと評価を非常に低レベルで制御したい場合は、独自のトレーニングと評価のループを最初から作成する必要があります。これがこのガイドの内容です。
GradientTape
使用:最初のエンドツーエンドの例
GradientTape
スコープ内でモデルを呼び出すと、損失値に関するレイヤーのトレーニング可能な重みの勾配を取得できます。オプティマイザーインスタンスを使用すると、これらのグラデーションを使用してこれらの変数を更新できます( model.trainable_weights
を使用して取得できます)。
単純なMNISTモデルを考えてみましょう。
inputs = keras.Input(shape=(784,), name="digits")
x1 = layers.Dense(64, activation="relu")(inputs)
x2 = layers.Dense(64, activation="relu")(x1)
outputs = layers.Dense(10, name="predictions")(x2)
model = keras.Model(inputs=inputs, outputs=outputs)
カスタムトレーニングループを備えたミニバッチ勾配を使用してトレーニングしましょう。
まず、オプティマイザー、損失関数、およびデータセットが必要になります。
# Instantiate an optimizer.
optimizer = keras.optimizers.SGD(learning_rate=1e-3)
# Instantiate a loss function.
loss_fn = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
# Prepare the training dataset.
batch_size = 64
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = np.reshape(x_train, (-1, 784))
x_test = np.reshape(x_test, (-1, 784))
# Reserve 10,000 samples for validation.
x_val = x_train[-10000:]
y_val = y_train[-10000:]
x_train = x_train[:-10000]
y_train = y_train[:-10000]
# Prepare the training dataset.
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_dataset = train_dataset.shuffle(buffer_size=1024).batch(batch_size)
# Prepare the validation dataset.
val_dataset = tf.data.Dataset.from_tensor_slices((x_val, y_val))
val_dataset = val_dataset.batch(batch_size)
トレーニングループは次のとおりです。
- エポックを反復する
for
ループを開きます - エポックごとに、データセットをバッチで反復する
for
ループを開きます。 - バッチごとに、
GradientTape()
スコープを開きます - このスコープ内で、モデル(フォワードパス)を呼び出し、損失を計算します
- 範囲外では、損失に関するモデルの重みの勾配を取得します
- 最後に、オプティマイザーを使用して、勾配に基づいてモデルの重みを更新します
epochs = 2
for epoch in range(epochs):
print("\nStart of epoch %d" % (epoch,))
# Iterate over the batches of the dataset.
for step, (x_batch_train, y_batch_train) in enumerate(train_dataset):
# Open a GradientTape to record the operations run
# during the forward pass, which enables auto-differentiation.
with tf.GradientTape() as tape:
# Run the forward pass of the layer.
# The operations that the layer applies
# to its inputs are going to be recorded
# on the GradientTape.
logits = model(x_batch_train, training=True) # Logits for this minibatch
# Compute the loss value for this minibatch.
loss_value = loss_fn(y_batch_train, logits)
# Use the gradient tape to automatically retrieve
# the gradients of the trainable variables with respect to the loss.
grads = tape.gradient(loss_value, model.trainable_weights)
# Run one step of gradient descent by updating
# the value of the variables to minimize the loss.
optimizer.apply_gradients(zip(grads, model.trainable_weights))
# Log every 200 batches.
if step % 200 == 0:
print(
"Training loss (for one batch) at step %d: %.4f"
% (step, float(loss_value))
)
print("Seen so far: %s samples" % ((step + 1) * batch_size))
Start of epoch 0 Training loss (for one batch) at step 0: 153.8545 Seen so far: 64 samples Training loss (for one batch) at step 200: 1.4767 Seen so far: 12864 samples Training loss (for one batch) at step 400: 1.4645 Seen so far: 25664 samples Training loss (for one batch) at step 600: 0.7049 Seen so far: 38464 samples Start of epoch 1 Training loss (for one batch) at step 0: 0.9202 Seen so far: 64 samples Training loss (for one batch) at step 200: 0.8473 Seen so far: 12864 samples Training loss (for one batch) at step 400: 0.6632 Seen so far: 25664 samples Training loss (for one batch) at step 600: 0.8758 Seen so far: 38464 samples
メトリックの低レベルの処理
この基本的なループにメトリック監視を追加しましょう。
ゼロから作成されたこのようなトレーニングループでは、組み込みの指標(または作成したカスタム指標)を簡単に再利用できます。フローは次のとおりです。
- ループの開始時にメトリックをインスタンス化します
- 各バッチの後に
metric.update_state()
呼び出しmetric.update_state()
- メトリックの現在の値を表示する必要がある場合は、
metric.result()
呼び出します - メトリックの状態をクリアする必要がある場合(通常はエポックの終わりに
metric.reset_states()
、metric.reset_states()
呼び出します。
この知識を使用して、各エポックの終了時に検証データのSparseCategoricalAccuracy
を計算してみましょう。
# Get model
inputs = keras.Input(shape=(784,), name="digits")
x = layers.Dense(64, activation="relu", name="dense_1")(inputs)
x = layers.Dense(64, activation="relu", name="dense_2")(x)
outputs = layers.Dense(10, name="predictions")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
# Instantiate an optimizer to train the model.
optimizer = keras.optimizers.SGD(learning_rate=1e-3)
# Instantiate a loss function.
loss_fn = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
# Prepare the metrics.
train_acc_metric = keras.metrics.SparseCategoricalAccuracy()
val_acc_metric = keras.metrics.SparseCategoricalAccuracy()
トレーニングと評価のループは次のとおりです。
import time
epochs = 2
for epoch in range(epochs):
print("\nStart of epoch %d" % (epoch,))
start_time = time.time()
# Iterate over the batches of the dataset.
for step, (x_batch_train, y_batch_train) in enumerate(train_dataset):
with tf.GradientTape() as tape:
logits = model(x_batch_train, training=True)
loss_value = loss_fn(y_batch_train, logits)
grads = tape.gradient(loss_value, model.trainable_weights)
optimizer.apply_gradients(zip(grads, model.trainable_weights))
# Update training metric.
train_acc_metric.update_state(y_batch_train, logits)
# Log every 200 batches.
if step % 200 == 0:
print(
"Training loss (for one batch) at step %d: %.4f"
% (step, float(loss_value))
)
print("Seen so far: %d samples" % ((step + 1) * batch_size))
# Display metrics at the end of each epoch.
train_acc = train_acc_metric.result()
print("Training acc over epoch: %.4f" % (float(train_acc),))
# Reset training metrics at the end of each epoch
train_acc_metric.reset_states()
# Run a validation loop at the end of each epoch.
for x_batch_val, y_batch_val in val_dataset:
val_logits = model(x_batch_val, training=False)
# Update val metrics
val_acc_metric.update_state(y_batch_val, val_logits)
val_acc = val_acc_metric.result()
val_acc_metric.reset_states()
print("Validation acc: %.4f" % (float(val_acc),))
print("Time taken: %.2fs" % (time.time() - start_time))
Start of epoch 0 Training loss (for one batch) at step 0: 114.3453 Seen so far: 64 samples Training loss (for one batch) at step 200: 2.2635 Seen so far: 12864 samples Training loss (for one batch) at step 400: 0.5206 Seen so far: 25664 samples Training loss (for one batch) at step 600: 1.0906 Seen so far: 38464 samples Training acc over epoch: 0.7022 Validation acc: 0.7853 Time taken: 5.38s Start of epoch 1 Training loss (for one batch) at step 0: 0.5879 Seen so far: 64 samples Training loss (for one batch) at step 200: 0.9477 Seen so far: 12864 samples Training loss (for one batch) at step 400: 0.4649 Seen so far: 25664 samples Training loss (for one batch) at step 600: 0.6874 Seen so far: 38464 samples Training acc over epoch: 0.8114 Validation acc: 0.8291 Time taken: 5.46s
tf.function
トレーニングステップをスピードアップ
TensorFlow 2.0のデフォルトのランタイムは、熱心な実行です。そのため、上記のトレーニングループは熱心に実行されます。
これはデバッグには最適ですが、グラフのコンパイルには明確なパフォーマンス上の利点があります。計算を静的グラフとして記述することで、フレームワークはグローバルなパフォーマンスの最適化を適用できます。これは、フレームワークが次に何が起こるかを知らずに、次々と貪欲に操作を実行するように制約されている場合は不可能です。
テンソルを入力として受け取る関数は、静的グラフにコンパイルできます。次のように、 @tf.function
デコレータを追加するだけです。
@tf.function
def train_step(x, y):
with tf.GradientTape() as tape:
logits = model(x, training=True)
loss_value = loss_fn(y, logits)
grads = tape.gradient(loss_value, model.trainable_weights)
optimizer.apply_gradients(zip(grads, model.trainable_weights))
train_acc_metric.update_state(y, logits)
return loss_value
評価ステップでも同じことをしましょう。
@tf.function
def test_step(x, y):
val_logits = model(x, training=False)
val_acc_metric.update_state(y, val_logits)
それでは、このコンパイルされたトレーニングステップでトレーニングループを再実行しましょう。
import time
epochs = 2
for epoch in range(epochs):
print("\nStart of epoch %d" % (epoch,))
start_time = time.time()
# Iterate over the batches of the dataset.
for step, (x_batch_train, y_batch_train) in enumerate(train_dataset):
loss_value = train_step(x_batch_train, y_batch_train)
# Log every 200 batches.
if step % 200 == 0:
print(
"Training loss (for one batch) at step %d: %.4f"
% (step, float(loss_value))
)
print("Seen so far: %d samples" % ((step + 1) * batch_size))
# Display metrics at the end of each epoch.
train_acc = train_acc_metric.result()
print("Training acc over epoch: %.4f" % (float(train_acc),))
# Reset training metrics at the end of each epoch
train_acc_metric.reset_states()
# Run a validation loop at the end of each epoch.
for x_batch_val, y_batch_val in val_dataset:
test_step(x_batch_val, y_batch_val)
val_acc = val_acc_metric.result()
val_acc_metric.reset_states()
print("Validation acc: %.4f" % (float(val_acc),))
print("Time taken: %.2fs" % (time.time() - start_time))
Start of epoch 0 Training loss (for one batch) at step 0: 0.4854 Seen so far: 64 samples Training loss (for one batch) at step 200: 0.5259 Seen so far: 12864 samples Training loss (for one batch) at step 400: 0.5035 Seen so far: 25664 samples Training loss (for one batch) at step 600: 0.2240 Seen so far: 38464 samples Training acc over epoch: 0.8502 Validation acc: 0.8616 Time taken: 1.32s Start of epoch 1 Training loss (for one batch) at step 0: 0.6278 Seen so far: 64 samples Training loss (for one batch) at step 200: 0.3667 Seen so far: 12864 samples Training loss (for one batch) at step 400: 0.3374 Seen so far: 25664 samples Training loss (for one batch) at step 600: 0.5318 Seen so far: 38464 samples Training acc over epoch: 0.8709 Validation acc: 0.8720 Time taken: 1.02s
はるかに速いですね。
モデルによって追跡される損失の低レベルの処理
レイヤーとモデルは、 self.add_loss(value)
を呼び出すレイヤーによるフォワードパス中に作成された損失を再帰的に追跡します。結果として得られるスカラー損失値のリストは、フォワードパスの最後にあるプロパティmodel.losses
から入手できます。
これらの損失コンポーネントを使用する場合は、それらを合計して、トレーニングステップの主な損失に追加する必要があります。
この層を考えてみましょう。これにより、アクティビティの正則化が失われます。
class ActivityRegularizationLayer(layers.Layer):
def call(self, inputs):
self.add_loss(1e-2 * tf.reduce_sum(inputs))
return inputs
それを使用する本当に単純なモデルを構築しましょう:
inputs = keras.Input(shape=(784,), name="digits")
x = layers.Dense(64, activation="relu")(inputs)
# Insert activity regularization as a layer
x = ActivityRegularizationLayer()(x)
x = layers.Dense(64, activation="relu")(x)
outputs = layers.Dense(10, name="predictions")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
トレーニングステップは次のようになります。
@tf.function
def train_step(x, y):
with tf.GradientTape() as tape:
logits = model(x, training=True)
loss_value = loss_fn(y, logits)
# Add any extra losses created during the forward pass.
loss_value += sum(model.losses)
grads = tape.gradient(loss_value, model.trainable_weights)
optimizer.apply_gradients(zip(grads, model.trainable_weights))
train_acc_metric.update_state(y, logits)
return loss_value
概要
これで、組み込みのトレーニングループを使用し、独自のループを最初から作成することについて知っておくべきことがすべてわかりました。
結論として、このガイドで学習したすべてを結び付ける簡単なエンドツーエンドの例を次に示します。MNISTディジットでトレーニングされたDCGANです。
エンドツーエンドの例:GANトレーニングループを最初から
あなたは生成的敵対的ネットワーク(GAN)に精通しているかもしれません。 GANは、画像のトレーニングデータセット(画像の「潜在空間」)の潜在的な分布を学習することにより、ほぼリアルに見える新しい画像を生成できます。
GANは、潜在空間のポイントを画像空間のポイントにマッピングする「ジェネレータ」モデル、「ディスクリミネーター」モデル、(トレーニングデータセットからの)実際の画像と偽物の違いを判別できる分類器の2つの部分で構成されます。画像(ジェネレータネットワークの出力)。
GANトレーニングループは次のようになります。
1)弁別器を訓練します。 -潜在空間内のランダムな点のバッチをサンプリングします。 -「ジェネレータ」モデルを使用して、ポイントを偽の画像に変換します。 -実際の画像のバッチを取得し、生成された画像と組み合わせます。 -「ディスクリミネーター」モデルをトレーニングして、生成された画像と実際の画像を分類します。
2)ジェネレーターをトレーニングします。 -潜在空間のランダムな点をサンプリングします。 -「ジェネレータ」ネットワークを介してポイントを偽の画像に変換します。 -実際の画像のバッチを取得し、生成された画像と組み合わせます。 -「ジェネレータ」モデルをトレーニングして、ディスクリミネータを「だまし」、偽の画像を本物として分類します。
GANがどのように機能するかについてのより詳細な概要については、Pythonを使用したディープラーニングを参照してください。
このトレーニングループを実装しましょう。まず、偽の数字と実際の数字を分類するための識別子を作成します。
discriminator = keras.Sequential(
[
keras.Input(shape=(28, 28, 1)),
layers.Conv2D(64, (3, 3), strides=(2, 2), padding="same"),
layers.LeakyReLU(alpha=0.2),
layers.Conv2D(128, (3, 3), strides=(2, 2), padding="same"),
layers.LeakyReLU(alpha=0.2),
layers.GlobalMaxPooling2D(),
layers.Dense(1),
],
name="discriminator",
)
discriminator.summary()
Model: "discriminator" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 14, 14, 64) 640 _________________________________________________________________ leaky_re_lu (LeakyReLU) (None, 14, 14, 64) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 7, 7, 128) 73856 _________________________________________________________________ leaky_re_lu_1 (LeakyReLU) (None, 7, 7, 128) 0 _________________________________________________________________ global_max_pooling2d (Global (None, 128) 0 _________________________________________________________________ dense_4 (Dense) (None, 1) 129 ================================================================= Total params: 74,625 Trainable params: 74,625 Non-trainable params: 0 _________________________________________________________________
次に、潜在ベクトルを形状(28, 28, 1)
28、28、1 (28, 28, 1)
(MNIST桁を表す(28, 28, 1)
出力に変換するジェネレーターネットワークを作成しましょう。
latent_dim = 128
generator = keras.Sequential(
[
keras.Input(shape=(latent_dim,)),
# We want to generate 128 coefficients to reshape into a 7x7x128 map
layers.Dense(7 * 7 * 128),
layers.LeakyReLU(alpha=0.2),
layers.Reshape((7, 7, 128)),
layers.Conv2DTranspose(128, (4, 4), strides=(2, 2), padding="same"),
layers.LeakyReLU(alpha=0.2),
layers.Conv2DTranspose(128, (4, 4), strides=(2, 2), padding="same"),
layers.LeakyReLU(alpha=0.2),
layers.Conv2D(1, (7, 7), padding="same", activation="sigmoid"),
],
name="generator",
)
重要な点は次のとおりです。トレーニングループです。ご覧のとおり、これは非常に簡単です。トレーニングステップ関数は17行しかかかりません。
# Instantiate one optimizer for the discriminator and another for the generator.
d_optimizer = keras.optimizers.Adam(learning_rate=0.0003)
g_optimizer = keras.optimizers.Adam(learning_rate=0.0004)
# Instantiate a loss function.
loss_fn = keras.losses.BinaryCrossentropy(from_logits=True)
@tf.function
def train_step(real_images):
# Sample random points in the latent space
random_latent_vectors = tf.random.normal(shape=(batch_size, latent_dim))
# Decode them to fake images
generated_images = generator(random_latent_vectors)
# Combine them with real images
combined_images = tf.concat([generated_images, real_images], axis=0)
# Assemble labels discriminating real from fake images
labels = tf.concat(
[tf.ones((batch_size, 1)), tf.zeros((real_images.shape[0], 1))], axis=0
)
# Add random noise to the labels - important trick!
labels += 0.05 * tf.random.uniform(labels.shape)
# Train the discriminator
with tf.GradientTape() as tape:
predictions = discriminator(combined_images)
d_loss = loss_fn(labels, predictions)
grads = tape.gradient(d_loss, discriminator.trainable_weights)
d_optimizer.apply_gradients(zip(grads, discriminator.trainable_weights))
# Sample random points in the latent space
random_latent_vectors = tf.random.normal(shape=(batch_size, latent_dim))
# Assemble labels that say "all real images"
misleading_labels = tf.zeros((batch_size, 1))
# Train the generator (note that we should *not* update the weights
# of the discriminator)!
with tf.GradientTape() as tape:
predictions = discriminator(generator(random_latent_vectors))
g_loss = loss_fn(misleading_labels, predictions)
grads = tape.gradient(g_loss, generator.trainable_weights)
g_optimizer.apply_gradients(zip(grads, generator.trainable_weights))
return d_loss, g_loss, generated_images
画像のバッチでtrain_step
を繰り返し呼び出すことにより、GANをトレーニングしましょう。
ディスクリミネーターとジェネレーターはconvnetであるため、このコードをGPUで実行する必要があります。
import os
# Prepare the dataset. We use both the training & test MNIST digits.
batch_size = 64
(x_train, _), (x_test, _) = keras.datasets.mnist.load_data()
all_digits = np.concatenate([x_train, x_test])
all_digits = all_digits.astype("float32") / 255.0
all_digits = np.reshape(all_digits, (-1, 28, 28, 1))
dataset = tf.data.Dataset.from_tensor_slices(all_digits)
dataset = dataset.shuffle(buffer_size=1024).batch(batch_size)
epochs = 1 # In practice you need at least 20 epochs to generate nice digits.
save_dir = "./"
for epoch in range(epochs):
print("\nStart epoch", epoch)
for step, real_images in enumerate(dataset):
# Train the discriminator & generator on one batch of real images.
d_loss, g_loss, generated_images = train_step(real_images)
# Logging.
if step % 200 == 0:
# Print metrics
print("discriminator loss at step %d: %.2f" % (step, d_loss))
print("adversarial loss at step %d: %.2f" % (step, g_loss))
# Save one generated image
img = tf.keras.preprocessing.image.array_to_img(
generated_images[0] * 255.0, scale=False
)
img.save(os.path.join(save_dir, "generated_img" + str(step) + ".png"))
# To limit execution time we stop after 10 steps.
# Remove the lines below to actually train the model!
if step > 10:
break
Start epoch 0 discriminator loss at step 0: 0.68 adversarial loss at step 0: 0.67
それでおしまい! Colab GPUでわずか30秒間のトレーニングを行うと、見栄えの良い偽のMNIST数字が得られます。