TensorFlow2:MNISTやってみる(keras model class)
TensorFlowのチュートリアルにそって、MNIST問題をやってみる。 TensorFlow 2 quickstart for experts 上記のコードをベースに、関数作ったりしながらソースを作成した。 1.xの時との違いとかをメモしながら記載する。といいつつ、1.xの時に近い書き方を選んでいると思う。 ざっくり処理フロー 以下のような流れになる。流れは1.xの時と変わらない。 MNISTデータ読み出し処理の定義 モデルの定義 学習の定義 テストの定義 学習ループの実行 コード 事前に タイピングを減らすために、パッケージ名にエイリアスを付けておく。 import tensorflow as tf # asign aliases tfk = tf . keras tfkl = tf . keras . layers MNISTデータ読み出し処理の定義 def load_dataset ( batch_size = 32 ) : ( x_train , y_train ) , ( x_test , y_test ) = tfk . datasets . mnist . load_data ( ) # normalize 0.0 ~ 1.0 x_train , x_test = x_train / 255.0 , x_test / 255.0 # add newaxis for channel. x_train = x_train [ . . . , tf . newaxis ] x_test = x_test [ . . . , tf . newaxis ] # build pipeline: shuffle -> batch. ds_train = tf . data . Dataset . from_tensor_slices ( ( x_train , y_train ) ) . shuffle ( 10000 ) . batch ( batch_size ) ds_test = tf . data . Dataset . from_tensor_slices...