如何在Tensorflow中实现自定义RNN(特别是ESN)?

2024-10-01 13:31:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图根据下面的定义在Tensorflow中定义我自己的RNNCell(回声状态网络)。在

x(t+1)=tanh(Win*u(t)+W*x(t)+Wfb*y(t))

y(t)=不会*z(t)

z(t)=[x(t),u(t)]

x是状态,u是输入,y是输出。Win、W和Wfb不可培训。所有权重都是随机初始化的,但是W是这样修改的:“将W元素的某个百分比设置为0,缩放W以使其光谱半径保持在1.0以下

我有这个代码来生成方程。在

x = tf.Variable(tf.reshape(tf.zeros([N]), [-1, N]), trainable=False, name="state_vector")
W = tf.Variable(tf.random_normal([N, N], 0.0, 0.05), trainable=False)
# TODO: setup W according to the ESN paper
W_x = tf.matmul(x, W)

u = tf.placeholder("float", [None, K], name="input_vector")
W_in = tf.Variable(tf.random_normal([K, N], 0.0, 0.05), trainable=False)
W_in_u = tf.matmul(u, W_in)

z = tf.concat(1, [x, u])
W_out = tf.Variable(tf.random_normal([K + N, L], 0.0, 0.05))
y = tf.matmul(z, W_out)
W_fb = tf.Variable(tf.random_normal([L, N], 0.0, 0.05), trainable=False)
W_fb_y = tf.matmul(y, W_fb)

x_next = tf.tanh(W_in_u + W_x + W_fb_y)

y_ = tf.placeholder("float", [None, L], name="train_output")

我的问题是双重的。首先,我不知道如何将其作为rncell的超类来实现。第二,我不知道如何根据上述规范生成W张量。在

如果有任何问题需要帮助,我们将不胜感激。也许我可以想出一种方法来准备W,但我肯定不明白如何将我自己的RNN作为rncell的超类来实现。在


Tags: nameinfalsefb定义状态tfrandom
1条回答
网友
1楼 · 发布于 2024-10-01 13:31:12

快速总结:

请查看python/ops/rnn_cell.py下的TensorFlow源代码,了解如何子类rncell。通常是这样的:

class MyRNNCell(RNNCell):
  def __init__(...):

  @property
  def output_size(self):
  ...

  @property
  def state_size(self):
  ...

  def __call__(self, input_, state, name=None):
     ... your per-step iteration here ...

相关问题 更多 >