版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zaf0516/article/details/89468702
代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/4/23 11:17
# @Author : Seven
# @Site :
# @File : demo.py
# @Software: PyCharm
import numpy as np
import tensorflow as tf
n_observations = 100
xs = np.linspace(-3, 3, n_observations)
ys = 0.8*xs + 0.1 + np.random.uniform(-0.5, 0.5, n_observations)
X = tf.placeholder(tf.float32, name='X')
Y = tf.placeholder(tf.float32, name='Y')
W = tf.Variable(tf.random_normal([1]), name='weight')
tf.summary.histogram('weight', W)
b = tf.Variable(tf.random_normal([1]), name='bias')
tf.summary.histogram('bias', b)
Y_pred = tf.add(tf.multiply(X, W), b)
loss = tf.square(Y - Y_pred, name='loss')
tf.summary.scalar('loss', tf.reshape(loss, []))
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
n_samples = xs.shape[0]
init = tf.global_variables_initializer()
with tf.Session() as sess:
# 记得初始化所有变量
sess.run(init)
merged = tf.summary.merge_all()
log_writer = tf.summary.FileWriter("./logs/linear_regression", sess.graph)
# 训练模型
for i in range(50):
total_loss = 0
for x, y in zip(xs, ys):
# 通过feed_dic把数据灌进去
_, loss_value, merged_summary = sess.run([optimizer, loss, merged], feed_dict={X: x, Y: y})
total_loss += loss_value
if i % 5 == 0:
print('Epoch {0}: {1}'.format(i, total_loss / n_samples))
log_writer.add_summary(merged_summary, i)
# 关闭writer
log_writer.close()
# 取出w和b的值
W, b = sess.run([W, b])
print(W, b)
print("W:"+str(W[0]))
print("b:"+str(b[0]))
输出结果:
Epoch 0: [1.3353578]
Epoch 5: [0.08880837]
Epoch 10: [0.08880833]
Epoch 15: [0.08880833]
Epoch 20: [0.08880833]
Epoch 25: [0.08880833]
Epoch 30: [0.08880833]
Epoch 35: [0.08880833]
Epoch 40: [0.08880833]
Epoch 45: [0.08880833]
[0.8415964] [0.09745394]
W:0.8415964
b:0.097453944
tensorboard
在终端执行代码:
tensorboard --logdir log (你保存文件所在位置)