如何在 Keras 2.10.0 中加载已保存的 Liquid 时间序列模型?

问题描述 投票:0回答:1

我在尝试加载模型时陷入困境。我已经对其进行了两个时期的训练,获得了两个保存的检查点,每个时期之后一个,但我在尝试加载模型时遇到了困难。

这是我在尝试使用加载模型时遇到的错误

model = load_model('my_chunkedmodel_vertical_epoch_01.h5')
Traceback (most recent call last):

  File "/tmp/ipykernel_682950/3467434784.py", line 1, in <module>
    model = load_model('my_chunkedmodel_vertical_epoch_01.h5')

  File "/home/ceausexu/anaconda3/envs/treisapte/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None

  File "/home/ceausexu/anaconda3/envs/treisapte/lib/python3.7/site-packages/ncps/tf/ltc_cell.py", line 347, in from_config
    return cls(wiring=wiring, **config)

  File "/home/ceausexu/anaconda3/envs/treisapte/lib/python3.7/site-packages/ncps/tf/ltc_cell.py", line 64, in __init__
    super().__init__(**kwargs)

TypeError: ('Keyword argument not understood:', 'adjacency_matrix')"

这是到目前为止的脚本:

import numpy as np
import gc
import time
import matplotlib.pyplot as plt
import pandas as pd

import plotly.graph_objects as go

from sklearn.preprocessing import MinMaxScaler
from keras import backend as K
from keras.callbacks import ModelCheckpoint
from keras.models import Model
from keras.layers import Input, LSTM, Conv1D, Dense, Dropout, Flatten, concatenate
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint
from keras.models import load_model
from keras.preprocessing.sequence import TimeseriesGenerator
from keras.callbacks import ReduceLROnPlateau

from ncps.tf import LTC  # or from ncps.tf import CfC based on what you want
from ncps.wirings import AutoNCP  # for custom wiring


# Custom DoubleInputTimeseriesGenerator for hybrid models
class DoubleInputTimeseriesGenerator(TimeseriesGenerator):
    def __getitem__(self, index):
        X, y = super().__getitem__(index)
        print(f"Shape of y in Data Generator: {y.shape}")  # Debug line
        return [X, X], y  # Return X twice for the two inputs
    
    
##checkpoint save after epoch
checkpoint_filepath = "my_chunkedmodel_vertical_epoch_{epoch:02d}.h5"
checkpoint_callback = ModelCheckpoint(
    filepath=checkpoint_filepath,
    save_best_only=False,  # Set to True if you only want to save the model if it's "better" (based on validation loss)
    monitor='loss',  # What metric to monitor (you can change this to val_loss if you have validation data)
    verbose=1,  # Verbosity mode, 1 means it will print the epoch number when saving
    save_weights_only=False,  # If True, then only the model's weights will be saved, else the full model is saved
    mode='auto',  # The decision to overwrite the current save file is made based on the monitored quantity
    save_freq='epoch'  # Save the model after each epoch
)


# Wiring configuration
wiring = AutoNCP(50, 5)  # 50 neurons, 5 outputs

# Model architecture
ncp_input = Input(shape=(None, 6))  # 6 features
x = LTC(wiring)(ncp_input)  # using wiring, no need to specify neurons here
x = Dropout(0.2)(x)
x = Dense(5)(x)  # 5 outputs

# Create the model
model = Model(inputs=ncp_input, outputs=x)

# Compile the model
model.compile(
    optimizer=Adam(learning_rate=0.001),
    loss='mean_squared_error',
    metrics=['mean_absolute_error']
)


# Load the entire training dataset
dataset_train = pd.read_csv('train_set.csv')
training_set = dataset_train.iloc[:, [1,2,3,4,5,8]].values

# Load the evaluation dataset
dataset_eval = pd.read_csv('evaluation_set.csv')
evaluation_set = dataset_eval.iloc[:, [1,2,3,4,5,8]].values

# Feature scaling
sc = MinMaxScaler(feature_range = (0, 1))
training_set_scaled = sc.fit_transform(training_set)
evaluation_set_scaled = sc.transform(evaluation_set)


