对数函数预测

时间:2019-08-06 22:12:28

标签: lstm recurrent-neural-network predict

我是ML初学者。我试图训练一个LSTM模型来预测log2函数,但是结果太不准确了。谁能帮我看看我应该如何改变?非常感谢。

我试图调整超级参数,但仍然没有运气

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt


NUM_EPOCH = 1000
HIDDEN_SIZE = 30
NUM_LAYERS = 2
TIMESTEPS = 10
TRAINING_STEPS = 20000
BATCH_SIZE = 20
TRAINING_EXAMPLES = 10000
TESTING_EXAMPLES = 1000
SAMPLE_GAP = 0.01


def generate_data(seq):
    X = []
    y = []
    for i in range(len(seq) - TIMESTEPS):
        X.append([seq[i: i + TIMESTEPS]])
        y.append([seq[i + TIMESTEPS]])
    return np.array(X, dtype=np.float32), np.array(y, dtype=np.float32)


def lstm_model(X, y, is_training):
    cell = tf.nn.rnn_cell.MultiRNNCell([tf.nn.rnn_cell.LSTMCell(HIDDEN_SIZE) for _ in range(NUM_LAYERS)])
    outputs, _ = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
    output = outputs[:, -1, :]
    predictions = tf.contrib.layers.fully_connected(output, 1, activation_fn=None)
    if not is_training:
        return predictions, None, None
    loss = tf.losses.mean_squared_error(labels=y, predictions=predictions)
    train_op = tf.contrib.layers.optimize_loss(
        loss, tf.train.get_global_step(), optimizer='Adagrad', learning_rate=0.1)
    return predictions, loss, train_op


def train(sess, train_X, train_Y):
    ds = tf.data.Dataset.from_tensor_slices((train_X, train_Y))
    ds = ds.repeat().shuffle(1000).batch(BATCH_SIZE)
    X, y = ds.make_one_shot_iterator().get_next()
    losses = np.array([])

    with tf.variable_scope('model'):
        predictions, loss, train_op = lstm_model(X, y, True)
    sess.run(tf.global_variables_initializer())
    for i in range(TRAINING_STEPS):
        _, l = sess.run([train_op, loss])
        losses = np.append(losses, l)
        if i % NUM_EPOCH == 0:
            print('train step: ' + str(i) + ', loss: ' + str(l))

    plt.figure()
    plt.plot(losses, label='loss')
    plt.legend()
    # plt.show()
    plt.savefig('./test_outputs/loss.png')


def run_eval(sess, test_X, test_y):
    ds = tf.data.Dataset.from_tensor_slices((test_X, test_y))
    ds = ds.batch(1)
    X, y = ds.make_one_shot_iterator().get_next()
    with tf.variable_scope('model', reuse=True):
        prediction, _, _ = lstm_model(X, [0, 0], False)
    predictions = []
    labels = []
    for i in range(int(TESTING_EXAMPLES / 2)):
        p, l = sess.run([prediction, y])
        predictions.append(p)
        labels.append(l)

    predictions = np.array(predictions).squeeze()
    labels = np.array(labels).squeeze()
    rmse = np.sqrt(((predictions - labels) ** 2).mean(axis=0))
    print('Mean Square Error is: %f' % rmse)

    plt.figure()
    print(predictions[:15])
    print(labels[:15])
    plt.plot(predictions, label='predictions')
    plt.plot(labels, label='real_val')
    plt.legend()
    # plt.show()
    plt.savefig('./test_outputs/test.png')


test_start = (TRAINING_EXAMPLES + TIMESTEPS) * SAMPLE_GAP + 1
test_end = test_start + (TESTING_EXAMPLES + TIMESTEPS) * SAMPLE_GAP + 1

train_X, train_y = generate_data(np.log2(np.linspace(
    SAMPLE_GAP, test_start, TRAINING_EXAMPLES + TIMESTEPS, dtype=np.float32)))
test_X, test_y = generate_data(np.log2(np.linspace(
    test_start, test_end, TESTING_EXAMPLES + TIMESTEPS, dtype=np.float32)))


x_val, test_X = np.split(test_X, 2)
y_val, test_y = np.split(test_y, 2)

with tf.Session() as sess:
    train(sess, train_X, train_y)
    run_eval(sess, test_X, test_y)

培训输出为:

火车步长:0,损失:6.226374 训练步数:1000,损失:0.50559336 训练步数:2000,损失:0.008770825 训练步数:3000,损失:0.006236521 训练步数:4000,损失:0.001877052 训练步数:5000,损失:0.0035174307 训练步数:6000,损失:0.0012169601 训练步数:7000,损失:0.001992577 训练步数:8000,损失:0.00071869115 训练步数:9000,损失:0.0015831447 训练步数:10000,损失:0.0015313074 训练步数:11000,损失:0.0011248669 训练步数:12000,损失:0.0009140003 训练步数:13000,损失:0.0008928403 训练步数:14000,损失:0.00045768628 训练步数:15000,损失:0.000446374 训练步数:16000,损失:0.0010496832 训练步数:17000,损失:0.0008943161 训练步数:18000,损失:0.0010290025 训练步数:19000,损失:0.0005238621 均方误差为:0.052667 [6.6955047 6.6956124 6.69572 6.695827 6.6959357 6.6960425 6.69615  6.6962576 6.6963654 6.696472 6.696579 6.6966867 6.696795 6.696902  6.697009] [6.7375584 6.737707 6.737856 6.7380047 6.738153 6.7383018 6.7384505  6.7385993 6.7387476 6.7388964 6.739045 6.7391934 6.739342 6.7394905  6.7396393] [在此处输入图片描述]

训练输出图像:

Loss output

Prediction Output

0 个答案:

没有答案