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

cvnn库复值神经网络中ComplexInput的shape参数报错问题

解决cvnn ComplexInput层shape参数报错及输入数据不匹配问题

问题分析

  1. 输入数据格式不匹配:你定义的模型期望接收形状为(2,)的单输入向量,但训练时传入的是两个独立的一维数组[data_train_x, data_train_y],和模型输入要求完全不符。
  2. cvnn与新版Keras兼容性问题:cvnn的ComplexInput层内部仍在使用已被Keras弃用的input_shape参数,导致新版Keras抛出"You must pass a shape argument"错误。

解决方案

步骤1:合并输入数据

将独立的x、y输入数组合并为形状为(样本数, 2)的复数数组,匹配模型期望的输入形状:

# 合并训练输入为(18, 2)的复数数组
data_train_input = np.stack([data_train_x, data_train_y], axis=1)
# 合并测试输入为(12, 2)的复数数组
data_test_input = np.stack([data_test_x, data_test_y], axis=1)

步骤2:替换ComplexInput为Keras原生Input层

用TensorFlow Keras原生的Input层替代cvnn的ComplexInput,彻底规避兼容性问题,同时保留复数数据类型:

model = tf.keras.models.Sequential([
    tf.keras.Input(shape=(2,), dtype='complex64'),  # 替换原ComplexInput层
    complex_layers.ComplexDense(2, activation='linear'),
    custom_output_layer(1)
])

步骤3:调整训练与预测的输入参数

使用合并后的输入数组进行训练和预测,确保数据格式与模型输入一致:

# 编译并训练模型
model.compile(optimizer='SGD', loss=l2_loss_function)
model.fit(data_train_input, data_train_z, epochs=100)

# 生成预测结果
predictions = model.predict(data_test_input)

额外检查点

  • 确认自定义输出层custom_output_layer能够正确处理复数张量输入,输出与标签data_train_z同类型的复数结果。
  • 如果坚持使用cvnn的ComplexInput,可以尝试降级cvnn到与当前Keras版本兼容的旧版本(不推荐,原生Input层稳定性更高)。

完整修改后的核心代码片段:

# 数据合并
data_train_input = np.stack([data_train_x, data_train_y], axis=1)
data_test_input = np.stack([data_test_x, data_test_y], axis=1)

# 模型定义
model = tf.keras.models.Sequential([
    tf.keras.Input(shape=(2,), dtype='complex64'),
    complex_layers.ComplexDense(2, activation='linear'),
    custom_output_layer(1)
])

# 训练与预测
model.compile(optimizer='SGD', loss=l2_loss_function)
model.fit(data_train_input, data_train_z, epochs=100)
predictions = model.predict(data_test_input)
print("predictions: ", predictions, "\n real data: ", data_test_z)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 09:02:32