Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import tensorflow as tf
  2. import numpy as np
  3. import data_helper
  4.  
  5. class TLU(object):
  6. """
  7. Thresholding Logic Unit class definition
  8. """
  9. graph = None
  10. weight = np.asarray(
  11. [[1.0],
  12. [-1.0]]
  13. )
  14.  
  15. def __init__(self):
  16. self.graph = tf.Graph()
  17. with self.graph.as_default():
  18. # Define input and weight
  19. self.weight = tf.Variable(self.weight, dtype=tf.float16)
  20. self._input = tf.placeholder(tf.float16, [None, 2], name='input')
  21.  
  22. # multiplication
  23. mul = tf.matmul(self._input, self.weight)
  24.  
  25. # Step function
  26. self._output = tf.where( tf.less(mul, tf.zeros(tf.shape(mul), dtype=tf.float16)),
  27. tf.zeros(tf.shape(mul)),
  28. tf.ones(tf.shape(mul)))
  29.  
  30. def predict(self, x):
  31. """
  32. Predict the result
  33. """
  34. with tf.Session(graph=self.graph) as sess:
  35. sess.run(tf.global_variables_initializer())
  36. return sess.run([self._output,], feed_dict={self._input: x})
  37.  
  38. if __name__ == '__main__':
  39. # Load data and build model
  40. train_x, train_y, test_x = data_helper.load()
  41. cell = TLU()
  42.  
  43. # Fit
  44. result = cell.predict(test_x)
  45. for i in range(len(test_x)):
  46. print "test index: ", i, '\tresult: ', result[0][i]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement