slim 搭建rnn_使用Keras搭建cnn+rnn, BRNN,DRNN等模型

Keras api 提前知道:

Normalize the activations of the previous layer at each batch, i.e. applies a transformation that maintains the mean activation close to 0 and the activation standard deviation close to 1.

TimeDistributed, 总的来说TimeDistributed层在每个时间步上均操作了Dense,比单一dense操作更能发现数据集中比较复杂的模式

简单的理解:

Bidrectional, keras封装了的双向包装函数。

Keras 相关导入

from keras import backend as K

from keras.models import Model

from keras.layers import (BatchNormalization, Conv1D, Conv2D, Dense, Input, Dropout,

TimeDistributed, Activation, Bidirectional, SimpleRNN, GRU, LSTM, MaxPooling1D, Flatten, MaxPooling2D)

RNN

def simple_rnn_model(input_dim, output_dim=29):

""" Build a recurrent network for speech

"""

# Main acoustic input

input_data = Input(name='the_input', shape=(None, input_dim))

# Add recurrent layer

simp_rnn = GRU(output_dim, return_sequences=True,

implementation=2, name='rnn')(input_data)

# Add softmax activation layer

y_pred = Activation('softmax', name='softmax')(simp_rnn)

# Specify the model

model = Model(inputs=input_data, outputs=y_pred)

model.output_length = lambda x: x

print(model.summary())

return model

或者直接使用Keras SimpleRNN

rnn + timedistribute

def rnn_model(input_dim, units, activation, output_dim=29):

""" Build a recurrent network for speech

"""

# Main acoustic input

input_data = Input(name='the_input', shape=(None, input_dim))

# Add recurrent layer

simp_rnn = LSTM(units, activation=activation,

return_sequences=True, implementation=2, name='rnn')(input_data)

# TODO: Add batch normalization

bn_rnn = BatchNormalization()(simp_rnn)

# TODO: Add a TimeDistributed(Dense(output_dim)) layer

time_dense = TimeDistributed(Dense(output_dim))(bn_rnn)

# Add softmax activation layer

y_pred = Activation('softmax', name='softmax', )(time_dense)

# Specify the model

model = Model(inputs=input_data, outputs=y_pred)

model.output_length = lambda x: x

print(model.summary())

return model

cnn+rnn+timedistribute

def cnn_output_length(input_length, filter_size, border_mode, stride,

dilation=1):

""" Compute the length of the output sequence after 1D convolution along

time. Note that this function is in line with the function used in

Convolution1D class from Keras.

Params:

input_length (int): Length of the input sequence.

filter_size (int): Width of the convolution kernel.

border_mode (str): Only support `same` or `valid`.

stride (int): Stride size used in 1D convolution.

dilation (int)

"""

if input_length is None:

return None

assert border_mode in {'same', 'valid', 'causal', 'full'}

dilated_filter_size = filter_size + (filter_size - 1) * (dilation - 1)

if border_mode == 'same':

output_length = input_length

elif border_mode == 'valid':

output_length = input_length - dilated_filter_size + 1

elif border_mode == 'causal':

output_length = input_length

elif border_mode == 'full':

output_length = input_length + dilated_filter_size - 1

return (output_length + stride - 1) // stride

def cnn_rnn_model(input_dim, filters, kernel_size, conv_stride,

conv_border_mode, units, output_dim=29):

""" Build a recurrent + convolutional network for speech

"""

# Main acoustic input

input_data = Input(name='the_input', shape=(None, input_dim))

# Add convolutional layer

conv_1d = Conv1D(filters, kernel_size,

strides=conv_stride,

padding=conv_border_mode,

activation='relu',

name='conv1d')(input_data)

# Add batch normalization

bn_cnn = BatchNormalization(name='bn_conv_1d')(conv_1d)

# Add a recurrent layer

simp_rnn = SimpleRNN(units, activation='relu',

return_sequences=True, implementation=2, name='rnn')(bn_cnn)

# TODO: Add batch normalization

bn_rnn = BatchNormalization()(simp_rnn)

# TODO: Add a TimeDistributed(Dense(output_dim)) layer

time_dense = TimeDistributed(Dense(output_dim))(bn_rnn)

# Add softmax activation layer

y_pred = Activation('softmax', name='softmax')(time_dense)

# Specify the model

model = Model(inputs=input_data, outputs=y_pred)

