【问题标题】:Tensorflow estimator error in google colab谷歌colab中的Tensorflow估计器错误
【发布时间】:2018-08-13 19:26:18
【问题描述】:

我正在谷歌 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.**

【问题讨论】:

    标签: python pandas tensorflow google-colaboratory tensorflow-estimator


    【解决方案1】:

    方法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)
    

    【讨论】:

    • 感谢 Ekaba,它成功了!但还是很惊讶为什么我之前没有报错,看来新版本中函数 tf.estimator.inputs.pandas_input_fn() 发生了变化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 2018-07-24
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 2023-02-17
    相关资源
    最近更新 更多