Guest User

Untitled

a guest
Sep 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. def train(self,data,num_epochs=150,num_labeled=-1,noise_std=0.3,lr=0.02,decay_after=15):
  2. tf.reset_default_graph()
  3.  
  4.  
  5. #Create placeholders. They will be assigned when we start the session with feed_dict{...}
  6. inputs = tf.placeholder(tf.float32, shape=(None, architecture[0]), name='inputs')
  7. outputs = tf.placeholder(tf.float32, name='outputs')
  8.  
  9. #This variable should be assigned in a loop in a function that is being called later in the script
  10. #We later want to reference that variable after loading the model.
  11. last_layer_activation = tf.Variable(tf.ones((1,architecture[L-1])),name='last_layer_activation')
  12.  
  13. #This is the function where the assignment is made.
  14. def encoder(inputs,noise_std):
  15. h = inputs + tf.random_normal(tf.shape(inputs)) * noise_std #Clean input if the noise std is set to zero
  16.  
  17. #Loop through all the layers. Doing forward propagation and updating the values we need to keep track of.
  18. for l in range(1, L+1): #Max. index: L
  19. m_l, v_l = tf.nn.moments(z_pre_l, axes=[0]) #CHANGE
  20.  
  21. if l == L:
  22. #Convert z and apply softmax for the last layer. (TODO: Only for prediction or if we pass through encoder?)
  23. h = tf.nn.softmax(weights['gamma'][l-1] * (z+weights['beta'][l-1]))
  24. elif l == L-1: #@@@@@ I want to save this in my model.
  25. h = tf.nn.relu(z + weights['beta'][l-1])
  26. tf.assign(last_layer_activation,h,name='last_layer_assignment')
  27. else:
  28. h = tf.nn.relu(z + weights['beta'][l-1]) #TODO: No gamma?
  29.  
  30. return h, d
  31.  
  32.  
  33. sess = tf.Session()
  34.  
  35.  
  36. init = tf.global_variables_initializer()
  37. sess.run(init)
  38. print(sess.run(tf.report_uninitialized_variables()))
  39.  
  40. epoch_n = 0
  41. for i in range(int(i_iter),num_iter):
  42. #Get the next batch of batch size (images and labels)
  43. images, labels = data.next_batch()
  44. sess.run(train_step, feed_dict={inputs: images, outputs: labels, training: True})
  45. if (i > 1) and ((i+1) % (num_iter//num_epochs) == 0):
  46. epoch_n = i // (n_examples//batch_size)
  47. print("Epoch: %s, Accuracy: %s" % (epoch_n, sess.run([accuracy], feed_dict={inputs: data.validate[0], outputs: data.validate[1], training: True})))
  48.  
  49.  
  50. #At the end of the loop, I want to specify the model inputs and outputs
  51. #That I want to save.
  52. model_inputs = {
  53. "inputs_placeholder":inputs,
  54. "outputs_placeholder":outputs
  55. }
  56. model_outputs = {
  57. "accuracy": accuracy,
  58. "clean_output": y,
  59. "last_layer_activation": last_layer_activation,
  60. "last_layer_assignment": last_layer_assignment
  61. }
  62. tf.saved_model.simple_save(sess, c + '_models/',model_inputs,model_outputs)
  63.  
  64. def get_last_layer_activation(self,X,dim):
  65. c = str(self._id)
  66. act = np.zeros((X.shape[0],dim))
  67. from tensorflow.python.saved_model import tag_constants
  68. graph = tf.Graph()
  69. saver = tf.train.import_meta_graph(self.get_latest_meta())
  70. restored_graph = tf.get_default_graph()
  71. with restored_graph.as_default():
  72. with tf.Session() as sess:
  73. tf.saved_model.loader.load(
  74. sess,
  75. [tag_constants.SERVING],
  76. c + '_models/'
  77. )
  78. inputs_placeholder = restored_graph.get_tensor_by_name('inputs:0')
  79. outputs_placeholder = restored_graph.get_tensor_by_name('outputs:0')
  80. training = restored_graph.get_tensor_by_name('training:0')
  81. last_layer_activation = restored_graph.get_tensor_by_name('last_layer_activation:0')
  82. last_layer_assignment = restored_graph.get_tensor_by_name('last_layer_assignment:0')
  83. for idx, row in enumerate(X):
  84. sess.run(last_layer_assignment, feed_dict={inputs_placeholder: np.reshape(row,[1,X.shape[1]]), training:True})
  85. out = sess.run(last_layer_activation, feed_dict={inputs_placeholder: np.reshape(row,[1,X.shape[1]]), training:True})
  86. act[idx,:] = out
  87. return act
Add Comment
Please, Sign In to add comment