在 Tensorflow 中创建高度可定制的 RNN

Creating a highly customizable RNN in Tensorflow

我想在不使用tensorflow提供的RNN函数的情况下实现一个RNN。这是我试过的代码,最终给了我一个错误

import tensorflow as tf
tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=(5,5))
InitialState = tf.zeros((5,1))
h = InitialState
W1 = tf.Variable(tf.random_normal([5, 5], stddev=0.35),
                      name="W1")
W2 = tf.Variable(tf.random_normal([5, 5], stddev=0.35),
                      name="W2")

for k in range(5):
    h = tf.matmul(W1,h) + tf.matmul(W2,x[:,k:(k+1)])
    h = tf.sigmoid(h)
with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    a = sess.run([h], feed_dict = {x:tf.ones((5,5))})

如何从头开始实施 RNN?网上有例子吗?

import tensorflow as tf
import numpy as np

hidden_size = 2   # hidden layer of two neurons
input_size = 5

# Weight of x will the be (hidden_layer_size x input_size)
Wx = tf.Variable(tf.random_normal([hidden_size, input_size], stddev=0.35),
                      name="Wx")    

# Weight of y will be (input_size x hidden_layer_size)
Wy = tf.Variable(tf.random_normal([input_size, hidden_size], stddev=0.35),
                      name="Wy")

# Weight of h will be (hidden_size, hidden_size)
Wh = tf.Variable(tf.random_normal([hidden_size, hidden_size], stddev=0.35),
                      name="Wh")

h = tf.zeros((hidden_size, input_size))

x = tf.placeholder(dtype = tf.float32,
    shape = (input_size,input_size))
y = tf.placeholder(dtype = tf.float32,
    shape = (input_size,input_size))

feed_dict = {
    x : np.ones((5,5), dtype = np.float32),
    y : np.ones((5,5), dtype = np.float32)
}

# RNN step
for _ in range(input_size):
    h = tf.tanh(tf.matmul(Wh, h) + tf.matmul(Wx, x))
    o = tf.nn.softmax(h)

init_op = tf.initialize_all_variables()
sess = tf.Session()

sess.run(init_op, feed_dict = feed_dict)

h_new, y_hat = sess.run([h, o], feed_dict = feed_dict)

print h_new
print y_hat