Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/ja/addons/tutorials/layers_weightnormalization.ipynb
25118 views
Kernel: Python 3
#@title Licensed under the Apache License, Version 2.0 # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.

概要

このノートブックでは、重み正規化レイヤーの使用方法と収束性を向上させる方法を説明します。

重み正規化

ディープニューラルネットワークのトレーニングを高速化する単純な再パラメータ化である。

Tim Salimans、Diederik P. Kingma(2016)

この方法で重みを再パラメータ化することにより、最適化問題の条件付けを改善し、確率的勾配降下法の収束を高速化します。我々の再パラメータ化はバッチ正規化から着想を得ていますが、ミニバッチ内で例と例の間の依存性は導入していません。つまり、この手法は LSTM のような再帰モデルや、深層強化学習や生成モデルのようなバッチ正規化があまり適していないノイズに敏感なアプリケーションにもうまく適用できることを意味します。この手法ははるかに単純ではありますが、完全なバッチ正規化の高速化の大部分を提供します。さらに、我々の手法の計算オーバーヘッドは低くなるため、同じ時間内により多くの最適化ステップを実行することができます。

https://arxiv.org/abs/1602.07868



MNIST モデルをビルドする

!pip install -U tensorflow-addons
import tensorflow as tf import tensorflow_addons as tfa
import numpy as np from matplotlib import pyplot as plt
# Hyper Parameters batch_size = 32 epochs = 10 num_classes=10

モデルを構築する

# Standard ConvNet reg_model = tf.keras.Sequential([ tf.keras.layers.Conv2D(6, 5, activation='relu'), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Conv2D(16, 5, activation='relu'), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Flatten(), tf.keras.layers.Dense(120, activation='relu'), tf.keras.layers.Dense(84, activation='relu'), tf.keras.layers.Dense(num_classes, activation='softmax'), ])
# WeightNorm ConvNet wn_model = tf.keras.Sequential([ tfa.layers.WeightNormalization(tf.keras.layers.Conv2D(6, 5, activation='relu')), tf.keras.layers.MaxPooling2D(2, 2), tfa.layers.WeightNormalization(tf.keras.layers.Conv2D(16, 5, activation='relu')), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Flatten(), tfa.layers.WeightNormalization(tf.keras.layers.Dense(120, activation='relu')), tfa.layers.WeightNormalization(tf.keras.layers.Dense(84, activation='relu')), tfa.layers.WeightNormalization(tf.keras.layers.Dense(num_classes, activation='softmax')), ])

データを読み込む

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data() # Convert class vectors to binary class matrices. y_train = tf.keras.utils.to_categorical(y_train, num_classes) y_test = tf.keras.utils.to_categorical(y_test, num_classes) x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255

モデルをトレーニングする

reg_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) reg_history = reg_model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test), shuffle=True)
wn_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) wn_history = wn_model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test), shuffle=True)
reg_accuracy = reg_history.history['accuracy'] wn_accuracy = wn_history.history['accuracy'] plt.plot(np.linspace(0, epochs, epochs), reg_accuracy, color='red', label='Regular ConvNet') plt.plot(np.linspace(0, epochs, epochs), wn_accuracy, color='blue', label='WeightNorm ConvNet') plt.title('WeightNorm Accuracy Comparison') plt.legend() plt.grid(True) plt.show()