运行 Tensorflow 中的循环神经网络
Running RNN in Tensorflow
如果我有一个包含 20 个浮点类型元素的数组。
基于前十个元素的值,我想要一个 RNN 来预测最后十个元素的值。通过使用各种在线资源和书籍,我构建了一个 RNN,它可以读取前 10 个元素并对其进行处理。但是我不知道如何让它使用最后十个元素作为 'answer key' 并以此为基础进行训练。
# 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 的 'answer key'。但是,我无法将其组合在一起。
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)
首先,看看 Keras - it's a module which uses TensorFlow as its back-end, but wraps the most important neural net bits in very easy to use objects. The RNN documentation can be found 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 来对此建模。
希望对您有所帮助。祝你好运!
如果我有一个包含 20 个浮点类型元素的数组。
基于前十个元素的值,我想要一个 RNN 来预测最后十个元素的值。通过使用各种在线资源和书籍,我构建了一个 RNN,它可以读取前 10 个元素并对其进行处理。但是我不知道如何让它使用最后十个元素作为 'answer key' 并以此为基础进行训练。
# 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 的 'answer key'。但是,我无法将其组合在一起。
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)
首先,看看 Keras - it's a module which uses TensorFlow as its back-end, but wraps the most important neural net bits in very easy to use objects. The RNN documentation can be found 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 来对此建模。
希望对您有所帮助。祝你好运!