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

运行fit函数时输入输出维度不匹配的ValueError求助

解决输入输出形状不匹配的方案

根据错误信息,模型输出形状为[?,1972,1],但你的目标数据y形状是[?,496,1],由于模型无法修改,只能调整目标数据的形状来匹配模型输出:

方法:对目标数据进行长度插值调整

使用TensorFlow的插值函数将y的序列长度从496调整为1972,代码如下:

import tensorflow as tf

def resize_target_sequence(y_data, target_length):
    # 去掉最后一个维度,变为(样本数, 496)
    y_squeezed = tf.squeeze(y_data, axis=-1)
    # 增加维度适配插值接口:(样本数, 1, 496)
    y_expanded = tf.expand_dims(y_squeezed, axis=1)
    # 插值调整长度到目标值
    y_resized = tf.image.resize(y_expanded, [1, target_length], method=tf.image.ResizeMethod.BILINEAR)
    # 恢复通道维度:(样本数, target_length, 1)
    return tf.expand_dims(y_resized, axis=-1)

# 调整所有目标数据集的形状
y_train1_resized = resize_target_sequence(y_train1_padded, 1972)
y_val1_resized = resize_target_sequence(y_val1_padded, 1972)
y_test1_resized = resize_target_sequence(y_test1_padded, 1972)

# 使用调整后的目标数据训练模型
model.fit(X_train1_padded, y_train1_resized, epochs=50, batch_size=16, 
          validation_data=(X_val1_padded, y_val1_resized), shuffle=True)

原理说明

你的模型输出长度计算逻辑:

  1. 输入经过两次MaxPooling1D(pool_size=2)后长度变为原长度的1/4(因padding="same")
  2. 全连接层后Reshape为(sequence_len-3, 8),再经过两次UpSampling1D(size=2),最终输出长度为4*(sequence_len-3)
  3. 当sequence_len=496时,输出长度为4*(496-3)=1972,因此需要将目标数据的长度同步调整为1972才能匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 20:34:57