Skip to content

Instantly share code, notes, and snippets.

@farizrahman4u
Created November 6, 2017 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farizrahman4u/f51fabd8d2e2539ef7343becb36be95c to your computer and use it in GitHub Desktop.
Save farizrahman4u/f51fabd8d2e2539ef7343becb36be95c to your computer and use it in GitHub Desktop.
import numpy as np
from simple_classifier import SimpleClassifier
def vectorize(x):
# vectorize a string
if len(x) > 1:
return np.sum([vectorize(c) for c in x], axis=0)
if x == '.':
i = 27
elif x == ' ':
i = 26
else:
x = x.lower()
i = ord(x) - 97
oh = np.zeros(28)
oh[i] = 1
return oh
def decode(x):
# decode a one hot to a character
i = np.argmax(x)
if i == 27:
return '.'
if i == 26:
return ' '
return chr(i + 97)
# the data
X = ['hi', 'his', 'hise', 'hisen', 'isend', 'send ', 'end n', 'nd nu', 'd nud', ' nude', 'nudes']
Y = ['s', 'e', 'n', 'd', ' ', 'n', 'u', 'd', 'e', 's', '.']
# vectorize data
X = np.array(list(map(vectorize, X)))
Y = np.array(list(map(vectorize, Y)))
clf = SimpleClassifier(28, 28, num_pop=50)
clf.fit(X, Y, epochs=1000, batch_size=11, validation_data=(X, Y))
assert clf.evaluate(X, Y)['accuracy'] == 1
# compress weights
# remove unnecessary weights to obtain sparse matrix
sparse_matrix = []
w = clf.weights[0]
for i in range(28):
for j in range(28):
x = w[i, j]
w[i, j] = 0
if clf.evaluate(X, Y)['accuracy'] < 1:
w[i, j] = x
sparse_matrix.append((i, j, float(str(np.around(x, 1)))))
print(sparse_matrix)
# test
w = np.zeros((28, 28))
for r in sparse_matrix:
w[r[0], r[1]] = r[2]
clf.weights = [w]
print(clf.evaluate(X, Y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment