如何在tf.data.Dataset.map()中使用Keras的predict_on_batch?

问题描述 投票:0回答:1

我想在qazxsw poi找到一种方法在qazxsw poi里面使用Keras'qazxsw poi

假设我有一个numpy数据集

predict_on_batch

tf.data.Dataset.map()模型

TF2.0.

我可以创建一个批量qazxsw poi

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期望一个numpy数组,它从数据集中得到一个张量对象。

似乎一种可能的解决方案是将def transform_predictions(inputs, outputs): predictions = my_model.predict_on_batch(inputs) # predictions = do_transformations_here(predictions) return predictions 包装在`tf.py_function中,尽管我也无法将其工作。

有谁知道如何做到这一点?

python tensorflow keras tensorflow-datasets tensorflow2.0
1个回答
0
投票

Dataset.map()返回没有numpy()方法的predict_on_batch

迭代数据集会返回具有numpy()方法的AttributeError: 'Tensor' object has no attribute 'numpy'

提供一个急切的张量来预测()一系列方法很好。

你可以尝试这样的事情:

predict_on_batch
© www.soinside.com 2019 - 2024. All rights reserved.