具有多个输入的 Keras 序列模型 [英] Keras Sequential model with multiple inputs
问题描述
我正在制作一个接受两个输入并产生一个输出的 MLP 模型.
我有两个输入数组(每个输入一个)和 1 个输出数组.神经网络有 1 个隐藏层和 2 个神经元.每个数组有 336 个元素.
model0 = keras.Sequential([keras.layers.Dense(2, input_dim=2, activation=keras.activations.sigmoid, use_bias=True),keras.layers.Dense(1, activation=keras.activations.relu, use_bias=True),])#编译神经网络#模型0.编译(优化器 = keras.optimizers.RMSprop(lr=0.02,rho=0.9,epsilon=None,decay=0),损失 = 'mean_squared_error',指标=['准确度'])
我试了两种方法,都报错.
model0.fit(numpy.array([array_1, array_2]),output,batch_size=16,epochs=100)
<块引用>
ValueError: 检查输入时出错:预期dense_input 具有形状(2,) 但得到形状为(336,) 的数组
第二种方式:
model0.fit([array_1, array_2],output, batch_size=16, epochs=100)
<块引用>
ValueError:检查模型输入时出错:您传递给模型的 Numpy 数组列表不是模型预期的大小.预计会看到 1 个数组,但得到了以下 2 个数组的列表:
类似问题.但不使用顺序模型.
要解决此问题,您有两种选择.
1.使用顺序模型
在馈入网络之前,您可以将两个数组连接为一个.假设两个数组的形状为 (Number_data_points, ),现在可以使用 numpy.stack
方法合并数组.
merged_array = np.stack([array_1, array_2],axis=1)
model0 = keras.Sequential([keras.layers.Dense(2, input_dim=2, activation=keras.activations.sigmoid, use_bias=True),keras.layers.Dense(1, activation=keras.activations.relu, use_bias=True),])model0.fit(merged_array,输出,batch_size=16,epochs=100)
2.使用函数式 API.
当模型有多个输入时,这是最推荐使用的方法.
input1 = keras.layers.Input(shape=(1, ))input2 = keras.layers.Input(shape=(1,))合并 = keras.layers.Concatenate(axis=1)([input1, input2])Dense1 = keras.layers.Dense(2, input_dim=2, activation=keras.activations.sigmoid, use_bias=True)(合并)输出 = keras.layers.Dense(1, activation=keras.activations.relu, use_bias=True)(dense1)模型10 = keras.models.Model(输入=[输入1,输入2],输出=输出)
现在您可以使用第二种方法来拟合模型
model0.fit([array_1, array_2],output, batch_size=16, epochs=100)
I am making a MLP model which takes two inputs and produces a single output.
I have two input arrays (one for each input) and 1 output array. The neural network has 1 hidden layer with 2 neurons. Each array has 336 elements.
model0 = keras.Sequential([
keras.layers.Dense(2, input_dim=2, activation=keras.activations.sigmoid, use_bias=True),
keras.layers.Dense(1, activation=keras.activations.relu, use_bias=True),
])
# Compile the neural network #
model0.compile(
optimizer = keras.optimizers.RMSprop(lr=0.02,rho=0.9,epsilon=None,decay=0),
loss = 'mean_squared_error',
metrics=['accuracy']
)
I tried two ways, both of them are giving errors.
model0.fit(numpy.array([array_1, array_2]),output, batch_size=16, epochs=100)
ValueError: Error when checking input: expected dense_input to have shape (2,) but got array with shape (336,)
The second way:
model0.fit([array_1, array_2],output, batch_size=16, epochs=100)
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 2 arrays:
Similar question. But not using sequential model.
To solve this problem you have two options.
1. Using a sequential model
You can concatenate both arrays into one before feeding to the network. Let's assume the two arrays have a shape of (Number_data_points, ), now the arrays can be merged using numpy.stack
method.
merged_array = np.stack([array_1, array_2], axis=1)
model0 = keras.Sequential([
keras.layers.Dense(2, input_dim=2, activation=keras.activations.sigmoid, use_bias=True),
keras.layers.Dense(1, activation=keras.activations.relu, use_bias=True),
])
model0.fit(merged_array,output, batch_size=16, epochs=100)
2. Using Functional API.
This is the most recommened way to use when there are multiple inputs to the model.
input1 = keras.layers.Input(shape=(1, ))
input2 = keras.layers.Input(shape=(1,))
merged = keras.layers.Concatenate(axis=1)([input1, input2])
dense1 = keras.layers.Dense(2, input_dim=2, activation=keras.activations.sigmoid, use_bias=True)(merged)
output = keras.layers.Dense(1, activation=keras.activations.relu, use_bias=True)(dense1)
model10 = keras.models.Model(inputs=[input1, input2], output=output)
Now you can use the second method you have trying to fit to the model
model0.fit([array_1, array_2],output, batch_size=16, epochs=100)
这篇关于具有多个输入的 Keras 序列模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!