Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.12 KB | None | 0 0
  1. import tensorflow as tf
  2. import os
  3. import shutil
  4.  
  5.  
  6. tensorboard_path = "/home/daniel/tensorboard_log/"
  7. tensorboard_tmp_dir = "test1"
  8.  
  9. # delete any former tensorboard log data
  10. if os.path.exists(tensorboard_path + tensorboard_tmp_dir):
  11.     shutil.rmtree(tensorboard_path + tensorboard_tmp_dir)
  12.  
  13.  
  14. #####################
  15. # preparation stuff #
  16. #####################
  17.  
  18. # define input and output data
  19. input_data = [[0., 0.], [0., 1.], [1., 0.], [1., 1.]]  # XOR input
  20. output_data = [[0.], [1.], [1.], [0.]]  # XOR output
  21. # input_data = [[0., 0.], [0., 0.], [0., 0.], [0., 0.]]  # XOR input
  22. # output_data = [0., 0., 0., 0.]  # XOR output
  23.  
  24. # create a placeholder for the input
  25. # None indicates a variable batch size for the input
  26. # one input's dimension is [1, 2] and output's [1, 1]
  27. n_input = tf.placeholder(tf.float32, shape=[None, 2], name="n_input")
  28. n_output = tf.placeholder(tf.float32, shape=[None, 1], name="n_output")
  29.  
  30. # number of neurons in the hidden layer
  31. hidden_nodes = 5
  32.  
  33.  
  34. ################
  35. # hidden layer #
  36. ################
  37.  
  38. # hidden layer's bias neuron
  39. b_hidden = tf.Variable(0.1, name="hidden_bias")
  40.  
  41. # hidden layer's weight matrix initialized with a uniform distribution
  42. W_hidden = tf.Variable(tf.random_uniform([2, hidden_nodes], -1.0, 1.0), name="hidden_weights")
  43.  
  44. # calc hidden layer's activation
  45. hidden = tf.sigmoid(tf.matmul(n_input, W_hidden) + b_hidden)
  46.  
  47.  
  48. ################
  49. # output layer #
  50. ################
  51.  
  52. W_output = tf.Variable(tf.random_uniform([hidden_nodes, 1], -1.0, 1.0), name="output_weights")  # output layer's weight matrix
  53. output = tf.sigmoid(tf.matmul(hidden, W_output))  # calc output layer's activation
  54.  
  55.  
  56. ############
  57. # learning #
  58. ############
  59. # cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(n_output, output)  # calc cross entropy between current
  60.                                                                               # output and desired output
  61. cross_entropy = -tf.reduce_sum(n_output * tf.log(output))
  62.  
  63. loss = tf.reduce_mean(cross_entropy)  # mean the cross_entropy
  64. optimizer = tf.train.GradientDescentOptimizer(0.01)  # take a gradient descent for optimizing with a "stepsize" of 0.1
  65. train = optimizer.minimize(loss)  # let the optimizer train
  66.  
  67.  
  68. ####################
  69. # initialize graph #
  70. ####################
  71. init = tf.initialize_all_variables()
  72.  
  73. sess = tf.Session()  # create the session and therefore the graph
  74. sess.run(init)  # initialize all variables
  75.  
  76.  
  77. #####################
  78. # tensorboard stuff #
  79. #####################
  80. tf.train.write_graph(sess.graph_def, tensorboard_path + tensorboard_tmp_dir, 'graph.pbtxt')
  81.  
  82. tf.histogram_summary('weights_hidden', W_hidden)
  83. tf.histogram_summary('bias hidden', b_hidden)
  84.  
  85. tf.histogram_summary('weights_output', W_output)
  86.  
  87. tf.histogram_summary('input', n_input)
  88. tf.histogram_summary('output', output)
  89.  
  90. merged_summary_op = tf.merge_all_summaries()
  91. summary_writer = tf.train.SummaryWriter(tensorboard_path + tensorboard_tmp_dir, sess.graph_def)
  92.  
  93.  
  94. #####################
  95. # train the network #
  96. #####################
  97. for epoch in xrange(0, 2001):
  98.     # run the training operation
  99.     cvalues = sess.run([train, loss, W_hidden, b_hidden, W_output],
  100.                        feed_dict={n_input: input_data, n_output: output_data})
  101.  
  102.     # print some debug stuff
  103.     if epoch % 200 == 0:
  104.         print("")
  105.         print("step: {:>3}".format(epoch))
  106.         print("loss: {}".format(cvalues[1]))
  107.         print("b_hidden: {}".format(cvalues[3]))
  108.         print("W_hidden: {}".format(cvalues[2]))
  109.         print("W_output: {}".format(cvalues[4]))
  110.         summary_str = sess.run(merged_summary_op, feed_dict={n_input: input_data})
  111.         summary_writer.add_summary(summary_str, epoch)
  112.  
  113. print("")
  114. print("input: {} | output: {}".format(input_data[0], sess.run(output, feed_dict={n_input: [input_data[0]]})))
  115. print("input: {} | output: {}".format(input_data[1], sess.run(output, feed_dict={n_input: [input_data[1]]})))
  116. print("input: {} | output: {}".format(input_data[2], sess.run(output, feed_dict={n_input: [input_data[2]]})))
  117. print("input: {} | output: {}".format(input_data[3], sess.run(output, feed_dict={n_input: [input_data[3]]})))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement