几个概念
batch_size、epoch
sample_size = 20000; //样本为20000个
batch_size = 64;//64个样本为一批进行一次训练
epoch = 200; //全部样本训练完一次,记1个epoch
Tensorflow训练
TensorFlow系列03——TensorFlow基本概念Tensor理解
tflearn分类-17flower
Deep Learning-TensorFlow (10) CNN卷积神经网络_ TFLearn 快速搭建深度学习模型
tensorboard使用
安装
安装tensorflow库后,就包含tensorboard。如果没有tensorboard.exe,重新卸载并安装tensorflow。
pip uninstall tensorflow
pip install tensorflow
数据准备
保存代码为test.py.
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'):
weight = tf.Variable(tf.random_uniform([1],-1.0,1.0))
bias = tf.Variable(tf.zeros([1]))
##get y_prediction
with tf.name_scope('y_prediction'):
y_prediction = weight*x_data+bias
##compute the loss
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.square(y_data-y_prediction))
##creat optimizer
optimizer = tf.train.GradientDescentOptimizer(0.5)
#creat train ,minimize the loss
with tf.name_scope('train'):
train = optimizer.minimize(loss)
#creat init
with tf.name_scope('init'):
init = tf.global_variables_initializer()
##creat a Session
sess = tf.Session()
##initialize
##注意设置数据输出目录 D:/logs
writer = tf.summary.FileWriter("D:/logs", sess.graph)
sess.run(init)
## Loop
for step in range(101):
sess.run(train)
if step %10==0 :
print step ,'weight:',sess.run(weight),'bias:',sess.run(bias)
运行结果为:
使用tensorboard
执行命令运行服务器
以下任一种方式都可以:
/*方式一*/
tensorboard --logdir=d:/logs
/*方式二*/
python main.py --logdir=d:/logs
访问
从上图执行结果中可以看出,打开浏览器访问:localhost:6006,就可以看到图形了。
出现的问题
网页显示空白
A:这个原因是浏览器版本太低,我之前用的4.9版本不行,更新到6.3可以了。
网页没有数据“No graph…”
A:数据路径设置不对。可以设置到D盘根目录。
参考:tensorflow笔记(三)之 tensorboard的使用
TensorFlow深度学习笔记 Tensorboard入门