Created
April 16, 2017 10:42
-
-
Save kris-singh/54aecbc1d61f1d7d79a43ae2bfac8516 to your computer and use it in GitHub Desktop.
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 numpy as np | |
import tensorflow as tf | |
from sklearn.model_selection import train_test_split | |
from sklearn.utils import shuffle | |
import matplotlib.pyplot as plt | |
def create_gaussian_data(mean1, mean2, variance1, variance2, size_t): | |
""" | |
Generate random gaussian data | |
with specified mean and variance | |
""" | |
data1 = np.random.normal(mean1, variance1, size = size_t) | |
data2 = np.random.normal(mean2, variance2, size = size_t) | |
data = np.hstack([data1, data2]) | |
labels = [ 0 if i < size_t else 1 for i in range(0, 2*size_t)] | |
data_train, data_test, labels_train, lables_test = train_test_split(data, | |
labels, | |
train_size = 0.7) | |
return data_train, data_test, labels_train, lables_test | |
def creat_network(x, x_labels, test_x, test_y): | |
""" | |
Create 2 layer Neural Network | |
""" | |
# Define the Network | |
data = tf.placeholder(tf.float32, shape = [None, 1]) # since only one dimensional | |
labels = tf.placeholder(tf.float32, shape = [None, 1]) # since only one dimensional | |
weight1 = tf.Variable(tf.random_normal([1, 10]), dtype = tf.float32) | |
bias1 = tf.Variable([10], dtype = tf.float32) | |
weight2 = tf.Variable(tf.random_normal([10, 1]), dtype = tf.float32) | |
bias2 = tf.Variable([2], dtype = tf.float32) | |
# Define the Operations | |
hidden = tf.nn.sigmoid(tf.multiply(data, weight1) + bias1) | |
y_ = tf.nn.sigmoid(tf.matmul(hidden, weight2) + bias2) | |
y_ = tf.cast(tf.greater_equal(y_, 0.5), tf.float32) | |
loss = tf.reduce_mean(tf.squared_difference(y_, labels)) | |
opt = tf.train.GradientDescentOptimizer(0.5).minimize(loss) | |
sess = tf.Session() | |
sess.run(tf.global_variables_initializer()) | |
# Run for 1000 epochs | |
for i in range(0, 1000): | |
_, l, weight = sess.run([opt, loss, weight2], feed_dict = {data: x, labels: x_labels}) | |
# Get Training Loss at every 100 iterations | |
if(i % 100 == 0): | |
print l, weight | |
# Run on test set and get the classification accuracy | |
correct_pred = (tf.equal(y_, test_y[0:10])) | |
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) | |
print test_x | |
ypred = sess.run([y_], feed_dict={data: test_x[0:10]}) | |
print ypred | |
def main(): | |
train_x, test_x, train_y, test_y = create_gaussian_data(0, 5, 1, 3, 10000) | |
train_x = train_x.reshape(1, train_x.shape[0]) | |
train_y = np.array(train_y, dtype = np.float32) | |
train_y = train_y.reshape(1, train_y.shape[0]) | |
test_x = test_x.reshape(1, test_x.shape[0]) | |
test_y = np.array(test_y, dtype = np.float32) | |
test_y = test_y.reshape(1, test_y.shape[0]) | |
print train_y.T.shape | |
creat_network(train_x.T, train_y.T, test_x.T, test_y.T) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment