python - 如何在 tf.data.Dataset.map() 中使用 Keras 的 predict_on_batch?
问题描述
我想找到一种predict_on_batch
在里面使用 Kerastf.data.Dataset.map()
的方法TF2.0.
假设我有一个 numpy 数据集
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
批处理
dataset = tf.data.Dataset.from_tensor_slices(data)
dataset = dataset.batch(10)
prediction_dataset = dataset.map(transform_predictions)
从哪里transform_predictions
获取预测的用户定义函数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 数组,并且它从数据集中获取一个张量对象。
似乎一种可能的解决方案是包装predict_on_batch
一个`tf.py_function,尽管我也无法让它工作。
有谁知道如何做到这一点?
解决方案
Dataset.map() 返回 <class 'tensorflow.python.framework.ops.Tensor'>
没有 numpy() 方法。
迭代 Dataset 返回
<class 'tensorflow.python.framework.ops.EagerTensor'>
具有 numpy() 方法。
为 predict() 系列方法提供一个热切的张量可以正常工作。
你可以尝试这样的事情:
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)
推荐阅读
- php - 如何在php中设置标题以确保安全
- spring-boot - 如何将闪存消息从控制器调用到百里香模板
- ruby-on-rails - 在哪里可以找到支持 ruby 1.8 的 simplecov 的 gem 包
- flutter - 购物车小部件在颤动中热重新加载时消失
- c++ - Visual Studio 2019 是否可以将文件输出为大写?
- angular - ArcGIS - 错误:编辑时不允许此操作 [可编辑]
- android - 抗锯齿导致recyclerview滞后
- php - 如何在laravel中提交表单之前在模态中添加带有值的附加输入
- node.js - Cors-policy 使用带有查询或参数的 axios get/post 阻止我的请求
- angular - Laravel 5.3 web.php 和 api.php:确保管理员只能访问指定站点部分的正确用法是什么?