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

Python多输入数组构建神经网络报错:Sequential层预期1个输入却收到2个输入张量的问题咨询

Hey there, let's fix this problem step by step. The error you're seeing happens because you're using a Sequential model—great for simple single-input/single-output networks, but not built to handle multiple inputs. To make your multi-input (and 3-output) network work, we'll switch to Keras' Functional API, which is designed for this exact scenario.

Why the Error Pops Up

Your Sequential model is defined to expect 1 input tensor (with shape [2]), but when you run model.fit(x=[x,x2], ...), you're feeding it two separate input arrays. The model has no way to interpret this mismatch, hence the ValueError saying it received 2 inputs instead of the expected 1.

Fixed Code: 2 Inputs + 3 Outputs

Here's the corrected implementation that matches your requirement of multiple inputs and 3 outputs:

import keras
import numpy as np
import tensorflow as tf

# Step 1: Define separate input layers for each input source
input1 = tf.keras.layers.Input(shape=(1,))  # Each sample has 1 feature for input 1
input2 = tf.keras.layers.Input(shape=(1,))  # Each sample has 1 feature for input 2

# Step 2: Merge the two inputs into one tensor (concatenation works here)
merged_inputs = tf.keras.layers.concatenate([input1, input2])

# Step 3: Add your hidden dense layers
hidden_layer = tf.keras.layers.Dense(units=5, activation='relu')(merged_inputs)

# Step 4: Output layer with 3 units (for your 3-output requirement)
output_layer = tf.keras.layers.Dense(units=3)(hidden_layer)

# Step 5: Build the model by linking inputs to outputs
model = tf.keras.Model(inputs=[input1, input2], outputs=output_layer)

# Compile the model with your chosen optimizer and loss
model.compile(optimizer='sgd', loss='mean_squared_error')

# Prepare your data: reshape 1D arrays to 2D (required by Keras)
x = np.array([0.0,1.0,2.0,3.0,4.0,5.0,6.0]).reshape(-1, 1)
x2 = np.array([2.0,3.0,4.0,5.0,6.0,7.0,8.0]).reshape(-1, 1)

# For 3 outputs, your target data needs 3 values per sample
# Replace this with your actual 3-output labels
y = np.array([
    [1.0, 2.0, 3.0],
    [2.0, 3.0, 4.0],
    [3.0, 4.0, 5.0],
    [4.0, 5.0, 6.0],
    [5.0, 6.0, 7.0],
    [6.0, 7.0, 8.0],
    [7.0, 8.0, 9.0]
])

# Train the model with both inputs
model.fit(x=[x, x2], y=y, epochs=3000)

# Make predictions: pass both inputs as a list (match training format)
print(model.predict([np.array([10.0]).reshape(-1,1), np.array([12.0]).reshape(-1,1)]))

Key Changes Breakdown

  • Functional API Usage: We define explicit Input layers for each input, then merge them before feeding into hidden layers. This tells Keras exactly how many inputs to expect.
  • Data Reshaping: Keras requires input data to be 2D (shape (number_of_samples, number_of_features)), so we use reshape(-1,1) to convert your 1D arrays into the correct format.
  • 3-Output Setup: The final Dense layer has 3 units, and the target y array is updated to have 3 values per sample (adjust this to your actual output data).
  • Prediction Consistency: When making predictions, we pass both inputs just like we did during training to keep the input format consistent.

Quick Tips

  • If your inputs have different feature counts (e.g., one input has 2 features, another has 3), just adjust the shape parameter in the corresponding Input layer (e.g., shape=(2,)).
  • You can use other merging layers like Add or Multiply instead of concatenate if that aligns with your model's logic.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 18:03:10