Created
June 11, 2018 08:39
-
-
Save 0x1306e6d/c8aec33c4f4231dd725229216f99f687 to your computer and use it in GitHub Desktop.
Radio Transmission Prediction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
import datetime | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import tensorflow as tf | |
import input_data2 | |
# hyperparameter | |
learning_rate = 0.05 | |
input_dim = 2 | |
output_dim = 1 | |
sequence_length = 32 | |
num_units = 1 | |
num_layers = 128 | |
iteration = 100000 | |
keep_prob = 0.5 | |
training_data_set_ratio = 0.7 | |
print("Starting arto at {}".format(datetime.datetime.now())) | |
print("Loading data set...") | |
data_set = input_data2.DataSet() | |
data_set.load("c47e2.CSV") | |
data_set.load("c5022.CSV") | |
print("Data set is loaded. size: {}".format(len(data_set.data_set))) | |
data_set = np.array(data_set.data_set) | |
data_set_x = data_set[:, [0]] | |
data_set_y = data_set[:, [2]] | |
data_x = [] | |
data_y = [] | |
for i in range(0, len(data_set) - sequence_length, sequence_length): | |
x = np.copy(data_set_x[i: i + sequence_length]) | |
y = data_set_y[i + sequence_length] | |
data_x.append(x) | |
data_y.append(y) | |
train_size = int(len(data_set) * training_data_set_ratio) | |
test_size = len(data_set) - train_size | |
training_data_set_x = np.array(data_x[:train_size]) | |
training_data_set_y = np.array(data_y[:train_size]) | |
test_data_set_x = np.array(data_x[train_size:]) | |
test_data_set_y = np.array(data_y[train_size:]) | |
print("Size of training: {}, test: {}".format(train_size, test_size)) | |
X = tf.placeholder(dtype=tf.float32, shape=[None, sequence_length, input_dim]) | |
Y = tf.placeholder(dtype=tf.float32, shape=[None, output_dim]) | |
def make_cell(): | |
cell = tf.contrib.rnn.LSTMBlockCell(num_units=num_units) | |
return tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=keep_prob) | |
cells = tf.contrib.rnn.MultiRNNCell([make_cell() for _ in range(num_layers)], | |
state_is_tuple=True) | |
outputs, state = tf.nn.dynamic_rnn(cells, X, dtype=tf.float32) | |
Y_prediction = tf.contrib.layers.fully_connected(outputs[:, -1], output_dim, | |
activation_fn=None) | |
loss_op = tf.reduce_sum(tf.square(Y_prediction - Y)) | |
train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss_op) | |
with tf.Session() as sess: | |
init = tf.global_variables_initializer() | |
sess.run(init) | |
for i in range(iteration): | |
_, loss = sess.run([train_op, loss_op], | |
feed_dict={X: training_data_set_x, | |
Y: training_data_set_y}) | |
if i % 10 == 0: | |
print("[{}] loss: {}".format(i, loss)) | |
if i % 100 == 0: | |
training_predict = sess.run(Y_prediction, | |
feed_dict={X: training_data_set_x}) | |
plt.plot(training_predict, alpha=0.5, label="prediction") | |
plt.plot(training_data_set_y, alpha=0.5, label="actual") | |
plt.legend() | |
plt.show() | |
test_predict = sess.run(Y_prediction, feed_dict={X: test_data_set_x}) | |
print("Test Prediction: {}".format(test_predict)) | |
print("Actual: {}".format(test_data_set_y)) | |
plt.plot(test_predict, alpha=0.5, label="prediction") | |
plt.plot(test_data_set_y, alpha=0.5, label="actual") | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment