tf.placeholder() is not compatible with eager execution的解决方法「建议收藏」

tf.placeholder() is not compatible with eager execution的解决方法「建议收藏」最近安装了TensoFlow2.0及以上的版本都发现啊出现这个问题:RuntimeError:tf.placeholder()isnotcompatiblewitheagerexecution.这是因为在运行**tf.compat.v1.placeholder(dtype,shape=None,name=None)**的时候急切执行了这条语句,但是我们一般都是在一…

大家好,又见面了,我是你们的朋友全栈君。

最近安装了TensoFlow2.0及以上的版本都发现啊出现这个问题:

RuntimeError: tf.placeholder() is not compatible with eager execution.

这是因为在运行**tf.compat.v1.placeholder(dtype, shape = None, name = None)**的时候急切执行了这条语句,但是我们一般都是在一个Session前先去定义placeholder,但是不会去执行,然后再在Sesion上下文管理器中去传入我们的数据,然后执行。
这里给出一个方法(对我有效)

在代码中添加这样一句:

tf.compat.v1.disable_eager_execution()

例子

Error

import tensorflow as tf
import numpy as np
# tf.compat.v1.disable_eager_execution()
x = tf.compat.v1.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)

with tf.compat.v1.Session() as sess:
    print(sess.run(y))

    # rand_array = np.random.rand(1024, 1024)
    # print(sess.run(y, feed_dict={x: rand_array}))

RuntimeError: tf.placeholder() is not compatible with eager execution.

Correct

import tensorflow as tf
import numpy as np
tf.compat.v1.disable_eager_execution()
x = tf.compat.v1.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)

with tf.compat.v1.Session() as sess:
    # print(sess.run(y))

    rand_array = np.random.rand(1024, 1024)
    print(sess.run(y, feed_dict={ 
   x: rand_array}))

[[252.48357 255.5346 248.42102 … 260.47867 257.15802 254.84673]
[247.44424 248.02411 250.1583 … 253.2936 251.4498 242.8446 ]
[259.25705 259.74298 259.33575 … 261.40015 257.27484 261.23822]

[245.12628 258.36353 246.82956 … 247.89975 253.03627 252.05295]
[247.1987 261.00418 254.8853 … 260.04547 260.02435 250.82901]
[256.0824 256.6464 255.48541 … 263.32083 259.73798 255.77368]]

参考资料:
1、eager execution not working with placeholders #18165

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/158379.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • “xxxxxxxxx”signer information does not match signer information of other classes in the same package

    “xxxxxxxxx”signer information does not match signer information of other classes in the same packageMaven在static方法下测试没问题,请求正常,但是服务启动后出现错误信息。在发起请求时抛出异常信息”xxxxxxxxx”signerinformationdoesnotmatchsignerinformationofotherclassesinthesamepackage操作:1、移除pom下冲突的依赖2、移除libraries项目中导入本地jar文件…

    2022年8月22日
    8
  • 如何用命令行编译、运行第一个java程序(Hello World!)[通俗易懂]

    如何用命令行编译、运行第一个java程序(Hello World!)[通俗易懂]一、什么是命令行(Windows下)?     命令行(CommandProcessor)(CMD)是在OS/2,WindowsCE与WindowsNT平台为基础的操作系统(包括Windows2000,WindowsXP,WindowsVista,WindowsServer2003,Windows7,Windows8,Windows8.1,Window

    2022年7月8日
    20
  • vue、css修改滚动条样式

    vue、css修改滚动条样式vue、css修改滚动条样式样式为/*滚动条*/body*::-webkit-scrollbar{ width:10px; height:10px;}body*::-webkit-scrollbar-track{ background:#fff; border-radius:2px;}body*::-webkit-scrollbar-thumb{ background:rgb(205,206,206); border-radius:10px;}

    2022年8月31日
    10
  • socket的sigpipe信号[通俗易懂]

    socket的sigpipe信号[通俗易懂]对一个对端已经关闭的socket调用两次write,第一次将会收到队端的RST响应,第二次将会生成SIGPIPE信号,该信号默认结束进程.具体的分析可以结合TCP的”四次握手”关闭.TCP是全双工的信道,可以看作两条单工信道,TCP连接两端的两个端点各负责一条.当对端调用close时,虽然本意是关闭整个两条信道,但本端只是收到FIN包.按照TCP协议的语义,表示对端只是关闭…

    2022年5月30日
    51
  • clone fail smartgit_SmartGit

    clone fail smartgit_SmartGit安装选择非商业的第三个设置username和邮箱简单的配置ignore忽略一些不需要上传的配置文件,需要配置.gitignore文件.可以在github上搜索到所有编程语言需要忽略的配置文件ignore列表,从列表中找到对应的OC语言需要忽略的文件就可以了。修改ignore文件删除某一类文件的命令在SVN版本控制的project中,drag文件到git版本控制下的project中时…

    2025年8月25日
    3
  • IntelliJ IDEA 第一个 Scala 程序

    IntelliJ IDEA 第一个 Scala 程序

    2021年6月28日
    113

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号