DeepTrading With TensorFlow 6 - TodoTrader
DeepTrading With TensorFlow 6 - TodoTrader
BLACK BELT
ata corrupts. Absolute Data corrupts absolutely. This is my impression every time I am faced with the amount of data that is available to us in the current
times.
D This is the moment of truth. Today you will learn how to make some predictions in the Forex market. This is probably the Far West of the financial markets.
But you have nothing to fear as I am revealing step by step what could take months and probably years, as it has cost me because I have gone the dark
(hard) side.
The progress of the model can be saved during and after training. This means that a model can be resumed where it left off and avoid long training times. Saving also
means that you can share your model and others can recreate your work.
We will illustrate how to create a multiple fully connected hidden layer NN, save it and make predictions with trained model after reload it.
In this post, we will build a four-hidden layer neural network to predict the next close price, from the other four features of the precedent period (open, high, low and close).
This is a practical exercise to learn how to make predictions with TensorFlow, but it is a naive approach to the real forecasting problem. Don’t worry we will be climbing
toward better approaches. Also, in the meantime, you will be able to elaborate on your own systems.
Load configuration
Below, it is an example of .env file for your customization:
PROJ_DIR=.
DATA_DIR=../data/
RAW_DIR=../data/raw/
INTERIM_DIR=../data/interim/
PROCESSED_DIR=../data/processed/
FIGURES_DIR=../reports/figures/
MODEL_DIR=../models/
EXTERNAL_DIR=../data/external/
PRODUCTION_DIR=/home/PRODUCTION/
In this part of the code we should load all our experiment parameters.
In this way, you can organize a long series of experiments without having to ask where the parameters are changed.
In [1]:
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import signature_def_utils
from tensorflow.python.saved_model import tag_constants
from tensorflow.python.saved_model.utils import build_tensor_info
#Reads the key,value pair from .env and adds them to environment variable
load_dotenv(find_dotenv())
# Check the env variables exist. Check as many variables as you need
raw_msg = "Set your raw data absolute path in the .env file at project root"
assert "RAW_DIR" in os.environ, raw_msg
data_msg = "Set your processed data absolute path in the .env file at project root"
assert "DATA_DIR" in os.environ, data_msg
interim_msg = "Set your interim data absolute path in the .env file at project root"
assert "INTERIM_DIR" in os.environ, interim_msg
#Data files
#raw_data =
#interim_data =
https://github.com/philipperemy/FX-1-Minute-Data
https://github.com/xeb/forex-histdata-etl
https://gist.github.com/EvianZhow/93b30edb5e1ac44f3dd2de7ef9a543d9
I include forex time series zipped files needed in this tutorial
In [2]:
Download .csv data file from HistData.com and save in ../data/raw dir
“Prepare” data for Machine Learning is a complex task depending on where the data is stored and where it is obtained from. And doubtless, it is one of the most time-
consuming task. Often the Forex data is not available in a single file. They may be distributed across different sources like multiple compressed CSV files, spreadsheets
or plain text files, normalized in database tables, or even in NoSql database like MongoDB. So we need a tool to stage, filter, transform when necessary, and finally export
to a single flat, text CSV file.
If your Forex data is small and the changes are simple such as adding a derived field or new events you can use a spreadsheet, make the necessary changes, and then
export it to a CSV file. Certainly, not too professional. But when the changes are more complex; e.g., joining several sources, filtering a subset of the data, or managing a
large number of timestamp rows, you might need a more powerful tool like an RDBMS. MySQL is a great one and it’s free and opensourced. In this tutorial, we have
selected SQLite which is enough for our purpose and data size. Here we treat several compressed .csv files distributed in different folders, which is very usual in real
trading. If the data size that we are managing is in the terabytes, then we should consider Hadoop. But trust us, that is another story.
In [3]:
In [4]:
# Clean database table
DATABASE_FILE = processed_dir+"Data.db"
def initialize_db(self):
with sqlite3.connect(DATABASE_FILE) as connection:
cursor = connection.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS History (timestamp INTEGER,'
'symbol VARCHAR(20), high FLOAT, low FLOAT,'
'open FLOAT, close FLOAT, volume FLOAT, '
'quoteVolume FLOAT, weightedAverage FLOAT,'
'PRIMARY KEY (timestamp, symbol));')
connection.commit()
initialize_db(DATABASE_FILE)
conn = sqlite3.connect(DATABASE_FILE)
In [5]:
In [6]:
#
# Database population
#
#
# All price instrument in cash currency base
#
# Initialicing dataframes
df1 = pd.DataFrame().iloc[0:0]
df2 = pd.DataFrame().iloc[0:0]
# Insert new columns with the instrument name and their values
df2.insert(loc=0, column='symbol', value=symbol)
#Only for compatibility with stocks code (optional, you may want to remove this fields from database)
df2['volume']=1000.
df2['quoteVolume']=1000.
df2['weightedAverage']=1.
# Liberate memory
del df1
del df2
inputfile: DAT_ASCII_EURUSD_M1_2017.csv
In [7]:
In [9]:
df
Out[9]:
… … … … … … … … … …
8649 1514458800 EURUSD 1.19366 1.19539 1.19366 1.19520 1000.0 1000.0 1.0
8650 1514462400 EURUSD 1.19513 1.19547 1.19497 1.19530 1000.0 1000.0 1.0
8651 1514466000 EURUSD 1.19530 1.19587 1.19530 1.19582 1000.0 1000.0 1.0
8652 1514469600 EURUSD 1.19578 1.19584 1.19500 1.19519 1000.0 1000.0 1.0
8653 1514473200 EURUSD 1.19527 1.19527 1.19413 1.19413 1000.0 1000.0 1.0
8654 1514476800 EURUSD 1.19403 1.19446 1.19391 1.19423 1000.0 1000.0 1.0
8655 1514480400 EURUSD 1.19415 1.19445 1.19364 1.19379 1000.0 1000.0 1.0
8656 1514484000 EURUSD 1.19384 1.19409 1.19384 1.19391 1000.0 1000.0 1.0
8657 1514487600 EURUSD 1.19394 1.19451 1.19393 1.19441 1000.0 1000.0 1.0
8658 1514491200 EURUSD 1.19441 1.19486 1.19373 1.19419 1000.0 1000.0 1.0
8659 1514494800 EURUSD 1.19421 1.19474 1.19376 1.19474 1000.0 1000.0 1.0
8660 1514498400 EURUSD 1.19476 1.19476 1.19418 1.19426 1000.0 1000.0 1.0
8661 1514502000 EURUSD 1.19426 1.19458 1.19415 1.19443 1000.0 1000.0 1.0
8662 1514505600 EURUSD 1.19444 1.19473 1.19438 1.19465 1000.0 1000.0 1.0
8663 1514509200 EURUSD 1.19489 1.19553 1.19487 1.19543 1000.0 1000.0 1.0
8664 1514512800 EURUSD 1.19551 1.19581 1.19464 1.19525 1000.0 1000.0 1.0
8665 1514516400 EURUSD 1.19523 1.19685 1.19523 1.19674 1000.0 1000.0 1.0
8666 1514520000 EURUSD 1.19674 1.19870 1.19674 1.19829 1000.0 1000.0 1.0
8667 1514523600 EURUSD 1.19814 1.19848 1.19736 1.19806 1000.0 1000.0 1.0
8668 1514527200 EURUSD 1.19803 1.19887 1.19800 1.19867 1000.0 1000.0 1.0
8669 1514530800 EURUSD 1.19870 1.19946 1.19849 1.19946 1000.0 1000.0 1.0
8670 1514534400 EURUSD 1.19951 1.19983 1.19839 1.19846 1000.0 1000.0 1.0
8671 1514538000 EURUSD 1.19867 1.19946 1.19861 1.19926 1000.0 1000.0 1.0
8672 1514541600 EURUSD 1.19927 1.20069 1.19880 1.20069 1000.0 1000.0 1.0
8673 1514545200 EURUSD 1.20097 1.20215 1.20023 1.20215 1000.0 1000.0 1.0
8674 1514548800 EURUSD 1.20220 1.20255 1.20193 1.20197 1000.0 1000.0 1.0
8675 1514552400 EURUSD 1.20214 1.20231 1.20124 1.20133 1000.0 1000.0 1.0
8676 1514556000 EURUSD 1.20134 1.20138 1.20071 1.20106 1000.0 1000.0 1.0
8677 1514559600 EURUSD 1.20092 1.20104 1.19978 1.19983 1000.0 1000.0 1.0
timestamp symbol open high low close volume quoteVolume weightedAverage
8678 1514563200 EURUSD 1.19978 1.20035 1.19927 1.19982 1000.0 1000.0 1.0
In [10]:
In [11]:
df1
Out[11]:
… … … … … …
# Create the apropiate features dataframe (In this case is open, high, low and close prices of symbol)
df2 = pd.DataFrame()
symbol_features = ['open', 'high', 'low', 'close']
for feature in symbol_features:
df1 = df.loc[(df['symbol'] == symbol),['timestamp', feature]]
df1.columns=['timestamp',symbol+feature]
# Setting the timestamp as the index
df1.set_index('timestamp', inplace=True)
# Filling the remaining gaps backguards (the initial gaps has not before value)
df2 = df2.fillna(method='bfill')
# Dimensions of dataset
print("Dimensions of dataset")
n = X_raw.shape[0]
p = X_raw.shape[1]
print("n=",n,"p=",p)
Dimensions of dataset
n= 8679 p= 4
In [13]:
X_raw
Out[13]:
timestamp
… … … … …
In [14]:
# Target
# We use as target one of the symbols rate, i.e. "EURUSD". That is we try to predict next value of EURUSD
lag = -1
y_raw = df2.loc[:,"EURUSDclose"].shift(periods=lag)
In [15]:
#
# Removal of Null values**
# Now since there still exists 'NaN' values in our target dataframe, and these are Null values,
# we have to do something about them. In here, I will just do the naive thing of replacing these NaNs
# with previous value because it is only the last value an error is negligible as such:
Out[15]:
timestamp
1483322400 1.04929
1483326000 1.04868
1483329600 1.04803
1483333200 1.04782
1483336800 1.04659
1483340400 1.04668
1483344000 1.04747
1483347600 1.04699
1483351200 1.04686
1483354800 1.04655
1483358400 1.04605
1483362000 1.04592
1483365600 1.04582
1483369200 1.04525
1483372800 1.04605
1483376400 1.04573
1483380000 1.04662
1483383600 1.04763
1483387200 1.04713
1483390800 1.04838
1483394400 1.04860
1483398000 1.04814
1483401600 1.04863
1483405200 1.04586
1483408800 1.04370
1483412400 1.04100
1483416000 1.03980
1483419600 1.03931
1483423200 1.03852
1483426800 1.03910
...
1514458800 1.19530
1514462400 1.19582
1514466000 1.19519
1514469600 1.19413
1514473200 1.19423
1514476800 1.19379
1514480400 1.19391
1514484000 1.19441
1514487600 1.19419
1514491200 1.19474
1514494800 1.19426
1514498400 1.19443
1514502000 1.19465
1514505600 1.19543
1514509200 1.19525
1514512800 1.19674
1514516400 1.19829
1514520000 1.19806
1514523600 1.19867
1514527200 1.19946
1514530800 1.19846
1514534400 1.19926
1514538000 1.20069
1514541600 1.20215
1514545200 1.20197
1514548800 1.20133
1514552400 1.20106
1514556000 1.19983
1514559600 1.19982
1514563200 1.19982
Name: EURUSDclose, Length: 8679, dtype: float64
In [16]:
Out[16]:
[<matplotlib.lines.Line2D at 0x7f109dd4b240>]
Split data
In [17]:
# Total samples
nsamples = n
# Samples in train
nsamples_train = jindex
# Samples in test
nsamples_test = nsamples - nsamples_train
print("Total number of samples: ",nsamples,"\nSamples in train set: ", nsamples_train,
"\nSamples in test set: ",nsamples_test)
X_test = X_raw.values[jindex:, :]
y_test = y_raw.values[jindex:]
In [18]:
#X_train as dataframe (optional, only for printing. See note in the beginning)
X_Train = pd.DataFrame(data=X_train)
X_Train.columns = X_raw.columns
print("X_train")
X_Train
X_train
Out[18]:
EURUSDopen EURUSDhigh EURUSDlow EURUSDclose
… … … … …
#X_test as dataframe (optional, only for printing. See note in the beginning)
X_Test = pd.DataFrame(data=X_test)
X_Test.columns = X_raw.columns
print("X_test")
X_Test
X_test
Out[19]:
Transform features
Note
Be careful not to write X_test_std = sc.fit_transform(X_test) instead of X_test_std = sc.transform(X_test) . In this case, it wouldn’t make a great difference since the mean and
standard deviation of the test set should be (quite) similar to the training set. However, this is not always the case in Forex market data, as has been well established in
the literature. The correct way is to re-use parameters from the training set if we are doing any kind of transformation. So, the test set should basically stand for “new,
unseen” data.
Pay special attention to how the data changes before and after the transformation. This way you will get a better feeling on the information you are handling.
In [20]:
# Scale data
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train_std = sc.fit_transform(X_train)
X_test_std = sc.transform(X_test)
In [21]:
print("Mean:",sc.mean_)
print("Variance",sc.var_)
Mean: [1.12383816]
Variance [0.00252308]
In [22]:
#X_train_std as dataframe (optional, only for printing. See note in the beginning)
X_Train_std = pd.DataFrame(data=X_train_std)
X_Train_std.columns = X_Train.columns
print("X_train_std")
X_Train_std
X_train_std
Out[22]:
… … … … …
#X_train_std as dataframe (optional, only for printing. See note in the beginning)
X_Test_std = pd.DataFrame(data=X_test_std)
X_Test_std.columns = X_Test.columns
print("X_test_std")
X_Test_std
X_test_std
Out[23]:
… … … … …
#y_train as panda dataframe (optional, only for printing. See note in the beginning)
y_Train = pd.DataFrame(data=y_train)
y_Train.columns=["EURUSDclose"]
y_Train
Out[24]:
EURUSDclose
0 1.04929
1 1.04868
2 1.04803
3 1.04782
4 1.04659
5 1.04668
6 1.04747
7 1.04699
8 1.04686
9 1.04655
EURUSDclose
10 1.04605
11 1.04592
12 1.04582
13 1.04525
14 1.04605
15 1.04573
16 1.04662
17 1.04763
18 1.04713
19 1.04838
20 1.04860
21 1.04814
22 1.04863
23 1.04586
24 1.04370
25 1.04100
26 1.03980
27 1.03931
28 1.03852
29 1.03910
… …
7781 1.17498
7782 1.17761
7783 1.17852
7784 1.17861
7785 1.17945
7786 1.17985
7787 1.18191
7788 1.18186
7789 1.18222
7790 1.18178
7791 1.18139
7792 1.18139
EURUSDclose
7793 1.18229
7794 1.18214
7795 1.18324
7796 1.18343
7797 1.18288
7798 1.18280
7799 1.18299
7800 1.18425
7801 1.18431
7802 1.18404
7803 1.18463
7804 1.18429
7805 1.18489
7806 1.18466
7807 1.18460
7808 1.18496
7809 1.18490
7810 1.18488
#y_train as panda dataframe (optional, only for printing. See note in the beginning)
y_Test = pd.DataFrame(data=y_test)
y_Test.columns=["EURUSDclose"]
y_Test
Out[25]:
EURUSDclose
0 1.18514
1 1.18502
2 1.18490
3 1.18496
4 1.18503
5 1.18427
6 1.18476
7 1.18534
8 1.18479
9 EURUSDclose
1.18526
10 1.18531
11 1.18474
12 1.18431
13 1.18619
14 1.18689
15 1.18659
16 1.18582
17 1.18724
18 1.18968
19 1.19197
20 1.19352
21 1.19357
22 1.19274
23 1.19258
24 1.19264
25 1.19303
26 1.19285
27 1.19285
28 1.19285
29 1.19285
… …
838 1.19530
839 1.19582
840 1.19519
841 1.19413
842 1.19423
843 1.19379
844 1.19391
845 1.19441
846 1.19419
847 1.19474
848 1.19426
849 1.19443
EURUSDclose
850 1.19465
851 1.19543
852 1.19525
853 1.19674
854 1.19829
855 1.19806
856 1.19867
857 1.19946
858 1.19846
859 1.19926
860 1.20069
861 1.20215
862 1.20197
863 1.20133
864 1.20106
865 1.19983
866 1.19982
867 1.19982
#y_train_std as panda dataframe (optional, only for printing. See note in the beginning)
y_Train_std = pd.DataFrame(data=y_train_std)
y_Train_std.columns=["EURUSDclose"]
y_Train_std
Out[26]:
EURUSDclose
0 -1.484129
1 -1.496273
2 -1.509213
3 -1.513394
4 -1.537881
5 -1.536089
6 -1.520362
7 -1.529918
8 -1.532506
EURUSDclose
9 -1.538677
10 -1.548632
11 -1.551220
12 -1.553211
13 -1.564558
14 -1.548632
15 -1.555002
16 -1.537284
17 -1.517176
18 -1.527131
19 -1.502245
20 -1.497865
21 -1.507023
22 -1.497268
23 -1.552414
24 -1.595416
25 -1.649169
26 -1.673059
27 -1.682814
28 -1.698541
29 -1.686994
… …
7781 1.018148
7782 1.070507
7783 1.088624
7784 1.090415
7785 1.107138
7786 1.115102
7787 1.156113
7788 1.155117
7789 1.162284
7790 1.153525
7791 1.145760
EURUSDclose
7792 1.145760
7793 1.163678
7794 1.160692
7795 1.182591
7796 1.186373
7797 1.175424
7798 1.173831
7799 1.177614
7800 1.202698
7801 1.203893
7802 1.198518
7803 1.210263
7804 1.203495
7805 1.215440
7806 1.210861
7807 1.209666
7808 1.216833
7809 1.215639
7810 1.215241
#y_train_std as panda dataframe (optional, only for printing. See note in the beginning)
y_Test_std = pd.DataFrame(data=y_train_std)
y_Test_std.columns=["EURUSDclose"]
y_Test_std
Out[27]:
EURUSDclose
0 -1.484129
1 -1.496273
2 -1.509213
3 -1.513394
4 -1.537881
5 -1.536089
6 -1.520362
7 -1.529918
8 EURUSDclose
-1.532506
9 -1.538677
10 -1.548632
11 -1.551220
12 -1.553211
13 -1.564558
14 -1.548632
15 -1.555002
16 -1.537284
17 -1.517176
18 -1.527131
19 -1.502245
20 -1.497865
21 -1.507023
22 -1.497268
23 -1.552414
24 -1.595416
25 -1.649169
26 -1.673059
27 -1.682814
28 -1.698541
29 -1.686994
… …
7781 1.018148
7782 1.070507
7783 1.088624
7784 1.090415
7785 1.107138
7786 1.115102
7787 1.156113
7788 1.155117
7789 1.162284
7790 1.153525
7791 1.145760
EURUSDclose
7792 1.145760
7793 1.163678
7794 1.160692
7795 1.182591
7796 1.186373
7797 1.175424
7798 1.173831
7799 1.177614
7800 1.202698
7801 1.203893
7802 1.198518
7803 1.210263
7804 1.203495
7805 1.215440
7806 1.210861
7807 1.209666
7808 1.216833
7809 1.215639
7810 1.215241
# Clears the default graph stack and resets the global default graph
ops.reset_default_graph()
In [29]:
# Parameters
learning_rate = 0.005
batch_size = 256
n_features = X_train.shape[1]# Number of features in training data
epochs = 1000
display_step = 100
model_path = model_dir+"07_First_Forex_Prediction"
production_model_path = production_dir+"models/"+"07_First_Forex_Prediction"
n_classes = 1
n_classes = 1
# Network Parameters
# See figure of the model
d0 = D = n_features # Layer 0 (Input layer number of features)
d1 = 1024 # Layer 1 (1024 hidden nodes)
d2 = 512 # Layer 2 (512 hidden nodes)
d3 = 256 # Layer 3 (256 hidden nodes)
d4 = 128 # Layer 4 (128 hidden nodes)
d5 = C = 1 # Layer 5 (Output layer)
# tf Graph input
print("Placeholders")
X = tf.placeholder(dtype=tf.float32, shape=[None, n_features], name="X")
y = tf.placeholder(dtype=tf.float32, shape=[None,n_classes], name="y")
# Initializers
print("Initializers")
sigma = 1
weight_initializer = tf.variance_scaling_initializer(mode="fan_avg", distribution="uniform", scale=sigma)
bias_initializer = tf.zeros_initializer()
# Create model
def multilayer_perceptron(X, variables):
# Hidden layer with ReLU activation
layer_1 = tf.nn.relu(tf.add(tf.matmul(X, variables['W1']), variables['bias1']), name="layer_1")
# Hidden layer with ReLU activation
layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1, variables['W2']), variables['bias2']), name="layer_2")
# Hidden layer with ReLU activation
layer_3 = tf.nn.relu(tf.add(tf.matmul(layer_2, variables['W3']), variables['bias3']), name="layer_3")
# Hidden layer with ReLU activation
layer_4 = tf.nn.relu(tf.add(tf.matmul(layer_3, variables['W4']), variables['bias4']), name="layer_4")
# Output layer with ReLU activation
out_layer = tf.nn.relu(tf.add(tf.matmul(layer_4, variables['W5']), variables['bias5']), name="out_layer")
return out_layer
# Construct model
y_hat = multilayer_perceptron(X, variables)
# Cost function
print("Cost function")
mse = tf.reduce_mean(tf.squared_difference(y_hat, y))
# Optimizer
print("Optimizer")
optimizer = tf.train.AdamOptimizer().minimize(mse)
batch_indices = range(num_batches)
batch_n = np.random.permutation(batch_indices)
for j in batch_n:
batch_X = X_train[j * batch_size: (j + 1) * batch_size]
batch_y = y_train[j * batch_size: (j + 1) * batch_size]
Placeholders
Initializers
Cost function
Optimizer
# Shape of tensors
print("X_train_std.shape = ", X_train_std.shape, "y_train.shape =", y_train.shape,
"\nX_train_std.shape = ", X_train_std.shape, "y_test.shape = ", y_test.shape)
In [31]:
# Writer to record image, scalar, histogram and graph for display in tensorboard
writer = tf.summary.FileWriter("/tmp/tensorflow_logs", sess.graph) # create writer
writer.add_graph(sess.graph)
# Run
print("Run")
printcounter = 0
for e in range(epochs):
# Minibatch training
# Show progress
if (printcounter == display_step):
printcounter = 0
print("Epoch: ", e)
# MSE train and test
mse_train.append(sess.run(mse, feed_dict={X: X_train_std, y: np.transpose([y_train])}))
mse_test.append(sess.run(mse, feed_dict={X: X_test_std, y: np.transpose([y_test])}))
print('MSE Train: ', mse_train[-1])
print('MSE Test: ', mse_test[-1])
printcounter += 1
# Close writer
writer.flush()
writer.close()
In [32]:
batch_y.shape
Out[32]:
(256,)
In [33]:
%matplotlib inline
# Plot loss (MSE) over time
plt.plot(mse_train, 'k-', label='mse_train')
plt.plot(mse_test, 'r--', label='mse_test')
plt.title('Loss (MSE) vs epoch')
plt.legend(loc='upper right')
plt.xlabel('Generation x'+str(display_step))
plt.ylabel('Loss')
plt.show()
Tensorboard Graph
What follows is the graph we have executed and all data about it. Note the “save” label and the several layers.
a) Meta graph: This is a protocol buffer which saves the complete Tensorflow graph; i.e. all variables, operations, collections etc. This file
has .meta extension.
b) y c) Checkpoint files: It is a binary file which contains all the values of the weights, biases, gradients and all the other variables saved.
Tensorflow has changed from version 0.11. Instead of a single .ckpt file, we have now two files: .index and .data file that contains our
training variables.
d) Along with this, Tensorflow also has a file named checkpoint which simply keeps a record of latest checkpoint files saved.
Predict
Finally, we can use the model to make some predictions. In [34]:
# Running a new session for predictions and export model to production
print("Starting prediction session...")
with tf.Session() as sess:
# Initialize variables
sess.run(init)
%matplotlib inline
# Plot Prices over time
plt.plot(y_test, 'k-', label='y_test')
plt.plot(prediction, 'r--', label='prediction')
plt.title('Price over time')
plt.legend(loc='upper right')
plt.xlabel('Time')
plt.ylabel('Price')
plt.show()
if to_production:
# Pick out the model input and output
X_tensor = sess.graph.get_tensor_by_name("X"+ ':0')
y_tensor = sess.graph.get_tensor_by_name("out_layer" + ':0')
model_input = build_tensor_info(X_tensor)
model_output = build_tensor_info(y_tensor)
model_version = 1
export_model_dir = production_model_path+"/"+str(model_version)
while os.path.exists(export_model_dir):
model_version += 1
export_model_dir = production_model_path+"/"+str(model_version)
builder = saved_model_builder.SavedModelBuilder(export_model_dir)
builder.add_meta_graph_and_variables(sess,
[tag_constants.SERVING],
signature_def_map={
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
signature_definition})
except Exception:
print("Unexpected error:", sys.exc_info()[0])
pass
Starting prediction session...
INFO:tensorflow:Restoring parameters from ../models/07_First_Forex_Prediction
Model restored from file: ../models/07_First_Forex_Prediction
[[1.1836615]
[1.1830435]
[1.1836624]
[1.1831424]
[1.1830487]
.
.
.
In [35]:
dfvisual = pd.DataFrame()
dfvisual["y_test"] = y_test
dfvisual["prediction"]= prediction
dfvisual["Abs.error"]=dfvisual["y_test"]-dfvisual["prediction"]
dfvisual["Relat.error"]=abs(dfvisual["Abs.error"]/dfvisual["y_test"])*100
dfvisual
Out[35]:
… … … … …
Out[36]:
<seaborn.axisgrid.PairGrid at 0x7f109de84518>
OK, better results, but still not very good results. We could try to improve them with a deeper network (more layers) or retouching the net parameters and number of
neurons. That is another story.
In [37]:
except Exception:
print("Unexpected error:", sys.exc_info()[0])
pass
Starting prediction session...
INFO:tensorflow:Restoring parameters from ../models/07_First_Forex_Prediction
Model restored from file: ../models/07_First_Forex_Prediction
[[1.2461773]]
In [38]:
X_test_std
Out[38]:
In [39]:
# Running a new session for predictions and export model to production
print("Starting prediction session...")
with tf.Session() as sess:
# Initialize variables
sess.run(init)
saver.restore(sess, model_path)
print("Model restored from file: %s" % model_path)
# We try to predict the close price of test samples
feed_dict = {X: X_test_std}
%matplotlib inline
# Plot Prices over time
plt.plot(y_test, 'k-', label='y_test')
plt.plot(prediction, 'r--', label='prediction')
plt.title('Price over time')
plt.legend(loc='upper right')
plt.xlabel('Time')
plt.ylabel('Price')
plt.show()
model_input = build_tensor_info(X_tensor)
model_output = build_tensor_info(y_tensor)
model_version = 1
export_model_dir = production_model_path+"/"+str(model_version)
while os.path.exists(export_model_dir):
model_version += 1
export_model_dir = production_model_path+"/"+str(model_version)
builder = saved_model_builder.SavedModelBuilder(export_model_dir)
builder.add_meta_graph_and_variables(sess,
[tag_constants.SERVING],
signature_def_map={
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
signature_definition})
Starting prediction session...INFO:tensorflow:Restoring parameters from ../models/07_First_Forex_PredictionModel restored from file: ../models/07_First_
Forex_Prediction[[1.1836615] [1.1830435] [1.1836624] [1.1831424] [1.1830487] [1.1834234] [1.1823709]...
INFO:tensorflow:No assets to save.
INFO:tensorflow:No assets to write.
INFO:tensorflow:SavedModel written to: b'/home/parrondo/PRODUCTION/models/07_First_Forex_Prediction/50/saved_model.pb'
In [40]:
len(y_train)
Out[40]:
7811
In [41]:
X_Train_std
Out[41]:
… … … … …
y_train.shape
Out[42]:
(7811,)
In [43]:
Out[43]:
0.15334987531611016
In [44]:
Out[44]:
0.0017724057803175793
In the next post, we will delve deeper into the art of prediction. You have all the notebooks at your disposal on my Github website:
https://github.com/parrondo/deeptrading
parrondo
TensorFlow
Related articles
1 COMMENT
Quantocracy's Daily Wrap for 07/08/2019 | Quantocracy
2019-07-09 at 07:16 R E P L Y
[…] DeepTrading with TensorFlow VI [Todo Trader] […]
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment
Your Name*
Email Address*
Save my name, email, and website in this browser for the next time I
comment.
L E A V E C O M M E N T
Type Here
Recent Posts
Archives
July 2019
June 2019
May 2019
April 2019
November 2017
Categories
Be Productive
Black Belt
Brown Belt
Create Systems
Software
Start to trade
Meta
Log in
Entries RSS
Comments RSS
WordPress.org
Theme by Powered by