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