google colab 中的 Tensorflow 估计器错误

Tensorflow estimator error in google colab

我正在 google colab 环境中的 tensorflow 中训练 DNN,该代码直到昨天都运行良好,但现在当我 运行 我的代码的估计器训练部分时,它给出了一个错误。

不知道具体是什么原因,google colab 是不是使用了更新版本的tensorflow,其中一些功能与旧版本不兼容?因为我之前的代码没有问题,我也没有改。 其他代码似乎存在这个问题,例如这个示例代码表单 stanford 之前是 运行 没有任何错误, https://colab.research.google.com/drive/1nG7Ga46jrWF5n7pHe0FK6anB0pLNgBVt

但现在当您 运行 部分时:

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

它给出了和我一样的错误:

> **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.**

方法 tf.estimator.inputs.pandas_input_fny 属性接收一个 Pandas Series 对象的输入。

要从 DataFrame 中提取目标 'sent_symbol',请调用 training_labels['sent_symbol']

要修复此脚本,请按如下方式修改代码:

# 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)