如何在tf.data.Dataset.map()中使用Keras的predict_on_batch? [英] How to use Keras' predict_on_batch in tf.data.Dataset.map()?
问题描述
我想找到一种在TF2.0.
假设我有一个numpy数据集
Let's say I have a numpy dataset
n_data = 10**5
my_data = np.random.random((n_data,10,1))
my_targets = np.random.randint(0,2,(n_data,1))
data = ({'x_input':my_data}, {'target':my_targets})
和tf.keras
模型
x_input = Input((None,1), name = 'x_input')
RNN = SimpleRNN(100, name = 'RNN')(x_input)
dense = Dense(1, name = 'target')(RNN)
my_model = Model(inputs = [x_input], outputs = [dense])
my_model.compile(optimizer='SGD', loss = 'binary_crossentropy')
我可以使用
dataset = tf.data.Dataset.from_tensor_slices(data)
dataset = dataset.batch(10)
prediction_dataset = dataset.map(transform_predictions)
其中transform_predictions
是用户定义的函数,可从predict_on_batch
where transform_predictions
is a user defined function that gets the predictions from predict_on_batch
def transform_predictions(inputs, outputs):
predictions = my_model.predict_on_batch(inputs)
# predictions = do_transformations_here(predictions)
return predictions
这给出了来自predict_on_batch
的错误:
AttributeError: 'Tensor' object has no attribute 'numpy'
据我了解,predict_on_batch
期望一个numpy数组,并且它正在从数据集中获取张量对象.
As far as I understand, predict_on_batch
expects a numpy array, and it is getting a tensor object from the dataset.
似乎一个可能的解决方案是将predict_on_batch
包装在`tf.py_function中,尽管我也无法使其正常工作.
It seems like one possible solution is to wrap predict_on_batch
in a `tf.py_function, though I have not been able to get that working either.
有人知道该怎么做吗?
推荐答案
Dataset.map()返回没有numpy()方法的<class 'tensorflow.python.framework.ops.Tensor'>
.
Dataset.map() returns <class 'tensorflow.python.framework.ops.Tensor'>
which doesn't have numpy() method.
遍历数据集返回
<class 'tensorflow.python.framework.ops.EagerTensor'>
具有numpy()方法.
Iterating over Dataset returns
<class 'tensorflow.python.framework.ops.EagerTensor'>
which has a numpy() method.
提供一个渴望的张量来预测()方法家族很好.
Feeding an eager tensor to predict() family of methods works fine.
您可以尝试这样的事情:
You could try something like this:
dataset = tf.data.Dataset.from_tensor_slices(data)
dataset = dataset.batch(10)
for x,y in dataset:
predictions = my_model.predict_on_batch(x['x_input'])
#or
predictions = my_model.predict_on_batch(x)
这篇关于如何在tf.data.Dataset.map()中使用Keras的predict_on_batch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!