import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
from IPython.display import Image
def show(id, w=6):
return Image('../pics/class3/{}.jpg'.format(id), width=w*100)
!ls Recurrent_Neural_Networks/
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset_train = pd.read_csv('Recurrent_Neural_Networks/Google_Stock_Price_Train.csv')
print dataset_train.shape
dataset_train.head()
training_set = dataset_train.iloc[:,1:2].values
print training_set.shape, type(training_set)
training_set
show(101)
from sklearn.preprocessing import MinMaxScaler, Normalizer, StandardScaler
# MinMaxScaler?
sc = MinMaxScaler()
sc
training_set_scaled = sc.fit_transform(training_set)
training_set_scaled
training_set_scaled.shape
# creating a data structure with 60 timesteps and 1 output
X_train = []
y_train = []
for i in range(60, training_set_scaled.shape[0]):
X_train.append(training_set_scaled[i-60:i, 0])
y_train.append(training_set_scaled[i, 0])
print len(X_train), len(y_train)
X_train, y_train = np.array(X_train), np.array(y_train)
X_train.shape, y_train.shape
pd.DataFrame(X_train).tail()
# reshaping
# np.reshape?
print np.reshape(X_train, (1198,60,1)).shape
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
# build RNN
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM
? return sequences
# initializing
regressor = Sequential()
# first LSTM layer
regressor.add(LSTM(units= 50, return_sequences= True, input_shape = (X_train.shape[1], 1)))
# add dropout
regressor.add(Dropout(0.2))
# second LSTM layer
regressor.add(LSTM(units= 50, return_sequences= True))
regressor.add(Dropout(0.2))
# third LSTM layer
regressor.add(LSTM(units= 50, return_sequences= True))
regressor.add(Dropout(0.2))
# fourth LSTM layer
# regressor.add(LSTM(units= 50, return_sequences= False))
regressor.add(LSTM(units= 50))
regressor.add(Dropout(0.2))
# output layer
regressor.add(Dense(units=1))
# compiling
regressor.compile(optimizer='adam', loss='mean_squared_error')
regressor.summary()
print X_train.shape, y_train.shape
regressor.fit(X_train, y_train, epochs= 100, batch_size= 32)
# CPU is slower
# print X_train.shape, y_train.shape
# regressor.fit(X_train, y_train, epochs= 100, batch_size= 32)
regressor.save('RNN_stock.h5')
!ls -hl
from keras.models import load_model
# predict
dataset_test = pd.read_csv('Recurrent_Neural_Networks/Google_Stock_Price_Test.csv')
real_stopck_price= dataset_test.iloc[:,1:2].values
real_stopck_price.shape
real_stopck_price[:3]
loads = load_model('./RNN_stock.h5')
loads
dataset_train = pd.read_csv('Recurrent_Neural_Networks/Google_Stock_Price_Train.csv')
print dataset_train.shape
dataset_train.head()
train_open_last=dataset_train['Open'].values
train_open_last.shape
train_open_last[-80:]==train_open_last[1258-20-60:]
train_open_last = train_open_last[-80:].reshape(-1,1)
train_open_last.shape
train_open_last.shape
train_open_last[:3]
train_open_last = sc.transform(train_open_last)
train_open_last[79]
Y_test = []
for i in range(60, 80):
Y_test.append(train_open_last[i-60:i,0])
Y_test = np.array(Y_test)
Y_test.shape
Y_test
Y_test=Y_test.reshape(20,60,1)
Y_test.shape
pred = loads.predict(Y_test)
pred.shape
pred[:3]
pred = sc.inverse_transform(pred)
pred[:3]
plt.figure(figsize=(10,4))
plt.plot(pred, c='r', label='pred')
plt.plot(real_stopck_price, c='g', label='read')
plt.legend(loc=2)
plt.title('prediction')
plt.show()
plt.legend?