tensorflow70 《深度学习原理与TensorFlow实战》05 RNN能说会道 01 正弦序列预测

01 基本环境

#《深度学习原理与TensorFlow实战》05 RNN能说会道
# 书源码地址:https://github.com/DeepVisionTeam/TensorFlowBook.git
# 视频讲座地址:http://edu.csdn.net/course/detail/5222
# win10 Tensorflow1.2.0 python3.6.1
# CUDA v8.0 cudnn-8.0-windows10-x64-v5.1
# 本地代码位置:D:\git\DeepLearning\TensorFlowBook\neural_style\neural_style.py
# https://github.com/DeepVisionTeam/TensorFlowBook/blob/master/rnn/sin/rnn_sin.py
# https://github.com/DeepVisionTeam/TensorFlowBook/blob/master/rnn/sin/lstm_sin.py
# 在tensorflow1.2.0版本上,对rnn_sin.py和lstm_sin.py稍作了修改

02 rnn_sin.py

from __future__ import absolute_import, division, print_function

import random

import numpy as np
import tensorflow as tf
from tensorflow.contrib import rnn

def build_data(n):
    xs = []
    ys = []
    for i in range(0, 2000):
        k = random.uniform(1, 50)

        x = [[np.sin(k + j)] for j in range(0, n)]
        y = [np.sin(k + n)]

        # x[i] = sin(k + i) (i = 0, 1, ..., n-1)
        # y[i] = sin(k + n)
        xs.append(x)
        ys.append(y)

    train_x = np.array(xs[0: 1500])
    train_y = np.array(ys[0: 1500])
    test_x = np.array(xs[1500:])
    test_y = np.array(ys[1500:])
    return (train_x, train_y, test_x, test_y)

length = 10
time_step_size = length
vector_size = 1
batch_size = 10
test_size = 10

# build data
(train_x, train_y, test_x, test_y) = build_data(length)
print(train_x.shape, train_y.shape, test_x.shape, test_y.shape)

X = tf.placeholder("float", [None, length, vector_size])
Y = tf.placeholder("float", [None, 1])

# get lstm_size and output predicted value
W = tf.Variable(tf.random_normal([10, 1], stddev=0.01))
B = tf.Variable(tf.random_normal([1], stddev=0.01))

def seq_predict_model(X, w, b, time_step_size, vector_size):
    # input X shape: [batch_size, time_step_size, vector_size]
    # transpose X to [time_step_size, batch_size, vector_size]
    X = tf.transpose(X, [1, 0, 2])
    # reshape X to [time_step_size * batch_size, vector_size]
    X = tf.reshape(X, [-1, vector_size])
    # split X, array[time_step_size], shape: [batch_size, vector_size]
    X = tf.split(X, time_step_size, 0)

    cell = rnn.BasicRNNCell(num_units=10)
    initial_state = tf.zeros([batch_size, cell.state_size])
    outputs, _states = rnn.static_rnn(cell, X, initial_state=initial_state)

    # Linear activation
    return tf.matmul(outputs[-1], w) + b, cell.state_size

pred_y, _ = seq_predict_model(X, W, B, time_step_size, vector_size)
loss = tf.square(tf.subtract(Y, pred_y))
train_op = tf.train.GradientDescentOptimizer(0.001).minimize(loss)

with tf.Session() as sess:
    tf.global_variables_initializer().run()

    # train
    for i in range(50):
        # train
        for end in range(batch_size, len(train_x), batch_size):
            begin = end - batch_size
            x_value = train_x[begin: end]
            y_value = train_y[begin: end]
            sess.run(train_op, feed_dict={X: x_value, Y: y_value})

        # randomly select validation set from test set
        test_indices = np.arange(len(test_x))
        np.random.shuffle(test_indices)
        test_indices = test_indices[0: test_size]
        x_value = test_x[test_indices]
        y_value = test_y[test_indices]

        # eval in validation set
        val_loss = np.mean(sess.run(loss,
                                    feed_dict={X: x_value, Y: y_value}))
        print('Run %s' % i, val_loss)

    for b in range(0, len(test_x), test_size):
        x_value = test_x[b: b + test_size]
        y_value = test_y[b: b + test_size]
        pred = sess.run(pred_y, feed_dict={X: x_value})
        for i in range(len(pred)):
            print(pred[i], y_value[i], pred[i] - y_value[i])
