【问题标题】:Tensorflow possible update to high level estimatorTensorflow 可能更新到高级估计器
【发布时间】:2018-04-15 03:41:24
【问题描述】:

我已经根据一些天气数据训练了一个深度神经网络回归器。当我尝试分类器.predict() 时,它返回一个生成器对象。通常我们所做的是将 list() 放在对象上以获取预测。

它曾经可以工作,但我相信在最近的更新之后,它不再工作了。我目前使用的是 tensorflow 1.7.0。我尝试降级到多个版本的 tensorflow,但找不到一个有效的版本。

import tensorflow as tf
feature_columns = [tf.feature_column.numeric_column("x", shape=[163])]
classifier = tf.estimator.DNNRegressor(feature_columns=feature_columns,
                                        hidden_units=[200,100,20],
                                        model_dir='model/'
                                        )
onehot,price=load_single_data([[5,18,16,1],'Mostly Sunny','Mostly Sunny',46.5])

prediction= classifier.predict(np.array(onehot))
#This line produced the error
print(list(prediction))

产生的错误是:

Traceback (most recent call last):
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\inspect.py", line 1089, in getfullargspec
    sigcls=Signature)
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\inspect.py", line 2156, in _signature_from_callable
    raise TypeError('{!r} is not a callable object'.format(obj))
TypeError: array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 1, 0, 0]) is not a callable object

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:/Users/5V/PycharmProjects/UberAPI/deep_learning.py", line 41, in <module>
    print(list(prediction))
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\estimator\estimator.py", line 486, in predict
    input_fn, model_fn_lib.ModeKeys.PREDICT)
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\estimator\estimator.py", line 662, in _get_features_from_input_fn
    result = self._call_input_fn(input_fn, mode)
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\estimator\estimator.py", line 763, in _call_input_fn
    input_fn_args = util.fn_args(input_fn)
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\estimator\util.py", line 55, in fn_args
    args = tf_inspect.getfullargspec(fn).args
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\util\tf_inspect.py", line 67, in getfullargspec
    if d.decorator_argspec is not None), spec_fn(target))
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\inspect.py", line 1095, in getfullargspec
    raise TypeError('unsupported callable') from ex
TypeError: unsupported callable

这样做会产生:

print(prediction)
>> <generator object Estimator.predict at 0x00000069E3AC0BF8>

好的,这是一个完整的回溯

Traceback (most recent call last):
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\inspect.py", line 1089, in getfullargspec
    sigcls=Signature)
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\inspect.py", line 2156, in _signature_from_callable
    raise TypeError('{!r} is not a callable object'.format(obj))
TypeError: array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 1, 0, 0]) is not a callable object

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:/Users/5V/PycharmProjects/UberAPI/deep_learning.py", line 41, in <module>
    print(list(prediction))
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\estimator\estimator.py", line 486, in predict
    input_fn, model_fn_lib.ModeKeys.PREDICT)
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\estimator\estimator.py", line 662, in _get_features_from_input_fn
    result = self._call_input_fn(input_fn, mode)
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\estimator\estimator.py", line 763, in _call_input_fn
    input_fn_args = util.fn_args(input_fn)
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\estimator\util.py", line 55, in fn_args
    args = tf_inspect.getfullargspec(fn).args
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\util\tf_inspect.py", line 67, in getfullargspec
    if d.decorator_argspec is not None), spec_fn(target))
  File "C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\inspect.py", line 1095, in getfullargspec
    raise TypeError('unsupported callable') from ex
TypeError: unsupported callable

【问题讨论】:

标签: python tensorflow machine-learning deep-learning generator


【解决方案1】:

我不确定您是否还有其他错误。根据回溯,很明显,此特定错误是由于将np.array(onehot) 用作input_fn 用于classifier.predict 导致的,即numpy 数组不是可调用对象。要解决此问题,您可以使用numpy_input_fn

input_predict= tf.estimator.inputs.numpy_input_fn({'x': np.array(onehot)}, shuffle=False)
print(list(model.predict(input_predict)))

编辑:关于形状不匹配

这是我的MCVE,以显示一个潜在的原因

InvalidArgumentError(回溯见上文):reshape 的输入是一个带有...的张量

import numpy as np
import tensorflow as tf

tf.logging.set_verbosity(tf.logging.INFO)

model = tf.estimator.DNNRegressor(
        hidden_units=[20, 20],
        feature_columns=[tf.feature_column.numeric_column(key='x')],
        model_dir=r"E:\GitHub\miscellaneous\ml-models\foobar\tensorflow\test"
    )

input_train= tf.estimator.inputs.numpy_input_fn(
        x={'x': np.arange(100)},
        y=np.arange(100),
        num_epochs=None,
        shuffle=True
    )
model.train(input_fn=input_train, steps=200)

test = np.array([[200]]) # correct input with shape 1 for each input (row)
#test = np.array([[200, 300]]) # incorrect input with shape 2 for each input (row)
input_predict= tf.estimator.inputs.numpy_input_fn(
        {'x': test}, shuffle=False
    )
print(list(model.predict(input_predict)))

正如代码中所述,正确的输入应该只有一个特征,而不是两个。

【讨论】:

  • 尝试产生以下结果: InvalidArgumentError(有关回溯,请参见上文):reshape 的输入是具有 128 个值的张量,但请求的形状有 20864 我无法发布完整的回溯,因为回复限制为 500 个字符。
  • @5Volts 这与您的型号有关。我不知道细节,但这意味着您的模型需要一个形状为 20864 的张量作为输入,而您的 onehot(预测输入)的形状为 128。我想这就是 MCVE 有用的地方。
猜你喜欢
  • 2019-01-04
  • 2018-10-05
  • 1970-01-01
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-04
  • 2018-06-15
相关资源
最近更新 更多