【发布时间】:2016-03-28 19:40:10
【问题描述】:
尝试在 tensorflow 中实现一个最小的玩具 RNN 示例。 目标是学习从输入数据到目标数据的映射,类似于这个精彩简洁的example in theanets。
更新:我们快到了。剩下的唯一部分是使其收敛(并且不那么复杂)。有人可以帮助将以下内容转换为运行代码或提供一个简单的示例吗?
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!?
【问题讨论】:
-
也许你最好从教程开始,从一个工作示例开发你的代码:tensorflow.org/versions/master/tutorials/recurrent/index.html
-
大部分代码是来自教程。我没有找到一个简单的工作示例:ptb_word_lm.py 有 322 行
-
Reddit 线程 reddit.com/r/MachineLearning/comments/3sok8k/… 表明 tensorflow 还没有为 RNN 工作做好准备——我也很想测试它,但你发现没有工作代码可以试驾。跨度>
-
这篇博文danijar.com/introduction-to-recurrent-networks-in-tensorflow 提供了一个简单的工作示例来学习这个问题正在寻找的顺序映射。
-
一些简单的例子:LSTM on MNIST with TensorFlow:github.com/aymericdamien/TensorFlow-Examples/blob/master/…。带有演练的 LSTM 示例:monik.in/…。计数设置位:gist.github.com/monikkinom/e97d518fe02a79177b081c028a83ec1c。还有一个:reddit.com/r/MachineLearning/comments/3sok8k/…
标签: tensorflow recurrent-neural-network