[Python] Why the loss values of class type and function type of tensorflow models are...

Discussão em 'Python' iniciado por Stack, Abril 17, 2025.

  1. Stack

    Stack Membro Participativo

    I made two different type models with tensorflow, one is Class type, the other is function type for learning Fashion MNIST. I think the structure of two models is exactly same, but the results are not.

    What is the difference of two models?, why the values are different? and how can I make these two models exactly same?

    The code is like below:

    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

    import keras
    import tensorflow as tf

    from sklearn.model_selection import train_test_split

    keras.utils.set_random_seed(42)
    tf.random.set_seed(42)

    (train_input, train_target), (test_input, test_target) = \
    keras.datasets.fashion_mnist.load_data()

    train_scaled = train_input / 255.0

    train_scaled, val_scaled, train_target, val_target = train_test_split(
    train_scaled, train_target, test_size=0.2, random_state=42)

    class TFModel(keras.Model):
    def __init__(self, a_layer=None):
    super(TFModel, self).__init__()
    self.flatten = keras.layers.Flatten()
    self.dense1 = keras.layers.Dense(100, activation='relu')
    self.option = a_layer
    self.dense2 = keras.layers.Dense(10, activation='softmax')

    def call(self, x):
    x = self.flatten(x)
    x = self.dense1(x)
    if self.option is not None:
    x = self.option(x, training=True)
    x = self.dense2(x)
    return x

    def model_fn(a_layer=None):
    model = keras.Sequential()
    model.add(keras.layers.Input(shape=(28,28)))
    model.add(keras.layers.Flatten())
    model.add(keras.layers.Dense(100, activation='relu'))
    if a_layer:
    model.add(a_layer)
    model.add(keras.layers.Dense(10, activation='softmax'))
    return model

    dropout = keras.layers.Dropout(0.3)

    # model = model_fn(dropout)
    # model2 = TFModel(dropout)

    model = model_fn()
    model2 = TFModel()

    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',
    metrics=['accuracy'])
    model2.compile(optimizer='adam', loss='sparse_categorical_crossentropy',
    metrics=['accuracy'])
    history = model.fit(train_scaled, train_target, epochs=20, verbose=0,
    validation_data=(val_scaled, val_target))
    history2 = model2.fit(train_scaled, train_target, epochs=20, verbose=0,
    validation_data=(val_scaled, val_target))

    plt.plot(history.history['loss'], label='train')
    plt.plot(history.history['val_loss'], label='val')
    # plt.xlabel('epoch')
    # plt.ylabel('loss')
    # plt.legend()
    # plt.show()

    plt.plot(history2.history['loss'], label='train2')
    plt.plot(history2.history['val_loss'], label='val2')
    plt.xlabel('epoch')
    plt.ylabel('loss')
    plt.legend()
    plt.show()


    The result is like this:

    without dropout:

    [​IMG]

    with dropout:

    [​IMG]

    I think both models should show same train loss and validation loss graph.

    Continue reading...

Compartilhe esta Página