我是靠谱客的博主 悲凉大象,这篇文章主要介绍keras自然语言处理(三)——标准网络模型,现在分享给大家,希望可以做个参考。

1.3 标准网路模型

在开始使用函数式API时,最好先了解一些标准神经网路模型的定义,本节我们将介绍如何定义一个简单的多层感知器、卷积神经网路和递归神经网路。这些示例将为后面更详细的示例提供基础。

1.3.1 多层感知器

在这里,我们将定义一个用于二分类的多层感知器模型,该模型有10个输入,3个隐藏层,分别有16,32和16个神经元,以及1个输出层,每个隐藏层中使用relu激活函数,输出层使用sigmoid激活函数,用于二分类。

复制代码
1
2
3
4
5
6
7
8
9
10
11
from keras.utils import plot_model from keras.models import Model from keras.layers import Input,Dense input_tensor = Input(shape=(10,)) x1 = Dense(16,activation='relu')(input_tensor) x2 = Dense(32,activation='relu')(x1) x3 = Dense(16,activation='relu')(x2) output = Dense(1,activation='sigmoid')(x3) model = Model(inputs = input_tensor,outputs = output) plot_model(model,to_file='model_graph.png')

注意创建模型图需要安装pydot和pygraphviz。

1.3.2 卷积神经网络

在此我们定义用于图像分类的卷积神经网络,该网络接受黑白64×64图像作为输入,然后具有两个卷积和池化层的序列作为特征提取器,接着全连接层,并用sigmoid来激活

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from keras.utils import plot_model from keras.models import Model from keras.layers import Input,Dense,Conv2D,MaxPooling2D input_tensor = Input(shape=(64,64,1)) conv1 = Conv2D(32,kernel_size=4,activation='relu')(input_tensor) pool1 = MaxPooling2D(pool_size=(2,2))(conv1) conv2 = Conv2D(16,kernel_size=4,activation='relu')(pool1) pool2 = MaxPooling2D(pool_size=(2,2))(conv2) x = Dense(16,activation='relu')(pool2) output = Dense(1,activation='sigmoid')(x) model = Model(inputs=input_tensor,outputs = output) model.summary() plot_model(model,to_file='conv model.png')

model.summary():

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) (None, 64, 64, 1) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 61, 61, 32) 544 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 30, 30, 32) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 27, 27, 16) 8208 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 13, 13, 16) 0 _________________________________________________________________ dense_1 (Dense) (None, 13, 13, 16) 272 _________________________________________________________________ dense_2 (Dense) (None, 13, 13, 1) 17 ================================================================= Total params: 9,041 Trainable params: 9,041 Non-trainable params: 0 _________________________________________________________________

使用函数式API定义的一个简单的CNN模型图
在这里插入图片描述

1.3.3 递归神经网络

在此我们定义一个用于序列分类的的长短期记忆递归神经网络,该模型需要100个时间步长作为输入,该模型具有单个LSTM隐藏层以从序列中提取特征,接着是全连接层来介绍LSTM的输出,接着用二元预测的输出层

复制代码
1
2
3
4
5
6
7
8
9
10
11
from keras.utils import plot_model from keras.models import Model from keras.layers import Input,Dense,LSTM input_tensor = Input(shape=(100,1)) lstm = LSTM(16)(input_tensor) x = Dense(16,activation='relu')(lstm) output = Dense(1,activation='sigmoid')(x) model = Model(input_tensor,output) model.summary() plot_model(model,to_file='recurrent_neural_network.png')

model.summary():

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) (None, 100, 1) 0 _________________________________________________________________ lstm_1 (LSTM) (None, 16) 1152 _________________________________________________________________ dense_1 (Dense) (None, 16) 272 _________________________________________________________________ dense_2 (Dense) (None, 1) 17 ================================================================= Total params: 1,441 Trainable params: 1,441 Non-trainable params: 0 _________________________________________________________________

在这里插入图片描述

最后

以上就是悲凉大象最近收集整理的关于keras自然语言处理(三)——标准网络模型的全部内容,更多相关keras自然语言处理(三)——标准网络模型内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部