'''
(1500, 10, 1) (1500, 1) (500, 10, 1) (500, 1)
Run 0 0.000285596
Run 1 7.50029e-05
Run 2 8.55531e-05
...
Run 48 4.05094e-05
Run 49 2.30997e-05
[ 0.60128832] [ 0.61017627] [-0.00888795]
[ 0.77109075] [ 0.77846642] [-0.00737568]
[ 0.41146952] [ 0.40745923] [ 0.00401029]
...
[ 0.98992503] [ 0.98445371] [ 0.00547131]
[ 0.35108942] [ 0.34326224] [ 0.00782718]
[-0.29042405] [-0.27972568] [-0.01069837]
'''

03 lstm_sin.py

from __future__ import absolute_import, division, print_function

import random

import numpy as np
import tensorflow as tf
from tensorflow.contrib import rnn

def build_data(n):
    xs = []
    ys = []
    for i in range(0, 2000):
        k = random.uniform(1, 50)

        x = [[np.sin(k + j)] for j in range(0, n)]
        y = [np.sin(k + n)]

        # x[i] = sin(k + i) (i = 0, 1, ..., n-1)
        # y[i] = sin(k + n)
        xs.append(x)
        ys.append(y)

    train_x = np.array(xs[0: 1500])
    train_y = np.array(ys[0: 1500])
    test_x = np.array(xs[1500:])
    test_y = np.array(ys[1500:])
    return (train_x, train_y, test_x, test_y)

length = 10
time_step_size = length
vector_size = 1
batch_size = 10
test_size = 10

# build data
(train_x, train_y, test_x, test_y) = build_data(length)
print(train_x.shape, train_y.shape, test_x.shape, test_y.shape)

X = tf.placeholder("float", [None, length, vector_size])
Y = tf.placeholder("float", [None, 1])

# get lstm_size and output predicted value
W = tf.Variable(tf.random_normal([10, 1], stddev=0.01))
B = tf.Variable(tf.random_normal([1], stddev=0.01))


def seq_predict_model(X, w, b, time_step_size, vector_size):
    # input X shape: [batch_size, time_step_size, vector_size]
    # transpose X to [time_step_size, batch_size, vector_size]
    X = tf.transpose(X, [1, 0, 2])
    # reshape X to [time_step_size * batch_size, vector_size]
    X = tf.reshape(X, [-1, vector_size])
    # split X, array[time_step_size], shape: [batch_size, vector_size]
    X = tf.split(X, time_step_size, 0)

    # LSTM model with state_size = 10
    cell = rnn.BasicLSTMCell(num_units=10,
                                       forget_bias=1.0,
                                       state_is_tuple=True)
    outputs, _states = rnn.static_rnn(cell, X, dtype=tf.float32)

    # Linear activation
    return tf.matmul(outputs[-1], w) + b, cell.state_size


pred_y, _ = seq_predict_model(X, W, B, time_step_size, vector_size)
loss = tf.square(tf.subtract(Y, pred_y))
train_op = tf.train.GradientDescentOptimizer(0.001).minimize(loss)

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    writer = tf.summary.FileWriter('./log', sess.graph)

    # train
    for i in range(50):
        # train
        for end in range(batch_size, len(train_x), batch_size):
            begin = end - batch_size
            x_value = train_x[begin: end]
            y_value = train_y[begin: end]
            sess.run(train_op, feed_dict={X: x_value, Y: y_value})

        # randomly select validation set from test set
        test_indices = np.arange(len(test_x))
        np.random.shuffle(test_indices)
        test_indices = test_indices[0: test_size]
        x_value = test_x[test_indices]
        y_value = test_y[test_indices]

        # eval in validation set
        val_loss = np.mean(sess.run(loss,
                                    feed_dict={X: x_value, Y: y_value}))
        print('Run %s' % i, val_loss)

    for b in range(0, len(test_x), test_size):
        x_value = test_x[b: b + test_size]
        y_value = test_y[b: b + test_size]
        pred = sess.run(pred_y, feed_dict={X: x_value})
        for i in range(len(pred)):
            print(pred[i], y_value[i], pred[i] - y_value[i])
'''
(1500, 10, 1) (1500, 1) (500, 10, 1) (500, 1)
Run 0 0.501963
Run 1 0.490687
Run 2 0.328985
...
Run 48 0.000195758
Run 49 0.000305991
[ 0.16057003] [ 0.16943663] [-0.00886661]
[ 0.25243664] [ 0.24671422] [ 0.00572242]
...
[-0.22830085] [-0.22997938] [ 0.00167852]
[-0.80274117] [-0.82659579] [ 0.02385462]
'''
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值