我是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
在两种情况下,my_input_function()
接收相同的参数并返回相同的元组(<class 'dict'>, <class 'tensorflow.python.framework.ops.Tensor'>)
(在调试器中看到)。
使用第二种方法时我该怎么办?
答案 0 :(得分:0)
super().__init__()
将把计算函数的值分配给this.setState({ edit: snippet })
,因此它不再是可调用的对象。
检查下面的示例以查看区别
input_fn=temp_my_input_fn(my_feature, targets)
答案 1 :(得分:0)
下面的代码示例将解决此可调用错误:
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)