Example #1
0
File: run.py Project: palisadoes/AI
    for i, data in enumerate(predicted_data):
        padding = [None for p in range(i * prediction_len)]
        plt.plot(padding + data, label='Prediction')
        plt.legend()
    plt.show()


#Main Run Thread
if __name__ == '__main__':
    global_start_time = time.time()
    epochs = 1
    seq_len = 50

    print('> Loading data... ')

    X_train, y_train, X_test, y_test = lstm.load_data('data/sp500.csv',
                                                      seq_len, True)

    print('> Data Loaded. Compiling...', X_train.shape, y_train.shape)

    model = lstm.build_model([1, 50, 100, 1])

    model.fit(X_train,
              y_train,
              batch_size=512,
              nb_epoch=epochs,
              validation_split=0.05)

    predictions = lstm.predict_sequences_multiple(model, X_test, seq_len, 50)
    #predicted = lstm.predict_sequence_full(model, X_test, seq_len)
    #predicted = lstm.predict_point_by_point(model, X_test)
Example #2
0
Created on Wed Nov 20 14:23:36 2019

@author: yyw
"""
from tensorflow.keras.layers import Dense, Activation, Dropout
from tensorflow.keras.layers import LSTM
from tensorflow.keras.models import Sequential
import lstm, time

#1단계: 데이터 불러오기
epochs = 1
seq_len = 50

print('> Loading data... ')

X_train, y_train, X_test, y_test = lstm.load_data(
    'C:/Users/yyw/Downloads/SP500.csv', seq_len, True)
#데이터 확인
print(X_train)

print('> Data Loaded. Compiling...')
#2단계: 모델 생성
model = lstm.build_model([1, 50, 100, 1])

model.fit(X_train,
          y_train,
          batch_size=512,
          nb_epoch=epochs,
          validation_split=0.05)

predicted = lstm.predict_point_by_point(model, X_test)
Example #3
0
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential

# Using TensorFlow backend.

import lstm, time

# step 1 load data
x_train, y_train, x_test, y_test = lstm.load_data('sp500.csv', t0, True)

# step 2 Build model

model = Sequential()

model.add(LSTM(input_dim=1, output_dim=50, return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(100, return_sequences=False))
model.add(Dropout(0.2))

model.add(Dense(output_dim=1))
model.add(Activation('linear'))

start= time.time()
model.compile(loss='mse', optimizer='rmsprop')
print ('comilation time :', (time.time() - start))
Example #4
0
    for i, data in enumerate(predicted_data):
        padding = [None for p in range(i * prediction_len)]
        plt.plot(padding + data, label='Prediction')
        plt.legend()
    plt.show()


# Main Run Thread
if __name__ == '__main__':
    global_start_time = time.time()
    epochs = 1
    seq_len = 50

    print('> Loading data... ')

    X_train, y_train, X_test, y_test = lstm.load_data(
        'D:/DataForMining/BinaryClassificationKeras/sp500.txt', seq_len, True)

    print('> Data Loaded. Compiling...')

    model = lstm.build_model([1, 50, 100, 1])

    model.fit(X_train,
              y_train,
              batch_size=512,
              nb_epoch=epochs,
              validation_split=0.05)

    predictions = lstm.predict_sequences_multiple(model, X_test, seq_len, 50)
    #predicted = lstm.predict_sequence_full(model, X_test, seq_len)
    #predicted = lstm.predict_point_by_point(model, X_test)
Example #5
0
import tensorflow as tf
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
import lstm, time

X_train, y_train, X_test, y_test = lstm.load_data('sp500.csv', 50, True)

#Step 2 Build Model
model = Sequential()

model.add(LSTM(input_dim=1, output_dim=50, return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(100, return_sequences=False))
model.add(Dropout(0.2))

model.add(Dense(output_dim=1))
model.add(Activation('linear'))

start = time.time()
model.compile(loss='mse', optimizer='rmsprop')
print('compilation time : ', time.time() - start)

#Step 3 Train the model
model.fit(X_train, y_train, batch_size=512, nb_epoch=1, validation_split=0.05)

#Step 4 - Plot the predictions!#Step 4
predictions = lstm.predict_sequences_multiple(model, X_test, 50, 50)
print(predictions)
# lstm.plot_results_multiple(predictions, y_test, 50)
Example #6
0
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
import lstm, time

# Step 1: load data
X_train, y_train, X_test, y_test = lstm.load_data('close.csv', 50, True)

# Step 2: build model
model = Sequential()

model.add(LSTM(input_dim=1, output_dim=50, return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(100, return_sequences=False))
model.add(Dropout(0.2))

model.add(Dense(output_dim=1))
model.add(Activation('linear'))

start = time.time()
model.compile(loss='mse', optimizer='rmsprop')
print('compilation time : %f\n' % (time.time() - start))

# Step 3: train the model
model.fit(X_train, y_train, batch_size=128, nb_epoch=1, validation_split=0.05)
#model.fit(X_train, y_train, batch_size=512, nb_epoch=1, validation_split=0.05)

# Step 4: plot the predictions
predictions = lstm.predict_sequences_multiple(model, X_test, 50, 50)
lstm.plot_results_multiple(predictions, y_test, 50)
Example #7
0
f3 = open('aapl1.csv', 'w')
f4 = open('aapl.csv', 'w')
for i in rearray:
    if (count < len(rearray)-365):
        f3.write(i+"\n")

    f4.write(i+"\n")
    count = count + 1






#Step 1 Load Data
X_train, y_train, X_test, y_test = lstm.load_data('aapl.csv', 50, True, today)

#Step 2 Build Model
model = Sequential()

model.add(LSTM(
    input_dim=1,
    output_dim=50,
    return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(
    100,
    return_sequences=False))
model.add(Dropout(0.2))
def plot_results(predicted_data, true_data):
    fig = plt.figure(facecolor='white')
    ax = fig.add_subplot(111)
    ax.plot(true_data, label='True Data')
    plt.plot(predicted_data, label='Prediction')
    plt.legend()
    plt.show()


seq_len = 10
train_samples = 100000
test_samples = 2000

x_raw, y_raw, info = lstm.load_data(path="../2014-04-01_1m_172800.csv",
                                    sequence_length=seq_len,
                                    row_start_ind=0,
                                    in_column_ind=[0, 1, 2, 3, 4, 5, 6],
                                    out_column_ind=[7, 8, 9, 10, 11, 12],
                                    do_normalize=True)

#print(info)

x_dim = x_raw.shape[2]
y_dim = y_raw.shape[2]
x_train, y_train = x_raw[:train_samples, :, :], y_raw[:train_samples, :, :]
x_test, y_test = x_raw[-test_samples:, :, :], y_raw[-test_samples:, :, :]

m_ = lstm.build_model(1, seq_len, x_dim, 100, 1, y_dim, False)
#m_.load_weights("./save_model/env.h5")
m_.fit(x_train, y_train, batch_size=1, nb_epoch=10)
m_.save_weights("./save_model/env.h5")
y_pred = lstm.predict_sequence(m_, x_test, batch_size=1)
Example #9
0
def plot_original(true_data):
    fig = plt.figure(facecolor='white')
    ax = fig.add_subplot(111)
    ax.plot(true_data, label='True Data')
    plt.legend()
    plt.show()

#Main Run Thread
if __name__=='__main__':
	global_start_time = time.time()
	epochs  = 1
	seq_len = 50

	print('> Loading data... ')

	X_train, y_train, X_test, y_test = lstm.load_data('sinwave.csv', seq_len, True)

	print('> Data Loaded. Compiling...')

	model = lstm.build_model([1, 50, 100, 1])

	model.fit(
	    X_train,
	    y_train,
	    batch_size=512,
	    nb_epoch=epochs,
	    validation_split=0.05)

	#predictions = lstm.predict_sequences_multiple(model, X_test, seq_len, 50)
	#predicted = lstm.predict_sequence_full(model, X_test, seq_len)
	predicted = lstm.predict_point_by_point(model, X_test)        
Example #10
0
File: run.py Project: qzpzd/maching
    fig = plt.figure(facecolor='white')
    ax = fig.add_subplot(111)
    ax.plot(true_data, label='True Data')
    #Pad the list of predictions to shift it in the graph to it's correct start
    for i, data in enumerate(predicted_data):
        padding = [None for p in range(i * prediction_len)]
        plt.plot(padding + data, label='Prediction')
        plt.legend()
    plt.show()


if __name__ == '__main__':
    global_start_time = time.time()
    seq_len = 100

    X_train, y_train, X_test, y_test = lstm.load_data('small_data.csv',
                                                      seq_len, True)
    #    训练模型
    filepath = "model.h5"
    checkpoint = ModelCheckpoint(filepath,
                                 monitor='loss',
                                 verbose=1,
                                 save_best_only=True,
                                 mode='min')
    callbacks_list = [checkpoint]

    model = lstm.build_model([1, 100, 200, 1])
    model.fit(X_train,
              y_train,
              batch_size=512,
              nb_epoch=1,
              validation_split=0.05,
Example #11
0
NO_FEATURES = 1

SEQ_LEN = 1
PREDICTION_LEN = 1
PREDICTION_DELAY = 0

NORMALISATION = True
TRAIN_TEST_RATIO = 0.9

matrix = return_data()

matrix = np.array(matrix[:NO_FEATURES])

# Get the training and test test
train_X, train_y, test_X, test_y = load_data(matrix, SEQ_LEN, PREDICTION_LEN,
                                             PREDICTION_DELAY, NORMALISATION,
                                             TRAIN_TEST_RATIO)

clf = svm.SVR()
# clf = svm.SVR(C=1.0, cache_size=500, coef0=0.0, degree=1, epsilon=0.3, gamma='auto',
#     kernel='rbf', max_iter=500, shrinking=False, tol=0.01, verbose=False)

train_X = np.array(train_X).reshape((len(train_X), 1))
train_y = np.array(train_y).reshape((len(train_y), 1))
test_X = np.array(test_X).reshape((len(test_X), 1))
test_y = np.array(test_y).reshape((len(test_y), 1))

clf.fit(train_X, train_y)
print(clf.predict(test_X))
print(clf.score(train_X, train_y))
print(clf.score(test_X, test_y))
input_name

#forecast_allts = np.zeros((100, 52))

#for i in range(1, 101, 1):
epochs = 300   #better to admitting variant epoch based on each case, here I use 800 for all items
seq_len = 78
fwd_len = 78
valid_ratio = 0.1
act_fnc = "linear"   # tanh is used for rerunning couple of over-trained series
print('> Loading data for stock ticker/tiemFreq ' + inputName + ' No.' + No + ' ... ')
  

import lstm
fp, maxList, minList, X_train, y_train = lstm.load_data(inputName, No, seq_len, fwd_len, True) # loading data and convert for training
"""
if (fp > seq_len - 20): # keep at least 20 windows for training, or go smaller
    # smaller window to forecast shorter series, keep seq_len = fwd_len here, match the month frame
    seq_len = max(2, 4*int((156 - fp)/3/4)) # keep seq_len positive
    fwd_len = seq_len
    if (seq_len == 2):
        Print("series is too short for LSTM training, skip this item No." + str(i))
        continue
   
