2

keras.Model.save()私の知る限り、結果とバイナリの完全な再現性のために必要なすべての手順を実行したのに、実行ごとに異なるバイナリが生成されるのはなぜですか?

これは、docker コンテナー内で次のスクリプトを実行するだけで確認できます。ディレクトリのsaved_model.pb内容はvariables常に変化します。以下を使用してコンテナーを作成します。
docker run --rm --volume=/host/dir/with/script:/workspace -it tensorflow/tensorflow:2.4.1 bash

# Setup environmental variables.
import os
os.environ["TF_DETERMINISTIC_OPS"] = "1"
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"

import numpy as np
import random
import tensorflow as tf
import time


def initialise_seeds(seed):
    os.environ["PYTHONHASHSEED"] = str(seed)
    random.seed(seed)
    np.random.seed(seed)
    tf.random.set_seed(seed)


SEED = 42
initialise_seeds(SEED)

if __name__ == '__main__':
    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

    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),
        tf.keras.layers.Softmax()
    ], name="best_ever")

    model.compile(
        optimizer='adam',
        loss=tf.keras.losses.SparseCategoricalCrossentropy(),
        metrics=['accuracy']
    )
    model.fit(x_train, y_train, epochs=2)
    model.evaluate(x_test,  y_test, verbose=2)

    # Save the model weights.
    ckpt = "/cp-0.ckpt"
    model.save_weights(model.name + ckpt)
    # Also save keras model.
    model.save(model.name)

    # Convert the model.
    converter = tf.lite.TFLiteConverter.from_keras_model(model)
    # Include necessary functions.
    converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
                                           tf.lite.OpsSet.SELECT_TF_OPS]
    tflite_model = converter.convert()

    # Save the model.
    with open('model.tflite', 'wb') as f:
        f.write(tflite_model)

    # Load the TFLite model from disk and allocate tensors.
    interpreter = tf.lite.Interpreter(model_path="model.tflite")
    interpreter.allocate_tensors()

    # Get input and output tensors.
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    # Test model ouput on the same data to ensure correct function. 
    input_shape = input_details[0]['shape']
    input_data = np.ones((input_shape), dtype=np.float32) * 0.5
    interpreter.set_tensor(input_details[0]['index'], input_data)

    interpreter.invoke()

    # The function `get_tensor()` returns a copy of the tensor data.
    # Use `tensor()` in order to get a pointer to the tensor.
    output_data = interpreter.get_tensor(output_details[0]['index'])
    print(output_data.shape)

ありがとう。

4

0 に答える 0