<jupyter_start><jupyter_text>Here’s an example of my script in which I make multilayer perceptron on some financial data. The data has 53 independent variables (X) and one independent variable (y). It is time series data and X exhibits plenty of time-series related information. The goal is to fit MP as good as possible.<jupyter_code>### Import packages:
import numpy as np
import sys
import theano
import theano.tensor as T
from keras.models import Sequential
import keras
### Load data and count the number of samples.
input_data_all = np.load('train_sample.npy')
output_data_all= np.load('train_target.npy')
n_samples= input_data_all.shape[0]
print(n_samples)
### shuffle 2 data series together randomly.
def shuffle_together(input_1, input_2):
    if input_1.shape[0]!= input_2.shape[0]:
        print ("Problem, y and x array are not of the same shape.")
        return None
    c= np.arange(input_1.shape[0])
    np.random.shuffle(c)
    return input_1[c], input_2[c], c
### splitting on train and validation input (note that it is splitted randomly).

input_data_all, output_data_all, _ = shuffle_together(input_data_all, output_data_all)
n_validation_samples= int(0.1*n_samples)
validation_input= input_data_all[-1*n_validation_samples:]
validation_output= output_data_all[-1*n_validation_samples:]
n_train_samples= n_samples-n_validation_samples
train_input= input_data_all[:n_train_samples]
train_output= output_data_all[:n_train_samples]
print(train_input.shape)
print(train_output.shape)
### Now we define matrix x and vector y in theano.
x=T.matrix('x', dtype= theano.config.floatX)
y=T.vector('y', dtype= theano.config.floatX)

train_input.shape[1]
# initialize model
model = keras.models.Sequential()
### First layer.  Linear activation layer.
model.add(keras.layers.Dense(input_data_all.shape[1], input_shape=(train_input.shape[1],),activation='linear',
                bias_initializer=keras.initializers.RandomUniform(minval=-1, maxval=1, seed=None),
                kernel_initializer=keras.initializers.RandomUniform(minval=-1, maxval=1, seed=None)))

# add hidden layer(s) with relu and tanh activation functions.
model.add(keras.layers.Dense(units=40,kernel_initializer=keras.initializers.RandomUniform(minval=-1, maxval=1, seed=None),
        bias_initializer=keras.initializers.RandomUniform(minval=-0.05, maxval=0.05, seed=None),
        activation='relu')
    )          
model.add(keras.layers.Dense(units=25,kernel_initializer=keras.initializers.RandomUniform(minval=-0.05, maxval=0.05, seed=None),
        bias_initializer=keras.initializers.RandomUniform(minval=-0.05, maxval=0.05, seed=None),
        activation='tanh')
    )          
model.add(keras.layers.Dense(units=15,kernel_initializer=keras.initializers.RandomUniform(minval=-0.05, maxval=0.05, seed=None),
        bias_initializer=keras.initializers.RandomUniform(minval=-0.05, maxval=0.05, seed=None),
        activation='tanh')
    )          
model.add(keras.layers.Dense(units=8,kernel_initializer=keras.initializers.RandomUniform(minval=-0.05, maxval=0.05, seed=None),
        bias_initializer=keras.initializers.RandomUniform(minval=-0.05, maxval=0.05, seed=None),
        activation='tanh')
    )          
model.add(keras.layers.Dense(units=4,kernel_initializer=keras.initializers.RandomUniform(minval=-0.05, maxval=0.05, seed=None),
        bias_initializer=keras.initializers.RandomUniform(minval=-1, maxval=1, seed=None),
        activation='relu')
    )          
### Final layer is again linear.
model.add(keras.layers.Dense(units=1,kernel_initializer=keras.initializers.RandomUniform(minval=-0.05, maxval=0.05, seed=None),
        bias_initializer=keras.initializers.RandomUniform(minval=-0.05, maxval=0.05, seed=None),
        activation='linear')
    )          

# define SGD optimizer
sgd_optimizer = keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-012)

# compile model
model.compile(
    optimizer=sgd_optimizer,
    loss='mean_squared_error'
)
# train model
history = model.fit(
    input_data_all, output_data_all,
    batch_size=300, epochs=100000,
    verbose=1, validation_split=0.1,validation_freq=100
)
### Now we load the test data and predict the model out of that
### What we do next is calculate R-squared and display it.
test_x = np.load('test_sample.npy')#,dtype=theano.config.floatX)
test_y= np.load('test_target.npy')#,dtype=theano.config.floatX)
test_x = np.load('test_sample.npy')#,dtype=theano.config.floatX)
test_y= np.load('test_target.npy')#,dtype=theano.config.floatX)
sample_out= model.predict(test_x)
ss_tot = np.sum((test_y-np.mean(test_y))**2)
SS_res = np.sum((sample_out.reshape(-1)-test_y)**2)
print("Rsquared:",(1 - SS_res/ss_tot)*100)<jupyter_output>Rsquared: 99.95008688561789<jupyter_text>R-squared of 99.95 is not great, but also not bad for first training. We can still improve this by retraining, just like we did it when we used lasagne package!<jupyter_code>model.predict(test_x)
### And we save the model;

model.save("model.h1")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1255952380952381, 'fraction_numerical': 0.03115079365079365, 'mean_word_length': 5.208128078817734, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '552981', 'license': 'MIT', 'n_tokens_mistral': 1808, 'n_tokens_neox': 1722, 'n_words': 337, 'path': 'keras/Tutorial_keras1.ipynb', 'repo_name': 'nejc9921/multilayer_perceptron', 'size': '5040'}