【发布时间】: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