04-10-2018, 03:09 PM
Tensorflow is an open source library developed by Google. Tensorflow has become very popular in machine learning, deep learning and reinforcement learning. If you want to develop algorithmic trading strategies using Python than you must master Tensorflow in addition to Pandas and Numpy. I will start separate threads for Pandas and Numpy. In this thread we only discuss Tensorflow. Tensorflow has been developed to efficiently do numerically computations involving very big arrays which are mathematically known as tensors. Tensors are just arrays with many indices. Matrix is a rank 1 tensor. Scalars like real numbers are rank zero tensors. Machine learning/deep learning involves heavy matrix operations and tensor operations.
In a different thread in MQL4 forum, I discussed how difficult it is to develop machine learning/deep learning/reinforcement learning libraries. Tensorflow development took many months spanning a few years. Tensorflow was developed by the Google Brain Team. Lot of effort went into its development so you can well imagine how much effort developing good machine learning/deep learning/reinforcement learning libraries can take. These libraries are developed by teams of professionals. Tensorflow is basically written in C++ with a Python wrapper at the front end.
Tensorflow GPU versus CPU
This is how Tensorflow works conceptually. First we define a computation graph using standard tensorflow components. Tensorflow takes that graph and performs the numerical computations using C++ in a highly efficient manner. Tensorflow has two versions CPU as well as GPU. GPU version is suited for doing very heavy numerical computations involving millions of parameters. First you need a GPU computer. If you have a GPU computer, like an NVIDIA GPU computer, you can install tensorflow gpu on it. As a trader, we won't be doing very heavy computations so CPU is good enough for us. You can install GPU version, its speed is almost the same as that for CPU for small datasets. Tensorflow can also be used for distributed computing and is being used in Google large scale systems. Tensorflow can run on Windows, Linux, macOs as well as mobile devices using iOs and Android. What this means is that you should master tensorflow, you can always find it handy. You can install tensorflow with this simple pip command
pip install tensorflow
Installing tensorflow gpu requires first installing CUDA and other things before you can install it. As I said I have personally tested it for small datasets there is no advantage going through the extra steps involved in installing gpu version as cpu version provides the services in almost the same time as gpu version. I used the following code to check how much time it takes tensorflow to load on my computer:
from time import time
t=time()
import tensorflow as tf
print(time()-t)
print(time()-t)
7.185936212539673
it took 7 seconds for tensorflow to load on my computer which is pretty fast. Let's start with a simple calculation x^3/y+8. As I have said first we will build a computation graph:
import tensorflow as tf
x=tf.Variable(89, name="x")
y=tf.Variable(27, name="y")
z=x*x*x/y+8
In the above code we have build a computation graph. Now we need to open a tensorflow session that will build this computation graph. We don't have to worry. Tensorflow will decide how many CPUs/GPUs to use. Of course this is a simple code example and not very complicated. But imagine we had over 20 million parameters. It would have been headache to decide how to do the numerical computation efficiently. Tensorflow would have helped us by deciding how to do numerical computations involving more than 20 million parameters by taking account of the computer's capabilities. For now this is how we are going to start the tensorflow session:
sess = tf.Session()
sess.run(x.initializer)
sess.run(y.initializer)
answer=sess.run(z)
print(answer)
print(answer)
26118.0
sess.close()
In the above code we first start the tensorflow session and then use run to build the graph. In the end we close the session which is important.
In a different thread in MQL4 forum, I discussed how difficult it is to develop machine learning/deep learning/reinforcement learning libraries. Tensorflow development took many months spanning a few years. Tensorflow was developed by the Google Brain Team. Lot of effort went into its development so you can well imagine how much effort developing good machine learning/deep learning/reinforcement learning libraries can take. These libraries are developed by teams of professionals. Tensorflow is basically written in C++ with a Python wrapper at the front end.
Tensorflow GPU versus CPU
This is how Tensorflow works conceptually. First we define a computation graph using standard tensorflow components. Tensorflow takes that graph and performs the numerical computations using C++ in a highly efficient manner. Tensorflow has two versions CPU as well as GPU. GPU version is suited for doing very heavy numerical computations involving millions of parameters. First you need a GPU computer. If you have a GPU computer, like an NVIDIA GPU computer, you can install tensorflow gpu on it. As a trader, we won't be doing very heavy computations so CPU is good enough for us. You can install GPU version, its speed is almost the same as that for CPU for small datasets. Tensorflow can also be used for distributed computing and is being used in Google large scale systems. Tensorflow can run on Windows, Linux, macOs as well as mobile devices using iOs and Android. What this means is that you should master tensorflow, you can always find it handy. You can install tensorflow with this simple pip command
pip install tensorflow
Installing tensorflow gpu requires first installing CUDA and other things before you can install it. As I said I have personally tested it for small datasets there is no advantage going through the extra steps involved in installing gpu version as cpu version provides the services in almost the same time as gpu version. I used the following code to check how much time it takes tensorflow to load on my computer:
from time import time
t=time()
import tensorflow as tf
print(time()-t)
print(time()-t)
7.185936212539673
it took 7 seconds for tensorflow to load on my computer which is pretty fast. Let's start with a simple calculation x^3/y+8. As I have said first we will build a computation graph:
import tensorflow as tf
x=tf.Variable(89, name="x")
y=tf.Variable(27, name="y")
z=x*x*x/y+8
In the above code we have build a computation graph. Now we need to open a tensorflow session that will build this computation graph. We don't have to worry. Tensorflow will decide how many CPUs/GPUs to use. Of course this is a simple code example and not very complicated. But imagine we had over 20 million parameters. It would have been headache to decide how to do the numerical computation efficiently. Tensorflow would have helped us by deciding how to do numerical computations involving more than 20 million parameters by taking account of the computer's capabilities. For now this is how we are going to start the tensorflow session:
sess = tf.Session()
sess.run(x.initializer)
sess.run(y.initializer)
answer=sess.run(z)
print(answer)
print(answer)
26118.0
sess.close()
In the above code we first start the tensorflow session and then use run to build the graph. In the end we close the session which is important.
Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw
Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/