导入包
import numpy as np |
设置生成的图像尺寸和去除警告
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' |
随机生成一个线性的数据
n_observations = 100 |
准备placeholder
X = tf.placeholder(tf.float32, name='X') |
初始化参数/权重
W = tf.Variable(tf.random_normal([1]), name='weight') |
计算预测结果
Y_pred = tf.add(tf.multiply(X, W), b) |
计算损失值
loss = tf.square(Y - Y_pred, name='loss') #tf.square:平方 |
初始化optimizer
learning_rate = 0.01 |
指定迭代次数,并在session里执行graph
n_samples = xs.shape[0] |
画出线性回归线
plt.plot(xs, ys, 'bo', label='Real data') |
Tensorboard查看图形数据
tensorboard --logdir path/to/logs(你保存文件所在位置) |
如:(log_writer = tf.summary.FileWriter(“./logs/linear_regression”, sess.graph)保存的地址):
tensorboard —logdir ./logs/linear_regression
输出:TensorBoard x.x.x at http://(你的用户名):6006 (Press CTRL+C to quit)
然后打开网页:http://localhost:6006