# Define a separate scaler for 'Open' price
sc_open = MinMaxScaler(feature_range = (0, 1))
training_set_open_scaled = sc_open.fit_transform(training_set[:, 0].reshape(-1, 1))
evaluation_set_open_scaled = sc_open.transform(evaluation_set[:, 0].reshape(-1, 1))


# Define Params for the generator
lookback = 300
batch_size = 10
# Use standard TimeseriesGenerator for training and evaluation
train_gen = TimeseriesGenerator(training_set_scaled, training_set_open_scaled, length=lookback, batch_size=batch_size)
eval_gen = TimeseriesGenerator(evaluation_set_scaled, evaluation_set_open_scaled, length=lookback, batch_size=batch_size)


# Learning rate reducer

reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.3, patience=6, min_lr=0.0001)


# Train the modele
model.fit(train_gen, epochs=9, callbacks=[checkpoint_callback, reduce_lr], validation_data=eval_gen)
tensorflow keras deep-learning time-series recurrent-neural-network
1个回答
0
投票

我认为,这只是一个加载问题,所以我尝试从保存的 h5 检查点文件中提取 LTC 层和密集层的唯一权重(由于数据集大小,每个周期需要一周时间) 。欢迎其他解决方法或解决方案!

以下是迄今为止的修改,实现了 LTC 层内形状的提取和匹配:

import h5py

# Open the .h5 file
filename = 'my_chunkedmodel_vertical_epoch_01.h5'  
with h5py.File(filename, 'r') as f:
    # Access the 'model_weights' group
    model_weights_group = f['model_weights']

    # Access the 'dense' and 'ltc' groups and list the keys in each
    dense_group = model_weights_group['dense/dense']
    ltc_cell_group = model_weights_group['ltc/ltc/ltc_cell']

    # Print the keys in the 'dense' group
    print("Keys in 'dense' group: ", list(dense_group.keys()))

    # Print the keys in the 'ltc_cell' group
    print("Keys in 'ltc_cell' group: ", list(ltc_cell_group.keys()))

    # Extract and save the weights from the 'dense' group
    dense_kernel_weights = dense_group['kernel:0'][()]
    dense_bias_weights = dense_group['bias:0'][()]
    np.save('dense_kernel_weights.npy', dense_kernel_weights)
    np.save('dense_bias_weights.npy', dense_bias_weights)

    # Extract and save the weights and parameters from the 'ltc_cell' group
    for key in ltc_cell_group.keys():
        weight = ltc_cell_group[key][()]
        np.save(f'ltc_cell_{key}.npy', weight)

    print("Weights from the 'dense' group and 'ltc_cell' group have been saved to numpy files.")


# Print current shapes of the weights in the LTC layer to match the order when loading
ltc_layer = model.layers[1].cell
current_weights = ltc_layer.get_weights()
for weight in current_weights:
    print(weight.shape)

# For the LTC layer
ltc_cell_weights = [
    np.load('ltc_cell_cm:0.npy'),
    np.load('ltc_cell_gleak:0.npy'),
    np.load('ltc_cell_vleak:0.npy'),
    np.load('ltc_cell_erev:0.npy'),
    np.load('ltc_cell_mu:0.npy'),
    np.load('ltc_cell_sigma:0.npy'),
    np.load('ltc_cell_w:0.npy'),
    np.load('ltc_cell_sensory_erev:0.npy'),
    np.load('ltc_cell_sensory_mu:0.npy'),
    np.load('ltc_cell_sensory_sigma:0.npy'),
    np.load('ltc_cell_sensory_w:0.npy'),
    np.load('ltc_cell_input_b:0.npy'),
    np.load('ltc_cell_input_w:0.npy'),
    np.load('ltc_cell_output_b:0.npy'),
    np.load('ltc_cell_output_w:0.npy'),
]


model.layers[1].cell.set_weights(ltc_cell_weights)


# For the dense layer
dense_kernel_weights = np.load('dense_kernel_weights.npy')
dense_bias_weights = np.load('dense_bias_weights.npy')
model.layers[3].set_weights([dense_kernel_weights, dense_bias_weights])
© www.soinside.com 2019 - 2024. All rights reserved.