1、tensorboard
"-----------------矩阵相乘----------------"
import tensorflow as tf
with tf.name_scope('graph') as scope:
matrix1 = tf.constant([[3., 3.]], name='matrix1')
matrix2 = tf.constant([[2.], [2.]], name='matrix2')
product = tf.matmul(matrix1, matrix2, name='product')
sess = tf.Session()
init = tf.global_variables_initializer()
writer = tf.summary.FileWriter("./logs", sess.graph)
sess.run(init)
print(sess.run(product))
# "----------------线性拟合----------------"
# import tensorflow as tf
# import numpy as np
#
# ## prepare the original data
# with tf.name_scope('data'):
# x_data = np.random.rand(100).astype(np.float32)
# y_data = 0.3 * x_data + 0.1
# ##creat parameters
# with tf.name_scope('parameters'):
# with tf.name_scope('weights'):
# weight = tf.Variable(tf.random_uniform([1],-1.0,1.0))
# tf.summary.histogram('weight', weight)
# with tf.name_scope('biases'):
# bias = tf.Variable(tf.zeros([1]))
# tf.summary.histogram('bias',bias)
#
# #预测y值_
# with tf.name_scope('y_prediction'):
# y_prediction = weight * x_data + bias
#
# #计算loss
# with tf.name_scope('loss'):
# loss = tf.reduce_mean(tf.square(y_data - y_prediction))
# tf.summary.scalar('loss',loss)
#
# #创建优化器
# optimizer = tf.train.GradientDescentOptimizer(0.5)
#
# #最小损失率
# with tf.name_scope('train'):
# train = optimizer.minimize(loss)
#
# #所有变量初始化
# with tf.name_scope('init'):
# init = tf.global_variables_initializer()
# #创建会话
# sess = tf.Session()
#
# #合并所有信息
# merged = tf.summary.merge_all()
#
# #保存图
# writer = tf.summary.FileWriter("logs/", sess.graph)
# sess.run(init)
#
# #训练
# for step in range(101):
# sess.run(train)
# rs = sess.run(merged)
# writer.add_summary(rs, step)
# print("loss: %df"%sess.run(loss), "step: %d"%step)