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

视频字幕任务LSTM改GRU测试报广播形状不匹配ValueError问题

错误原因

你最初的推测方向正确,报错的根本原因是两个维度不匹配问题:

  • GRU层要求解码器的输入序列为三维张量(批量大小, 时间步, 词表大小),你当前构造的target_seq是二维张量(1,1500),缺少了时间步维度
  • 编码器输出的隐藏状态是单个张量,未封装为列表,和target_seq拼接时会触发维度广播错误
可行解决方案

按照以下3处修改代码即可解决问题:

1. 修正推断阶段target_seq的维度

修改greedy_search方法中target_seq的构造逻辑,补充时间步维度:

# 原代码
# target_seq = np.zeros((1, 1500))
# target_seq[0, self.tokenizer.word_index['bos']] = 1

# 修改后代码
target_seq = np.zeros((1, 1, 1500))
target_seq[0, 0, self.tokenizer.word_index['bos']] = 1

同时修改循环内重置target_seq的逻辑:

# 原代码
# target_seq = np.zeros((1, 1500))
# target_seq[0, y_hat] = 1

# 修改后代码
target_seq = np.zeros((1, 1, 1500))
target_seq[0, 0, y_hat] = 1

2. 封装编码器输出状态为列表

修改greedy_search方法中编码器预测后的状态处理逻辑:

states_value = self.inf_encoder_model.predict(f.reshape(-1, 80, 4096))
# 新增该行,将单个隐藏状态张量封装为列表
states_value = [states_value]

3. 清理推断解码器的冗余LSTM代码

修改load_inference_models方法,删除GRU不需要的cell状态输入定义,避免潜在的权重加载冲突:

# 原代码
# decoder_state_input_h = Input(shape=(self.latent_dim,))
# decoder_state_input_c = Input(shape=(self.latent_dim,))
# decoder_state_input=[decoder_state_input_h]

# 修改后代码
decoder_state_input_h = Input(shape=(self.latent_dim,))
decoder_state_input = [decoder_state_input_h]

修改完成后重新运行测试,解码器的输入维度就会匹配为[(1,1,1500), (1,512)],不会再触发广播错误。

内容的提问来源于stack exchange,提问作者Shahir

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 05:54:04