print('> training item No.', str(i), ' of window ', str(seq_len), ', start training......')
if seq_len in [4, 8]:
    valid_ratio = 0.25
elif seq_len == 2:
    valid_ratio = 0.5
if seq_len <= 2:
Example #13
0
        padding = [None for p in range(i * prediction_len)]
        plt.plot(padding + data, label='Prediction')
        plt.legend()

    plt.show()


#Main Run Thread
if __name__ == '__main__':
    global_start_time = time.time()
    epochs = 10
    seq_len = 50

    print('> Loading data... ')

    X_train, y_train, X_test, y_test = lstm.load_data('siraj.csv', seq_len,
                                                      False)

    print('> Data Loaded. Compiling...')

    model = lstm.build_model([1, 50, 100, 1])

    model.fit(X_train,
              y_train,
              batch_size=32,
              nb_epoch=epochs,
              validation_split=0.05)

    predictions = lstm.predict_sequences_multiple(model, X_test, seq_len, 50)
    #predictedfull = lstm.predict_sequence_full(model, X_test, seq_len)
    #predictedpoint = lstm.predict_point_by_point(model, X_test)
Example #14
0
        rmse = 100.0
        mape = 100.0
    print('Test RMSE: %.3f' % rmse)
    print('Test MAPE: %.3f ' % mape)

    pyplot.plot(true_load, label='True')
    pyplot.plot(predicted_load, '--', label='predicted')
    pyplot.legend()
    pyplot.show()
    return predicted_load, true_load


