Search code examples
pythonpandastensorflowgoogle-colaboratorytensorflow-estimator

Tensorflow estimator error in google colab


I am training a DNN in tensorflow in a google colab environment, the code works well till yesterday, but now when I run the the estimator training section of my code, It gives an error.

I don't know exactly what is the reason, is google colab using any updated version of tensorflow, in which some functions are not compatible with older versions? because I had no problem with the code before, and I didn't change it. It seems this problem exist for the other codes, for example this sample code form stanford was ran without any error before, https://colab.research.google.com/drive/1nG7Ga46jrWF5n7pHe0FK6anB0pLNgBVt

but now when you run the section :

estimator.train(input_fn=train_input_fn, steps=1000);

It gives the same error as mine:

> **TypeError                                 Traceback (most recent call last)
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py
> in make_tensor_proto(values, dtype, shape, verify_shape)**
> 
> **TypeError: Expected binary or unicode string, got {'sent_symbol': <tf.Tensor 'random_shuffle_queue_DequeueMany:3' shape=(128,)
> dtype=int64>}**
> 
> **TypeError                                 Traceback (most recent call last) <ipython-input-10-9dfe23a4bf62> in <module>()
> ----> 1 estimator.train(input_fn=train_input_fn, steps=1000);**
> 
> **TypeError: Failed to convert object of type <class 'dict'> to Tensor. Contents: {'sent_symbol': <tf.Tensor
> 'random_shuffle_queue_DequeueMany:3' shape=(128,) dtype=int64>}.
> Consider casting elements to a supported type.**

Solution

  • The y attribute of the method tf.estimator.inputs.pandas_input_fn receives an input a Pandas Series object.

    To extract the target 'sent_symbol' from the DataFrame, call training_labels['sent_symbol'].

    To fix this script, modify the code as follows:

    # Training input on the whole training set with no limit on training epochs.
    train_input_fn = tf.estimator.inputs.pandas_input_fn(
        training_examples, training_labels['sent_symbol'], num_epochs=None, shuffle=True)
    
    # Prediction on the whole training set.
    predict_train_input_fn = tf.estimator.inputs.pandas_input_fn(
        training_examples, training_labels['sent_symbol'], shuffle=False)
    # Prediction on the test set.
    predict_test_input_fn = tf.estimator.inputs.pandas_input_fn(
        validation_examples, validation_labels['sent_symbol'], shuffle=False)