2016-04-23 10 views
0

tensorflowが提供するRNN関数を使用せずにRNNを実装しようとしています。私が試したコードは、最終的に私にエラーを与えましたTensorflowで高度にカスタマイズ可能な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を実装できますか?オンラインの例がありますか?

+0

1)RNNを実装する最良の例は、テンソルフロー自体の実際のRNN実装です。 – runDOSrun

答えて

1
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