tensorflow-LSTM源码

更新时间:2023-06-15 02:18:27 阅读: 评论:0

tensorflow-LSTM源码
class LSTMCell(RNNCell):
"""Long short-term memory unit (LSTM) recurrent network cell.
The default non-peephole implementation is bad on:
u.edu/pdfs/Hochreiter97_lstm.pdf
S. Hochreiter and J. Schmidhuber.
"Long Short-Term Memory". Neural Computation, 9(8):1735-1780, 1997.
The peephole implementation is bad on:
Hasim Sak, Andrew Senior, and Francoi Beaufays.
"Long short-term memory recurrent neural network architectures for
large scale acoustic modeling." INTERSPEECH, 2014.
The class us optional peep-hole connections, optional cell clipping, and
an optional projection layer.
"""
def__init__(lf, num_units, input_size=None,
u_peepholes=Fal, cell_clip=None,
initializer=None, num_proj=None, proj_clip=None,
num_unit_shards=1, num_proj_shards=1,
forget_bias=1.0, state_is_tuple=True,
activation=tanh):
"""Initialize the parameters for an LSTM cell.
Args:
num_units: int, The number of units in the LSTM cell
input_size: Deprecated and unud.
u_peepholes: bool, t True to enable diagonal/peephole connections.
cell_clip: (optional) A float value, if provided the cell state is clipped
by this value prior to the cell output activation.
initializer: (optional) The initializer to u for the weight and
projection matrices.
num_proj: (optional) int, The output dimensionality for the projection
matrices.  If None, no projection is performed.
proj_clip: (optional) A float value.  If `num_proj > 0` and `proj_clip` is
provided, then the projected values are clipped elementwi to within
`[-proj_clip, proj_clip]`.
num_unit_shards: How to split the weight matrix.  If >1, the weight
matrix is stored across num_unit_shards.
num_proj_shards: How to split the projection matrix.  If >1, the
projection matrix is stored across num_proj_shards.
forget_bias: Bias of the forget gate are initialized by default to 1
in order to reduce the scale of forgetting at the beginning of
the training.
state_is_tuple: If True, accepted and returned states are 2-tuples of
the `c_state` and `m_state`.  If Fal, they are concatenated
网页打开很慢along the column axis.  This latter behavior will soon be deprecated.
activation: Activation function of the inner states.
"""
if not state_is_tuple:
logging.warn("%s: Using a concatenated state is slower and will soon be "
"deprecated.  U state_is_tuple=True.", lf)
if input_size is not None:
logging.warn("%s: The input_size parameter is deprecated.", lf)
誓师lf._num_units = num_units
lf._u_peepholes = u_peepholes
lf._u_peepholes = u_peepholes
lf._cell_clip = cell_clip
lf._initializer = initializer
lf._num_proj = num_proj
lf._proj_clip = proj_clip
lf._num_unit_shards = num_unit_shards
lf._num_proj_shards = num_proj_shards
lf._forget_bias = forget_bias
lf._state_is_tuple = state_is_tuple
lf._activation = activation
if num_proj:
lf._state_size = (
LSTMStateTuple(num_units, num_proj)
if state_is_tuple el num_units + num_proj)
lf._output_size = num_proj
el:
lf._state_size = (
LSTMStateTuple(num_units, num_units)
关于泰山的传说if state_is_tuple el2 * num_units)
lf._output_size = num_units
@property
def state_size(lf):
return lf._state_size
@property
def output_size(lf):小学英语手抄报
return lf._output_size
def__call__(lf, inputs, state, scope=None):
"""Run one step of LSTM.
Args:
属兔几月出生最好命运inputs: input Tensor, 2D, batch x num_units.
state: if `state_is_tuple` is Fal, this must be a state Tensor,
`2-D, batch x state_size`.  If `state_is_tuple` is True, this must be a
tuple of state Tensors, both `2-D`, with column sizes `c_state` and
`m_state`.
scope: VariableScope for the created subgraph; defaults to "LSTMCell".
Returns:
A tuple containing:
- A `2-D, [batch x output_dim]`, Tensor reprenting the output of the
LSTM after reading `inputs` when previous state was `state`.
读书摘要Here output_dim is:
num_proj if num_proj was t,
num_units otherwi.
- Tensor(s) reprenting the new state of LSTM after reading `inputs` when        the previous state was `state`.  Same type and shape(s) as `state`.
Rais:
ValueError: If input size cannot be inferred from inputs via
static shape inference.
"""
num_proj = lf._num_units if lf._num_proj is None el lf._num_proj
if lf._state_is_tuple:
(c_prev, m_prev) = state
el:
c_prev = array_ops.slice(state, [0, 0], [-1, lf._num_units])
m_prev = array_ops.slice(state, [0, lf._num_units], [-1, num_proj])
dtype = inputs.dtype
新生儿怎么拍嗝input_size = _shape().with_rank(2)[1]
if input_size.value is None:
if input_size.value is None:
rai ValueError("Could not infer input size _shape()[-1]") with vs.variable_scope(scope or type(lf).__name__,
initializer=lf._initializer):  # "LSTMCell"
concat_w = _get_concat_variable(
"W", [input_size.value + num_proj, 4 * lf._num_units],
dtype, lf._num_unit_shards)
b = vs.get_variable(
"B", shape=[4 * lf._num_units],
initializer=s_initializer, dtype=dtype)
# i = input_gate, j = new_input, f = forget_gate, o = output_gate
cell_inputs = at(1, [inputs, m_prev])
lstm_matrix = nn_ops.bias_add(math_ops.matmul(cell_inputs, concat_w), b)      i, j, f, o = array_ops.split(1, 4, lstm_matrix)
# Diagonal connections
if lf._u_peepholes:
w_f_diag = vs.get_variable(
"W_F_diag", shape=[lf._num_units], dtype=dtype)
w_i_diag = vs.get_variable(
"W_I_diag", shape=[lf._num_units], dtype=dtype)
w_o_diag = vs.get_variable(
"W_O_diag", shape=[lf._num_units], dtype=dtype)
if lf._u_peepholes:
c = (sigmoid(f + lf._forget_bias + w_f_diag * c_prev) * c_prev +
sigmoid(i + w_i_diag * c_prev) * lf._activation(j))
el:
c = (sigmoid(f + lf._forget_bias) * c_prev + sigmoid(i) *
lf._activation(j))
if lf._cell_clip is not None:
# pylint: disable=invalid-unary-operand-type
c = clip_ops.clip_by_value(c, -lf._cell_clip, lf._cell_clip)
# pylint: enable=invalid-unary-operand-type
if lf._u_peepholes:
m = sigmoid(o + w_o_diag * c) * lf._activation(c)
el:
m = sigmoid(o) * lf._activation(c)
if lf._num_proj is not None:
concat_w_proj = _get_concat_variable(
"W_P", [lf._num_units, lf._num_proj],
dtype, lf._num_proj_shards)
m = math_ops.matmul(m, concat_w_proj)
if lf._proj_clip is not None:
# pylint: disable=invalid-unary-operand-type
m = clip_ops.clip_by_value(m, -lf._proj_clip, lf._proj_clip) # pylint: enable=invalid-unary-operand-type皇帝的英文
new_state = (LSTMStateTuple(c, m) if lf._state_is_tuple
el at(1, [c, m]))
return m, new_state

本文发布于:2023-06-15 02:18:27,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1038947.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:网页   泰山   手抄报   命运   读书
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图