Python Tensorflow可能更新为高级估计器
我在一些天气数据上训练了一个深度神经网络回归器。当我尝试classifier.predict()时,它返回一个生成器对象。通常我们要做的是将list()放在对象上以获得预测 它过去可以工作,但我相信经过最近的更新后,它不再工作了。我目前使用的是tensorflow 1.7.0。我尝试降级到tensorflow的几个版本,但找不到一个有效的版本Python Tensorflow可能更新为高级估计器,python,tensorflow,machine-learning,deep-learning,generator,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("
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
回溯(最近一次呼叫最后一次):
文件“C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\inspect.py”,第1089行,在getfullargspec中
sigcls=签名)
文件“C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\inspect.py”,第2156行,位于可调用的\u签名\u中
raise TypeError(“{!r}不是可调用对象”。格式(obj))
TypeError:数组([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, 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,0,1,0,0])不是可调用对象
上述异常是以下异常的直接原因:
回溯(最近一次呼叫最后一次):
文件“C:/Users/5V/PycharmProjects/UberAPI/deep_learning.py”,第41行,在
打印(列表(预测))
文件“C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\estimator\estimator.py”,第486行,在predict中
输入\u fn,模型\u fn\u lib.ModeKeys.PREDICT)
文件“C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\estimator\estimator.py”,第662行,从输入获取功能
结果=自身。调用输入(输入,模式)
文件“C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\estimator\estimator.py”,第763行,在调用输入中
input\u fn\u args=util.fn\u args(input\u fn)
文件“C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\estimator\util.py”,第55行,在fn_参数中
args=tf_inspect.getfullargspec(fn).args
文件“C:\Users\5V\AppData\Local\Programs\Python\35\lib\site packages\tensorflow\Python\util\tf\u inspect.py”,第67行,在getfullargspec中
如果d.decorator_argspec不是None),则spec_fn(目标))
getfullargspec中的文件“C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\inspect.py”,第1095行
从ex引发TypeError(“不支持的可调用”)
TypeError:不支持的可调用
这样做将产生:
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
回溯(最近一次呼叫最后一次):
文件“C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\inspect.py”,第1089行,在getfullargspec中
sigcls=签名)
文件“C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\inspect.py”,第2156行,位于可调用的\u签名\u中
raise TypeError(“{!r}不是可调用对象”。格式(obj))
TypeError:数组([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, 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,0,1,0,0])不是可调用对象
上述异常是以下异常的直接原因:
回溯(最近一次呼叫最后一次):
文件“C:/Users/5V/PycharmProjects/UberAPI/deep_learning.py”,第41行,在
打印(列表(预测))
文件“C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\estimator\estimator.py”,第486行,在predict中
输入\u fn,模型\u fn\u lib.ModeKeys.PREDICT)
文件“C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\estimator\estimator.py”,第662行,从输入获取功能
结果=自身。调用输入(输入,模式)
文件“C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\estimator\estimator.py”,第763行,在调用输入中
input\u fn\u args=util.fn\u args(input\u fn)
文件“C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\estimator\util.py”,第55行,在fn_参数中
args=tf_inspect.getfullargspec(fn).args
文件“C:\Users\5V\AppData\Local\Programs\Python\35\lib\site packages\tensorflow\Python\util\tf\u inspect.py”,第67行,在getfullargspec中
如果d.decorator_argspec不是None),则spec_fn(目标))
getfullargspec中的文件“C:\Users\5V\AppData\Local\Programs\Python\Python35\lib\inspect.py”,第1095行
从ex引发TypeError(“不支持的可调用”)
TypeError:不支持的可调用
我不确定您是否还有其他错误。基于回溯,很明显,此特定错误是由于使用np.array(onehot)
作为分类器.predict的输入而导致的,即numpy数组不是可调用对象。要解决此问题,您可以使用:
编辑:关于形状不匹配
下面是我的一个例子,来说明一个潜在的原因
InvalidArgumentError(回溯见上文):重塑的输入是一个带有
正如代码中所述,正确的输入应该只有一个特性,而不是两个。在我看来,问题在于np.array(onehot)
不是正确的输入。你确定这以前对你有用吗?你能提供一个完整的回溯吗
input_predict= tf.estimator.inputs.numpy_input_fn({'x': np.array(onehot)}, shuffle=False)
print(list(model.predict(input_predict)))
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)))