Last active
April 2, 2017 23:21
-
-
Save greninja/6dc5df415d6560babd61bb04ae60e8f2 to your computer and use it in GitHub Desktop.
Feedforward neural network template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import numpy as np | |
def get_batch(X, size): | |
# will return batches ( of size approx 10 ) of tuples ( or list ) of input features | |
# ( i.e. our data collected from the portals ) and corresponding output labels | |
def getdata(self): | |
# will fetch data from our aggregated dataset ( combined data from 3 portals in the form of [[input],[corresponding_output]] ) | |
class FeedForward: | |
def __init__(self,epoch, input_dim, hidden_dim, output_dim, batch_size,learning_rate,window): | |
self.epoch = epoch | |
self.input_dim = input_dim | |
self.hidden_dim = hidden_dim | |
self.output_dim = output_dim | |
self.batch_size = batch_size | |
self.learning_rate = learning_rate | |
with tf.name_scope('input_to_hidden'): | |
self.W1 = tf.Variable(tf.random_normal([self.input_dim, self.hidden_dim]),name='W1') | |
self.d = tf.Variable(tf.zeros([self.hidden_dim,1)) # Hidden layer biases | |
with tf.name_scope('hidden_to_output'): | |
self.W2 = tf.Variable(tf.random_normal([self.hidden_dim, self.output_dim],dtype=float32),name='U') | |
def main(): | |
# Forward Phase | |
Hidden = tf.tanh(tf.matmul(self.W1, input_set) + self.d ) | |
predicted_output = tf.nn.relu( tf.matmul(Hidden, self.W2) ) | |
# Backward Phase | |
loss = -( predicted_output - actual_output ) ** 2 # Squared error loss function | |
optimizer = tf.train.GradientDescentOptimizer(self.learning_rate) | |
train_op = optimizer.minimize(loss) | |
X = tf.placeholder(tf.float32, shape=[None, self.input_dim]) | |
Y = tf.placeholder(tf.float32, shape=[None, self.output_dim]) | |
sess.run(tf.global_variables_initializer()) | |
with tf.Session() as sess: | |
for epoch in range(self.epoch): | |
X, Y = get_batch() | |
sess.run(train_op, feed_dict={ input_set : X, actual_output : Y } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment