You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Keras Tuner构建自编码器时遇张量形状不兼容错误求助

问题描述

使用HyperModel构建自编码器,通过RandomSearch和GridSearch做超参数调优,训练数据是两个形状为(600, 411001)的矩阵(每行对应一个样本)。训练完成后调用get_best_models时触发如下错误:

ValueError: Received incompatible tensor with shape (352,) when attempting to restore variable with shape (64,) and name dense_1/bias:0.

相关代码

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] 

完整错误信息

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_models
return 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_models
models = [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_model
model.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_handler
raise 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_tensors
raise ValueError(

ValueError: Received incompatible tensor with shape (352,) when attempting to restore variable with shape (64,) and name dense_1/bias:0.

问题分析:352的来源

这个错误的核心原因是两次调优任务(RandomSearch和GridSearch)共用了同一个HyperModel实例,且未指定独立的工作目录,导致训练生成的权重文件互相覆盖、混淆:

  • dense_1对应自编码器的中间隐藏层(Dense(hidden_in, activation='relu')),当超参数hidden_in选64时,该层的偏置(bias)形状为(64,);
  • 352是权重文件混淆后生成的错误形状:GridSearch加载模型时,误读取了RandomSearch阶段保存的权重文件,而两个阶段的模型超参数组合不同,权重张量被错误拼接,最终出现形状不匹配的冲突。

修复思路

1. 为每个tuner设置独立的工作目录

初始化RandomSearch和GridSearch时,指定不同的directory和project_name,彻底避免文件冲突:

# RandomSearch的独立存储目录
tuner = RandomSearch(
    hypermodel, 
    objective='val_loss',
    max_trials=10,
    seed=42,
    directory='./tuner_results',
    project_name='random_search_autoencoder'
)

# GridSearch的独立存储目录
tuner_grid = GridSearch(
    hypermodel,
    objective='val_loss', 
    max_trials=8,  # 超参数组合共2*2*2=8种,无需设为10
    seed=42,
    directory='./tuner_results',
    project_name='grid_search_autoencoder'
)

2. 为每个tuner创建独立的HyperModel实例(可选,更稳妥)

避免同一个HyperModel实例被重复使用导致的潜在状态污染:

# 为RandomSearch创建专属HyperModel实例
hypermodel_random = MyHyperModel(input_shape, output_shape)
tuner = RandomSearch(hypermodel_random, ...)

# 为GridSearch创建专属HyperModel实例
hypermodel_grid = MyHyperModel(input_shape, output_shape)
tuner_grid = GridSearch(hypermodel_grid, ...)

3. 清理旧调优缓存

删除之前自动生成的keras_tuner相关目录,清除残留的旧权重文件,避免干扰新训练。


内容的提问来源于stack exchange,提问作者Lucía Santadino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 20:47:03