Advertisement
Guest User

XOR working example

a guest
Mar 1st, 2017
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | None | 0 0
  1. import tensorflow as tf
  2. import numpy as np
  3. from random import randint
  4.  
  5. DEBUG = True
  6.  
  7. total_inputs = 10000
  8. batch_size = 100
  9. n_hiddent_units = 2
  10.  
  11. ones_for_layer3 = tf.ones([batch_size, 1])
  12.  
  13. def init_weights(shape):
  14.     return tf.Variable(tf.random_normal(shape, stddev=1))
  15.  
  16.  
  17. def model(X, weight_hidden, weight_output):
  18.     # [1,3] x [3,n_hiddent_units] = [1,n_hiddent_units]
  19.     layer2_output = tf.nn.sigmoid(tf.matmul(X, weight_hidden))
  20.    
  21.     layer3_input = tf.concat([layer2_output, ones_for_layer3], axis=1)
  22.    
  23.     # [1,n_hiddent_units+1] x [1+n_hiddent_units, 2] = [1,2]
  24.     return tf.matmul(layer3_input, weight_output)
  25.     # return hiddern_units_output
  26.  
  27.  
  28. def getHiddenLayerOutput(X, weight_hidden):
  29.     hidden_units_output = tf.nn.sigmoid(tf.matmul(X, weight_hidden))
  30.     return hidden_units_output
  31.  
  32.  
  33. zeros = tf.zeros([total_inputs, 1])
  34. ones = tf.ones([total_inputs, 1])
  35. around_zeros = tf.random_normal([total_inputs, 1], mean=0, stddev=0.01)
  36. around_ones = tf.random_normal([total_inputs, 1], mean=1, stddev=0.01)
  37.  
  38. X = tf.placeholder("float", [None, 3])
  39. Y = tf.placeholder("float", [None, 2])
  40.  
  41. weight_hidden = init_weights([3, n_hiddent_units])
  42. weight_output = init_weights([n_hiddent_units + 1, 2])
  43.  
  44. hiddern_units_output = getHiddenLayerOutput(X, weight_hidden)
  45. py_x = model(X, weight_hidden, weight_output)
  46.  
  47. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y))
  48. train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost)
  49. predict_op = tf.argmax(py_x, 1)
  50.  
  51. with tf.Session() as sess:
  52.     tf.global_variables_initializer().run()
  53.    
  54.     trX_0_0 = sess.run(tf.concat([ones, zeros, zeros, ones, zeros], axis=1))
  55.     trX_0_1 = sess.run(tf.concat([ones, zeros, ones, zeros, ones], axis=1))
  56.     trX_1_0 = sess.run(tf.concat([ones, ones, zeros, zeros, ones], axis=1))
  57.     trX_1_1 = sess.run(tf.concat([ones, ones, ones, ones, zeros], axis=1))
  58.     trX = sess.run(tf.concat([trX_0_0, trX_0_1, trX_1_0, trX_1_1], axis=0))
  59.     trX = sess.run(tf.random_shuffle(trX))
  60.     print(trX)
  61.    
  62.     trY = sess.run(tf.reshape(tf.identity(trX[0:total_inputs * 4, 3:5]), [total_inputs * 4, 2]))
  63.    
  64.     for i in range(100):
  65.         for start, end in zip(range(0, len(trX), batch_size), range(batch_size, len(trX) + 1, batch_size)):
  66.             sess.run(train_op, feed_dict={ X: trX[start:end, 0:3], Y: trY[start:end] })
  67.            
  68.         start_index = randint(0, (total_inputs * 4) - batch_size)
  69.        
  70.         predicted_output = sess.run(predict_op, feed_dict={ X: trX[start_index:start_index + batch_size, 0:3] })
  71.         #print(predicted_output)
  72.        
  73.         actual_output = sess.run(tf.argmax(trX[start_index:start_index + batch_size, 3:5], 1))
  74.         #print(actual_output)
  75.    
  76.         print("iteration :", i, " accuracy :", np.mean(actual_output == predicted_output), "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement