【问题标题】:Running RNN in Tensorflow在 TensorFlow 中运行 RNN
【发布时间】:2018-07-16 22:08:54
【问题描述】:

如果我有一个包含 20 个浮点类型元素的数组。
根据前十个元素的值,我希望 RNN 预测最后十个元素的值。使用各种在线资源和书籍,我构建了一个 RNN,它可以读取前 10 个元素并对其进行处理。但是我不知道如何让它使用最后十个元素作为“答案键”并以此为基础进行训练。

# To support both python 2 and python 3
from __future__ import division, print_function, unicode_literals

# Common imports
import numpy as np
import os
import tensorflow as tf
import numpy as np
import pymysql as pym

# to make this notebook's output stable across runs
def reset_graph(seed=42):
    tf.reset_default_graph()
    tf.set_random_seed(seed)
    np.random.seed(seed)

conn = pym.connect("host.docker.internal","root","","DynaSystems" )
cursor = conn.cursor()
cursor.execute("USE DynaSystems")
cursor.execute("SELECT * FROM simulation")
D = []
for row in cursor:
    D.append(np.fromiter(row, dtype=float, count=-1))
#print(D)

cursor.close()
conn.close()

#get data into a np array
data_np = np.asarray(D, np.float32)
steps = data_np[0:,2:12]
steps = steps.tolist()

a = []
for x in steps:
    c = []
    c.append(x)
    a.append(c)
#get evars out of simulation data
#print(a)

#Rough draft running a Dynamic unrolling and a Basic RNN Cell.
#It works but there's not training and thus no learning happening yet...

n_steps = 1
n_inputs = 10
n_neurons = 10

reset_graph()

X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])

basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)
outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    init.run()
    outputs_val = outputs.eval(feed_dict={X: a})

print(outputs_val)

我提供给 feed dict 的“a”中的数据如下所示:

[[[0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]], [[0.800000011920929, 0.5, 0.800000011920929, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]], [[0.800000011920929, 0.5, 0.800000011920929, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]], [[0.800000011920929, 0.5, 0.800000011920929, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]], [[0.800000011920929, 0.5, 0.800000011920929, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]], [[0.800000011920929, 0.5, 0.800000011920929, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]], [[0.800000011920929, 0.5, 0.800000011920929, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]], [[0.800000011920929, 0.5, 0.800000011920929, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]], [[0.800000011920929, 0.5, 0.800000011920929, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]], [[0.800000011920929, 0.5, 0.800000011920929, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]], [[0.800000011920929, 0.5, 0.800000011920929, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]], [[0.800000011920929, 0.5, 0.800000011920929, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]], [[0.800000011920929, 0.5, 0.800000011920929, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]], [[0.800000011920929, 0.5, 0.800000011920929, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]], [[0.800000011920929, 0.5, 0.800000011920929, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]]]

在我像这样切片 data_np 的步骤中: 步骤 = data_np[0:,2:12]

我成功地得到了前十个数字,但我如何抓住最后十个并将它们输入以训练网络?我假设我的代码的结尾需要如下所示,其中y 占位符包含 RNN 的“答案键”。但是,我不能把它放在一起。

n_steps = 1
n_inputs = 10
n_neurons = 10
n_outputs = 10
learning_rate = 0.001

reset_graph()

X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.int32, [None])

basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)
outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32)
logits = tf.layers.dense(states, n_outputs)
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits)

loss = tf.reduce_mean(xentropy)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(loss)
correct = tf.nn.in_top_k(logits, y, 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

init = tf.global_variables_initializer()

with tf.Session() as sess:
    init.run()
    outputs_val = outputs.eval(feed_dict={X: a})

print(outputs_val)

【问题讨论】:

    标签: python numpy tensorflow machine-learning recurrent-neural-network


    【解决方案1】:

    首先,看看Keras - 这是一个使用 TensorFlow 作为其后端的模块,但将最重要的神经网络位包装在非常易于使用的对象中。 RNN 文档可以在 here 找到。

    所以我从这个问题中了解到的是,您有一个数字序列,并且您想使用以前的数字来预测未来的数字。如果您拥有的每个数据点都代表该序列中的一个时间步长,那么您可以采用我认为的两种方式之一。这取决于您要建模的内容。阅读this article,它可以更好地理解 LSTM 网络,然后回到这里。两种方式分别是:

    1。多对一数据关系

    如果您的数据只是一系列步骤,您可以将每个时间步定义为前一个时间步的输出。这意味着在 t[0] 处,预期输出为 t1。要放置模型,您需要将数据放入具有以下形状的 numpy 数组中:

    input shape: (number of samples, number of time steps, input data)
    i.e. (1, 1, 1) would mean you have 1 sample with 1 step and 1 feature dimension
    
    output shape: (number of samples, output data)
    i.e. (1, 1) would mean you have 1 sample with 1 output dimension
    

    并直接将其翻译为您的示例:

    形状可能是这样的: (20, 1, 1) 其中有 20 个样本,每个样本有 1 个步骤和 1 个特征维度。然后输入你的numpy数组看起来像 [ [[0.5]], [[0.5]], [[0.5]] ... 20 times ] 你的输出数组将是 [[0.5], [0.5], [0.5] ... 20 times]

    通过这样做,您的神经网络将一次输入 1 个步骤,并使用所有之前的步骤来预测下一个步骤。例如,如果您想预测 20 序列中的第 11 步,您的神经网络将使用前 10 步来执行此操作。你可以认为这是t[0-10] => t[11]

    2。多对多关系

    如果您确实需要保留您在问题中描述的关系 - 前 10 个步骤预测其余 10 个步骤 - 您需要使用多对多关系。 Karpathy 的文章涉及到这个话题,所以看看那里。老实说,我对这种情况没有太多经验,所以我唯一能指出的是,你需要使用 Keras 的TimeDistributed Dense layer 来建模。

    我希望这会有所帮助。祝你好运!

    【讨论】:

    • 感谢您的回复!那里有很多材料你把我带到那里,所以我要花一两天时间来回顾一下,看看它是否能让我到达我需要的地方。如果是这样,我一定会接受你的回答,如果不是,我相信我的后续问题将比我原来的问题更有针对性。不管怎样,这些都是你给我的好线索!
    • @Damisco 是的,绝对不要急于求成! :) 当我第一次使用 RNN 时,我对这一切感到非常困惑,我花了一段时间才理解它。
    • 所以这些材料最终帮了大忙!我仍然需要对我的实现方式进行一些改进,但总体而言,使用在将我的预测映射到单热向量之上提供的链接是让我到达我需要的地方的原因。我将把它标记为已接受的答案,再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    相关资源
    最近更新 更多