Skip to content

Instantly share code, notes, and snippets.

@nkt1546789
Created January 23, 2017 18:01
Show Gist options
  • Save nkt1546789/5871b132346a5aa3f58946c1d898a6c5 to your computer and use it in GitHub Desktop.
Save nkt1546789/5871b132346a5aa3f58946c1d898a6c5 to your computer and use it in GitHub Desktop.
Simple neural network with tensorflow.
import tensorflow as tf
import numpy as np
class SimpleClassifier(object):
def __init__(self, m=None, random_state=1, n_epochs=20):
self.m = m
self.random_state = random_state
self.n_epochs = n_epochs
def fit(self, X, y):
n, d = X.shape
if self.m is None:
self.m = d
self.mu = X.mean(axis=0)
X_train = X - self.mu
tf.set_random_seed(self.random_state)
x_ = tf.placeholder(tf.float32, shape=[n, d])
y_ = tf.placeholder(tf.float32, shape=[n, 1])
W = tf.Variable(tf.random_normal([d, self.m]))
b = tf.Variable(tf.random_normal([self.m]))
w = tf.Variable(tf.random_normal([self.m, 1]))
u = tf.sign(tf.matmul(x_, W) + b)
ypred = tf.matmul(u, w)
loss = -tf.reduce_sum(tf.multiply(y_, ypred))
#loss = -tf.reduce_mean(tf.multiply(y_, ypred))
train = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(self.n_epochs):
sess.run(train, feed_dict={x_: X_train, y_: np.c_[y]})
self.W = sess.run(W)
self.b = sess.run(b)
self.w = sess.run(w)
return self
def decision_function(self, X):
Xce = X - self.mu
u = np.sign(Xce.dot(self.W) + self.b)
return u.dot(self.w)
def predict(self, X):
z = self.decision_function(X)
return (z >= 0) * 2 - 1
if __name__ == '__main__':
import matplotlib.pyplot as plt
r = np.random.RandomState(1)
n = 500
X = np.r_[
r.multivariate_normal([10, 10], [[0.1, 0], [0, 0.1]], size=n),
r.multivariate_normal([9, 9], [[0.1, 0], [0, 0.1]], size=n)
]
y = np.concatenate([
np.repeat(1, n),
np.repeat(-1, n)
])
sc = SimpleClassifier(m=None, random_state=1, n_epochs=1)
sc.fit(X, y)
for v1, v2 in np.c_[sc.decision_function(X), y]:
print(v1, v2)
xx, yy = np.meshgrid(
np.linspace(np.min(X[:,0])-1.0, np.max(X[:,0])+1.0, 100),
np.linspace(np.min(X[:,1])-1.0, np.max(X[:,1])+1.0, 100)
)
Xte = np.c_[xx.ravel(), yy.ravel()]
Z = sc.decision_function(Xte)
Z = Z.reshape(xx.shape)
plt.scatter(X[:,0], X[:,1], c=y, marker="o", s=30)
plt.contour(xx, yy, Z, levels=[0])
plt.tight_layout()
plt.show()
@nkt1546789
Copy link
Author

Decision boundary of a demo data:

simple_nn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment