如果在多模型功能API中使用生成器,应该返回什么? [英] What should the generator return if it is used in a multi-model functional API?

查看:77
本文介绍了如果在多模型功能API中使用生成器,应该返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

跟随本文,我正在尝试实现生成型RNN.在提到的文章中,训练和验证数据作为完全加载的np.array传递.但是我正在尝试使用model.fit_generator方法并提供一个生成器.

Following this article, I'm trying to implement a generative RNN. In the mentioned article, the training and validation data are passed as fully loaded np.arrays. But I'm trying to use the model.fit_generator method and provide a generator instead.

我知道,如果这是一个简单的模型,则生成器应返回:

I know that if it was a straightforward model, the generator should return:

def generator():
    ...
    yield (samples, targets)

但这是一个生成模型,这意味着涉及两个模型:

But this is a generative model which means there are two models involved:

encoder_inputs = Input(shape=(None,))
x = Embedding(num_encoder_tokens, embedding_dim)(encoder_inputs)
x.set_weights([embedding_matrix])
x.trainable = False
x, state_h, state_c = LSTM(embedding_dim, return_state=True)(x)
encoder_states = [state_h, state_c]

decoder_inputs = Input(shape=(None,))
x = Embedding(num_decoder_tokens, embedding_dim)(decoder_inputs)
x.set_weights([embedding_matrix])
x.trainable = False
x = LSTM(embedding_dim, return_sequences=True)(x, initial_state=encoder_states)
decoder_outputs = Dense(num_decoder_tokens, activation='softmax')(x)

model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

model.fit([encoder_input_data, decoder_input_data], decoder_target_data,
          batch_size=batch_size,
          epochs=epochs,
          validation_split=0.2)

如前所述,我正在尝试使用生成器:

As mentioned before, I'm trying to use a generator:

model.fit_generator(generator(),
                   steps_per_epoch=500,
                   epochs=20,
                   validation_data=generator(),
                   validation_steps=val_steps)

但是generator()应该返回什么?我有点困惑,因为有两个输入集合和一个目标.

But what should the generator() return? I'm a little confused since there are two input collections and one target.

推荐答案

由于模型具有两个输入和一个输出,因此生成器应返回包含两个元素的元组,其中第一个元素是列表包含两个数组,分别对应于两个输入层,第二个元素是对应于输出层的数组:

Since your model has two inputs and one output, the generator should return a tuple with two elements where the first element is a list containing two arrays, which corresponds to two input layers, and the second element is an array corresponding to output layer:

def generator():
    ...
    yield [input_samples1, input_samples2], targets

通常,在具有M输入和N输出的模型中,生成器应返回两个列表的元组,其中第一个具有M数组,第二个具有N数组:

Generally, in a model with M inputs and N outputs, the generator should return a tuple of two lists where the first one has M arrays and the second one has N arrays:

def generator():
        ...
        yield [in1, in2, ..., inM], [out1, out2, ..., outN]

这篇关于如果在多模型功能API中使用生成器,应该返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