if __name__ == '__main__':
    global_start_time = time.time()
    # # epochs  = 10
    seq_len = 2

    print('> Loading data... ')

    X_train, y_train, X_test, y_test, scaler = lstm.load_data(
        'fuzzy_out.csv', seq_len)
    num_features = X_train.shape[2]
    dataset = [X_train, y_train, X_test, y_test]
    predicted_load, true_load = train_score()

    # Plot results
    pyplot.plot(true_load, label='True')
    pyplot.plot(predicted_load, label='predicted')
    pyplot.legend()
    pyplot.show()
Example #15
0
    #Pad the list of predictions to shift it in the graph to it's correct start
    for i, data in enumerate(predicted_data):
        padding = [None for p in range(i * prediction_len)]
        plt.plot(padding + data, label='Prediction')
        plt.legend()
    plt.show()

#Main Run Thread
if __name__=='__main__':
	global_start_time = time.time()
	epochs  =50
	seq_len = 50
	
	print('> Loading data... ')

	orig_data, X_train, y_train, X_test, y_test = lstm.load_data('hu.csv', seq_len, True)

	print('> Data Loaded. Compiling...')

	model = lstm.build_model([1, 400,400, 1])

	hist = model.fit(
	    X_train,
	    y_train,
	    batch_size=128,
	    nb_epoch=epochs,
	    validation_split=0.05)


	plt.plot(hist.history['loss'], label = 'train')
	plt.plot(hist.history['val_loss'], label = 'test')
