wutian713 2023-10-21 10:05 采纳率: 33.3%
浏览 14

Tensor.op is undefined when eager execution is enabled

代码

    user_layer_model = keras.models.Model(
        inputs=[m.input[0], m.input[1], m.input[2]],
        outputs=m.get_layer("user_embedding").output
    )

    user_input = [
            np.reshape(user_id, [1, 1]),
            np.reshape(user_type, [1, 1]),
            np.reshape(actor, [1, 1])
    ]

    u_embedding = user_layer_model(user_input)
    user_embedding.append(u_embedding.numpy().flatten())

报错如下:

Exception encountered when calling layer 'embedding' (type Embedding).

Tensor.op is undefined when eager execution is enabled.

Call arguments received by layer 'embedding' (type Embedding):
  • inputs=tf.Tensor(shape=(1, 1), dtype=float32)
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-10-21 12:03
    关注

    【以下回答由 GPT 生成】

    这个问题是因为在启用了eager execution(即动态图模式)的情况下,Tensor对象没有op属性。解决这个问题的方法有两种:

    1. 禁用eager execution:将以下代码添加到你的程序的开头,可以禁用eager execution:

    python import tensorflow as tf tf.compat.v1.disable_eager_execution()

    这样就可以继续使用原有的代码,不会再出现报错。

    1. 修改代码以适应eager execution:在启用eager execution的情况下,需要修改你的代码,以适应动态图模式。可以使用keras.backend.function函数来创建一个函数,将模型的input作为输入,模型的output作为返回值。以下是修改后的代码:

    ```python import tensorflow as tf from tensorflow import keras

    user_layer_model = keras.models.Model( inputs=[m.input[0], m.input[1], m.input[2]], outputs=m.get_layer("user_embedding").output )

    @tf.function def get_u_embedding(user_input): u_embedding = user_layer_model(user_input) return u_embedding.numpy().flatten()

    user_input = [ tf.convert_to_tensor(np.reshape(user_id, [1, 1])), tf.convert_to_tensor(np.reshape(user_type, [1, 1])), tf.convert_to_tensor(np.reshape(actor, [1, 1])) ]

    u_embedding = get_u_embedding(user_input) user_embedding.append(u_embedding) ```

    这样修改后的代码将在启用了eager execution的情况下正常运行。


    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 修改了问题 10月21日
  • 创建了问题 10月21日

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)