Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.78 KB | None | 0 0
  1. class ReLULayer(tf.keras.layers.Layer):
  2.     """ classic ReLU function for non-linearity """
  3.  
  4.     def call(self, inputs):
  5.         """
  6.        :param inputs: outputs from the layer before
  7.        :return: ReLu(inputs)
  8.        """
  9.         # TODO (a) ReLU function, you are allowed to use tf.math
  10.         return tf.math.maximum(0., inputs)
  11.  
  12.  
  13. class SoftMaxLayer(tf.keras.layers.Layer):
  14.     """ SoftMax (or SoftArgMax) function to transform logits into probabilities """
  15.  
  16.     def call(self, inputs):
  17.         """
  18.        :param inputs: outputs from the layer before
  19.        :return: SoftMax(inputs)
  20.        """
  21.         maxx = tf.math.reduce_max(inputs, keepdims=True, axis=1)
  22.         sinputs = inputs - maxx
  23.         z = tf.math.reduce_sum(tf.math.exp(sinputs), keepdims=True, axis=1)
  24.         return tf.math.divide(tf.math.exp(sinputs), z)
  25.  
  26. class DenseLayer(tf.keras.layers.Layer):
  27.     """ a fully connected layer """
  28.  
  29.     def __init__(self, num_neurons: int, use_bias=True):
  30.         """
  31.        :param num_neurons: number of output neurons
  32.        :param use_bias: whether to use a bias term or not
  33.        """
  34.         super().__init__()
  35.         self.num_neurons = num_neurons
  36.         self.bias = use_bias
  37.         self.w = self.b = None
  38.  
  39.     def build(self, input_shape):
  40.         # TODO (a) add weights and possibly use_bias variables
  41.         self.w = tf.Variable(initializer(shape=[input_shape[-1],
  42.             self.num_neurons]), trainable=True)
  43.  
  44.         if self.bias:
  45.             self.b = tf.Variable(initializer(shape=[1,self.num_neurons]),
  46.                     trainable=True)
  47.  
  48.     def call(self, inputs):
  49.         # TODO (a) linear/affine transformation
  50.         mul = tf.matmul(inputs, self.w)
  51.         if self.bias:
  52.             mul += self.b
  53.         return mul
  54.  
  55. class SequentialModel(tf.keras.layers.Layer):
  56.     """ a sequential model containing other layers """
  57.  
  58.     def __init__(self, num_neurons: [int], use_bias=True):
  59.         """
  60.        :param num_neurons: number of output neurons for each DenseLayer.
  61.        :param use_bias: whether a use_bias terms should be used or not
  62.        """
  63.         super().__init__()
  64.         self.modules = []
  65.         # TODO (b) interleave DenseLayer and ReLULayer of given sizes, start and end with DenseLayer
  66.         for i in range(len(num_neurons) - 1):
  67.             self.modules.append(DenseLayer(num_neurons=num_neurons[i],
  68.                 use_bias=use_bias))
  69.             self.modules.append(ReLULayer())
  70.        
  71.         self.modules.append(DenseLayer(num_neurons=num_neurons[-1],
  72.             use_bias=use_bias))
  73.         # TODO (b) add a SoftMaxLayer
  74.         self.modules.append(SoftMaxLayer())
  75.  
  76.     def call(self, inputs):
  77.         # TODO (b) propagate input sequentially
  78.         x = inputs
  79.        
  80.         for module in self.modules:
  81.             x = module(x)
  82.  
  83.         return x
  84.  
  85. def train_model(model, train_set, eval_set, loss, learning_rate, epochs) -> (list, list):
  86.     """
  87.    :param model: a sequential model defining the network
  88.    :param train_set: a tf.data.Dataset providing the training data
  89.    :param eval_set: a tf.data.Dataset providing the evaluation data
  90.    :param loss: a tensor defining the kind of lost used
  91.    :param learning_rate: learning rate (step size) for stochastic gradient descent
  92.    :param epochs: num epochs to train
  93.    :return: list of evaluation accuracies and list of train accuracies
  94.    """
  95.     # Instantiate an optimizer.
  96.     optimizer = tf.keras.optimizers.SGD(learning_rate)
  97.     train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy()
  98.     train_accuracy_per_epoch = []
  99.     eval_accuracy_per_epoch = []
  100.  
  101.     # TODO (c) for every batch in the set...
  102.  
  103.     # TODO (c) compute outputs (logits), loss, gradients
  104.  
  105.     # TODO (c) update the model
  106.  
  107.     # TODO (c) update the accuracy
  108.  
  109.     # some train loop
  110.     for e in range(epochs):
  111.         train_accuracy.reset_states()
  112.         for batch_input, batch_target in train_set:
  113.             with tf.GradientTape() as t:
  114.                 batch_output = model(batch_input)
  115.                 batch_loss = loss(batch_target, batch_output)
  116.            
  117.             weights = []
  118.             for layer in model.modules:
  119.                 if type(layer) is DenseLayer:
  120.                     weights += [layer.w, layer.b]
  121.  
  122.             grads = t.gradient(batch_loss, weights)
  123.             optimizer.apply_gradients(zip(grads, weights))
  124.  
  125.             train_accuracy.update_state(batch_target, batch_output)
  126.  
  127.         train_accuracy_per_epoch.append(train_accuracy.result())
  128.         eval_accuracy_per_epoch.append(test_model(model, eval_set))
  129.         tf.print("epoch: ", e, "\t train accuracy: ", train_accuracy_per_epoch[-1], "\t eval accuracy: ",
  130.                  eval_accuracy_per_epoch[-1])
  131.  
  132.     return eval_accuracy_per_epoch, train_accuracy_per_epoch
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement