python - keras 中多输入数组的形状错误
问题描述
我正在尝试使用 keras 功能 api 构建多输入网络。现在我被卡住了,因为我得到了错误
ValueError:检查输入时出错:预期 input_1 的形状为 (5,),但得到的数组的形状为 (1,)
鉴于我的代码(如下),我看不出这是怎么可能的。我传递的数组肯定具有我的网络指定的形状 (5,) 和 (15,)。
def buildModel():
# define two sets of inputs
inputA = ls.Input(shape=(5,))
inputB = ls.Input(shape=(15,))
# the first branch operates on the first input
x = ls.Dense(8, activation="relu")(inputA)
x = ls.Dense(4, activation="relu")(x)
x = ks.Model(inputs=inputA, outputs=x)
# the second branch opreates on the second input
y = ls.Dense(64, activation="relu")(inputB)
y = ls.Dense(32, activation="relu")(y)
y = ls.Dense(4, activation="relu")(y)
y = ks.Model(inputs=inputB, outputs=y)
# combine the output of the two branches
combined = ls.concatenate([x.output, y.output])
# apply a FC layer and then a regression prediction on the
# combined outputs
z1 = ls.Dense(2, activation="relu")(combined)
z1 = ls.Dense(5, activation="relu")(z1)
z2 = ls.Dense(2, activation="relu")(combined)
z2 = ls.Dense(15, activation="relu")(z2)
# our model will accept the inputs of the two branches and
# then output a single value
model = ks.Model(inputs=[x.input, y.input], outputs=[z1, z2])
model.compile(optimizer='adam', loss='poisson', metrics=['accuracy'])
return model
def train_model(model, x1, x2, y1, y2):
print(x1.shape)
model.fit([x1, x2], [y1, y2], batch_size=1, epochs=100) # Error raised here
if __name__ == "__main__":
mod = buildModel()
x1 = np.array([5, 2, 1, 4, 5])
x2 = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
y1 = np.array([0, 0, 1, 0, 0])
y2 = np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
train_model(mod, x1, x2, y1, y2)
通过的数组仅用于测试目的,人工智能最终会解决一个真正的问题。
如果您认为问题没有明确定义或需要更多规范,请告诉我。
编辑:这是回溯:
Traceback (most recent call last):
File "/Users/henrihlo/project/ai.py", line 70, in <module>
train_model(mod, x1, x2, y1, y2)
File "/Users/henrihlo/project/ai.py", line 49, in train_model
model.fit([x1, x2], [y1, y2], batch_size=1, epochs=100)
File "/Users/henrihlo/anaconda3/lib/python3.7/site packages/keras/engine/training.py", line 1154, in fit
batch_size=batch_size)
File "/Users/henrihlo/anaconda3/lib/python3.7/site packages/keras/engine/training.py", line 579, in _standardize_user_data
exception_prefix='input')
File "/Users/henrihlo/anaconda3/lib/python3.7/site packages/keras/engine/training_utils.py", line 145, in
standardize_input_data
str(data_shape))
ValueError: Error when checking input: expected input_1 to have shape (5,) but got array with shape (1,)
打印 x1 的形状得到 (5,)。
解决方案
you need to feed keras model with 2D array (n_sample, n_feat)
. A simple reshape in your case does the trick
mod = buildModel()
x1 = np.array([5, 2, 1, 4, 5]).reshape(1,-1)
x2 = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]).reshape(1,-1)
y1 = np.array([0, 0, 1, 0, 0]).reshape(1,-1)
y2 = np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).reshape(1,-1)
train_model(mod, x1, x2, y1, y2)
推荐阅读
- c# - 如果我不需要结果值,哪个更有效:线程或任务?
- stripe-payments - 什么是 batch.created 和 batch.updated webhook 事件?
- spring-batch - Spring Batch 元数据数据库事务问题
- regex - 匹配的花括号之间的 sed 匹配
- python - 关于在python中打乱列表的问题
- reactjs - 反应:用状态和钩子创建动态组件?
- beagleboneblack - beagle bone black 中的 SYS_BOOT 寄存器是什么?SYS_BOOT[4:0] 、 SYS_BOOT[15:0] 、 SYS_BOOT[15:14] 寄存器的意义是什么?
- sql - 按和 FOR XML PATH 分组
- amazon-web-services - 如何在 dynamodb 表中包含非键属性并使用 batch_writer 为其赋值?
- java - 注册后如何修复向多对多添加实体