The first thing I am going to show you guys is the folder structure, so you guys can make it yourself. As you should write the code yourself, so you can understand what is happening.
This is the folder structure. You don’t need to worry about models and deployed models.
Now let me show you the code:
cnn.py
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os
SEED = 42
tf.set_random_seed(SEED)
class CNN():
def __init__(self, num_features, num_historical_days, is_train=True):
self.X = tf.placeholder(tf.float32, shape=[None, num_historical_days, num_features])
X = tf.reshape(self.X, [-1, num_historical_days, 1, num_features])
self.Y = tf.placeholder(tf.int32, shape=[None, 2])
self.keep_prob = tf.placeholder(tf.float32, shape=[])
with tf.variable_scope("cnn"):
#[filter_height, filter_width, in_channels, out_channels]
k1 = tf.Variable(tf.truncated_normal([3, 1, num_features, 16],
stddev=0.1,seed=SEED, dtype=tf.float32))
b1 = tf.Variable(tf.zeros([16], dtype=tf.float32))
conv = tf.nn.conv2d(X,k1,strides=[1, 1, 1, 1],padding='SAME')
relu = tf.nn.relu(tf.nn.bias_add(conv, b1))
if is_train:
relu = tf.nn.dropout(relu, keep_prob = self.keep_prob)
print(relu)
k2 = tf.Variable(tf.truncated_normal([3, 1, 16, 32],
stddev=0.1,seed=SEED, dtype=tf.float32))
b2 = tf.Variable(tf.zeros([32], dtype=tf.float32))
conv = tf.nn.conv2d(relu, k2,strides=[1, 1, 1, 1],padding='SAME')
relu = tf.nn.relu(tf.nn.bias_add(conv, b2))
if is_train:
relu = tf.nn.dropout(relu, keep_prob = self.keep_prob)
print(relu)
k3 = tf.Variable(tf.truncated_normal([3, 1, 32, 64],
stddev=0.1,seed=SEED, dtype=tf.float32))
b3 = tf.Variable(tf.zeros([64], dtype=tf.float32))
conv = tf.nn.conv2d(relu, k3, strides=[1, 1, 1, 1], padding='VALID')
relu = tf.nn.relu(tf.nn.bias_add(conv, b3))
if is_train:
relu = tf.nn.dropout(relu, keep_prob=self.keep_prob)
print(relu)
flattened_convolution_size = int(relu.shape[1]) * int(relu.shape[2]) * int(relu.shape[3])
print(flattened_convolution_size)
flattened_convolution = features = tf.reshape(relu, [-1, flattened_convolution_size])
if is_train:
flattened_convolution = tf.nn.dropout(flattened_convolution, keep_prob=self.keep_prob)
W1 = tf.Variable(tf.truncated_normal([18*1*64, 32]))
b4 = tf.Variable(tf.truncated_normal([32]))
h1 = tf.nn.relu(tf.matmul(flattened_convolution, W1) + b4)
W2 = tf.Variable(tf.truncated_normal([32, 2]))
logits = tf.matmul(h1, W2)
#self.accuracy = tf.metrics.accuracy(tf.argmax(self.Y, 1), tf.argmax(logits, 1))
self.accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(self.Y, 1), tf.argmax(logits, 1)), tf.float32))
self.confusion_matrix = tf.confusion_matrix(tf.argmax(self.Y, 1), tf.argmax(logits, 1))
tf.summary.scalar('accuracy', self.accuracy)
theta_D = [k1, b1, k2, b2, k3, b3, W1, b4, W2]
# D_prob = tf.nn.sigmoid(D_logit)
self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.Y, logits=logits))
tf.summary.scalar('loss', self.loss)
# self.D_l2_loss = (0.0001 * tf.add_n([tf.nn.l2_loss(t) for t in theta_D]) / len(theta_D))
# self.D_loss = D_loss_real + D_loss_fake + self.D_l2_loss
# self.G_l2_loss = (0.00001 * tf.add_n([tf.nn.l2_loss(t) for t in theta_G]) / len(theta_G))
# self.G_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=D_logit_fake, labels=tf.ones_like(D_logit_fake))) + self.G_l2_loss
self.optimizer = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(self.loss)
self.summary = tf.summary.merge_all()
The rest of the code is under a paid wall. Please consider subscribing to it, it helps me pay for my tuition fees and you guys get access to all of the source code which i publish on youtube channel along with datasets and also you get articles and notes. Please consider subscribing, or you can visit medium to read for free read articles here