Tensorflow for beginner
    • PDF

    Tensorflow for beginner

    • PDF

    Article Summary

    Classic/VPC環境で利用できます。

    さらに多くの例や詳しい案内は、TensorFlowチュートリアルをご参照ください。

    始めに、プログラムにTensorFlowライブラリをインポートします。

    import tensorflow as tf
    

    MNISTデータセットをロードして準備します。サンプルの値を整数から浮動小数点数に変換します。

    mnist = tf.keras.datasets.mnist
    
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x_train, x_test = x_train / 255.0, x_test / 255.0
    

    層を順に積み重ね、tf.keras.Sequentialモデルを作成します。訓練に使用するオプティマイザー(optimizer)と損失関数を選択します。

    model = tf.keras.models.Sequential([
      tf.keras.layers.Flatten(input_shape=(28, 28)),
      tf.keras.layers.Dense(128, activation='relu'),
      tf.keras.layers.Dropout(0.2),
      tf.keras.layers.Dense(10, activation='softmax')
    ])
    
    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
    

    モデルを訓練して評価します。

    model.fit(x_train, y_train, epochs=5)
    
    model.evaluate(x_test,  y_test, verbose=2)
    
    '''
    Train on 60000 samples
    Epoch 1/5
    WARNING:tensorflow:Entity <function Function._initialize_uninitialized_variables.<locals>.initialize_variables at 0x7f2d11707048> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Num'
    WARNING: Entity <function Function._initialize_uninitialized_variables.<locals>.initialize_variables at 0x7f2d11707048> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Num'
    60000/60000 [==============================] - 4s 68us/sample - loss: 0.2941 - accuracy: 0.9140
    Epoch 2/5
    60000/60000 [==============================] - 4s 62us/sample - loss: 0.1396 - accuracy: 0.9587
    Epoch 3/5
    60000/60000 [==============================] - 4s 62us/sample - loss: 0.1046 - accuracy: 0.9680
    Epoch 4/5
    60000/60000 [==============================] - 4s 62us/sample - loss: 0.0859 - accuracy: 0.9742
    Epoch 5/5
    60000/60000 [==============================] - 4s 62us/sample - loss: 0.0724 - accuracy: 0.9771
    10000/1 - 0s - loss: 0.0345 - accuracy: 0.9788
    [0. 06729823819857557, 0.9788]
    '''
    

    訓練された画像分類器は、このデータセットで約98%の精度を達成します。詳細内容は、TensorFlowチュートリアルをご参照ください。


    この記事は役に立ちましたか?

    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.