BasicLSTMCell中num_units参数解释

理解LSTM的num_units参数
部署运行你感兴趣的模型镜像

前言
关于LSTM原理: http://colah.github.io/posts/2015-08-Understanding-LSTMs/
关于LSTM原理(译文):https://blog.youkuaiyun.com/Jerr__y/article/details/58598296
关于Tensorflow+LSTM的使用:https://www.knowledgemapper.com/knowmap/knowbook/jasdeepchhabra94@gmail.comUnderstandingLSTMinTensorflow(MNISTdataset)
关于Tensorflow+LSTM的使用(译文):https://yq.aliyun.com/articles/202939
正文
本文只是介绍tensorflow中的BasicLSTMCell中num_units,关于LSTM和如何使用请看前言的教程。
在使用Tensorflow跑LSTM的试验中, 有个num_units的参数,这个参数是什么意思呢?

先总结一下,num_units这个参数的大小就是LSTM输出结果的维度。例如num_units=128, 那么LSTM网络最后输出就是一个128维的向量。

我们先换个角度举个例子,最后再用公式来说明。

假设在我们的训练数据中,每一个样本 x 是 28*28 维的一个矩阵,那么将这个样本的每一行当成一个输入,通过28个时间步骤展开LSTM,在每一个LSTM单元,我们输入一行维度为28的向量,如下图所示。

这里写图片描述


那么,对每一个LSTM单元,参数 num_units=128 的话,就是每一个单元的输出为 128*1 的向量,在展开的网络维度来看,如下图所示,对于每一个输入28维的向量,LSTM单元都把它映射到128维的维度, 在下一个LSTM单元时,LSTM会接收上一个128维的输出,和新的28维的输入,处理之后再映射成一个新的128维的向量输出,就这么一直处理下去,知道网络中最后一个LSTM单元,输出一个128维的向量。

这里写图片描述
从LSTM的公式的角度看是什么原理呢?我们先看一下LSTM的结构和公式:


参数 num_units=128 的话,

对于公式 (1) ,h=128∗1 h=128*1h=128∗1维, x=28∗1 x=28*1x=28∗1 维,[h,x]便等于156∗1 156*1156∗1维,W=128∗156 W=128*156W=128∗156 维,所以 W∗[h,x]=128∗156∗156∗1=128∗1,b=128∗1 W*[h,x]=128*156 * 156*1=128*1, b=128*1W∗[h,x]=128∗156∗156∗1=128∗1,b=128∗1 维, 所以 f=128∗1+128∗1=128∗1 f=128*1+128*1=128*1f=128∗1+128∗1=128∗1 维;
对于公式 (2) 和 (3),同上可分析得 i=128∗1 i=128*1i=128∗1 维,C˜=128∗1 \widetilde{C}=128*1 
C
 =128∗1 维;
对于公式 (4) ,f(t)=128∗1,C(t−1)=128∗1,f(t).∗C(t−1)=128∗1.∗128∗1=128∗1 f(t)=128*1, C(t-1)=128*1, f(t) .* C(t-1) = 128*1 .* 128*1 = 128*1f(t)=128∗1,C(t−1)=128∗1,f(t).∗C(t−1)=128∗1.∗128∗1=128∗1 , 同理可得 C(t)=128∗1 C(t)=128*1C(t)=128∗1 维;
对于公式 (5) 和 (6) , 同理可得 O=128∗1 O=128*1O=128∗1 维, h=O.∗tanh(C)=128∗1 h=O.*tanh(C)=128*1h=O.∗tanh(C)=128∗1 维。
所以最后LSTM单元输出的h就是 128∗1 128*1128∗1 的向量。

以上就是 num_units 参数的含义。

如有错误请指出谢谢

参考链接:
https://stackoverflow.com/questions/37901047/what-is-num-units-in-tensorflow-basiclstmcell
https://www.zhihu.com/question/64470274

原文:https://blog.youkuaiyun.com/notHeadache/article/details/81164264 
 

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

class ConvRNNCell(object): def __call__(self, inputs, state, scope=None): raise NotImplementedError("Abstract method") @property def state_size(self): raise NotImplementedError("Abstract method") @property def output_size(self): raise NotImplementedError("Abstract method") def zero_state(self, batch_size, dtype): shape = self.shape num_features = self.num_features zeros = tf.zeros([batch_size, shape[0], shape[1], num_features * 2]) return zeros class BasicConvLSTMCell(ConvRNNCell): def __init__(self, shape, filter_size, num_features, forget_bias=1.0, input_size=None, state_is_tuple=False, activation=tf.nn.tanh): if input_size is not None: logging.warn("%s: The input_size parameter is deprecated.", self) self.shape = shape self.filter_size = filter_size self.num_features = num_features self._forget_bias = forget_bias self._state_is_tuple = state_is_tuple self._activation = activation @property def state_size(self): return (LSTMStateTuple(self._num_units, self._num_units) if self._state_is_tuple else 2 * self._num_units) @property def output_size(self): return self._num_units def __call__(self, inputs, state, scope='convLSTM'): """Long short-term memory cell (LSTM).""" with tf.variable_scope(scope or type(self).__name__): # "BasicLSTMCell" # Parameters of gates are concatenated into one multiply for efficiency. if self._state_is_tuple: c, h = state else: c, h = tf.split(state, 2, 3) concat = _conv_linear([inputs, h], self.filter_size, self.num_features * 4, True) # i = input_gate, j = new_input, f = forget_gate, o = output_gate i, j, f, o = tf.split(concat, 4, 3) new_c = (c * tf.nn.sigmoid(f + self._forget_bias) + tf.nn.sigmoid(i) * self._activation(j)) new_h = self._activation(new_c) * tf.nn.sigmoid(o) if self._state_is_tuple: new_state = LSTMStateTuple(new_c, new_h) else: new_state = tf.concat([new_c, new_h], 3) return new_h, new_state filter_size是怎么定义的
最新发布
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值