2015-12-22 56 views
15

試圖在張量流中實現最小的玩具RNN示例。 目標是學習從輸入數據到目標數據的映射,類似於這個精彩的簡潔example in theanets張量流中的最小RNN示例

更新:我們到了那裏。剩下的唯一部分是使其趨於一致(並且不那麼複雜)。有人可以幫助將以下內容轉換爲運行代碼或提供一個簡單的示例嗎?

import tensorflow as tf 
from tensorflow.python.ops import rnn_cell 

init_scale = 0.1 
num_steps = 7 
num_units = 7 
input_data = [1, 2, 3, 4, 5, 6, 7] 
target = [2, 3, 4, 5, 6, 7, 7] 
#target = [1,1,1,1,1,1,1] #converges, but not what we want 


batch_size = 1 

with tf.Graph().as_default(), tf.Session() as session: 
    # Placeholder for the inputs and target of the net 
    # inputs = tf.placeholder(tf.int32, [batch_size, num_steps]) 
    input1 = tf.placeholder(tf.float32, [batch_size, 1]) 
    inputs = [input1 for _ in range(num_steps)] 
    outputs = tf.placeholder(tf.float32, [batch_size, num_steps]) 

    gru = rnn_cell.GRUCell(num_units) 
    initial_state = state = tf.zeros([batch_size, num_units]) 
    loss = tf.constant(0.0) 

    # setup model: unroll 
    for time_step in range(num_steps): 
    if time_step > 0: tf.get_variable_scope().reuse_variables() 
    step_ = inputs[time_step] 
    output, state = gru(step_, state) 
    loss += tf.reduce_sum(abs(output - target)) # all norms work equally well? NO! 
    final_state = state 

    optimizer = tf.train.AdamOptimizer(0.1) # CONVERGEs sooo much better 
    train = optimizer.minimize(loss) # let the optimizer train 

    numpy_state = initial_state.eval() 
    session.run(tf.initialize_all_variables()) 
    for epoch in range(10): # now 
    for i in range(7): # feed fake 2D matrix of 1 byte at a time ;) 
     feed_dict = {initial_state: numpy_state, input1: [[input_data[i]]]} # no 
     numpy_state, current_loss,_ = session.run([final_state, loss,train], feed_dict=feed_dict) 
    print(current_loss) # hopefully going down, always stuck at 189, why!? 
+0

也許你最好從教程開始,並從一個工作示例開發代碼:https://www.tensorflow.org/versions/master/tutorials/recurrent/index.html – GavinBrelstaff

+3

大部分代碼*來自教程。我沒有找到一個簡單的工作示例:ptb_word_lm.py有322行 – Anona112

+1

Reddit線程https://www.reddit.com/r/MachineLearning/comments/3sok8k/tensorflow_basic_rnn_example_with_variable_length/表明tensorflow還沒有準備好RNN工作 - 我真的很想測試它,但正如你發現沒有工作代碼來測試驅動器。 – GavinBrelstaff

回答

6

我認爲你的代碼有幾個問題,但這個想法是正確的。

主要問題是您對輸入和輸出使用單張量,如:
inputs = tf.placeholder(tf.int32, [batch_size, num_steps])

在TensorFlow中,RNN函數採用張量列表(因爲num_steps在某些模型中可能會有所不同)。所以,你應該構建輸入這樣的:
inputs = [tf.placeholder(tf.int32, [batch_size, 1]) for _ in xrange(num_steps)]

然後,你需要採取的事實,你的輸入是int32s照顧,但一個RNN電池工作在浮動載體 - 這就是embedding_lookup是什麼。

最後,您需要調整您的Feed以將其放入輸入列表中。

我認爲ptb教程是一個合理的看點,但是如果你想要一個更加簡單的開箱即用RNN例子,你可以看看一些rnn單元測試,例如,這裏。 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/kernel_tests/rnn_test.py#L164

+0

非常感謝您的建議!現在執行它們會產生運行代碼(儘管它仍然相當複雜[雙關語])。剩下的唯一部分就是讓它收斂。 – Anona112