Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # from tensorflow.python.tools import freeze_graph
- import tensorflow as tf
- from tensorflow.python.ops import variable_scope as vs
- from tensorflow.python.ops import init_ops
- import time
- tf.reset_default_graph()
- # Input properties
- xSize = 13 # xData[0].shape[1] #13
- ySize = 18 # xData[0].shape[0] #18
- # Network Parameter
- n_input = xSize * ySize
- n_hidden_1 = 600
- n_hidden_2 = 500
- learning_rate = 0.1
- power_t = 0.25
- # epoch = 100000
- epoch = 100000
- dropout = 1.0
- batch_size = 100
- bias_start = 0.0
- def reg_perceptron(t, weights, biases):
- t = tf.nn.relu(tf.add(tf.matmul(t, weights['h1']), biases['b1']), name="layer_1")
- t = tf.nn.sigmoid(tf.add(tf.matmul(t, weights['h2']), biases['b2']), name="layer_2")
- t = tf.add(tf.matmul(t, weights['hOut'], name="LOut_MatMul"), biases['bOut'], name="LOut_Add")
- return tf.reshape(t, [-1], name="output")
- def sum_of_squares(predictions, targets):
- predictions.get_shape().assert_is_compatible_with(targets.get_shape()) # Just a check
- predictions = tf.to_float(predictions) * const200
- targets = tf.to_float(targets) * const200
- losses = tf.square(tf.sub(predictions, targets))
- RMSE = tf.sqrt(tf.reduce_mean(losses))
- return RMSE
- def initialize_variable(vs_name): # other necessary arguments
- # a function for initialize variables
- with tf.variable_scope(vs_name, reuse=None) as vs:
- h1 = tf.get_variable("weights0", [n_input, n_hidden_1], initializer=tf.contrib.layers.xavier_initializer())
- h2 = tf.get_variable("weights1", [n_hidden_1, n_hidden_2], initializer=tf.contrib.layers.xavier_initializer())
- hout = tf.get_variable("weightsOut", [n_hidden_2, 1], initializer=tf.contrib.layers.xavier_initializer())
- b1 = tf.get_variable("bias0", [n_hidden_1], initializer=init_ops.constant_initializer(bias_start))
- b2 = tf.get_variable("bias1", [n_hidden_2], initializer=init_ops.constant_initializer(bias_start))
- bout = tf.get_variable("biasOut", [1], initializer=init_ops.constant_initializer(bias_start))
- vs.reuse_variables()
- g = tf.Graph()
- with g.as_default():
- # Tensor placeholders and variables
- _x = tf.placeholder(tf.float32, [None, n_input], name="X_Input")
- _y = tf.placeholder(tf.float32, [None], name="Y_GroundTruth")
- _adaptive_learning_rate = tf.placeholder(tf.float32, shape=[], name="adaptive_learning_rate")
- _epoch_count = tf.Variable(0, dtype=tf.float32, name="epoch_count")
- #_cost = tf.Variable(1, dtype=tf.float32, name="cost")
- #_error = tf.Variable(tf.zeros([len(test_y)]), dtype=tf.float32, name="error")
- const200 = tf.constant(200.0, dtype=tf.float32)
- # Network weights and biases
- rg_weights = {
- 'h1': vs.get_variable("weights0", [n_input, n_hidden_1], initializer=tf.contrib.layers.xavier_initializer()),
- 'h2': vs.get_variable("weights1", [n_hidden_1, n_hidden_2], initializer=tf.contrib.layers.xavier_initializer()),
- 'hOut': vs.get_variable("weightsOut", [n_hidden_2, 1], initializer=tf.contrib.layers.xavier_initializer())
- }
- rg_biases = {
- 'b1': vs.get_variable("bias0", [n_hidden_1], initializer=init_ops.constant_initializer(bias_start)),
- 'b2': vs.get_variable("bias1", [n_hidden_2], initializer=init_ops.constant_initializer(bias_start)),
- 'bOut': vs.get_variable("biasOut", [1], initializer=init_ops.constant_initializer(bias_start))
- }
- # Network layer definitions
- pred = reg_perceptron(_x, rg_weights, rg_biases)
- print(str(pred))
- # Definition of cost function
- cost = sum_of_squares(pred, _y)
- # Create optimizer
- optimizer = tf.train.AdagradOptimizer(learning_rate=_adaptive_learning_rate).minimize(cost)
- # Create summary for TensorBoard
- merged_summary = tf.merge_all_summaries()
- timestamp = int(time.time())
- print "Starting session, TS =", timestamp
- # Session operations
- init_op = tf.initialize_all_variables()
- inc_epoch_op = _epoch_count.assign_add(1.0) # increase epoch counter by 1
- saver = tf.train.Saver()
- sess = tf.Session()
- # saver.restore(sess, model_save_path)
- saver.restore(sess, "/Users/leslie/Downloads/Y6_1476978999")
- sess.run(tf.initialize_all_variables())
- # a_adaptive_learning_rate = _adaptive_learning_rate.eval(sess)
- a_epoch_count = _epoch_count.eval(sess)
- #a_cost = _cost.eval(sess)
- #a_error = _error.eval(sess)
- # aconst200 = const200.eval(sess)
- sess.close()
- g_2 = tf.Graph()
- with g_2.as_default():
- _x_2 = tf.placeholder(tf.float32, [None, n_input], name="X_Input")
- _adaptive_learning_rate = tf.placeholder(tf.float32, shape=[], name="adaptive_learning_rate")
- _epoch_count_2 = tf.constant(a_epoch_count, name="epoch_count")
- #_cost_2 = tf.constant(a_cost, name="cost")
- #_error_2 = tf.constant(a_error, name="error")
- const200 = tf.constant(200.0, dtype=tf.float32)
- initialize_variable("vs_name") # fill in other necessary arguments
- with tf.variable_scope("vs_name", reuse=True):
- rg_weights_2 = {'h1': tf.get_variable("weights0"),
- 'h2': tf.get_variable("weights1"),
- 'hOut': tf.get_variable("weightsOut")}
- rg_biases_2 = {'b1': tf.get_variable("bias0"),
- 'b2': tf.get_variable("bias1"),
- 'bOut': tf.get_variable("biasOut")}
- pred_2 = reg_perceptron(_x_2, rg_weights_2, rg_biases_2)
- sess_2 = tf.Session()
- init_2 = tf.initialize_all_variables()
- sess_2.run(init_2)
- graph_def = g_2.as_graph_def()
- #tf.train.write_graph(graph_def, '/Users/leslie/Downloads/', 'trained_model.pb', as_text=False)
- print("saysomething")
- predy = sess_2.run(pred_2, feed_dict={_x_2:
- [[0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.32271653, 0.4093307, 0.2990945, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.8187795, 1.0431889, 0.73610234, 0.3030315,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 1.0116929, 1.1849213, 1.1494882,
- 0.83059055, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.6888583, 1.1652362,
- 1.1731102, 1.1652362, 0.43295276, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.32271653,
- 1.0392519, 1.1967323, 1.2006693, 0.562874, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.6101181, 1.1809843, 1.1967323, 0.49200788, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.75185037, 0.8542126, 0.36208662,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.31877953, 0.38964567,
- 0.31090552, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228, 0.19673228, 0.19673228, 0.19673228, 0.19673228,
- 0.19673228]]})
- print(predy)
- #_y_2 = tf.placeholder(tf.float32, [None], name="Y_GroundTruth")
- #feed_dict_2 = {_x_2: test_x, _y_2: test_y}
- #pred_y = sess_2.run(pred_2, feed_dict=feed_dict_2)
- print("done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement