python - 模型拟合错误 数据基数不明确
问题描述
我正在尝试创建具有多个输入分支的 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.
解决方案
推荐阅读
- python - 重试失败的 sqlalchemy 查询
- javascript - 如何在maven项目中使用javascript文件?
- windows - java.lang.RuntimeException: 无法启动 redis 服务器。检查日志以获取详细信息
- html - 在 Firefox 中显示顶部而不是底部的下拉菜单
- python - Python将“True”和“False”解析为字符串
- go - GO中字符串的ASCII编码
- c# - 操作来自 App.config 的 url 的正确方法
- javascript - 在 javascript 中,是否使用 eval() 将下拉列表中选择的文本与数组相匹配,或者我错过了更好的方法来做到这一点?
- javascript - 使用 Javascript 从数据中过滤选定的子项
- ios - Xcode 中的自动布局失败。不小心禁用了,如何恢复原状?