Guest User

Untitled

a guest
Oct 30th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.08 KB | None | 0 0
  1.  
  2. # blahmports
  3. import tensorflow as tf
  4. import numpy as np
  5.  
  6. # define hyperparameters
  7. learning_rate = 0.01
  8. training_epochs = 1000
  9. batch_size = 100
  10. display_step = 10
  11.  
  12. # Step 1:
  13. # Calculating the layer's output.
  14. # (now dynamic)
  15. def get_layer_op(input_layer, weight_shape, bias_shape):
  16.  
  17.     # first initialize constants
  18.     # (since we are not doing that anywhere)
  19.     # weight_stddev = (2.0/weight_shape[0])**0.5
  20.     weight_init = tf.random_uniform_initializer(minval=-1, maxval=1)
  21.     bias_init = tf.constant_initializer(value=0)
  22.  
  23.     # initialize weights
  24.     # remember scoping & get_variable?
  25.     W = tf.get_variable("W", weight_shape, initializer=weight_init)
  26.  
  27.     # initialize biases
  28.     b = tf.get_variable("b", bias_shape, initializer=bias_init)
  29.  
  30.     # return the output
  31.     return tf.nn.relu(tf.matmul(input_layer, W) + b)
  32.  
  33. # Step 2:
  34. # Computing the losses
  35. # by comparing layer output and actual output.
  36. def compute_losses(op, y):
  37.  
  38.     xentropy = tf.nn.softmax_cross_entropy_with_logits(logits=op, labels=y)
  39.     #~
  40.  
  41.     loss = tf.reduce_mean(xentropy)
  42.     #~
  43.  
  44.     return loss
  45.  
  46. # Step 3:
  47. # Training This Shit
  48. # Over Step 1 & 2.
  49. def training(cost,  global_step):
  50.  
  51.     tf.summary.scalar("cost", cost)
  52.  
  53.     optimizer = tf.train.GradientDescentOptimizer(learning_rate)
  54.     #~  optimizer?
  55.  
  56.     train_op = optimizer.minimize(cost, global_step=global_step)
  57.     #~  minimize opt?
  58.  
  59.     return train_op
  60.  
  61. # Step 4:
  62. # Evaluate the samples, return accuracy
  63. def evaluate(op, y):
  64.  
  65.     predicted_op = tf.argmax(op, 1)
  66.     actual_op = tf.argmax(y, 1)
  67.     #~  argmaxes?
  68.  
  69.     correct_pred = tf.equal(predicted_op, actual_op)
  70.     #~  check correctness of predictions
  71.  
  72.     accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
  73.     #~  reduce mean?
  74.  
  75.     return accuracy
  76.  
  77. # an aggreagation of layers here
  78. def actual_network(x):
  79.  
  80.     #~~# what shapes to use?
  81.     with tf.variable_scope('layer_1'):
  82.         layer_1_output = get_layer_op(x, [batch_size, 3, 256], [256])
  83.  
  84.     with tf.variable_scope('layer_2'):
  85.         layer_2_output = get_layer_op(layer_1_output, [batch_size, 256, 256], [256])
  86.  
  87.     with tf.variable_scope('layer_3'):
  88.         layer_3_output = get_layer_op(layer_2_output, [batch_size, 256, 256], [256])
  89.  
  90.     with tf.variable_scope('layer_out'):
  91.         layer_4_output = get_layer_op(layer_3_output, [batch_size, 256, 1], [1])
  92.  
  93.     return layer_4_output
  94.  
  95. # reutrns batches
  96. def get_batch(batch_size, x_train, y_train, i):
  97.     #
  98.     x_train_new = {}
  99.     for each_ in x_train:
  100.         try:
  101.             x_train_new['path'] += [each_['path']]
  102.             x_train_new['belief'] += [each_['belief']]
  103.             x_train_new['problem'] += [each_['problem']]
  104.         except:
  105.             x_train_new['path'] = [each_['path']]
  106.             x_train_new['belief'] = [each_['belief']]
  107.             x_train_new['problem'] = [each_['problem']]
  108.     return x_train_new, y_train[i:batch_size+i]
  109.  
  110. # create input sequences
  111. def create_input_sets():
  112.    
  113.     problems = list(np.random.randint(0, vocab_size, train_test_size))
  114.     traversal_paths = []
  115.     for i in range(train_test_size):
  116.             path_traversal = []
  117.             for i in range(10):
  118.                 path_traversal += [list(np.random.randint(0, train_test_size, 3))]
  119.             traversal_paths += [list(path_traversal)]
  120.     beliefs = []
  121.     for i in range(train_test_size):
  122.         beliefs += [list(np.random.randint(0, train_test_size, 3))]
  123.  
  124.     full_set = []
  125.     for i in range(train_test_size):
  126.         full_set += [{'problem' : problems[i],
  127.                 'path' : traversal_paths[i],
  128.                 'belief' : beliefs[i]}]
  129.  
  130.     return full_set[:split], full_set[split:]
  131.  
  132. # create wanted sequences
  133. def create_output_sets():
  134.    
  135.     traversal_paths = []
  136.     for i in range(train_test_size):
  137.             path_traversal = []
  138.             for i in range(10):
  139.                 path_traversal += [list(np.random.randint(0, train_test_size, 3))]
  140.             traversal_paths += [list(path_traversal)]
  141.  
  142.     return traversal_paths[:split], traversal_paths[split:]
  143.  
  144. if __name__ == '__main__':
  145.     #
  146.     x_train, x_test = create_input_sets()
  147.     #
  148.     y_train, y_test = create_output_sets()
  149.     #
  150.     # create the graph
  151.     with tf.Graph().as_default():
  152. #       #
  153.         # launch into our scope
  154.         with tf.variable_scope('creativity_approximator'):
  155.             #
  156.             # input placeholders
  157.             # ###################
  158.             #~~# are shapes okay for these?
  159.             #
  160.             # x = list of lists (all probable traversal paths for a solution 'a')
  161.             # [
  162.             #   [
  163.             #       [node, edge_property, node], [n, p, n], ...
  164.             #   ],
  165.             #   [
  166.             #       [node, edge_property, node], [n, p, n], ...
  167.             #   ],
  168.             # ]
  169.             x = tf.placeholder(tf.float32, shape=[None, None, None, None], name='probable_traversals')
  170.             #
  171.             # y = a singular traversal path
  172.             # [
  173.             #   [node, edge_property, node], [n, p, n], ...
  174.             # ]
  175.             y = tf.placeholder(tf.float32, shape=[None], name='core_belief')
  176.             #
  177.             # z = a word
  178.             # 'this_problem'
  179.             z = tf.placeholder(tf.float32, shape=None, name='problem')
  180.             #
  181.             # output placeholder
  182.             # ###################
  183.             #~~# are shapes okay for these?
  184.             # a = a singular traversal path
  185.             # [
  186.             #   [node, edge_property, node], [n, p, n], ...
  187.             # ]
  188.             a = tf.placeholder(tf.float32, shape=[None], name='perfect_solution')
  189.             #
  190.             # merge all input placeholders
  191.             # since those are inputs to
  192.             # the neural network
  193.             w = tf.stack([x, y, z])
  194.             #~~# this obviously doesnt work
  195.             # because SHAPES!
  196.  
  197.             # get network output
  198.             output = actual_network(w)
  199.             #
  200.             # get loss
  201.             cost = compute_losses(output, y)
  202.             #
  203.             # define global step to keep
  204.             # track of training process
  205.             global_step = tf.Variable(0, name='global_step', trainable=False)
  206.             #
  207.             # change weights
  208.             train_op = training(cost, global_step)
  209.             #
  210.             # evaluate changed weights
  211.             eval_op = evaluate(output, y)
  212.  
  213.             # initiate saver and summarizer
  214.             saver = tf.train.Saver()
  215.             summary_op = tf.summary.merge_all()
  216.             #
  217.             # initiate and run session
  218.             sess = tf.Session()
  219.             summary_writer = tf.summary.FileWriter('model/', graph_def=sess.graph_def)
  220.             init_op = tf.global_variables_initializer()
  221.             sess.run(init_op)
  222.  
  223.             i = 1
  224.  
  225.             # training cycle
  226.             for each_epoch in range(training_epochs):
  227.  
  228.                 avg_cost = 0.
  229.                 total_batches = int(len(x_train)/batch_size)
  230.  
  231.                 # loop over all batches now
  232.                 for i in range(total_batches):
  233.  
  234.                     batch_x, batch_y = get_batch(batch_size, x_train, y_train, i)
  235.                     feed_dict = {x : batch_x['path'],
  236.                             y : batch_x['belief'],
  237.                             z: batch_x['problem'],
  238.                             a: batch_y['solution']}
  239.                     sess.run(train_op, feed_dict=feed_dict)
  240.  
  241.                     batch_cost = sess.run(cost, feed_dict=feed_dict)
  242.                     avg_cost += batch_cost/total_batches
  243.  
  244.                     i += (i * batch_size)
  245.  
  246.                 # do updates
  247.                 if each_epoch % display_step == 0:
  248.  
  249.                     # show stats
  250.                     validate_feed = {x: x_test, y: y_test}
  251.                     accuracy = sess.run(eval_op, feed_dict=validate_feed)
  252.                     print('Model Error Rate:', (1 - accuracy) * 100, '%')
  253.  
  254.                     # save summary and model
  255.                     summary_str = sess.run(summary_op, feed_dict=feed_dict)
  256.                     summary_writer.add_summary(summary_str, sess.run(global_step))
  257.                     saver.save(sess, "model/model-checkpoint", global_step=global_step)
  258.  
  259.             print('Iterations Complete.')
  260.        
  261.             test_feed = {x: x_test, y: y_test}
  262.             test_accuracy = sess.run(eval_op, feed_dict=test_feed)
  263.             print(accuracy * 100, '% Accuracy.')
  264.  
  265.             # you can see tensorboard by -
  266.             # tensorboard --logdir=model
Add Comment
Please, Sign In to add comment