tensorflow2中model predict和__call__方法的区别
Posted oldbook
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tensorflow2中model predict和__call__方法的区别相关的知识,希望对你有一定的参考价值。
@disable_multi_worker def predict(self, x, batch_size=None, verbose=0, steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False): """Generates output predictions for the input samples. Computation is done in batches. This method is designed for performance in large scale inputs. For small amount of inputs that fit in one batch, directly using `__call__` is recommended for faster execution, e.g., `model(x)`, or `model(x, training=False)` if you have layers such as `tf.keras.layers.BatchNormalization` that behaves differently during inference. Also, note the fact that test loss is not affected by regularization layers like noise and dropout.
上述代码是tensorflow2.0.0中的一段源码,下面的注释中提到当少量数据的时候使用__call__比较快。
实际使用中有些地方使用predict会直接卡住。
__call__方法调用后得到的是Tensor对象,使用numpy()方法可以得到ndarray对象。