Example #16
0
        plt.legend()
    plt.show()


#Main Run Thread
if __name__ == '__main__':
    global_start_time = time.time()
    epochs = 1
    window_size = 50
    prediction_len = 50
    batch_size = 5000
    validation_split = 0.005

    print('> Loading data... ')

    X_train, y_train, X_test, y_test = lstm.load_data(
        'dice_amplified/20_mil_dos_mixed.csv', window_size, True)

    print('> Data Loaded. Compiling...')

    model = lstm.build_model([1, 50, 100, 1])

    model.fit(X_train,
              y_train,
              batch_size=batch_size,
              nb_epoch=epochs,
              validation_split=validation_split)

    predictions = lstm.predict_sequences_multiple(model, X_test, window_size,
                                                  prediction_len)
    #predicted = lstm.predict_sequence_full(model, X_test, window_size)
    #predicted = lstm.predict_point_by_point(model, X_test)
Example #17
0
File: run.py Project: palisadoes/AI
    #Pad the list of predictions to shift it in the graph to it's correct start
    for i, data in enumerate(predicted_data):
        padding = [None for p in range(i * prediction_len)]
        plt.plot(padding + data, label='Prediction')
        plt.legend()
    plt.show()

#Main Run Thread
if __name__=='__main__':
    global_start_time = time.time()
    epochs  = 1
    seq_len = 50

    print('> Loading data... ')

    X_train, y_train, X_test, y_test = lstm.load_data('data/sp500.csv', seq_len, True)

    print('> Data Loaded. Compiling...', X_train.shape, y_train.shape)

    model = lstm.build_model([1, 50, 100, 1])

    model.fit(
        X_train,
        y_train,
        batch_size=512,
        nb_epoch=epochs,
        validation_split=0.05)

    predictions = lstm.predict_sequences_multiple(model, X_test, seq_len, 50)
    #predicted = lstm.predict_sequence_full(model, X_test, seq_len)
    #predicted = lstm.predict_point_by_point(model, X_test)
Example #18
0
row = round(0.9 * result.shape[0])
train = result[:int(row), :]
np.random.shuffle(train)
x_train = train[:, :-1]
y_train = train[:, -1]
x_test = result[int(row):, :-1]
y_test = result[int(row):, -1]

sys.exit()

x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))

print[x_train, y_train, x_test, y_test]

X_train, y_train, X_test, y_test = lstm.load_data(input_data_filename, seq_len,
                                                  True)

print("TRAINING ROWS: {0}     TEST ROWS: {1}".format(X_train.shape[0],
                                                     X_test.shape[0]))

print('> Data Loaded. Compiling...')

#model = lstm.build_model([1, 50, 100, 1])
# Don't hardcode "50" but use seq_len instead because seq_len is the lookback length
# original model layers were [1, 50, 100, 1]
model = lstm.build_model([1, seq_len, seq_len * 2, 1])

model.fit(X_train,
          y_train,
          batch_size=512,
          nb_epoch=epochs,