- import tensorflow as tf
with tf.name_scope("data-set"):
rlog = "./singlecell"
x = tf.constant(2.0, name = "input")
w = tf.Variable(0.8, name = "weight")
y_predict = tf.multiply(w, x, name = "output")
y = tf.constant(0.0, name = "real_result")
loss = tf.pow(y_predict - y, 2, name = "loss")
with tf.name_scope("train"):
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
with tf.name_scope("summar"):
for value in [x, w, y_predict, y, loss]:
tf.summary.scalar(value.op.name, value)
summaries = tf.summary.merge_all()
ss = tf.Session()
xsum = tf.summary.FileWriter(rlog, ss.graph)
init = tf.global_variables_initializer()
ss.run(init)
with tf.name_scope("session"):
for i in range(1000):
xdat = ss.run(summaries)
xsum.add_summary(xdat, i)
xdat = ss.run(train_step)
x2 = ss.run(x)
y2 = ss.run(y_predict)
w2 = ss.run(w)
s2 = ss.run(loss)
if i % 100 == 0:
print(i, "#, y2:",y2, ", s2:", s2, ", x2:", x2, ", w2:", w2 )
ss.close()
结果是:
0 #, y2: 1.5872 , s2: 2.5192 , x2: 2.0 , w2: 0.7936
100 #, y2: 0.710885 , s2: 0.505357 , x2: 2.0 , w2: 0.355442
200 #, y2: 0.318395 , s2: 0.101375 , x2: 2.0 , w2: 0.159197
300 #, y2: 0.142605 , s2: 0.020336 , x2: 2.0 , w2: 0.0713023
400 #, y2: 0.0638705 , s2: 0.00407944 , x2: 2.0 , w2: 0.0319353
500 #, y2: 0.0286067 , s2: 0.000818343 , x2: 2.0 , w2: 0.0143034
600 #, y2: 0.0128125 , s2: 0.000164161 , x2: 2.0 , w2: 0.00640627
700 #, y2: 0.00573855 , s2: 3.2931e-05 , x2: 2.0 , w2: 0.00286928
800 #, y2: 0.00257022 , s2: 6.60601e-06 , x2: 2.0 , w2: 0.00128511
900 #, y2: 0.00115116 , s2: 1.32518e-06 , x2: 2.0 , w2: 0.000575581
在cmd窗口输入: tensorboard -logdir=C:\Users\wx\tensorflowcode\singlecell 然后在浏览器的地址栏中输入: http://computer:6006 可以看到如下结果: 整个训练的数据流图如下:
|