Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. import tensorflow as tf
  2. import numpy as np
  3. import data
  4. import matplotlib.pyplot as plt
  5.  
  6. class TFLogreg:
  7. def __init__(self, D, C, param_delta=0.5, param_lambda = 0.3):
  8. """Arguments:
  9. - D: dimensions of each datapoint
  10. - C: number of classes
  11. - param_delta: training step
  12. """
  13.  
  14. # definicija podataka i parametara:
  15. # definirati self.X, self.Yoh_, self.W, self.b
  16. # ...
  17. self.X = tf.placeholder(tf.float32, [None, D])
  18. self.Yoh_ = tf.placeholder(tf.float32, [None, C])
  19. self.W = tf.Variable(tf.zeros([D, C]))
  20. self.b = tf.Variable(tf.zeros([C]))
  21.  
  22. # formulacija modela: izračunati self.probs
  23. # koristiti: tf.matmul, tf.nn.softmax
  24. # ...
  25. self.probs = tf.nn.softmax(tf.matmul(self.X, self.W) + self.b)
  26.  
  27. # formulacija gubitka: self.loss
  28. # koristiti: tf.log, tf.reduce_sum, tf.reduce_mean
  29. # ...
  30. self.unreg_loss = tf.reduce_mean(-tf.reduce_sum(self.Yoh_ * tf.log(self.probs), reduction_indices=1))
  31. self.loss = self.unreg_loss + param_lambda * tf.nn.l2_loss(self.W)
  32.  
  33. # formulacija operacije učenja: self.train_step
  34. # koristiti: tf.train.GradientDescentOptimizer,
  35. # tf.train.GradientDescentOptimizer.minimize
  36. # ...
  37. trainer = tf.train.GradientDescentOptimizer(param_delta)
  38. self.train_step = trainer.minimize(self.loss)
  39.  
  40. # instanciranje izvedbenog konteksta: self.session
  41. # koristiti: tf.Session
  42. self.session = tf.Session()
  43.  
  44. def train(self, X, Yoh_, param_niter):
  45. """Arguments:
  46. - X: actual datapoints [NxD]
  47. - Yoh_: one-hot encoded labels [NxC]
  48. - param_niter: number of iterations
  49. """
  50.  
  51. # incijalizacija parametara
  52. # koristiti: tf.initializers.global_variables
  53. # ...
  54. init = tf.initializers.global_variables()
  55.  
  56. # optimizacijska petlja
  57. # koristiti: tf.Session.run
  58. # ...
  59. self.session.run(init)
  60.  
  61. for i in range(param_niter):
  62. self.session.run([self.train_step, self.loss], feed_dict={self.X:X, self.Yoh_:Yoh_})
  63.  
  64. def eval(self, X):
  65. """Arguments:
  66. - X: actual datapoints [NxD]
  67. Returns: predicted class probabilites [NxC]
  68. """
  69. # koristiti: tf.Session.run
  70. return self.session.run(self.probs, feed_dict={self.X:X})
  71.  
  72. def eval_class(self, X):
  73. return self.eval(X).argmax(axis=1)
  74.  
  75. if __name__ == "__main__":
  76. # inicijaliziraj generatore slučajnih brojeva
  77. np.random.seed(100)
  78. tf.set_random_seed(100)
  79.  
  80. # instanciraj podatke X i labele Yoh_
  81. X, Y_ = data.sample_gauss_2d(3, 100)
  82. Yoh_ = data.class_to_onehot(Y_)
  83. print(Yoh_)
  84.  
  85. # izgradi graf:
  86. tflr = TFLogreg(X.shape[1], Yoh_.shape[1], 0.1)
  87.  
  88. # nauči parametre:
  89. tflr.train(X, Yoh_, 1000)
  90.  
  91. # dohvati vjerojatnosti na skupu za učenje
  92. probs = tflr.eval(X)
  93.  
  94. # ispiši performansu (preciznost i odziv po razredima)
  95. Y = tflr.eval_class(X)
  96. accuracy, pr, M = data.eval_perf_multi(tflr.eval_class(X), Y_)
  97. print("Accuracy: ", accuracy)
  98. print("Precision / Recall: ", pr)
  99. print("Confusion Matrix: ", M)
  100.  
  101. # iscrtaj rezultate, decizijsku plohu
  102. rect = (np.min(X, axis=0), np.max(X, axis=0))
  103. data.graph_surface(tflr.eval_class, rect, offset=1)
  104. data.graph_data(X, Y_, Y_)
  105. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement