Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. from keras.layers import Dense, Input
  2. from keras.models import Model
  3. import numpy as np
  4. import data_helper
  5.  
  6. class TLU(object):
  7. """
  8. Thresholding Logic Unit class definition
  9. """
  10. epoch = None
  11. model = None
  12.  
  13. def __init__(self, epoch=5000):
  14. self.epoch = epoch
  15. self.input = Input((2,))
  16. mul = Dense((1), activation='sigmoid')(self.input)
  17. self.model = Model(inputs=self.input, outputs=mul)
  18. self.model.compile(loss='mse', optimizer='adam')
  19.  
  20. def train(self, x, y):
  21. """
  22. Train the TLU model for specific training epoch
  23. """
  24. for i in range(self.epoch):
  25. self.model.fit(x, y)
  26.  
  27. def predict(self, x):
  28. """
  29. Predict the result
  30. """
  31. return self.model.predict(x)
  32.  
  33. if __name__ == '__main__':
  34. # Load data and build model
  35. train_x, train_y, test_x = data_helper.load()
  36. cell = TLU()
  37.  
  38. # Train
  39. cell.train(train_x, train_y)
  40.  
  41. # Predict
  42. print "<< testing >>"
  43. result = cell.predict(test_x)
  44. for i in range(len(test_x)):
  45. print "test index: ", i, '\tresult: ', result[i]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement