Tensorflow2——tensorboard可视化

2021-06-21 17:41:02 浏览数 (1)

下面通过手写数字数据集来介绍如何使用tensorboard可视化 可以两种方法,一种是再notebook里,还有一种是网页打开。 jupyter notebook 调试

代码语言:javascript复制
import tensorflow as tf 
import numpy as np
import datetime
import os
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)

(train_images,train_labels),(test_images,test_labels)=tf.keras.datasets.mnist.load_data()

train_images=train_images/255
test_images=test_images/255


train_images=tf.expand_dims(train_images,-1)
test_images=tf.expand_dims(test_images,-1)

train_labels=tf.cast(train_labels,tf.int64)
test_labels=tf.cast(test_labels,tf.int64)


train_images=tf.cast(train_images,tf.float32)
test_images=tf.cast(test_images,tf.float32)


train_dataset=tf.data.Dataset.from_tensor_slices((train_images,train_labels))

test_dataset=tf.data.Dataset.from_tensor_slices((test_images,test_labels))

train_dataset=train_dataset.shuffle(60000).repeat().batch(128)

test_dataset=test_dataset.repeat().batch(128)


model=tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(16,(3,3),activation="relu",input_shape=(None,None,1)))
model.add(tf.keras.layers.Conv2D(32,(3,3),activation="relu"))
model.add(tf.keras.layers.GlobalAveragePooling2D())
model.add(tf.keras.layers.Dense(10,activation="softmax"))

model.compile(optimizer="adam",loss="sparse_categorical_crossentropy",metrics=["acc"])

log_dir=os.path.join("logs",datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))  #什么时候运行就会产生这么一个时间

tensorbord_callback=tf.keras.callbacks.TensorBoard(log_dir,histogram_freq=1)  #tensorbord_callback的回调函数


model.fit(train_dataset,epochs=5,steps_per_epoch=60000//128,validation_data=test_dataset,validation_steps=10000//128,callbacks=[tensorbord_callback])

利用

代码语言:javascript复制
%load_ext tensorboard
%matplotlib inline  
%tensorboard --logdir logs

这个logs指的是保存的文件夹的路径

或者是用网页版打开 定位到logs文件夹 输入命令:tensorboard --logdir logs

复制这个网址在浏览器打开

0 人点赞