2016-04-23 5 views
0

Я пытаюсь реализовать RNN без использования функций RNN, предоставляемых функцией tensorflow. Вот код, который я пробовал, что в конечном итоге дал мне ошибкуСоздание настраиваемого RNN в Tensorflow

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) мы не знаем, какая у вас ошибка 2) лучшим примером того, как реализовать 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 
Смежные вопросы