TensorFlow,使用`tf.tensor`作为python`BOOL了

发布于 2025-01-23 14:55:19 字数 923 浏览 4 评论 0原文

 raise errors.OperatorNotAllowedInGraphError(
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function

代码就是这样。

        self.target = tf.compat.v1.placeholder(tf.float32,    shape = [None], name='Qvals')
        self.actions= tf.compat.v1.placeholder(tf.int32,      shape = [None], name='Actions')

        self.model = AlexNetDuel(self.X, cfg.num_actions, cfg.train_fc)

        self.predict = self.model.output
        ind = tf.one_hot(self.actions, cfg.num_actions)
        pred_Q = tf.reduce_sum(tf.multiply(self.model.output, ind), axis=1)
        self.loss = huber_loss(pred_Q, self.target)
        self.train = tf.cond(tf.optimizers.Adam(lr=self.lr, beta1=0.9, beta2=0.99).minimize(self.loss, name="train"))
 raise errors.OperatorNotAllowedInGraphError(
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function

Code is this.

        self.target = tf.compat.v1.placeholder(tf.float32,    shape = [None], name='Qvals')
        self.actions= tf.compat.v1.placeholder(tf.int32,      shape = [None], name='Actions')

        self.model = AlexNetDuel(self.X, cfg.num_actions, cfg.train_fc)

        self.predict = self.model.output
        ind = tf.one_hot(self.actions, cfg.num_actions)
        pred_Q = tf.reduce_sum(tf.multiply(self.model.output, ind), axis=1)
        self.loss = huber_loss(pred_Q, self.target)
        self.train = tf.cond(tf.optimizers.Adam(lr=self.lr, beta1=0.9, beta2=0.99).minimize(self.loss, name="train"))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文