我得到“启用急切执行时,Tensor.op毫无意义。”在我的简单编码器模型中(TF 2.0)

时间:2020-06-01 16:41:35

标签: tensorflow keras lstm tensorflow2.0 keras-layer

下面给出了我的编码器模型的代码,我已经使用功能性API(TF 2.0)进行了编码

embed_obj = EndTokenLayer()
def encoder_model(inp):
  input_1 = embed_obj(inp)
  h = Masking([(lambda x: x*0)(x) for x in range(128)])(input_1)
  lstm1 , state_h, state_c = LSTM(512, return_sequences=True, return_state=True)(h)
  model = Model(inputs=input_1, outputs=[lstm1, state_h, state_c])

  return model

当我调用模型时:


for x,y in train.take(1):
  k = x
model = encoder_model(k)

我遇到以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-98-46e9c9596137> in <module>()
      2 for x,y in train.take(1):
      3   k = x
----> 4 model = encoder_model(k)

7 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in op(self)
   1111   def op(self):
   1112     raise AttributeError(
-> 1113         "Tensor.op is meaningless when eager execution is enabled.")
   1114 
   1115   @property

AttributeError: Tensor.op is meaningless when eager execution is enabled.

1 个答案:

答案 0 :(得分:1)

在TF2中,可以通过使用装饰器来构造静态图(使用动态图防止急于执行)。 尝试@ tf.function装饰器

[30/Jun/2020 22:39:59] "GET / HTTP/1.1" 200 1154
Not Found: /css/bootstrap.min.css
[30/Jun/2020 22:39:59] "GET /css/bootstrap.min.css HTTP/1.1" 404 2313
Not Found: /css/fontAwesome.css
[30/Jun/2020 22:39:59] "GET /css/fontAwesome.css HTTP/1.1" 404 2307
[30/Jun/2020 22:39:59] "GET /static/frontend/main.js HTTP/1.1" 200 1004248
[30/Jun/2020 22:40:00] "GET /api/event HTTP/1.1" 301 0

然后调用函数

@tf.function
def encoder_model(inp):
  input_1 = embed_obj(inp)
  h = Masking([(lambda x: x*0)(x) for x in range(128)])(input_1)
  lstm1 , state_h, state_c = LSTM(512, return_sequences=True, return_state=True)(h)
  model = Model(inputs=input_1, outputs=[lstm1, state_h, state_c])

  return model