LSTM Tensorflow模型不考虑序列

时间:2017-05-03 12:08:17

标签: python tensorflow neural-network deep-learning lstm

我一直在努力解决这个宠物问题,所以任何帮助都会受到赞赏!

我有一个csv文件,其中包含一些随机列,以及一个基于第一列最后几个值之和的最终列。我正在尝试使用LSTM模型来捕获这个结构,即预测最后几列的最后一列。

这是我一直在使用的模型:

# Generate test data

train_input = train_input.reshape(m, n_input, 1) # is nr of rows, n_input is number of input columns

NUM_EXAMPLES = int(m * training_size)

test_input = train_input[NUM_EXAMPLES:]
test_output = train_output[NUM_EXAMPLES:]

train_input = train_input[:NUM_EXAMPLES]
train_output = train_output[:NUM_EXAMPLES]
#
# # Design model
#
data = tf.placeholder(tf.float32, [None, n_input, 1])
target = tf.placeholder(tf.float32, [None, n_classes])

num_hidden = 24
cell = tf.contrib.rnn.LSTMCell(num_hidden, state_is_tuple=True)

val, state = tf.nn.dynamic_rnn(cell, data, dtype=tf.float32)

val = tf.transpose(val, [1, 0, 2])
last = tf.gather(val, int(val.get_shape()[0]) - 1)

weight = tf.Variable(tf.truncated_normal([num_hidden, int(target.get_shape()[1])]))
bias = tf.Variable(tf.constant(0.1, shape=[target.get_shape()[1]]))

prediction = tf.nn.softmax(tf.matmul(last, weight) + bias)

cross_entropy = -tf.reduce_sum(target * tf.log(tf.clip_by_value(prediction,1e-10,1.0)))

optimizer = tf.train.AdamOptimizer()
minimize = optimizer.minimize(cross_entropy)

mistakes = tf.not_equal(tf.argmax(target, 1), tf.argmax(prediction, 1))
error = tf.reduce_mean(tf.cast(mistakes, tf.float32))

init_op = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init_op)

no_of_batches = int(len(train_input)/batch_size)
for i in range(epoch):
    ptr = 0
    for j in range(no_of_batches):
        inp, out = train_input[ptr:ptr+batch_size], train_output[ptr:ptr+batch_size]
        ptr+=batch_size
        sess.run(minimize,{data: inp, target: out})
    print("Epoch - {}".format(i))
incorrect = sess.run(error,{data: test_input, target: test_output})
print('Epoch {:2d} error {:3.1f}%'.format(i + 1, 100 * incorrect))
sess.close()

我已经尝试了几个随机数的电子表格,而且我的错误率一直在83%左右。另一方面,该算法可以了解目标列是否不是连续的。

提前致谢!

1 个答案:

答案 0 :(得分:0)

我无法明确指出你的意思,你是说你有这样的csv文件吗?

x1   x2   x3   x4   ... xn
v11  v21  v31  v41  ... vn1
v12  v22  v32  v42  ... vn2
...
v1n  v2n  v3n  v4n  ... vnn
y1   y2   y3   y4   ... yn

yn基于sum(vn1+...+vnn)?喜欢a * sum(V) + b