我正在使用 Keras Functional API 进行测试,因为我需要将模型迁移到 Tensorflow LITE。我构建了一个具有 3 个输入和 3 个输出的模型。如果所有输入具有相同数量的观测值,则该模型有效。我不明白这一点,因为它们是独立的。
ValueError: All input arrays (x) should have the same number of samples. Got array shapes: [(10, 5), (20, 5), (30, 5)
我想建立一个模型,其中包含多个具有不同观察数量的输入。那可能吗?
import numpy as np
from keras.layers import Input, Dense
from keras.models import Model
capas = 3
data = [ np.random.random(size=(50,5)) for i in range(3)]
labels = [ np.random.random(size=(50,2)) for i in range(3)]
visible=[]
preds=[]
for i in range( capas):
visible.append(Input(shape=(5,)))
x=Dense(5, activation='relu')(visible[i])
x=Dense(10, activation='relu')(x)
preds.append( Dense(2)(x))
model = Model(inputs=visible,output=preds)
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['accuracy'])
model.fit(data, labels,epochs=50)
子模型是否每个独立都没有关系,因为如果你制作一个多输入多输出模型,它是通过将每个模型的损失组合(加权)到执行梯度下降的单个损失来训练的,这需要每个输入和输出中的样本数量相同。
既然你说模型都是独立的,那么你可以独立训练它们,然后制作一个新模型,将三个模型(及其训练的权重)与多个输入和输出结合起来。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句