具有多个输入的Keras顺序模型 [英] Keras Sequential model with multiple inputs

查看:94
本文介绍了具有多个输入的Keras顺序模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个MLP模型,该模型需要两个输入并产生一个输出.

I am making a MLP model which takes two inputs and produces a single output.

我有两个输入数组(每个输入一个)和一个输出数组.神经网络具有1个具有2个神经元的隐藏层.每个数组有336个元素.

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:检查输入时出错:预期density_input具有形状(2,)但具有形状为(336,)的数组

ValueError: Error when checking input: expected dense_input to have shape (2,) but got array with shape (336,)

第二种方式:

model0.fit([array_1, array_2],output, batch_size=16, epochs=100)

ValueError:检查模型输入时出错:传递给模型的Numpy数组列表不是模型预期的大小.预计会看到1个数组,但获得了以下2个数组的列表:

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.使用顺序模型

您可以将两个阵列连接成一个阵列,然后再馈入网络.假设两个数组的形状为(Number_data_points,),现在可以使用numpy.stack方法合并这些数组.

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.使用功能性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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