Keras大法(10)——在Keras中调用Tensorboard工具

Keras大法(10)——在Keras中调用Tensorboard工具


在这里插入图片描述

(一)前 言

Tensorboard是TensorFlow自带的一款强大的可视化工具,作为TensorFlow的一大“门面”,它可以对模型的训练过程进行十分生动地可视化,Tensorboard非常好用,当我们使用Keras以TensorFlow作为backend时,我们也可以通过callback方法调用它:

import keras.callbacks
tb_config = keras.callbacks.TensorBoard(
    log_dir='./logs', # 日志文件保存位置
    histogram_freq=0, # 计算各个层激活值直方图的频率(每多少个epoch计算一次),如果设置为0则不计算。
    batch_size=32, # 计算直方图的批处理数
    write_graph=True,# 是否在tensorboard中可视化计算图
    write_grads=False, # 是否在tensorboard中可视化梯度直方图
    write_images=False, # 是否写入模型权重以在tensorboard以图像形式进行可视化
    embeddings_freq=0, # 下列四个关于embedding的参数不常用,在此不做解释
    embeddings_layer_names=None, 
    embeddings_metadata=None,
    embeddings_data=None,
    update_freq='batch' # 更新频率)

(二)示例代码

import keras.callbacks
import numpy as np
import tensorflow as tf
from keras.datasets import mnist
from keras.layers.core import Activation, Dense, Dropout
from keras.models import Sequential
from keras.optimizers import SGD
from keras.utils import np_utils

# 导入数据
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1] * X_train.shape[2])
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1] * X_test.shape[2])
X_train = X_train.astype(np.float32)
X_train /= 255
X_test = X_test.astype(np.float32)
X_test /= 255
# 标签数据one-hot化
Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)

# 定义模型
model = Sequential()
model.add(Dense(512, input_shape=(784,), init='normal', name='dense1')) 
model.add(Activation('relu', name='relu1'))
model.add(Dropout(0.2, name='dropout1'))
model.add(Dense(512, init='normal', name='dense2'))
model.add(Activation('relu', name='relu2'))
model.add(Dropout(0.2, name='dropout2'))
model.add(Dense(10, init='normal', name='dense3'))
model.add(Activation('softmax', name='softmax1'))
model.summary()
 
model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.001), metrics=['accuracy'])
 
tb_config = keras.callbacks.TensorBoard(
    log_dir= 'C:/Users/12394/PycharmProjects/Keras/logs', 
    write_images= 1, 
    histogram_freq= 1)

cbks = [tb_config]
history = model.fit(
    X_train, Y_train, 
    batch_size= 128, 
    epochs= 3, 
    verbose= 1, 
    callbacks= cbks, 
    validation_data= (X_test, Y_test))

model.evaluate(X_test, Y_test, verbose= 1)

# 输出:
Epoch 1/3
60000/60000 [==============================] - 4s 73us/step - loss: 2.1546 - acc: 0.2693 - val_loss: 1.9333 - val_acc: 0.5596
Epoch 2/3
60000/60000 [==============================] - 6s 92us/step - loss: 1.8351 - acc: 0.5164 - val_loss: 1.6099 - val_acc: 0.7067
Epoch 3/3
60000/60000 [==============================] - 5s 90us/step - loss: 1.5474 - acc: 0.6314 - val_loss: 1.3154 - val_acc: 0.7660

(三)读取训练日志

使用以下命令读取日志:

tensorboard.exe --logdir="你的日志保存位置"

如下图所示:
在这里插入图片描述

(三)总 结

在这一节中我们介绍了如何在Keras中使用tensorboard,有任何的问题可以在评论区留言,我会尽快回复,谢谢支持!

发布了80 篇原创文章 · 获赞 74 · 访问量 32万+
展开阅读全文

tensorboard 可视化时 embedding没有数据怎么回事?

03-09

``` import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_data as input_data from tensorflow.contrib.tensorboard.plugins import projector mnist=input_data.read_data_sets('mnist_data/', one_hot=True) tf.reset_default_graph() max_step=1001 image_num=3000 DIR="D:/Jupyter" sess=tf.Session() embedding=tf.Variable(tf.stack(mnist.test.images[:image_num]),trainable=False,name="embedding") def variable_sumaries(var): with tf.name_scope("sumaries"): mean=tf.reduce_mean(var) tf.summary.scalar("mean",mean) with tf.name_scope("stddev"): stddev=tf.sqrt(tf.reduce_mean(mean-var)) tf.summary.scalar("stddev",stddev) tf.summary.scalar("max",tf.reduce_max(var)) tf.summary.scalar("min",tf.reduce_min(var)) tf.summary.histogram("histogram",var) with tf.name_scope("input"): x = tf.placeholder(tf.float32, [None, 784],name="x-input") y = tf.placeholder(tf.float32, [None,10],name="y-input") with tf.name_scope("input_reshape"): image_reshape_input=tf.reshape(x,shape=[-1,28,28,1]) tf.summary.image("input",image_reshape_input,10) with tf.name_scope("layer"): with tf.name_scope("weights"): W = tf.Variable(tf.zeros([784,10]),tf.float32,name="W") variable_sumaries(W) with tf.name_scope("biases"): b= tf.Variable(tf.zeros([10]),tf.float32,name="b") variable_sumaries(b) with tf.name_scope("wx_plus_b"): wx_plus_b=tf.matmul(x,W)+b with tf.name_scope("softmax"): softmax=tf.nn.softmax(wx_plus_b) sess.run(tf.global_variables_initializer()) with tf.name_scope("loss"): loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y,logits=softmax)) tf.summary.scalar("loss",loss) with tf.name_scope("train"): train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss) with tf.name_scope("accuracy"): with tf.name_scope("correct_prediction"): correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(softmax,1)) with tf.name_scope("accuracy"): accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) tf.summary.scalar("accuracy",accuracy) if tf.gfile.Exists(DIR+"/projector/projector/metadata.csv"): tf.gfile.DeleteRecursively(DIR+"/projector/projector/metadata.csv") with open(DIR+"/projector/projector/metadata.csv","w") as f: lables= sess.run(tf.argmax(mnist.test.labels[:],1)) for i in range(image_num): f.write(str(lables[i])+'\n') merged=tf.summary.merge_all() projector_writer=tf.summary.FileWriter(DIR+"/projector/projector",sess.graph) saver=tf.train.Saver() config=projector.ProjectorConfig() embed=config.embeddings.add() embed.tensor_name=embedding.name embed.metadata_path=DIR+"/projector/projector/metadata.csv" embed.sprite.image_path=DIR+"/projector/data/mnist_spliter.png" embed.sprite.single_image_dim.extend([28,28]) projector.visualize_embeddings(projector_writer,config) for i in range(max_step): batch_xs, batch_ys = mnist.train.next_batch(100) run_options=tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata=tf.RunMetadata() summary, _ =sess.run([merged,train_step],feed_dict={x:batch_xs,y:batch_ys},options=run_options,run_metadata=run_metadata) projector_writer.add_run_metadata(run_metadata,'step %03d' % i) projector_writer.add_summary(summary,i) if i% 100==0: print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})) saver.save(sess, DIR+"/projector/projector/a_model.ckpt",global_step=max_step) projector_writer.close() sess.close() ``` ![图片说明](https://img-ask.csdn.net/upload/201803/09/1520567615_834707.jpg) 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览