Can I use model.fit to upgrade my gradient every time

Viewed 41
def tf_model_2(epochz, lay1, lay2):
    model = tf.keras.Sequential([
        tf.keras.layers.Input(shape=(3,)),
        tf.keras.layers.Dense(lay1, activation='relu'),
        tf.keras.layers.Dense(lay2, activation='relu'),
        tf.keras.layers.Dense(1, activation='sigmoid')
    ])

    model.compile(optimizer=tf.keras.optimizers.RMSprop(),
                  loss=tf.keras.losses.BinaryCrossentropy(),
                  metrics=tf.keras.metrics.BinaryAccuracy())
    
    for epoch in range(epochz):
        path = '/home'
        for j in os.listdir(path):

                
            for i in range(25, 10, -1):

                X_train, X_test, y_train, y_test = NN.getdata(j, i) 
                # this returns (n x 3) dataframe of digits for x and series of boolean for y

                model.fit(X_train, y_train, epochs=1, verbose=0)

                
    model.save('first_model')

each time I call getdata with a new i and j. it returns a new (X_train, X_test, y_train, y_test). Can I use model.fit to upgrade my gradient every time?

1 Answers

This is not a very good way of doing it.

If it's one batch per i,j, you should use model.train_on_batch instead of fit. It would be way faster.

Also, you could use a generator to read the data and call a single fit once that will keep reading the data.

Related