热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【keras】用tensorboard监视CNN每一层的输出

fromkeras.modelsimportSequentialfromkeras.layersimportDense,Dropoutfromkeras.layersimp

from
keras.models import Sequential from keras.layers import Dense, Dropout from keras.layers import Conv1D, MaxPooling1D import scipy.io as sio import matplotlib.pyplot as plt from keras.utils import np_utils import keras import numpy as np from keras import regularizers from keras.callbacks import TensorBoard from keras.utils import plot_model from keras import backend as K from os.path import exists, join from os import makedirs batch_sizes = 256 nb_class = 10 nb_epochs = 2 log_dir = './bgbv2_log_dir' if not exists(log_dir): makedirs(log_dir) # input image dimensions img_rows, img_cols = 1, 2048 ''' 第一步 准备数据 ''' # matlab文件名 准备数据 file_name = u'G:/GANCode/CSWU/12k drive end vps/trainset/D/D_dataset.mat' original_data = sio.loadmat(file_name) X_train = original_data['x_train'] Y_train = original_data['y_train'] X_test = original_data['x_test'] Y_test = original_data['y_test'] channel = 1 X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], channel)) X_test = X_test.reshape((X_test.shape[0], X_test.shape[1], channel)) input_shape = (X_train.shape[1], channel) # 标签打乱 permutation = np.random.permutation(Y_train.shape[0]) X_train = X_train[permutation, :, :] Y_train = Y_train[permutation] permutation = np.random.permutation(Y_test.shape[0]) X_test = X_test[permutation, :, :] Y_test = Y_test[permutation] X_train = X_train.astype('float32') # astype SET AS TYPE INTO X_test = X_test.astype('float32') #X_train = (X_train+1)/2 #X_test = (X_test+1)/2 print('x_train shape:', X_train.shape) print(X_train.shape[0], 'train samples') print(X_test.shape[0], 'test samples') X_meta = X_test.reshape((X_test.shape[0], X_test.shape[1])) kkkkk=0 # save class labels to disk to color data points in TensorBoard accordingly with open(join(log_dir, 'metadata.tsv'), 'w') as f: np.savetxt(f, Y_test[:200]) ''' 第三步 设置标签 one-hot ''' Y_test = np_utils.to_categorical(Y_test, nb_class) # Label Y_train = np_utils.to_categorical(Y_train, nb_class) ''' 第四步 网络model ''' model = Sequential() model.add(Conv1D(64, 11, activation='relu', input_shape=(2048, 1))) model.add(Conv1D(64, 11, activation='relu')) model.add(MaxPooling1D(3)) model.add(Conv1D(128, 11, activation='relu')) model.add(Conv1D(128, 11, activation='relu')) ''' model.add(GlobalAveragePooling1D()) model.add(Dropout(0.5)) ''' model.add(MaxPooling1D(3)) model.add(Dropout(0.25)) model.add(keras.layers.Flatten()) model.add(Dense(1000, activation='relu')) model.add(Dense(100, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(10, activation='softmax')) embedding_layer_names = set(layer.name for layer in model.layers if layer.name.startswith('dense_')) # https://stackoverflow.com/questions/45265436/keras-save-image-embedding-of-the-mnist-data-set model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) callbacks = [keras.callbacks.TensorBoard( log_dir='bgbv2_log_dir', embeddings_layer_names=['dense_2'], #监视某一层,就要写某一层的名字,可以同时监视很多层,用上面的字典形式。 #embeddings_metadata='metadata.tsv', embeddings_freq=1, #histogram_freq=1, embeddings_data=X_test # 数据要和X_train保持一致。这里我用的是一维数据,(60000,2048,1)表示有6万个样本,每个样本有2048个长度,且每个样本有1个通道(1个传感器),换成多个通道的话,就要使用多个传感器的数据。 )] model.fit(X_train, Y_train, batch_size=batch_sizes, callbacks=callbacks, epochs=nb_epochs, verbose=1, validation_data=(X_test, Y_test)) xxasfs=1 # You can now launch tensorboard with `tensorboard --logdir=./logs` on your # command line and then go to http://localhost:6006/#projector to view the # embeddings # keras.callbacks.TensorBoard( # log_dir='./logs', # histogram_freq=0, # batch_size=32, # write_graph=True, # write_grads=False, # write_images=False, # embeddings_freq=0, # embeddings_layer_names=None, # embeddings_metadata=None, # embeddings_data=None, # update_freq='epoch')

坑死我了。

没有人教,自己琢磨了一天。

下面就能清楚地看见我们的三维图啦~用来写paper和PPT都是极好的素材。

PS:任何一个图层的输出:

https://stackoverflow.com/questions/41711190/keras-how-to-get-the-output-of-each-layer

参考1,keras Tensorboard官方说明

https://keras.io/callbacks/#tensorboard

from __future__ import print_function

from os import makedirs
from os.path import exists, join

import keras
from keras.callbacks import TensorBoard
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K

import numpy as np

