为什么我必须调用model.predict(x)而不是model(x)? [英] Why do I have to call model.predict(x) instead of model(x)?

查看:500
本文介绍了为什么我必须调用model.predict(x)而不是model(x)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下keras模型:

I have the following keras model:

def model_1(vocab_size, output_dim, batch_input_dims, rnn_units, input_shape_LSTM, name='model_1'):

model = Sequential(name=name)

model.add(Embedding(input_dim=vocab_size+1, output_dim=output_dim, mask_zero=True, batch_input_shape=batch_input_dims))

model.add(LSTM(units=rnn_units, input_shape=input_shape_LSTM, 
    stateful=True, 
    return_sequences=True, 
    recurrent_initializer='glorot_uniform',
    recurrent_activation='sigmoid'))

model.add(Dense(units=vocab_size))

return model

然后我得到向量x_和y_的给定值:

I then have the vector x_ and y_ given by:

x_, y_ = get_batches(X, y, batch_size)
x_shape: (32, 200000)
y_ shape: (32, 200000)

现在,我不明白为什么打电话给我

Now, I do not understand why if I call:

model.predict(x_)

我得到了预期的预测数组.

I get the predicted array as expected.

相反,如果我打电话给我

Instead, if I call:

model(x_)

出现以下错误:

ValueError: Layer model_1 was called with an input that isn't a symbolic tensor. Received type: <class 'numpy.ndarray'>. Full input: [array([[    0,     0,     0, ..., 21839, 21841,  9579],
   [    0,     0,     0, ...,     1,     1, 23012],
   [    0,     0,     0, ...,   246,  9832,  9832],
   ...,
   [    0,     0,     0, ..., 24827, 24867, 24868],
   [    0,     0,     0, ..., 22448, 22426, 22426],
   [    0,     0,     0, ...,     1,     1,     1]])]. All inputs to the layer should be tensors.

有人可以解释我为什么吗?我对这个主题提出了一些建议性的问题,但没有找到答案.

Can someone explain me why? I red the suggested questions for this topic and I didn't find the answear...

推荐答案

来自

此[预测]方法专为大规模输入而设计.对于批量处理的少量输入,建议直接使用调用以加快执行速度,例如model(x)或model(x,training = False)

This [predict] 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)

如果您不熟悉python的魔术函数,则使用my_object(x)等效于编写my_object.__call__(x).

If you are unfamiliar with python's magic functions, using my_object(x) is equivalent to writing my_object.__call__(x).

那么,这句话如何适用于您的情况?

So, how does this quote apply to your case?

扩展的模型也是tf.keras.layer.Layer,这就是为什么您可以将多个模型堆叠到更高级别的模型中的原因.因为它也是一个层,所以当您像model(x)一样调用它时,它的行为就像一个层,只返回转换后的输入,仅此而已.但是,当您像model.predict(x)这样称呼它时,它的行为就更像是一个模型,并且以您在使用模型进行预测时更有用的方式为您提供转换.

The model by extension is also a tf.keras.layer.Layer, that's why you can stack multiple models into higher level models. Because it is also a layer, when you call it like model(x), it is acting like a layer and just returns transformed inputs, nothing more. But when you call it like model.predict(x), it acts more like a model and gives you transformations in the way that is more useful when predicting with model.

为什么会发生异常?

因为Embedding层期望使用tf.Tensor,并且使用model(x)调用模型不会执行该转换.您可以使用tf.constant([array])手动进行操作,然后它将起作用.或者,您可以在开始添加tf.keras.layers.Input层,它也可以解决问题.

Because the Embedding layer is expecting a tf.Tensor and calling the model with model(x) does not perform that conversion. You can do it manually with tf.constant([array]) and then it will work. Or, you could add the tf.keras.layers.Input layer at the start and it will also solve the problem.

这能回答您的问题吗?

这篇关于为什么我必须调用model.predict(x)而不是model(x)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