model.output_length = lambda x: cnn_output_length(

x, kernel_size, conv_border_mode, conv_stride)

print(model.summary())

return model

deep rnn + timedistribute

def deep_rnn_model(input_dim, units, recur_layers, output_dim=29):

""" Build a deep recurrent network for speech

"""

# Main acoustic input

input_data = Input(name='the_input', shape=(None, input_dim))

# TODO: Add recurrent layers, each with batch normalization

# Add a recurrent layer

for i in range(recur_layers):

if i:

simp_rnn = GRU(units, return_sequences=True,

implementation=2)(simp_rnn)

else:

simp_rnn = GRU(units, return_sequences=True,

implementation=2)(input_data)

# TODO: Add batch normalization

bn_rnn = BatchNormalization()(simp_rnn)

# TODO: Add a TimeDistributed(Dense(output_dim)) layer

time_dense = TimeDistributed(Dense(output_dim))(bn_rnn)

# Add softmax activation layer

y_pred = Activation('softmax', name='softmax')(time_dense)

# Specify the model

model = Model(inputs=input_data, outputs=y_pred)

model.output_length = lambda x: x

print(model.summary())

return model

bidirection rnn + timedistribute

def bidirectional_rnn_model(input_dim, units, output_dim=29):

""" Build a bidirectional recurrent network for speech

"""

# Main acoustic input

input_data = Input(name='the_input', shape=(None, input_dim))

# TODO: Add bidirectional recurrent layer

bidir_rnn = Bidirectional(GRU(units, return_sequences=True))(input_data)

bidir_rnn = BatchNormalization()(bidir_rnn)

# TODO: Add a TimeDistributed(Dense(output_dim)) layer

time_dense = TimeDistributed(Dense(output_dim))(bidir_rnn)

# Add softmax activation layer

y_pred = Activation('softmax', name='softmax')(time_dense)

# Specify the model

model = Model(inputs=input_data, outputs=y_pred)

model.output_length = lambda x: x

print(model.summary())

return model

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/501676.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JAVA入门级教学之(零基础了解计算机)

JAVA小白入门级教学(零基础了解计算机基础知识) 多动手自己操作,一点一点积累 首先我们了解一下计算机的基础知识 1.计算机构成(基础): 硬件:CPU、内存、硬盘、鼠标、显示器、鼠标等&#x…

oppo 手机侧滑快捷菜单_关于oppo手机菜单键调出的方法,原来是这样的

用OPPO手机朋友们,今天来教大家如何调出手机菜单键。工具/材料OPPO手机操作方法01首先,在手机桌面上找到设置,点击进入。02进入设置找到【面部与密码】,并点击它。03然后点击【关闭密码】,要想设置手机菜单键&#xff…

JAVA入门级教学之(JAVA程序的加载和运行)

JAVA程序的加载和运行 多思考多动脑(边参考文章最后的示意图,边按步骤理解) 1.JAVA程序的加载和运行包括两个非常重要的阶段: 编译阶段运行阶段 2.我们先来了解一下什么是编译阶段: 首先,我们自己动手敲…

abstract类中可以有private的成员_C++|static成员与单例模式

如果需要一个全局对象,如对话框、系统日志、显卡等设备的驱动程序对象、一台PC连接一个键盘等。这样的全局对象只能是一个且是全局的,这就是单例模式,如何实现呢?1 不能在类外部通过构造函数新建对象:构造函数的访问方…

JAVA入门级教学之(JDK安装-JDK、JRE、JVM)

