I am trying to build an autoencoder using HyperModel to later perform hyperparameter tuning using RandomSearch. After training the autoencoder with my data (2 matrices of shape(600, 411001) where each row is a sample) I get the error message "ValueError: Received incompatible tensor with shape (352,) when attempting to restore variable with shape (64,) and name dense_1/bias:0."
Where does this 352 comes from? I am a beginner in this world and I would really appreciate ideas to fix the code.
train_matrix_noise = pd.read_csv('C:/Users/Student/Desktop/Lucia/Project/train_matrix_noise.txt', sep='\t', index_col= False, nrows = 600)
train_matrix_clean = pd.read_csv('C:/Users/Student/Desktop/Lucia/Project/train_matrix_clean.txt', sep='\t', index_col= False, nrows = 600)
test_matrix_noise = pd.read_csv('C:/Users/Student/Desktop/Lucia/Project/test_matrix_noise.txt', sep='\t', index_col=False, nrows = 300)
test_matrix_clean = pd.read_csv('C:/Users/Student/Desktop/Lucia/Project/test_matrix_clean.txt', sep='\t', index_col=False, nrows = 300)
input_shape = train_matrix_noise.shape[1] #411001
output_shape = input_shape #411001
class MyHyperModel(HyperModel):
def __init__(self, input_shape, output_shape):
self.input_shape = input_shape
self.output_shape = output_shape
def build(self, hp):
input_layer = Input(shape = (self.input_shape,))
hidden_out = hp.Choice('hidden_out', [256, 512])
hidden_in = hp.Choice('hidden_in', [64, 128])
lr = hp.Choice('lr', [0.01, 0.001])
hidden_layer = Dense(hidden_out, activation = 'relu')(input_layer)
hidden_layer = Dense(hidden_in, activation = 'relu')(hidden_layer)
hidden_layer = Dense(hidden_out, activation = 'relu')(hidden_layer)
output_layer = Dense(self.output_shape, activation = 'sigmoid')(hidden_layer)
model = Model(input_layer, output_layer)
model.compile(loss = keras.losses.CosineSimilarity(),
optimizer = keras.optimizers.Adam(learning_rate= lr),
metrics = keras.metrics.CosineSimilarity())
return model
hypermodel = MyHyperModel(input_shape, output_shape)
tuner = RandomSearch(hypermodel, objective='val_loss',max_trials=10,seed=42)
tuner.search(train_matrix_noise, train_matrix_clean,epochs=10,validation_data=(test_matrix_noise, test_matrix_clean))
best_model = tuner.get_best_models(num_models=1)[0]
tuner_grid = GridSearch(hypermodel,objective='val_loss', max_trials=10, seed=42) tuner_grid.search(train_matrix_noise, train_matrix_clean, epochs=10,validation_data=(test_matrix_noise, test_matrix_clean))
best_model = tuner_grid.get_best_models(num_models=1)[0]
Error Message
WARNING:tensorflow:Inconsistent references when loading the checkpoint into this object graph. For example, in the saved checkpoint object, model.layer.weight and model.layer_copy.weight reference the same variable, while in the current object these are two different variables. The referenced variables are:(<keras.layers.core.dense.Dense object at 0x000001BF86058E20> and <keras.engine.input_layer.InputLayer object at 0x000001BFDF5B5DC0>).WARNING:tensorflow:Inconsistent references when loading the checkpoint into this object graph. For example, in the saved checkpoint object, model.layer.weight and model.layer_copy.weight reference the same variable, while in the current object these are two different variables. The referenced variables are:(<keras.layers.core.dense.Dense object at 0x000001BF8424BA30> and <keras.layers.core.dense.Dense object at 0x000001BF86058E20>).WARNING:tensorflow:Inconsistent references when loading the checkpoint into this object graph. For example, in the saved checkpoint object, model.layer.weight and model.layer_copy.weight reference the same variable, while in the current object these are two different variables. The referenced variables are:(<keras.layers.core.dense.Dense object at 0x000001BFD088FB80> and <keras.layers.core.dense.Dense object at 0x000001BF8424BA30>).WARNING:tensorflow:Inconsistent references when loading the checkpoint into this object graph. For example, in the saved checkpoint object, model.layer.weight and model.layer_copy.weight reference the same variable, while in the current object these are two different variables. The referenced variables are:(<keras.layers.core.dense.Dense object at 0x000001BF8424B790> and <keras.layers.core.dense.Dense object at 0x000001BFD088FB80>).Traceback (most recent call last):
File "C:\Users\Student\AppData\Local\Temp\ipykernel_18148\2601419992.py", line 1, in <module>best_model = tuner_grid.get_best_models(num_models=1)[0]
File "C:\Users\Student\anaconda3\lib\site-packages\keras_tuner\engine\tuner.py", line 366, in get_best_modelsreturn super().get_best_models(num_models)
File "C:\Users\Student\anaconda3\lib\site-packages\keras_tuner\engine\base_tuner.py", line 364, in get_best_modelsmodels = [self.load_model(trial) for trial in best_trials]
File "C:\Users\Student\anaconda3\lib\site-packages\keras_tuner\engine\base_tuner.py", line 364, in <listcomp>models = [self.load_model(trial) for trial in best_trials]
File "C:\Users\Student\anaconda3\lib\site-packages\keras_tuner\engine\tuner.py", line 297, in load_modelmodel.load_weights(self._get_checkpoint_fname(trial.trial_id))
File "C:\Users\Student\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handlerraise e.with_traceback(filtered_tb) from None
File "C:\Users\Student\anaconda3\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 720, in _restore_from_tensorsraise ValueError(
ValueError: Received incompatible tensor with shape (352,) when attempting to restore variable with shape (64,) and name dense_1/bias:0.