Skip to content

Instantly share code, notes, and snippets.

@mountain
Last active March 27, 2017 11:58
Show Gist options
  • Save mountain/bdb9a8bba63926e04827b476816ac50e to your computer and use it in GitHub Desktop.
Save mountain/bdb9a8bba63926e04827b476816ac50e to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import numpy as np
import sys
import keras
from keras.layers import Input, Dense
from keras.models import Model
def TRUE(p, q):
return np.repeat(1.0, p.size)
def NOR(p, q):
return np.round((1 - np.round(p)) * (1 - np.round(q))) > 0.0
def XQ(p, q):
return np.round((1 - np.round(p)) * np.round(q)) > 0.0
def NEGP(p, q):
return np.round(1 - np.round(p)) > 0.0
def CNIMP(p, q):
return np.round(np.round(p) * (1 - np.round(q))) > 0.0
def NEGQ(p, q):
return np.round(1 - np.round(q)) > 0.0
def XOR(p, q):
return np.round(((np.round(p) + np.round(q)) > 0.0) * (((1 - np.round(p)) + (1 - np.round(q))) > 0.0)) > 0.0
def NAND(p, q):
return np.round((1 - np.round(p)) + (1 - np.round(q))) > 0.0
def AND(p, q):
return np.round(np.round(p) * np.round(q)) > 0.0
def XNOR(p, q):
return np.round(np.round(p) * np.round(q) + (1 - np.round(p)) * (1 - np.round(q))) > 0.0
def Q(p, q):
return np.round(q) > 0.0
def IMP(p, q):
return np.round(np.round(p) * np.round(q) + (1 - np.round(p)) * np.round(q) + (1 - np.round(p))* (1 - np.round(q))) > 0.0
def P(p, q):
return np.round(p) > 0.0
def CIMP(p, q):
return np.round(np.round(p) * np.round(q) + np.round(p) * (1 - np.round(q)) + (1 - np.round(p))* (1 - np.round(q))) > 0.0
def OR(p, q):
return np.round(np.round(p) + np.round(q)) > 0.0
def FALSE(p, q):
return np.repeat(0.0, p.size)
gates = {
'TRUE': TRUE,
'NOR': NOR,
'XQ': XQ,
'NEGP': NEGP,
'CNIMP': CNIMP,
'NEGQ': NEGQ,
'XOR': XOR,
'NAND': NAND,
'AND': AND,
'XNOR': XNOR,
'Q': Q,
'IMP': IMP,
'P': P,
'CIMP': CIMP,
'FALSE': FALSE,
}
def gen_model(width, depth):
inputs = Input(shape=(2,)) # p, q
trans = Dense(2, activation='linear')(inputs)
for i in range(depth):
trans = Dense(width, activation='relu')(trans)
trans = Dense(width, activation='linear')(trans)
trans = Dense(width, activation='relu')(trans)
outputs = Dense(1, activation='linear')(trans)
model = Model(input=inputs, output=outputs)
model.compile(optimizer='adam', loss='mean_squared_error')
return model
def test_gate(ix, gname, gate):
n = 1
ratio = 1.0
progress = ''
for i in range(len(gates)):
if i < ix:
progress = '%s-' % progress
else:
progress = '%s+' % progress
print '-----------------------------------------'
sys.stdout.flush()
while ratio > 0.01:
n = n + 1
for m in range(5):
model = gen_model(n, m)
for i in range(5000):
a = np.random.sample(100)
b = np.random.sample(100)
c = gate(a, b)
data = np.array([a, b]).swapaxes(0, 1)
model.fit(data, c, batch_size=100, nb_epoch=40, verbose=0)
sys.stdout.flush()
d = np.random.sample(1000)
e = np.random.sample(1000)
f = gate(d, e)
test = np.array([d, e]).swapaxes(0, 1)
ratio = model.evaluate(test, f, batch_size=1000, verbose=0)
print '%s\t%03d\t%03d %s' % (gname, n, m, progress)
print ratio
print '-----------------------------------------'
sys.stdout.flush()
return [gname, n]
if __name__ == "__main__":
results = [test_gate(ix, *pair) for ix, pair in enumerate(gates.iteritems())]
print results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment