简体   繁体   中英

Build a model with Keras Functional API for Tensorflow LITE

I was testing with Keras Functional API because I need to migrate a model to Tensorflow LITE. I built a model with 3 inputs and 3 outputs. The model works if all inputs have the same number of observations. I don't understand that point because the are independent.

ValueError: All input arrays (x) should have the same number of samples. Got array shapes: [(10, 5), (20, 5), (30, 5)

I would like to build a model with several inputs with different number of observations. Is that possible?

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)

It does not matter if the sub-models are each independent, because if you make a multi-input multi-output model, it is trained by combining (weighting) the losses of each model into a single loss from where gradient descent is performed, and this requires the same number of samples in each input and output.

Since you say that the models are each independent, you can train them independently, and then make a new model that combines the three models (and their trained weights) with multiple inputs and outputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM