import tensorflow as tf import numpy as np import matplotlib.pyplot as plt def load_weather(file_path, seq_length): weather = np.loadtxt(file_path, delimiter=' ', skiprows=1) weather = (weather[:,1:]) weather = weather.transpose() weather = weather.reshape(-1, 1) xx, yy = [], [] for i in range(len(weather)-seq_length): xx.append(weather[i:i+seq_length]) yy.append(weather[i+seq_length]) return np.float32(xx), np.float32(yy) # 날씨 데이타# http://www.weather.go.kr/weather/climate/past_table.jspdef rnn_weather(): batch_size = 1 seq_length = 7 n_classes = 1 hidden_size = 5 output_dim = 1 xx, yy = load_weather("Data/weather.txt", seq_length) xx_test, yy_test = load_weather("Data/weather_test.txt", seq_length) x = tf.placeholder(tf.float32, shape=[None, 7, 1]) cells = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size, activation=tf.tanh) output, state_ = tf.nn.dynamic_rnn(cells, x, dtype=tf.float32) z = tf.contrib.layers.fully_connected(inputs=output[:, -1], num_outputs=1, activation_fn=None) loss = tf.reduce_mean((z-yy)**2) optimizer = tf.train.AdamOptimizer(0.01) train = optimizer.minimize(loss) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(5000): sess.run(train, {x:xx}) print(sess.run(loss , {x:xx})) p = sess.run(z, {x:xx_test[30:100]}) plt.plot(p, 'r') plt.plot(yy_test[30:100], 'g') plt.show() rnn_weather()
2018년 10월 20일 토요일
RNN 연습
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기