Tensorflow可能更新为高级估算器

时间:2018-04-15 03:41:24

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

我在一些天气数据上训练了一个深度神经网络回归器。当我尝试classifier.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

1 个答案:

答案 0 :(得分:0)

我不确定你是否还有其他错误。基于回溯,很明显,这个特定错误是由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(参见上面的回溯):重塑的输入是一个张量... ...

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

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