【问题标题】:LSTM Tensorflow model not taking sequences into accountLSTM Tensorflow 模型不考虑序列
【发布时间】:2017-05-03 12:08:17
【问题描述】:

我已经为这个宠物问题苦苦挣扎了一段时间,所以任何帮助都将不胜感激!

我有一个 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% 左右。另一方面,该算法可以学习目标列是否不连续。

提前致谢!

【问题讨论】:

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


    【解决方案1】:

    我不太明白你的意思,你的意思是你有这样的 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

    【讨论】:

    • 抱歉,回答迟了,yn 仅基于 x1 列中的最后 5 个条目。 yn 中的前四个条目是 0,之后是 yn = x1(n-5) + x1(n-4) ... + x1n
    猜你喜欢
    • 2020-10-07
    • 2017-02-10
    • 2016-08-24
    • 2021-10-28
    • 2016-10-20
    • 1970-01-01
    • 2021-02-21
    • 2016-11-21
    • 2018-02-06
    相关资源
    最近更新 更多