Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python LinearRegressor.train()直通“;。。。不是可调用对象”;例外_Python_Python 3.x_Tensorflow_Lambda - Fatal编程技术网

Python LinearRegressor.train()直通“;。。。不是可调用对象”;例外

Python LinearRegressor.train()直通“;。。。不是可调用对象”;例外,python,python-3.x,tensorflow,lambda,Python,Python 3.x,Tensorflow,Lambda,我是TensorFlow的新手,对Python不是很有经验。我正在学习以下教程: 如果我使用lambda来定义输入函数(如本教程中所述),一切正常: def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None) : ... features, labels = ds.make_one_shot_iterator().get_next() return features,

我是TensorFlow的新手,对Python不是很有经验。我正在学习以下教程:

如果我使用lambda来定义输入函数(如本教程中所述),一切正常:

def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None) : 
    ...
    features, labels = ds.make_one_shot_iterator().get_next()
    return features, labels

_=linear_regressor.train(input_fn=lambda: my_input_fn(my_feature, targets), steps=100)
如果我按以下方式更改脚本:

def get_my_input_fn() :
    def my_input_func(features, targets, batch_size=1, shuffle=True, num_epochs=None) :
        ...
        features, labels = ds.make_one_shot_iterator().get_next()
        return features, labels
    return my_input_func

temp_my_input_fn=get_my_input_fn()
_=linear_regressor.train(input_fn=temp_my_input_fn(my_feature, targets), steps=100)
我收到一个例外:

Traceback (most recent call last):
  File "/usr/lib/python3.6/inspect.py", line 1126, in getfullargspec
    sigcls=Signature)
  File "/usr/lib/python3.6/inspect.py", line 2193, in _signature_from_callable
    raise TypeError('{!r} is not a callable object'.format(obj))
TypeError: ({'MeanHHInc': <tf.Tensor 'IteratorGetNext:0' shape=(?,) dtype=float64>}, <tf.Tensor 'IteratorGetNext:1' shape=(?,) dtype=int64>) is not a callable object
回溯(最近一次呼叫最后一次):
getfullargspec中的文件“/usr/lib/python3.6/inspect.py”,第1126行
sigcls=签名)
文件“/usr/lib/python3.6/inspect.py”,第2193行,在可调用的
raise TypeError(“{!r}不是可调用对象”。格式(obj))
TypeError:({'MeanHHInc':},)不是可调用对象
在这两种情况下,
my\u input\u function()
接收相同的参数并返回相同的元组
(,)
(在调试器中看到)

当我使用第二种方法时,我做错了什么

input_fn=temp_my_input_fn(my_feature, targets)
将计算函数的值分配给
input\u fn
,因此它不再是可调用对象

检查下面的示例以查看差异

>>> def getfunc():
...     def square(x):
...             return x**2
...     return square
...
>>> d = getfunc()

>>> def test(fn = None):
...     return fn(x)
...
>>> x = 2
>>>
>>> test(fn=d(x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in test
TypeError: 'int' object is not callable
>>> test(fn=d)
4
>>def getfunc():
...     def方形(x):
...             返回x**2
...     返回广场
...
>>>d=getfunc()
>>>def测试(fn=无):
...     返回fn(x)
...
>>>x=2
>>>
>>>试验(fn=d(x))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第2行,在测试中
TypeError:“int”对象不可调用
>>>试验(fn=d)
4.

下面的代码示例将解决此可调用错误:

def get_my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None) :
  def my_input_fn():
    """Trains a linear regression model of one feature.

    Args:
      features: pandas DataFrame of features
      targets: pandas DataFrame of targets
      batch_size: Size of batches to be passed to the model
      shuffle: True or False. Whether to shuffle the data.
      num_epochs: Number of epochs for which data should be repeated. None = repeat indefinitely
    Returns:
      Tuple of (features, labels) for next data batch
    """
    nonlocal features, targets, batch_size, shuffle, num_epochs

    # Convert pandas data into a dict of np arrays.
    features = {key:np.array(value) for key,value in dict(features).items()}                                           

    # Construct a dataset, and configure batching/repeating.
    ds = Dataset.from_tensor_slices((features,targets)) # warning: 2GB limit
    ds = ds.batch(batch_size).repeat(num_epochs)

    # Shuffle the data, if specified.
    if shuffle:
      ds = ds.shuffle(buffer_size=10000)

    # Return the next batch of data.
    features, labels = ds.make_one_shot_iterator().get_next()
    return features, labels
  return my_input_fn

temp_my_input_fn=get_my_input_fn(my_feature, targets)
_ = linear_regressor.train(input_fn=temp_my_input_fn, steps=100)

谢谢你的回答。在调用
test(fn=d)
之前,您在代码中指定了
x=2
,然后它就工作了。当我尝试做同样的事情时:
temp\u my\u input\u fn=get\u my\u input\u fn()features=my\u feature1 targets=targets1\u=linear\u regressor.train(input\u fn=temp\u my\u input\u fn,steps=100)
它抛出了以下异常:TypeError:my\u input\u func()缺少2个必需的位置参数:“features”和“targets”在其他定义之前设置为
features=my_feature1 targets=targets1
。在我提供的示例中,您可以在repl周围玩,看看定义x时的影响。在您的示例中,x必须在
test(fn=d)
之前定义。如果没有-它引发异常:NameError:未定义名称“x”。在我的例子中,错误有点不同。我正在努力理解它现在意味着什么。不过还是要谢谢你的回答!找到了最后一个问题的解决方案。它可以通过以下声明函数来解决:
def get\u my\u input\u fn(features1,targets1):def my\u input\u func(features=features1,targets=targets1,batch\u size=1,shuffle=True,num\u epochs=None):
和调用函数:
temp\u my\u input\u fn=get\u my\u input\u fn()
(很抱歉,代码的格式有点错误)非常感谢。很抱歉,我没有足够的声誉点数来为您的答案添加“有用性”。