关于在使用tensorflow2.0版本时,出现RuntimeError:tf.placeholder() is not compatible with eager execution.的问题

关于在使用tensorflow2.0版本时,出现RuntimeError:tf.placeholder() is not compatible with eager execution.的问题

今天在运行程序:(部分代码)

import tensorflow as tf
import numpy as np
tf.set_random_seed(777)  # for reproducibility
learning_rate = 0.1
x_data = [[0, 0],
          [0, 1],
          [1, 0],
          [1, 1]]
y_data = [[0],
          [1],
          [1],
          [0]]
x_data = np.array(x_data, dtype=np.float32)
y_data = np.array(y_data, dtype=np.float32)
X = tf.placeholder(tf.float32, [None, 2])
Y = tf.placeholder(tf.float32, [None, 1])

遇到的第一个问题是:
AttributeError: module ‘tensorflow’ has no attribute ‘set_random_seed’
由于使用的tensorflow版本为最新的版本,所以考虑到函数为低版本下的函数,所以找到2.0版本以下和2.0版本的函数对照表:(链接:https://docs.google.com/spreadsheets/d/1FLFJLzg7WNP6JHODX5q8BDgptKafq_slHpnHVbJIteQ/edit#gid=0)
在这里插入图片描述
所以把代码

tf.set_random_seed(777)

改为

tf.compat.v1.set_random_seed(777)

这个问题解决之后,运行时又出现了其他问题:
raise RuntimeError("tf.placeholder() is not compatible with "
RuntimeError: tf.placeholder() is not compatible with eager execution.
再次查看是否是因为版本的原因导致的问题:
在这里插入图片描述
果然,
再次修改代码:

X = tf.placeholder(tf.float32, [None, 2])
Y = tf.placeholder(tf.float32, [None, 1])

为:

X = tf.compat.v1.placeholder(tf.float32, [None, 2])
Y = tf.compat.v1.placeholder(tf.float32, [None, 1])

再次执行程序:
但是问题依然是
raise RuntimeError("tf.placeholder() is not compatible with "
RuntimeError: tf.placeholder() is not compatible with eager execution.
然后查看了底层函数代码:

@tf_export(v1=["placeholder"])
def placeholder(dtype, shape=None, name=None):
  """Inserts a placeholder for a tensor that will be always fed.

  **Important**: This tensor will produce an error if evaluated. Its value must
  be fed using the `feed_dict` optional argument to `Session.run()`,
  `Tensor.eval()`, or `Operation.run()`.

  For example:

  ```python
  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))  # ERROR: will fail because x was not fed.

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

  @compatibility(eager)
  Placeholders are not compatible with eager execution.
  @end_compatibility

  Args:
    dtype: The type of elements in the tensor to be fed.
    shape: The shape of the tensor to be fed (optional). If the shape is not
      specified, you can feed a tensor of any shape.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` that may be used as a handle for feeding a value, but not
    evaluated directly.

  Raises:
    RuntimeError: if eager execution is enabled
  """
  if context.executing_eagerly():
    raise RuntimeError("tf.placeholder() is not compatible with "
                       "eager execution.")

  return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)

看到一句话:
RuntimeError: if eager execution is enabled
如果启动了紧急执行,会出错。
最终查到了一个解决方案,放上链接:
https://stackoverflow.com/questions/53429896/how-do-i-disable-tensorflows-eager-execution
在import tensorflow as tf 后加上
tf.compat.v1.disable_eager_execution()
关闭紧急执行。

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

再次运行代码:
结果:
在这里插入图片描述
执行正确。

补充一个图片:
关于在使用tensorflow2.0版本时,出现RuntimeError:tf.placeholder() is not compatible with eager execution.的问题_第1张图片
翻译过来就是:
关于在使用tensorflow2.0版本时,出现RuntimeError:tf.placeholder() is not compatible with eager execution.的问题_第2张图片

你可能感兴趣的:(Anaconda,vsc)