原来代码

import tensorflow as tf

import numpy as np
import matplotlib.pyplot as plt

train_X = np.linspace(-1,1,100)
train_Y = 2 * train_X + np.random.randn(*train_X.shape) * 0.3 # y=2x,但是加入了噪声
#显示模拟数据点
plt.plot(train_X, train_Y,'ro',label='Original data')
plt.legend()
plt.show()

#创建模型
#占位符
X = tf.compat.v1.placeholder("float")
Y = tf.compat.v1.placeholder("float")
#模型参数
W = tf.Variable(tf.random.normal([1]),name="weight")
b = tf.Variable(tf.zeros([1]),name="bias")
#前向结构
z = tf.multiply(X,W)+b

 

后面出错

 Traceback (most recent call last):

  File "D:\python\线性回归.py", line 24, in <module>
    X = tf.compat.v1.placeholder("float")

  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 3169, in placeholder
    raise RuntimeError("tf.placeholder() is not compatible with "

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

正确代码

tf.compat.v1.disable_eager_execution()
X = tf.compat.v1.placeholder("float")
Y = tf.compat.v1.placeholder("float")

Logo

更多推荐