1.下面我们开支安装JDK【JDK开源、免费】 安装地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 选择 Windows x64-->jdk-8u221-window-x64.exe 下载 下载完成后 双击JDK安装包-->进入安装界面(…

vim显示python嵌套级_在Vim中为Python突出显示语法

Vim中针对Python的语法突出显示(目标是Ubuntu 12.10)这是有关如何在Ubuntu 12.10的Python中设置语法突出显示的演练。 你所看到的就是你得到的:[https://github.com/sentientmachine/Pretty-Vim-Python/]一旦掌握了如何为特定用户将配色方案和语法突出显示注入到vi编…

JAVA入门级教学之(编写第一个HelloWorld程序)

目录 1.创建一个HelloWorld.java文件 2.用记事本打开HelloWorld.java 3.接下来我们要完成xxx.java-->xxx.class的步骤 怎么解决javac不可用的问题: 4.配置环境变量path 怎么配环境变量Path? 5.接下来就是在DOS命令窗口下进行编译【javac java源…

springboot api文档_SpringCloud/SpringBoot - 自动生成API文档

SpringCloud/SpringBoot 的项目一定要前后分离,这就需要一个API文档生成的工具,后端一定要和前端或者是移动端对接接口,那么问题来了,接口是不是要自己写给他们看,一般的会采用Excel或者Word来写,高级一点的…

JAVA入门级教学之(classpath的配置)

目录 JAVA入门级教学之(classpath的配置) 1.打开DOS命令窗口,执行java HelloWorld 2.疑问?:为什么会出现找不到HelloWorld.class文件? 3.解释: JAVA入门级教学之(classpath的配置…

语法手册_程序员必备——SQL语法速成手册

一、基本概念数据库术语数据库(database) - 保存有组织的数据的容器(通常是一个文件或一组文件)。数据表(table) - 某种特定类型数据的结构化清单。模式(schema) - 关于数据库和表的布局及特性的信息。模式定义了数据在表中如何存储,包含存储什么样的数据&#xff0…

JAVA入门级教学之(JAVA注释)

目录 JAVA入门级教学之(JAVA注释) 1.关于java源程序中的注释: 2.java中的注释怎么写? JAVA入门级教学之(JAVA注释) 1.关于java源程序中的注释: 什么是注释?注释的作用是什么&…

JAVA入门级教学之(你是否理解HelloWorld的这段经典的代码的注释)

//public表示公开的 //class表示一个类 //HelloWorld表示一个类名public class HelloWorld{//表示一个公开的类,类名是HelloWorld//在这个大括号内写的是类体,类体中不能直接编写java语句【除声明变量之外】,否则会报错/*public表示公开的sta…

devtools的ctrl加r_Chrome DevTools调试技巧

【1】DevTools触发伪类右键单击Elements面板中的元素节点并选择“ force state ”。或者在Sytle子窗格中单击“:hov ”图标。可以触发元素上的伪类来研究元素在悬停时的效果和样式​【2】DevTools-在任何网页上运行预定义的代码片段DevTools中有一个叫做代码段的特性…

JAVA入门级教学之(public class和class的区别)

目录 JAVA入门级教学之(public class和class的区别) 1.public class 和 class 的区别: 2.public class 类名A{ }的 类名A需要和源文件 类名 保持一致 总的来说: JAVA入门级教学之(public class和class的区别&…

c 程序中的注释相当于空白字符_Python专题 | (三)注释、变量与输出

小伙伴们,大家好呀,欢迎回到我们的python专题。前两篇文章已经把编程简单的知识和大家介绍过了,今天我们正式开始学习python语法。第一个python程序我们在第一篇文章中介绍pycharm的安装时曾给大家展示过这样的一段代码:print(&qu…

JAVA入门级教学之(第一章总结)

总结第一章需要掌握的内容: 理解java的加载与执行能理解自己搭建的java开发环境(JDK)能够独立编写HelloWorld程序,编译并运行掌握环境变量path的原理以及如何配置掌握环境变量classpath的原理以及如何配置java中的注释public cla…

JAVA入门级教学之(标识符与关键字)

目录 JAVA入门级教学之(标识符与关键字) 1.什么是标识符? 2.标识符的命名规则? 3.标识符的命名规范? 4.严格遵守规则和规范: 在java里的关键字都是小写的 JAVA入门级教学之(标识符与关键字) 1.什…

birt project mysql_eclipse birt如何连接mysql数据库 配置文件

双击.rptdesign打开data explorer,右键点Data Sources -> New Data Sources ->JDBC Data Source到Next如果没有mysql-connector-java的包,请去下一个http://dev.mysql.com/downloads/connector/j/5.0.html然后在Driver Class这里就可以选 com.mysql.jdbc.Driv…

JAVA入门级教学之(变量)

目录 关于java语当中的变量: 1、什么是变量? 2、数据类型的作用? 3、变量要求 4、声明/定义变量的语法格式: 5、变量声明之后怎么赋值? 6、声明和赋值可以放到一起完成 7、变量赋值之后,可以重新赋值&…

mysql添加timestamp有什么用_mysql中timestamp的使用

mysql中timestamp的使用mysql> CREATE TABLE t1 (-> id mediumint(9) NOT NULL auto_increment,-> name char(11) default NULL,-> rq timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,-> PRIMARY KEY (id)-> ) ;Query OK, 0 rows…