【问题标题】:Build a model with Keras Functional API for Tensorflow LITE使用 Keras 功能 API 为 Tensorflow LITE 构建模型
【发布时间】:2019-12-11 15:16:05
【问题描述】:

我正在使用 Keras 功能 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)

【问题讨论】:

    标签: python keras tensorflow-lite


    【解决方案1】:

    子模型是否独立无关紧要,因为如果你制作一个多输入多输出模型,它是通过将每个模型的损失组合(加权)成一个单一的损失来训练的,从哪里梯度下降执行,这要求每个输入和输出中的样本数量相同。

    既然你说模型每个都是独立的,你可以独立训练它们,然后制作一个新模型,将三个模型(及其训练的权重)与多个输入和输出相结合。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多