Tensorflow2里使用Tensorboard——以MINIST手写数据集为例

此篇为纯代码


import tensorflow as tf
import datetime

batchSize = 128
EPOCH = 30

#载入fashion_mnist数据集
(x_train,y_train),(x_test,y_test) = tf.keras.datasets.fashion_mnist.load_data()

# print(x_train.shape,y_train.shape)

def preprocessing(x,y):
    x = tf.cast(x,dtype=tf.float32) / 255.
    y = tf.cast(y,dtype=tf.int32)
    return x,y


#将数据预处理、打乱和设置batch
dataBase = tf.data.Dataset.from_tensor_slices((x_train,y_train)).map(preprocessing)
dataBase = dataBase.shuffle(10000).batch(batchSize)

dataBase_test = tf.data.Dataset.from_tensor_slices((x_test,y_test)).map(preprocessing)
dataBase_test = dataBase_test.shuffle(10000).batch(batchSize)


#构建深度学习网络
modle = tf.keras.Sequential([
    tf.keras.layers.Dense(256,activation = tf.nn.leaky_relu),
    tf.keras.layers.Dense(128,activation=tf.nn.relu),
    tf.keras.layers.Dense(64,activation = tf.nn.relu),
    tf.keras.layers.Dense(32,activation = tf.nn.relu),
    tf.keras.layers.Dense(10)
])

modle.build(input_shape=[None,28*28])
modle.summary()
optimizer = tf.keras.optimizers.Adam(lr = 1e-3)

loss_metric = tf.keras.metrics.Mean()
acc_metric = tf.keras.metrics.Accuracy()

def main():
    totalCorrect = 0
    totalNum = 0
    for epoch in range(EPOCH):
        print("="*30,"Epoch",epoch+1,"="*30)
        for step,(x,y) in enumerate(dataBase):
            x = tf.reshape(x,[-1,28*28])
            with tf.GradientTape() as  tape:
                y = tf.one_hot(y,depth=10)
                logits = modle(x)
                loss = tf.reduce_mean(tf.keras.losses.categorical_crossentropy(y,logits,from_logits=True))
                grad = tape.gradient(loss,modle.trainable_variables)
                optimizer.apply_gradients(zip(grad,modle.trainable_variables))

            if step % 6000 == 0:
                loss_metric.update_state(loss)
                print("Loss CrossEntropy Value:",loss_metric.result().numpy())
                loss_metric.reset_state()

        for x_,y_ in dataBase_test:
            x_ = tf.reshape(x_,[-1,28*28])
            logits = modle(x_)
            prob = tf.nn.softmax(logits,axis=1)
            pred = tf.argmax(prob,axis=1)
            pred = tf.cast(pred,dtype=tf.int32)
            correct = tf.equal(pred,y_)
            correct = tf.reduce_sum(tf.cast(correct,dtype=tf.int32))
            totalCorrect += correct
            totalNum += batchSize
            acc  = float(totalCorrect / totalNum ) * 100
            acc_metric.update_state(y_,pred)
        print("Acc:" , acc_metric.result().numpy(),"%")
        acc_metric.reset_state()
		
		#定义一个Tensorboard监视的路径,这路径是图片存入的路径
        log_dir = "logs/" 
        
        #定义一个writer
        summary_writer = tf.summary.create_file_writer(log_dir)
        with summary_writer.as_default():
            tf.summary.scalar("Loss",float(loss),step = epoch)  #写入标量
            tf.summary.scalar("Acc",acc,step=epoch)
            image_all = tf.reshape(x_,[-1,28,28,1])
            tf.summary.image("Test Image",image_all,step=epoch,max_outputs=50) #写入图片

if __name__ == '__main__':
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值