1
$\begingroup$

I am trying to train a model using Tensorflow. I am reading a huge csv file using tf.data.experimental.make_csv_dataset

Here is my code:

Imports:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing

LABEL_COLUMN = 'venda_qtde'

Reading a csv into a tf.data.Dataset:

def get_dataset(file_path, **kwargs):
  dataset = tf.data.experimental.make_csv_dataset(
      file_path,
      batch_size=4096, 
      na_value="?",
      label_name=LABEL_COLUMN,
      num_epochs=1,
      ignore_errors=False,
      shuffle=False,
      **kwargs)
  return dataset

Buiding a model instance:

def build_model():
  model = None
  model = keras.Sequential([
    layers.Dense(520, activation='relu'),
    layers.Dense(520, activation='relu'),
    layers.Dense(520, activation='relu'),
    layers.Dense(1)
  ])

  model.compile(loss='mean_squared_error',
                optimizer='adam',
                metrics=['mae'])
  return model

Executing the functions:

ds_treino = get_dataset('data/processed/curva_a/curva_a_train.csv')
nn_model = build_model()
nn_model.fit(ds_treino, epochs=10)

But when the fit function is called, I get the error:

ValueError: in user code:

    /home/machine-learning/.virtualenvs/jupyter-n5c7sT9n/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:806 train_function  *
        return step_function(self, iterator)
    /home/machine-learning/.virtualenvs/jupyter-n5c7sT9n/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:796 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /home/machine-learning/.virtualenvs/jupyter-n5c7sT9n/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /home/machine-learning/.virtualenvs/jupyter-n5c7sT9n/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /home/machine-learning/.virtualenvs/jupyter-n5c7sT9n/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
        return fn(*args, **kwargs)
    /home/machine-learning/.virtualenvs/jupyter-n5c7sT9n/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:789 run_step  **
        outputs = model.train_step(data)
    /home/machine-learning/.virtualenvs/jupyter-n5c7sT9n/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:747 train_step
        y_pred = self(x, training=True)
    /home/machine-learning/.virtualenvs/jupyter-n5c7sT9n/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:975 __call__
        input_spec.assert_input_compatibility(self.input_spec, inputs,
    /home/machine-learning/.virtualenvs/jupyter-n5c7sT9n/lib/python3.8/site-packages/tensorflow/python/keras/engine/input_spec.py:155 assert_input_compatibility
        raise ValueError('Layer ' + layer_name + ' expects ' +

    ValueError: Layer sequential expects 1 inputs, but it received 520 input tensors. Inputs received: ...

My dataset has 519 features and 1 label and about 17M lines.

Can anyone help me what I am doing wrong?

$\endgroup$

1 Answer 1

0
$\begingroup$

First of all look at the shape of tensors that your tf.data.Dataset returns then try to set the input_shape of the first Dense layer like:

model = keras.Sequential([
    layers.Dense(520, activation='relu', input_shape=(1, 519)),
    layers.Dense(520, activation='relu'),
    layers.Dense(520, activation='relu'),
    layers.Dense(1)
  ])

or explicitly add the Input layer

or set the number of neurons of the first Dense layer corresponding to the number of features (519)

Also read the docs they are really great: https://www.tensorflow.org/api_docs/python/tf/keras/Sequential

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.