摘要:网络结构来自固定随机数种子以复现结果创建维向量,并扩展维度适应对输入的要求,的大小为定义卷积层卷积核数量为卷积核大小为定义最大化池化层平铺层,调整维度适应全链接层定义全链接层编译模型打印层的输出打印网络结构最终输出如下卷积结果网络结
网络结构来自https://github.com/nfmcclure/...
Conv1Dimport numpy as np import keras # 固定随机数种子以复现结果 seed=13 np.random.seed(seed) # 创建 1 维向量,并扩展维度适应 Keras 对输入的要求, data_1d 的大小为 (1, 25, 1) data_1d = np.random.normal(size=25) data_1d = np.expand_dims(data_1d, 0) data_1d = np.expand_dims(data_1d, 2) # 定义卷积层 filters = 1 # 卷积核数量为 1 kernel_size = 5 # 卷积核大小为 5 convolution_1d_layer = keras.layers.convolutional.Conv1D(filters, kernel_size, strides=1, padding="valid", input_shape=(25, 1), activation="relu", name="convolution_1d_layer") # 定义最大化池化层 max_pooling_layer = keras.layers.MaxPool1D(pool_size=5, strides=1, padding="valid", name="max_pooling_layer") # 平铺层,调整维度适应全链接层 reshape_layer = keras.layers.core.Flatten(name="reshape_layer") # 定义全链接层 full_connect_layer = keras.layers.Dense(5, kernel_initializer=keras.initializers.RandomNormal(mean=0.0, stddev=0.1, seed=seed), bias_initializer="random_normal", use_bias=True, name="full_connect_layer") # 编译模型 model = keras.Sequential() model.add(convolution_1d_layer) model.add(max_pooling_layer) model.add(reshape_layer) model.add(full_connect_layer) # 打印 full_connect_layer 层的输出 output = keras.Model(inputs=model.input, outputs=model.get_layer("full_connect_layer").output).predict(data_1d) print(output) # 打印网络结构 print(model.summary())
最终输出如下
======================卷积结果========================= [[-0.0131043 -0.11734447 0.13395447 -0.75453871 -0.69782442]] ======================网络结构========================= _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= convolution_1d_layer (Conv1D (None, 21, 1) 6 _________________________________________________________________ max_pooling_layer (MaxPoolin (None, 17, 1) 0 _________________________________________________________________ reshape_layer (Flatten) (None, 17) 0 _________________________________________________________________ full_connect_layer (Dense) (None, 5) 90 ================================================================= Total params: 96 Trainable params: 96 Non-trainable params: 0 _________________________________________________________________ NoneConv2D
data_size = [10, 10] data_2d = np.random.normal(size=data_size) data_2d = np.expand_dims(data_2d, 0) data_2d = np.expand_dims(data_2d, 3) print data_2d.shape # 定义卷积层 conv_size = 2 conv_stride_size = 2 convolution_2d_layer = keras.layers.Conv2D(filters=1, kernel_size=(conv_size, conv_size), strides=(conv_stride_size, conv_stride_size), input_shape=(data_size[0], data_size[0], 1)) # convolution_2d_layer = keras.layers.Conv2D(filter=1, kernel_size=kernel, strides=[1,1], padding="valid", activation="relu", name="convolution_2d_layer", input_shape=(1, data_size[0], data_size[0])) # 定义最大化池化层 pooling_size = (2, 2) max_pooling_2d_layer = keras.layers.MaxPool2D(pool_size=pooling_size, strides=1, padding="valid", name="max_pooling_2d_layer") # 平铺层,调整维度适应全链接层 reshape_layer = keras.layers.core.Flatten(name="reshape_layer") # 定义全链接层 full_connect_layer = keras.layers.Dense(5, kernel_initializer=keras.initializers.RandomNormal(mean=0.0, stddev=0.1, seed=seed), bias_initializer="random_normal", use_bias=True, name="full_connect_layer") model_2d = keras.Sequential() model_2d.add(convolution_2d_layer) model_2d.add(max_pooling_2d_layer) model_2d.add(reshape_layer) model_2d.add(full_connect_layer) # 打印 full_connect_layer 层的输出 output = keras.Model(inputs=model_2d.input, outputs=model_2d.get_layer("full_connect_layer").output).predict(data_2d) print("======================卷积结果=========================") print(output) # 打印网络结构 print("======================网络结构=========================") print(model_2d.summary())
输出
======================卷积结果========================= [[ 0.30173036 -0.10435719 -0.03354734 0.24000235 -0.09962128]] ======================网络结构========================= _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_1 (Conv2D) (None, 5, 5, 1) 5 _________________________________________________________________ max_pooling_2d_layer (MaxPoo (None, 4, 4, 1) 0 _________________________________________________________________ reshape_layer (Flatten) (None, 16) 0 _________________________________________________________________ full_connect_layer (Dense) (None, 5) 85 ================================================================= Total params: 90 Trainable params: 90 Non-trainable params: 0 _________________________________________________________________ None
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/41192.html
摘要:一维卷积常用于序列模型,自然语言处理领域。三维卷积这里采用代数的方式对三维卷积进行介绍,具体思想与一维卷积二维卷积相同。 由于计算机视觉的大红大紫,二维卷积的用处范围最广。因此本文首先介绍二维卷积,之后再介绍一维卷积与三维卷积的具体流程,并描述其各自的具体应用。1、二维卷积 • 图中的输入的数据维度为 14 × 14 ,过滤器大小为 5 × 5,二者做卷积,输出的数据维度为 10 × 1...
摘要:可以这样说,库使得创建深度学习模型变得快速且简单。在本教程中,你将了解如何用中更具灵活性的函数式来定义深度学习模型。如何使用函数式定义简单的多层感知器卷积神经网络以及循环神经网络模型。 可以这样说,Keras Python库使得创建深度学习模型变得快速且简单。序列API使得你能够为大多数问题逐层创建模型。当然它也是有局限性的,那就是它并不能让你创建拥有共享层或具有多个输入或输出的模型。Ker...
摘要:目前,是成长最快的一种深度学习框架。这将是对社区发展的一个巨大的推动作用。以下代码是如何开始导入和构建序列模型。现在,我们来构建一个简单的线性回归模型。 作者:chen_h微信号 & QQ:862251340微信公众号:coderpai简书地址:https://www.jianshu.com/p/205... Keras 是提供一些高可用的 Python API ,能帮助你快速的构建...
阅读 2477·2021-09-22 16:05
阅读 2960·2021-09-10 11:24
阅读 3631·2019-08-30 12:47
阅读 2940·2019-08-29 15:42
阅读 3378·2019-08-29 15:32
阅读 1944·2019-08-26 11:48
阅读 1081·2019-08-23 14:40
阅读 902·2019-08-23 14:33