Keras(一)——常用功能总结(20200915)

1、kr.preprocessing.sequence.pad_sequences(转化为相同序列长度)

为了实现的简便,keras只能接受长度相同的序列输入。因此如果目前序列长度参差不齐,这时需要使用pad_sequences()。该函数是将序列转化为经过填充以后的一个长度相同的新序列新序列。

  • 官方语法:

keras.preprocessing.sequence.pad_sequences(sequences, 
	maxlen=None,
	dtype='int32',
	padding='pre',
	truncating='pre', 
	value=0.)
  • 参数说明

  • sequences:浮点数或整数构成的两层嵌套列表
  • maxlen:None或整数,为序列的最大长度。大于此长度的序列将被截短,小于此长度的序列将在后部填0.
  • dtype:返回的numpy array的数据类型
  • padding:‘pre’或‘post’,确定当需要补0时,在序列的起始还是结尾补`
  • truncating:‘pre’或‘post’,确定当需要截断序列时,从起始还是结尾截断
  • value:浮点数,此值将在填充时代替默认的填充值0
  • 返回值

返回的是个2维张量,长度为maxlen.

https://blog.csdn.net/wcy23580/article/details/84957471

2、kr.utils.to_categorical

将id数组转化为one-hot编码。

  • 语法

to_categorical(y, num_classes=None, dtype='float32')

y为int数组,num_classes为标签类别总数,大于max(y)(标签从0开始的)。

  • 用例:

import keras

ohl=keras.utils.to_categorical([1,3])
# ohl=keras.utils.to_categorical([[1],[3]])
print(ohl)
"""
[[0. 1. 0. 0.]
 [0. 0. 0. 1.]]
"""
ohl=keras.utils.to_categorical([1,3],num_classes=5)
print(ohl)
"""
[[0. 1. 0. 0. 0.]
 [0. 0. 0. 1. 0.]]
"""

https://blog.csdn.net/wcy23580/article/details/84957471

3、model.compile

def compile(optimizer,
            loss=None,
             metrics=None,
             loss_weights=None,
             sample_weight_mode=None,
             weighted_metrics=None,
             target_tensors=None,
              **kwargs):

optimizer:优化器

loss:损失函数

metrics:评价函数,,与损失函数类似,只不过 评价函数的结果不会用于训练过程中,可以自己传递已有的评价函数名称,或者传递一个自定义的theano/tensorflow函数来使用,自带的评价函数。

loss_weight:可选项,是一个list或者字典,指定不同的损失 系数。

 

 

4、Input

from keras.layers import Input

Input(
    shape=None,
    batch_size=None,
    name=None,
    dtype=None,
    sparse=False,
    tensor=None,
    **kwargs
)

 参数:
shape:形状元组(整数),不包括批量大小。例如,shape=(32,)表示预期输入将是32维向量的批次。
batch_size:可选的静态批处理大小(整数)。
name:图层的可选名称字符串。在模型中应该是唯一的(不要重复使用相同的名称两次)。如果没有提供,它将自动生成。
dtype:数据类型由输入预期的,作为字符串(float32,float64,int32...)
sparse:一个布尔值,指定要创建的占位符是否稀疏。
tensor:可选的现有张量以包装到Input图层中。如果设置,该图层将不会创建占位符张量。
**kwargs:不推荐的参数支持。

5、Bidirectional

双向RNN包装器。

  • layer:Recurrent对象
  • merge_mode:前向和后向RNN输出的结合方式,为sum,mul,concat,aveNone之一,若设为None,则返回值不结合,而是以列表的形式返回
from keras.layers import Bidirectional
tf.keras.layers.Bidirectional(
    layer, merge_mode='concat', weights=None, backward_layer=None, **kwargs
)

例子:

model = Sequential()
model.add(Bidirectional(LSTM(10, return_sequences=True), input_shape=(5, 10)))
model.add(Bidirectional(LSTM(10)))
model.add(Dense(5))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

 # With custom backward layer
 model = Sequential()
 forward_layer = LSTM(10, return_sequences=True)
 backward_layer = LSTM(10, activation='relu', return_sequences=True,
                       go_backwards=True)
 model.add(Bidirectional(forward_layer, backward_layer=backward_layer,
                         input_shape=(5, 10)))
 model.add(Dense(5))
 model.add(Activation('softmax'))
 model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

链接:https://tensorflow.google.cn/api_docs/python/tf/keras/layers/Bidirectional?hl=zh-cn

6、GRU

from keras.layers import Gru

 

7、Dense

 

from keras.layers import Dense
keras.layers.Dense(units, 
				  activation=None, 
				  use_bias=True, 
				  kernel_initializer='glorot_uniform', 
				  bias_initializer='zeros', 
				  kernel_regularizer=None, 
				  bias_regularizer=None, 
			      activity_regularizer=None, 
				  kernel_constraint=None, 
				  bias_constraint=None)

 units:该层有几个神经元

activation:该层使用的激活函数

use_bias:是否添加偏置项

kernel_initializer:权重初始化方法

bias_initializer:偏置初始化方法

kernel_regularizer:权重规范化函数

bias_regularizer:偏置值规范化方法

activity_regularizer:输出的规范化方法

kernel_constraint:权重变化限制函数

bias_constraint:偏置值变化限制函数

例子:

keras.layers.Dense(512,activation='sigmoid', input_dim=2,use_bias=True)

512节点,使用sigmoid激活函数,定制第一层的时候需要指定数据输入的形状,即input_dim,才能让数据喂进去。 

8、Model

from keras.models import Model

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值