ValueError:数据基数不明确.请提供具有相同第一维度的数据 [英] ValueError: Data cardinality is ambiguous. Please provide data which shares the same first dimension
本文介绍了ValueError:数据基数不明确.请提供具有相同第一维度的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建具有多个输入分支的keras模型,但是keras不喜欢输入具有不同的大小.
I'm trying to create a keras model with multiple input branches, but keras doesn't like that the inputs have different sizes.
这是一个最小的示例:
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)
a = np.array([1, 2])
b = np.array([3, 4, 5])
model.predict([a, b])
哪个会导致错误:
ValueError: Data cardinality is ambiguous:
x sizes: 2, 3
Please provide data which shares the same first dimension.
在喀拉拉邦有更好的方法吗?我已经阅读了其他参考相同错误的问题,但我不太了解需要更改的内容.
Is there a better way to do this in keras? I've read the other questions referencing the same error, but I'm not really understanding what I need to change.
推荐答案
您需要以正确的格式传递数组...(n_batch,n_feat).一个简单的重塑就足以创建批处理尺寸
you need to pass array in the correct format... (n_batch, n_feat). A simple reshape is sufficient to create the batch dimensionality
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)
a = np.array([1, 2]).reshape(1,-1)
b = np.array([3, 4, 5]).reshape(1,-1)
model.predict([a, b])
这篇关于ValueError:数据基数不明确.请提供具有相同第一维度的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文