batch_size = 128
num_classes = 10
epochs = 12
log_dir = './logs'

if not exists(log_dir):
    makedirs(log_dir)

# input image dimensions
img_rows, img_cols = 28, 28

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# save class labels to disk to color data points in TensorBoard accordingly
with open(join(log_dir, 'metadata.tsv'), 'w') as f:
    np.savetxt(f, y_test)

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

tensorboard = TensorBoard(batch_size=batch_size,
                          embeddings_freq=1,
                          embeddings_layer_names=['features'],
                          embeddings_metadata='metadata.tsv',
                          embeddings_data=x_test)

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu', name='features'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.fit(x_train, y_train,
          batch_size=batch_size,
          callbacks=[tensorboard],
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

# You can now launch tensorboard with `tensorboard --logdir=./logs` on your
# command line and then go to http://localhost:6006/#projector to view the
# embeddings

 

参考2,keras Mnist最后一层可视化。

https://keras.io/examples/tensorboard_embeddings_mnist/

参考3,IMDB影视评论最后一层可是化

import keras
from keras import layers
from keras.datasets import imdb
from keras.preprocessing import sequence
max_features = 500 # 原文为2000
max_len = 500
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
x_train = sequence.pad_sequences(x_train, maxlen=max_len)
x_test = sequence.pad_sequences(x_test, maxlen=max_len)

KK=x_train[:100].astype("float32")
MM=1

model = keras.models.Sequential()
model.add(layers.Embedding(max_features, 128, input_length=max_len, name='embed'))
model.add(layers.Conv1D(32, 7, activation='relu'))
model.add(layers.MaxPooling1D(5))
model.add(layers.Conv1D(32, 7, activation='relu'))
model.add(layers.GlobalMaxPooling1D())
model.add(layers.Dense(1))
model.summary()
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
callbacks = [keras.callbacks.TensorBoard(
                      log_dir='my_log_dir',
                      histogram_freq=1,
                      embeddings_freq=1,
                      embeddings_data=x_train[:100].astype("float32")
)]
history = model.fit(x_train, y_train,  epochs=20, batch_size=128, validation_split=0.2, callbacks=callbacks)


#补充 https://codeday.me/bug/20180924/267508.html

推荐阅读
  • Python处理Word文档的高效技巧
    本文详细介绍了如何使用Python处理Word文档,涵盖从基础操作到高级功能的各种技巧。我们将探讨如何生成文档、定义样式、提取表格数据以及处理超链接和图片等内容。 ... [详细]
  • PHP 过滤器详解
    本文深入探讨了 PHP 中的过滤器机制,包括常见的 $_SERVER 变量、filter_has_var() 函数、filter_id() 函数、filter_input() 函数及其数组形式、filter_list() 函数以及 filter_var() 和其数组形式。同时,详细介绍了各种过滤器的用途和用法。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 本文介绍了如何通过 Maven 依赖引入 SQLiteJDBC 和 HikariCP 包,从而在 Java 应用中高效地连接和操作 SQLite 数据库。文章提供了详细的代码示例,并解释了每个步骤的实现细节。 ... [详细]
  • 本文介绍如何使用Python进行文本处理,包括分词和生成词云图。通过整合多个文本文件、去除停用词并生成词云图,展示文本数据的可视化分析方法。 ... [详细]
  • 毕业设计:基于机器学习与深度学习的垃圾邮件(短信)分类算法实现
    本文详细介绍了如何使用机器学习和深度学习技术对垃圾邮件和短信进行分类。内容涵盖从数据集介绍、预处理、特征提取到模型训练与评估的完整流程,并提供了具体的代码示例和实验结果。 ... [详细]
  • 本文详细介绍如何在VSCode中配置自定义代码片段,使其具备与IDEA相似的代码生成快捷键功能。通过具体的Java和HTML代码片段示例,展示配置步骤及效果。 ... [详细]
  • 本文将探讨2015年RCTF竞赛中的一道PWN题目——shaxian,重点分析其利用Fastbin和堆溢出的技巧。通过详细解析代码流程和漏洞利用过程,帮助读者理解此类题目的破解方法。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 精选30本C# ASP.NET SQL中文PDF电子书合集
    欢迎订阅我们的技术博客,获取更多关于C#、ASP.NET和SQL的最新资讯和资源。 ... [详细]
  • MySQL索引详解与优化
    本文深入探讨了MySQL中的索引机制,包括索引的基本概念、优势与劣势、分类及其实现原理,并详细介绍了索引的使用场景和优化技巧。通过具体示例,帮助读者更好地理解和应用索引以提升数据库性能。 ... [详细]
  • 本文介绍如何使用 Angular 6 的 HttpClient 模块来获取 HTTP 响应头,包括代码示例和常见问题的解决方案。 ... [详细]
  • js常用方法(1)startWithJava代码varstartsWithfunction(str,regex){if(regexundefined||strundefined|| ... [详细]
author-avatar
手机用户26536338_53
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有