taurus
taurus

Reputation: 490

Keras InvalidArgumentError With Model.Fit()

I am trying to call model.fit() on a sequential Keras model, but get this error:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-30-3fc420144082> in <module>
     15     return model
     16 
---> 17 trained_model = build_model()

<ipython-input-30-3fc420144082> in build_model()
     10     # fit model
     11     es = tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=1)
---> 12     model.fit(train_data[0], train_data[1], epochs=100,verbose=1)
     13     # validation_data = (val_data[0], val_data[1])
     14     print(model.summary())

~/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, max_queue_size, workers, use_multiprocessing, **kwargs)
    878           initial_epoch=initial_epoch,
    879           steps_per_epoch=steps_per_epoch,
--> 880           validation_steps=validation_steps)
    881 
    882   def evaluate(self,

~/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, mode, validation_in_fit, **kwargs)
    327 
    328         # Get outputs.
--> 329         batch_outs = f(ins_batch)
    330         if not isinstance(batch_outs, list):
    331           batch_outs = [batch_outs]

~/.local/lib/python3.6/site-packages/tensorflow/python/keras/backend.py in __call__(self, inputs)
   3074 
   3075     fetched = self._callable_fn(*array_vals,
-> 3076                                 run_metadata=self.run_metadata)
   3077     self._call_fetch_callbacks(fetched[-len(self._fetches):])
   3078     return nest.pack_sequence_as(self._outputs_structure,

~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
   1437           ret = tf_session.TF_SessionRunCallable(
   1438               self._session._session, self._handle, args, status,
-> 1439               run_metadata_ptr)
   1440         if run_metadata:
   1441           proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~/.local/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    526             None, None,
    527             compat.as_text(c_api.TF_Message(self.status.status)),
--> 528             c_api.TF_GetCode(self.status.status))
    529     # Delete the underlying status object from memory otherwise it stays alive
    530     # as there is a reference to status from this from the traceback due to

InvalidArgumentError: data[0].shape = [3] does not start with indices[0].shape = [2]
     [[{{node training_40/Adam/gradients/loss_21/dense_21_loss/MeanSquaredError/Mean_grad/DynamicStitch}}]]

I have created a set of training points, each 1 x 3, called by train_data[0] and a set of training labels, each 1x1 called by train_data[1]. This is the code I use to build the model:

def build_model():
    '''
    Function to build a LSTM RNN model that takes in quantitiy, converted week; outputs predicted price
    '''
    # define model
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.LSTM(128, activation='relu', input_shape=(num_steps,num_features*input_size)))
    model.add(tf.keras.layers.Dense(input_size))
    model.compile(optimizer='adam', loss='mse')
    # fit model
    es = tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=1)
    model.fit(train_data[0], train_data[1], epochs=100,verbose=1)
    # validation_data = (val_data[0], val_data[1])
    print(model.summary())
    return model

trained_model = build_model()

I'm unsure of why, but when I call model.fit(train_data, epochs = 100) and do not break it up into points and labels, everything works well. Any insights would be much appreciated!

Upvotes: 1

Views: 7138

Answers (1)

greyside
greyside

Reputation: 151

It makes sense according to tensorflow's documentation for tf.keras.models.Model:

https://www.tensorflow.org/api_docs/python/tf/keras/models/Model#fit

 fit(x=None, y=None, batch_size=None, epochs=1, ...)

It precises:

y: Target data. Like the input data x, it could be either Numpy array(s) or TensorFlow tensor(s). It should be consistent with x (you cannot have Numpy inputs and tensor targets, or inversely). If x is a dataset, dataset iterator, generator, or keras.utils.Sequence instance, y should not be specified (since targets will be obtained from x).

Your lstm being a sequential model, i guess you prepared the train_data to be of type keras.utils.Sequence ?

Please also be aware of your tensorflow version, the documentation link above is for r1.13

Edit:

Try to prepare your dataset this way:

features_type = tf.float32
target_type = tf.int32

train_dataset = tf.data.Dataset.from_tensor_slices(
    tf.cast(train_data[0].values, features_type),
    tf.cast(train_data[1].values, target_type)
)

model.fit(train_dataset, epochs=100, verbose=1)

Make sure you adapt features_type (all features casted as float32) and target_type (int32 for classification) to your the problem you're currently aiming to solve.

Upvotes: 2

Related Questions