简体   繁体   中英

Why my TensorFlow model is not changing their space after training?

After I load the saved model and fit new data, and save the model again then the disk space stays the same.

First of all, I check if the model exists in my dictionary. If models exist I load them from the folder and fit new data. After saving again, disk space doesn't change.

Here is my code:

    df = df[['y', 'h', 'o', 'l']]  
    df2 = df.values
 
    training = int(np.ceil(len(df) * .95)) 
    # prepere data for tensorflow
    # MinMaxScaler expecting like 1 feature
    scaler = MinMaxScaler(feature_range=(0, 1))
    scaled_data = scaler.fit_transform(df)
    print(f"scaled_data {len(scaled_data)}")

    # How many past days of data we want to use to predict the next day price
    prediction_days = 500
    train_data = scaled_data[0:int(training), :]
    print(f"train_data {len(train_data)}")
    # Preparing the Training data
    X_train = []
    y_train = []

    X_test = []
    y_test = []

    for x in range(prediction_days, len(train_data)):
        X_train.append(scaled_data[x - prediction_days:x, 0])
        y_train.append(scaled_data[x, 0])

        X_test.append(scaled_data[x - prediction_days:x, 0])
        y_test.append(scaled_data[x, 0])

    X_train, y_train = np.array(X_train), np.array(y_train)
    X_test, y_test = np.array(X_test), np.array(y_test)

    # Reshaping so that it will work in Neural net
    X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
    X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
  

    print("Files")
    print(os.path.isfile('model.h5'))
    if os.path.isfile('model.h5') is False:
        model = Sequential()
        model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1)))
        model.add(Dropout(0.2))
        model.add(LSTM(units=50, return_sequences=True))
        model.add(Dropout(0.2))
        model.add(LSTM(units=50))
        model.add(Dropout(0.2))
        model.add(Dense(units=60))
        # define the optimization algorithm
        opt = SGD(learning_rate=0.01, momentum=0.9)
        model.compile(optimizer="adam", loss='mean_squared_error')
        model.fit(X_train, y_train,  epochs=5, validation_data=(X_test, y_test)) #, callbacks=[keras.callbacks.LearningRateScheduler(lambda epoch: 1e-8 * 10 ** (epoch / 30))]

        # evaluate the model

        model.save('model.h5') 
        model = load_model('model.h5')

        model_json = model.to_json()
        with open("model.json", "w") as json_file:
            json_file.write(model_json)

        model.save_weights('model_weight.h5')
        model.load_weights('model_weight.h5')

    else:

        # load json and create model
        json_file = open('model.json', 'r')
        loaded_model_json = json_file.read()
        json_file.close()
        model = model_from_json(loaded_model_json)

        # load weights into new model
        model.load_weights("model.h5")
        print("Loaded model from disk")
        model.compile(optimizer='adam', loss='mean_squared_error')
        # train the model, iterating on the data in batches
        model.fit(X_train, y_train, epochs=5, validation_data=(X_test, y_test)) # callbacks=[keras.callbacks.LearningRateScheduler(lambda epoch: 1e-8 * 10 ** (epoch / 30))]

        # check if model learning

        model_json = model.to_json()
        with open("model.json", "w") as json_file:
            json_file.write(model_json)

        model.save_weights('model_weight.h5')
        model.load_weights('model_weight.h5')

And my model space look like thisIMG

Thank you @Nick ODell and @Alberto Sinigaglia . Mentioning these comments in the answer section for the community benefit.

Every weight in the trained version also exists in the untrained version.

the model is parametric... what changes is the value of the attributes, not the number of attributes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM