odbhut.shei.chhele
odbhut.shei.chhele

Reputation: 6224

Cannot have names for input and output in keras model.fit

In short: Why does this line work -

model.fit(x_train, y_train, epochs=30, batch_size=40, verbose=2)

And this line doesn't

model.fit({"word_input": x_train, "main_output": y_train}, epochs=30, batch_size=40, verbose=2)

Further explanation

I am trying to implement LSTM with keras. I have written the following code

word_input = Input(shape=(mxlen,), dtype="int32", name="word_input")
x1 = Embedding(len(vocab), 100, input_length=mxlen, weights=[embeddings], trainable=False)(word_input)
x1 = LSTM(100)(x1)

y = Dense(6, activation="softmax", name="main_output")(x1)

model = Model(inputs=[word_input], outputs=[y])
adam = optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False)
model.compile(optimizer=adam,loss="categorical_crossentropy",metrics=["categorical_accuracy"]) # have to look into it
model.fit({"word_input": x_train, "main_output": y_train}, epochs=30, batch_size=40, verbose=2)

I am getting an error in the last line saying "ValueError: No gradients provided for any variable". But the exact code works when I change the last line to this

model.fit(x_train, y_train, epochs=30, batch_size=40, verbose=2)

The reason I want to have names or labels for the input and output in the model.fit is because I want to have mutiple inputs. I got the idea of having labels from the following line of code

model.fit(
        {'main_input': X_train, 'pos_input': X_train_pos, 
         'aux_input': X_train_meta, 'dep_input': X_train_dep},
        {'main_output': Y_train}, epochs = num_epochs, batch_size = batch_size,
        validation_data = (
            {'main_input': X_val, 'pos_input': X_val_pos, 
             'aux_input': X_val_meta, 'dep_input' : X_val_dep},
            {'main_output': Y_val}
        ), callbacks=[csv_logger,checkpoint])

The entire error screenshot is given below

enter image description here

Upvotes: 0

Views: 1105

Answers (1)

CypherX
CypherX

Reputation: 7353

Solution

It appears that this is an open-github-issue for tensorflow, currently under development and named-output has not yet been implemented using a dict.

The documentation for Model.fit() shows that currently the input, x could be a named-input by using a dict, however, the output, y does not accept any dict yet.

fit(
    x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None,
    validation_split=0.0, validation_data=None, shuffle=True, class_weight=None,
    sample_weight=None, initial_epoch=0, steps_per_epoch=None,
    validation_steps=None, validation_freq=1, max_queue_size=10, workers=1,
    use_multiprocessing=False, **kwargs
)

Where,

INPUT, x = either a numpy array/list-of-arrays or a tensor or a dict or a generator.

A dict mapping input names to the corresponding array/tensors, if the model has named inputs.

OUTPUT, y = either a numpy array or tensor or not specified when x is a generator.


For Quick Reference

enter image description here

Upvotes: 2

Related Questions