首页 > 解决方案 > 模型拟合错误 数据基数不明确

问题描述

我正在尝试创建具有多个输入分支的 keras 模型,但 keras 不喜欢输入具有不同的大小。

import numpy as np
from tensorflow import keras
from tensorflow.keras import layers


inputA = layers.Input(shape=(2,))
xA = layers.Dense(8, activation='relu')(inputA)

inputB = layers.Input(shape=(3,))
xB = layers.Dense(8, activation='relu')(inputB)

merged = layers.Concatenate()([xA, xB])

output = layers.Dense(8, activation='linear')(merged)    

model = keras.Model(inputs=[inputA, inputB], outputs=output)


display(X1)
display(X1[0])
display(y[0]) 

model.fit([X1, X2],y)

X1,X2,y 的输出:

array([[0.10078526, 0.08405071, 0.02819094, ..., 0.35036755, 0.24952677,
        0.20884278]], dtype=float32)
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
      dtype=int8)
array([0, 0, 0, ..., 0, 0, 0], dtype=int8)

导致错误:

ValueError: Data cardinality is ambiguous:
  x sizes: 1, 2
  y sizes: 1
Make sure all arrays contain the same number of samples.

标签: pythonkerasdeep-learning

解决方案


推荐阅读