Skip to content

Instantly share code, notes, and snippets.

@ecsplendid
Last active April 23, 2019 02:45
Show Gist options
  • Save ecsplendid/65f45a0f58da46eccffa02c8800f6015 to your computer and use it in GitHub Desktop.
Save ecsplendid/65f45a0f58da46eccffa02c8800f6015 to your computer and use it in GitHub Desktop.
Use of 1D convolution across overlapping sliding windows of trading data. There is a time-dependency in the data which I want to accentuate
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d_1 (Conv1D) (None, 33, 50) 25050
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 6, 50) 0
_________________________________________________________________
lstm_1 (LSTM) (None, 30) 9720
_________________________________________________________________
dense_1 (Dense) (None, 200) 6200
_________________________________________________________________
dropout_1 (Dropout) (None, 200) 0
_________________________________________________________________
dense_2 (Dense) (None, 200) 40200
_________________________________________________________________
dense_3 (Dense) (None, 20) 4020
_________________________________________________________________
dense_4 (Dense) (None, 1) 21
=================================================================
from __future__ import print_function
import keras
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Bidirectional, Dense, Dropout, Activation, Flatten, GaussianDropout,GaussianNoise
from keras.layers import Conv1D, Conv2D, Reshape, MaxPooling1D, LSTM
from keras.utils.np_utils import to_categorical
import numpy as np
from keras import regularizers
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
# train == (4525, 34) last col is binary label
dataset_train = np.loadtxt("train.csv",delimiter=",",skiprows=1)
# test == (1507, 34) last col is binary label
dataset_test = np.loadtxt("test.csv",delimiter=",",skiprows=1)
# window size
ws = 100;
def windowize( dataset, ws ):
recs, features = dataset.shape
signals = np.ndarray(shape=(recs-ws,features-1,ws), dtype=float, order='F')
# we want to compute (samplesize-ws) rectangular overlapping windows.
# this means we can not make predictions on the first ws records
for i in range(0,recs-ws):
signals[i, :, :] = dataset[ i:i+ws, :-1].transpose()
labels = dataset[ws:,-1].astype(int)
return (signals , labels)
train_x, train_y = windowize(dataset_train, ws)
test_x, test_y = windowize(dataset_test, ws)
# looking good?
plt.show( plt.imshow(train_x[100,:,:],interpolation="none") )
model = Sequential()
model.add(Conv1D(filters = 50,
activation = "relu",
kernel_size = (5),
dilation_rate = 2,
kernel_regularizer=regularizers.l2(0.01),
activity_regularizer=regularizers.l1(0.01),
padding='same',
input_shape=train_x.shape[1:]))
model.add(MaxPooling1D(pool_size=5))
model.add(LSTM(30))
model.add(Dense(200, activation="relu"))
model.add(Dropout(0.25))
model.add(Dense(200, activation="relu"))
model.add(Dense(20, activation="relu"))
model.add(Dense(1, activation="sigmoid"))
model.summary()
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(train_x,
train_y,
nb_epoch=1000,
batch_size=15,
verbose=1,
validation_data=(test_x,test_y))
scores = model.evaluate(test_x, test_y)
model.save_weights(filepath="weights.w")
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment