37859

What should the generator return if it is used in a multi-model functional API?

Question:

Following <a href="https://blog.keras.io/a-ten-minute-introduction-to-sequence-to-sequence-learning-in-keras.html" rel="nofollow">this article</a>, 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)

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

Answer1:

Since your model has two inputs and one output, the generator should return a tuple with two elements where the first element is a <strong>list</strong> 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

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]

Recommend

  • Keras throws `'Tensor' object has no attribute '_keras_shape'` when splitting a
  • Keras function api, setting weight manually to a layer
  • HTML text editor for webpage
  • how to assign option string containing to editions value attribute of jqGrid
  • Cut off UITableViewController when embedded into a Container in a UIViewController
  • “mvn: not found” in Jenkins slave
  • Python Scrapy Get HTML tag
  • Passing multiple parameters to Crystal Report
  • How do i join the rows into dynamic columns in this case? [duplicate]
  • Cannot log into SQL Server in mssql-server-linux container
  • Recursive function for matrix determinant calculator
  • Exception: System.ArgumentException: Keyword not supported: 'initial catalog' when hard co
  • git pull add, commit and push in java [closed]
  • SQL Server 2008 - Attach mdf without log - Operating system error 5: 5(Access is denied.)
  • SQLException: the result set is closed
  • Entity Framework Connection String Definition
  • Using mgsub function with word boundaries for replacement values
  • create a dynamic SUM column in SQL
  • Dynamically populate a dropdown box with Jquery and Java
  • Ruby - passing a method to a DLL library as a callback
  • Rectangular Nesting - Convergence to optimal solution using Simulated Annealing
  • How to display rendered html content in text widget of tkinter in python 3.4.x
  • handling sigterm in OSx
  • Grails error: table or view does not exist
  • Why would you want to use composition in golang?
  • FSC: Error FS2024: Static linking may not use assembly that targets different profile
  • how to edit project settings in xcode 4.2?
  • dbus Variant: How to preserve boolean datatype in Python?
  • Skip a project while building a solution using msbuild 3.5
  • Base64 as method of sanitizing user input for Mysql
  • Cythonized function unexpectedly slow
  • How to reshape a 3D numpy array?
  • How to change placeholder text in an autocomplete activity of android google place?
  • Unix Network Programming Clarification
  • FFmpeg Conversion Error
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • what is the difference between the asp.net mvc application and asp.net web application
  • Matrix multiplication with MKL
  • Binding checkboxes to object values in AngularJs