lstm作為乙個優秀的rnn變體,在時間序列的**中有著優秀的運用。在了解其原理1
之後,繼續來了解實踐中的lstm如何運用。
說道lstm,首先得了解rnn在tensorflow中的基本函式tf.nn.rnn_cell.lstmcell,相比基本的basiclstmcell模組,lstmcell中有加入一些變種的特性clipping,projection layer,peep-hole等,如果不了解可以保持預設設定。lstm的基本設定為:
tf.nn.rnn_cell.lstmcell(num_units=
64, state_is_tuple=
true
)
其中num_units是lstm的狀態h和c的長度,這兩者長度在tensorflow中一樣(雖然**中的長度可以任意指定,個人覺得這裡是和原理中有點不一樣的地方)
我們的輸入x的長度可以任意長,這個長度在lstmcell的初始化中不需要指定。
我們看下tf.nn.rnn_cell.lstmcell的build函式:
@tf_utils.shape_type_conversion
defbuild
(self, inputs_shape)
:if inputs_shape[-1
]isnone
:raise valueerror(
"expected inputs.shape[-1] to be known, saw shape: %s"
% inputs_shape)
input_depth = inputs_shape[-1
] h_depth = self._num_units if self._num_proj is
none
else self._num_proj
maybe_partitioner =
( partitioned_variables.fixed_size_partitioner(self._num_unit_shards)
if self._num_unit_shards is
notnone
else
none
) self._kernel = self.add_variable(
_weights_variable_name,
shape=
[input_depth + h_depth,
4* self._num_units]
, initializer=self._initializer,
partitioner=maybe_partitioner)
if self.dtype is
none
: initializer = init_ops.zeros_initializer
else
: initializer = init_ops.zeros_initializer(dtype=self.dtype)
self._bias = self.add_variable(
_bias_variable_name,
shape=[4
* self._num_units]
, initializer=initializer)
if self._use_peepholes:
self._w_f_diag = self.add_variable(
"w_f_diag"
, shape=
[self._num_units]
, initializer=self._initializer)
self._w_i_diag = self.add_variable(
"w_i_diag"
, shape=
[self._num_units]
, initializer=self._initializer)
self._w_o_diag = self.add_variable(
"w_o_diag"
, shape=
[self._num_units]
, initializer=self._initializer)
if self._num_proj is
notnone
: maybe_proj_partitioner =
( partitioned_variables.fixed_size_partitioner(self._num_proj_shards)
if self._num_proj_shards is
notnone
else
none
) self._proj_kernel = self.add_variable(
"projection/%s"
% _weights_variable_name,
shape=
[self._num_units, self._num_proj]
, initializer=self._initializer,
partitioner=maybe_proj_partitioner)
self.built =
true
build函式在tensorflow圖的構建時會執行。其中input_depth也是由輸入推斷出來的,不需要自己指定,但是輸入的維度確定後就不會再更改了,否則會報錯
強烈推薦這篇博文:深入淺出,講的非常好 ↩︎
tensorflow實現基於LSTM的文字分類方法
學習一段時間的tensor flow之後,想找個專案試試手,然後想起了之前在看theano教程中的乙個文字分類的例項,這個星期就用tensorflow實現了一下,感覺和之前使用的theano還是有很大的區別,有必要總結mark一下 這個分類的模型其實也是很簡單,主要就是乙個單層的lstm模型,當然也...
Tensorflow實現LSTM文字分類
最近需要寫乙個神經網路模型來做分類。作為此前沒有實戰過深度學習,只寫過svm之類的,學習過一些理論知識的新手,為了快速上手,第一想法就是找乙個簡單的demo跑通,對整個流程和結構有乙個初步體驗。於是在網上找了乙個tensorflow實戰系列 手把手教你使用lstm進行文字分類 但是教程存在乙個問題,...
tensorflow實現lstm中遇到的函式記錄
函式一 initializer tf.random uniform initializer 0.1,0.1,seed 123 tf.random uniform initializer 引數 函式二 lstm tf.contrib.rnn.lstmcell rnn size,initializer ...