Advertisement
Schlez

protobufcreation

Nov 13th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.54 KB | None | 0 0
  1. # from tensorflow.python.tools import freeze_graph
  2. import tensorflow as tf
  3. from tensorflow.python.ops import variable_scope as vs
  4. from tensorflow.python.ops import init_ops
  5. import time
  6.  
  7.  
  8. tf.reset_default_graph()
  9.  
  10. # Input properties
  11. xSize = 13  # xData[0].shape[1] #13
  12. ySize = 18  # xData[0].shape[0] #18
  13.  
  14. # Network Parameter
  15. n_input = xSize * ySize
  16. n_hidden_1 = 600
  17. n_hidden_2 = 500
  18.  
  19. learning_rate = 0.1
  20. power_t = 0.25
  21. # epoch = 100000
  22. epoch = 100000
  23. dropout = 1.0
  24. batch_size = 100
  25. bias_start = 0.0
  26.  
  27.  
  28. def reg_perceptron(t, weights, biases):
  29.     t = tf.nn.relu(tf.add(tf.matmul(t, weights['h1']), biases['b1']), name="layer_1")
  30.     t = tf.nn.sigmoid(tf.add(tf.matmul(t, weights['h2']), biases['b2']), name="layer_2")
  31.     t = tf.add(tf.matmul(t, weights['hOut'], name="LOut_MatMul"), biases['bOut'], name="LOut_Add")
  32.  
  33.     return tf.reshape(t, [-1], name="output")
  34.  
  35.  
  36. def sum_of_squares(predictions, targets):
  37.     predictions.get_shape().assert_is_compatible_with(targets.get_shape())  # Just a check
  38.     predictions = tf.to_float(predictions) * const200
  39.     targets = tf.to_float(targets) * const200
  40.  
  41.     losses = tf.square(tf.sub(predictions, targets))
  42.     RMSE = tf.sqrt(tf.reduce_mean(losses))
  43.     return RMSE
  44.  
  45. def initialize_variable(vs_name): # other necessary arguments
  46.     # a function for initialize variables
  47.     with tf.variable_scope(vs_name, reuse=None) as vs:
  48.         h1 = tf.get_variable("weights0", [n_input, n_hidden_1], initializer=tf.contrib.layers.xavier_initializer())
  49.         h2 = tf.get_variable("weights1", [n_hidden_1, n_hidden_2], initializer=tf.contrib.layers.xavier_initializer())
  50.         hout = tf.get_variable("weightsOut", [n_hidden_2, 1], initializer=tf.contrib.layers.xavier_initializer())
  51.         b1 = tf.get_variable("bias0", [n_hidden_1], initializer=init_ops.constant_initializer(bias_start))
  52.         b2 = tf.get_variable("bias1", [n_hidden_2], initializer=init_ops.constant_initializer(bias_start))
  53.         bout = tf.get_variable("biasOut", [1], initializer=init_ops.constant_initializer(bias_start))
  54.         vs.reuse_variables()
  55.  
  56.  
  57. g = tf.Graph()
  58. with g.as_default():
  59.     # Tensor placeholders and variables
  60.     _x = tf.placeholder(tf.float32, [None, n_input], name="X_Input")
  61.     _y = tf.placeholder(tf.float32, [None], name="Y_GroundTruth")
  62.  
  63.     _adaptive_learning_rate = tf.placeholder(tf.float32, shape=[], name="adaptive_learning_rate")
  64.     _epoch_count = tf.Variable(0, dtype=tf.float32, name="epoch_count")
  65.  
  66.     #_cost = tf.Variable(1, dtype=tf.float32, name="cost")
  67.  
  68.     #_error = tf.Variable(tf.zeros([len(test_y)]), dtype=tf.float32, name="error")
  69.  
  70.     const200 = tf.constant(200.0, dtype=tf.float32)
  71.  
  72.     # Network weights and biases
  73.  
  74.     rg_weights = {
  75.         'h1': vs.get_variable("weights0", [n_input, n_hidden_1], initializer=tf.contrib.layers.xavier_initializer()),
  76.         'h2': vs.get_variable("weights1", [n_hidden_1, n_hidden_2], initializer=tf.contrib.layers.xavier_initializer()),
  77.         'hOut': vs.get_variable("weightsOut", [n_hidden_2, 1], initializer=tf.contrib.layers.xavier_initializer())
  78.     }
  79.  
  80.     rg_biases = {
  81.         'b1': vs.get_variable("bias0", [n_hidden_1], initializer=init_ops.constant_initializer(bias_start)),
  82.         'b2': vs.get_variable("bias1", [n_hidden_2], initializer=init_ops.constant_initializer(bias_start)),
  83.         'bOut': vs.get_variable("biasOut", [1], initializer=init_ops.constant_initializer(bias_start))
  84.     }
  85.  
  86.     # Network layer definitions
  87.     pred = reg_perceptron(_x, rg_weights, rg_biases)
  88.     print(str(pred))
  89.  
  90.     # Definition of cost function
  91.     cost = sum_of_squares(pred, _y)
  92.  
  93.     # Create optimizer
  94.     optimizer = tf.train.AdagradOptimizer(learning_rate=_adaptive_learning_rate).minimize(cost)
  95.  
  96.     # Create summary for TensorBoard
  97.     merged_summary = tf.merge_all_summaries()
  98.     timestamp = int(time.time())
  99.     print "Starting session, TS =", timestamp
  100.  
  101.     # Session operations
  102.     init_op = tf.initialize_all_variables()
  103.     inc_epoch_op = _epoch_count.assign_add(1.0)  # increase epoch counter by 1
  104.     saver = tf.train.Saver()
  105.     sess = tf.Session()
  106.  
  107.     # saver.restore(sess, model_save_path)
  108.     saver.restore(sess, "/Users/leslie/Downloads/Y6_1476978999")
  109.     sess.run(tf.initialize_all_variables())
  110.  
  111. # a_adaptive_learning_rate = _adaptive_learning_rate.eval(sess)
  112. a_epoch_count = _epoch_count.eval(sess)
  113. #a_cost = _cost.eval(sess)
  114. #a_error = _error.eval(sess)
  115. # aconst200 = const200.eval(sess)
  116. sess.close()
  117.  
  118. g_2 = tf.Graph()
  119. with g_2.as_default():
  120.     _x_2 = tf.placeholder(tf.float32, [None, n_input], name="X_Input")
  121.  
  122.     _adaptive_learning_rate = tf.placeholder(tf.float32, shape=[], name="adaptive_learning_rate")
  123.     _epoch_count_2 = tf.constant(a_epoch_count, name="epoch_count")
  124.  
  125.     #_cost_2 = tf.constant(a_cost, name="cost")
  126.  
  127.     #_error_2 = tf.constant(a_error, name="error")
  128.  
  129.     const200 = tf.constant(200.0, dtype=tf.float32)
  130.  
  131.     initialize_variable("vs_name")  # fill in other necessary arguments
  132.     with tf.variable_scope("vs_name", reuse=True):
  133.         rg_weights_2 = {'h1': tf.get_variable("weights0"),
  134.                       'h2': tf.get_variable("weights1"),
  135.                       'hOut': tf.get_variable("weightsOut")}
  136.         rg_biases_2 = {'b1': tf.get_variable("bias0"),
  137.                      'b2': tf.get_variable("bias1"),
  138.                      'bOut': tf.get_variable("biasOut")}
  139.  
  140.     pred_2 = reg_perceptron(_x_2, rg_weights_2, rg_biases_2)
  141.  
  142.     sess_2 = tf.Session()
  143.     init_2 = tf.initialize_all_variables()
  144.     sess_2.run(init_2)
  145.  
  146.     graph_def = g_2.as_graph_def()
  147.     #tf.train.write_graph(graph_def, '/Users/leslie/Downloads/', 'trained_model.pb', as_text=False)
  148.  
  149.     print("saysomething")
  150.  
  151.     predy = sess_2.run(pred_2, feed_dict={_x_2:
  152.                                               [[0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  153.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  154.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  155.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  156.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  157.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  158.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  159.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  160.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  161.                                                 0.19673228, 0.32271653, 0.4093307, 0.2990945, 0.19673228, 0.19673228,
  162.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  163.                                                 0.19673228, 0.19673228, 0.8187795, 1.0431889, 0.73610234, 0.3030315,
  164.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  165.                                                 0.19673228, 0.19673228, 0.19673228, 1.0116929, 1.1849213, 1.1494882,
  166.                                                 0.83059055, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  167.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.6888583, 1.1652362,
  168.                                                 1.1731102, 1.1652362, 0.43295276, 0.19673228, 0.19673228, 0.19673228,
  169.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.32271653,
  170.                                                 1.0392519, 1.1967323, 1.2006693, 0.562874, 0.19673228, 0.19673228,
  171.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  172.                                                 0.19673228, 0.6101181, 1.1809843, 1.1967323, 0.49200788, 0.19673228,
  173.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  174.                                                 0.19673228, 0.19673228, 0.19673228, 0.75185037, 0.8542126, 0.36208662,
  175.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  176.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.31877953, 0.38964567,
  177.                                                 0.31090552, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  178.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  179.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  180.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  181.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  182.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  183.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  184.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  185.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  186.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  187.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  188.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  189.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  190.                                                 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
  191.                                                 0.19673228]]})
  192.  
  193.     print(predy)
  194.  
  195. #_y_2 = tf.placeholder(tf.float32, [None], name="Y_GroundTruth")
  196.  
  197. #feed_dict_2 = {_x_2: test_x, _y_2: test_y}
  198. #pred_y = sess_2.run(pred_2, feed_dict=feed_dict_2)
  199. print("done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement