Kears中的可视化

1.使用 keras 中的Tensorboard实现可视化

注意:

np.array ( ), 如果要使用数组的话,必须要用 【】

1.对 .shape 的理解:从 第一个大括号 看最里面有多少层,从里往外看。

import numpy as np

a=np.array([[1,2,3],[4,5,6]])
print(a.shape)
print(a)

b=np.array([1,2,3])
print(b.shape)

输出是:
(2, 3)
[[1 2 3]
 [4 5 6]]
(3,)

2.对 np.expand_dims(a,axis=0) 的理解

代码:

a=np.array([[1,2,3],[4,5,6]])
print(a.shape)
print(a)

print("*************************")

b=np.expand_dims(a,axis=0)
print(b.shape)
print(b)

print("*********************")

b=np.expand_dims(a,axis=1)
print(b.shape)
print(b)

结果如下图:
在这里插入图片描述
3.对Flatten层的理解:

Flatten层用来将输入“压平”,即把多维的输入一维化,常用在从卷积层到全连接层的过渡。Flatten不影响batch的大小。
在这里插入图片描述
在pycharm中如何用 tensorboard 可视化:

tensorboard可视化

2.代码:

import numpy as np

from keras.layers import Input, Dense, Dropout, Activation,Conv2D,MaxPool2D,Flatten

#Flatten 用来输入“压平”,把多维的输入一维化,常用在从卷积层到全连接层的过度,Flatten不影响batch大小。

from keras.datasets import mnist
from keras.models import Model
from keras.utils import to_categorical

#可以查看模型的损失函数和准确率
#一般acc是用在分类问题上,目标检测问题上一般只使用loss
from keras.callbacks import TensorBoard  #在回掉库里导入 Tensorboard


if __name__=="__main__":
    (x_train,y_train),(x_test,y_test) = mnist.load_data()

    x_train=np.expand_dims(x_train,axis=-1)
    x_test=np.expand_dims(x_test,axis=-1)
    y_train=to_categorical(y_train,num_classes=10)
    y_test=to_categorical(y_test,num_classes=10)
    batch_size=128
    epochs=10

    inputs = Input([28,28,1])
    x = Conv2D(32, (5,5), activation='relu')(inputs)
    x = Conv2D(64, (5,5), activation='relu')(x)
    x = MaxPool2D(pool_size=(2,2))(x)
    x = Flatten()(x)
    x = Dense(128, activation='relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(10, activation='softmax')(x)

    model = Model(inputs,x)

    model.compile(loss='categorical_crossentropy', optimizer="adam",metrics=['acc'])
    Tensorboard= TensorBoard(#tensorboard是在这里创建的。
        log_dir="./TensorboardModel",  #模型中日志文件保存的位置
        #如果只要获得acc和loss 可以直接把后边的 改成=0, =flase 即可
        histogram_freq=1,              #对于模型中 各个层计算激活函数的值和模型权重直方图频率。
        write_grads=True    #是否进行可视化图像
    )
    history=model.fit(x_train, y_train, batch_size=batch_size,
                      epochs=epochs, shuffle=True, validation_split=0.2,callbacks=[Tensorboard])

最后可以运用命令行,查看最后生成的日志文件,能可视化损失函数和精度。具体我也没调出来,用的时候再调吧。

参考链接:
b站bubbliiing

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值