【问题标题】:Tensorflow GRU cell error when fetching activations with variable sequence length获取具有可变序列长度的激活时,Tensorflow GRU 单元错误
【发布时间】:2023-03-14 18:53:01
【问题描述】:

我想在一些时间序列数据上运行 GRU 单元,以根据最后一层中的激活对它们进行聚类。我对 GRU 单元的实现做了一个小改动

def __call__(self, inputs, state, scope=None):
"""Gated recurrent unit (GRU) with nunits cells."""
with vs.variable_scope(scope or type(self).__name__):  # "GRUCell"
  with vs.variable_scope("Gates"):  # Reset gate and update gate.
    # We start with bias of 1.0 to not reset and not update.
    r, u = array_ops.split(1, 2, linear([inputs, state], 2 * self._num_units, True, 1.0))
    r, u = sigmoid(r), sigmoid(u)
  with vs.variable_scope("Candidate"):
    c = tanh(linear([inputs, r * state], self._num_units, True))
  new_h = u * state + (1 - u) * c

  # store the activations, everything else is the same
  self.activations = [r,u,c]
return new_h, new_h

在此之后,我以以下方式连接激活,然后在调用此 GRU 单元的脚本中返回它们

@property
def activations(self):
    return self._activations


@activations.setter
def activations(self, activations_array):
    print "PRINT THIS"         
    concactivations = tf.concat(concat_dim=0, values=activations_array, name='concat_activations')
    self._activations = tf.reshape(tensor=concactivations, shape=[-1], name='flatten_activations')

我以以下方式调用 GRU 单元

outputs, state = rnn.rnn(cell=cell, inputs=x, initial_state=initial_state, sequence_length=s)

其中s 是一个批处理长度数组,其中包含输入批处理的每个元素中的时间戳数。

最后我使用

fetched = sess.run(fetches=cell.activations, feed_dict=feed_dict)

执行时出现以下错误

Traceback(最近一次调用最后一次): 文件“xxx.py”,第 162 行,在 fetched = sess.run(fetches=cell.activations, feed_dict=feed_dict) 运行中的文件“/xxx/local/lib/python2.7/site-packages/tensorflow/python/client/session.py”,第 315 行 return self._run(None, fetches, feed_dict) _run 中的文件“/xxx/local/lib/python2.7/site-packages/tensorflow/python/client/session.py”,第 511 行 feed_dict_string) _do_run 中的文件“/xxx/local/lib/python2.7/site-packages/tensorflow/python/client/session.py”,第 564 行 目标列表) _do_call 中的文件“/xxx/local/lib/python2.7/site-packages/tensorflow/python/client/session.py”,第 588 行 六.reraise(e_type, e_value, e_traceback) _do_call 中的文件“/xxx/local/lib/python2.7/site-packages/tensorflow/python/client/session.py”,第 571 行 返回 fn(*args) _run_fn 中的文件“/xxx/local/lib/python2.7/site-packages/tensorflow/python/client/session.py”,第 555 行

返回 tf_session.TF_Run(session, feed_dict, fetch_list, target_list) tensorflow.python.pywrap_tensorflow.StatusNotOK:无效参数:为 RNN/cond_396/ClusterableGRUCell/flatten_activations:0 返回的张量无效。

有人可以提供有关如何在最后一步从 GRU 单元中获取激活的见解,并传递可变长度的序列吗?谢谢。

【问题讨论】:

    标签: python python-2.7 tensorflow recurrent-neural-network gated-recurrent-unit


    【解决方案1】:

    要从最后一步获取激活,您希望使激活成为状态的一部分,由 tf.rnn 返回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 2018-11-11
      • 1970-01-01
      相关资源
      最近更新 更多