【问题标题】:I'm getting "Tensor.op is meaningless when eager execution is enabled." in my simple encoder model. (TF 2.0)我得到“启用急切执行时,Tensor.op 毫无意义。”在我的简单编码器模型中。 (TF 2.0)
【发布时间】:2020-09-20 00:35:59
【问题描述】:

我的编码器模型的代码如下,我使用函数式 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.

【问题讨论】:

  • 你找到答案了吗?

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


【解决方案1】:

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

@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

然后调用函数

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

【讨论】:

    猜你喜欢
    • 2019-08-28
    • 2020-11-04
    • 2020-09-21
    • 2020-02-16
    • 2020-08-28
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多