Advertisement
Guest User

Untitled

a guest
Aug 19th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.18 KB | None | 0 0
  1. """
  2. All tensorflow objects, if not otherwise specified, should be explicity
  3. created with tf.float32 datatypes. Not specifying this datatype for variables and
  4. placeholders will cause your code to fail some tests.
  5.  
  6. You do not need to import any other libraries for this assignment.
  7.  
  8. Along with the provided functional prototypes, there is another file,
  9. "train.py" which calls the functions listed in this file. It trains the
  10. specified network on the MNIST dataset, and then optimizes the loss using a
  11. standard gradient decent optimizer. You can run this code to check the models
  12. you create in part II.
  13. """
  14.  
  15. import tensorflow as tf
  16.  
  17. ##############################################
  18. # Ambrose Hill z3470165 COMP9444 Assigment 1 #
  19. ##############################################
  20.  
  21. """ PART I """
  22.  
  23. def add_consts():
  24.     """
  25.    EXAMPLE:
  26.    Construct a TensorFlow graph that declares 3 constants, 5.1, 1.0 and 5.9
  27.    and adds these together, returning the resulting tensor.
  28.    """
  29.     c1 = tf.constant(5.1,dtype=tf.float32)
  30.     c2 = tf.constant(1.0,dtype=tf.float32)
  31.     c3 = tf.constant(5.9,dtype=tf.float32)
  32.     a1 = tf.add(c1, c2)
  33.     af = tf.add(a1, c3)
  34.     return af
  35.  
  36.  
  37. def add_consts_with_placeholder():
  38.     """
  39.    Construct a TensorFlow graph that constructs 2 constants, 5.1, 1.0 and one
  40.    TensorFlow placeholder of type tf.float32 that accepts a scalar input,
  41.    and adds these three values together, returning as a tuple, and in the
  42.    following order:
  43.    (the resulting tensor, the constructed placeholder).
  44.    """
  45.     c1 = tf.constant(5.1,dtype=tf.float32)
  46.     c2 = tf.constant(1.0,dtype=tf.float32)
  47.     c3 = tf.placeholder(tf.float32)
  48.  
  49.     a1 = tf.add(c1,c2)
  50.     af = tf.add(a1,c3)
  51.  
  52.     return af, c3
  53.  
  54.  
  55. def my_relu(in_value):
  56.     """
  57.    Implement a ReLU activation function that takes a scalar tf.placeholder as input
  58.    and returns the appropriate output. For more information see the assignment spec.
  59.    """
  60.     c0 = tf.constant(0.0,dtype=tf.float32)
  61.     def f1(): return tf.constant(0.0,dtype=tf.float32)
  62.     def f2(): return in_value
  63.     out_value = tf.cond(tf.less(in_value,c0),f1,f2)
  64.  
  65.     return out_value
  66.  
  67.  
  68. def my_perceptron(x):
  69.     """
  70.    Implement a single perception that takes four inputs and produces one output,
  71.    using the RelU activation function you defined previously.
  72.  
  73.    Specifically, implement a function that takes a list of 4 floats x, and
  74.    creates a tf.placeholder the same length as x. Then create a trainable TF
  75.    variable that for the weights w. Ensure this variable is
  76.    set to be initialized as all ones.
  77.  
  78.    Multiply and sum the weights and inputs following the peceptron outlined in the
  79.    lecture slides. Finally, call your relu activation function.
  80.    hint: look at tf.get_variable() and the initalizer argument.
  81.    return the placeholder and output in that order as a tuple
  82.  
  83.    Note: The code will be tested using the following init scheme
  84.        # graph def (your code called)
  85.        init = tf.global_variables_initializer()
  86.        self.sess.run(init)
  87.        # tests here
  88.  
  89.    """
  90.  
  91.     """
  92.    1. Need to set up x variable as a placeholder
  93.    2. trainable tf object for the weights vector w.
  94.    3. Initilise w to 1.
  95.    4. Multiply and sum the weights and inputs.
  96.    5. Call activation functon on summed weights.
  97.    """
  98.  
  99.     i = tf.placeholder(tf.float32,shape=[x])
  100.     w = tf.get_variable("w", shape=[x],initializer=tf.ones_initializer,dtype=tf.float32)
  101.  
  102.     mul = tf.multiply(w,i)
  103.  
  104.     sum1 = tf.reduce_sum(mul)
  105.  
  106.     out =  my_relu(sum1)
  107.  
  108.     return i, out
  109.  
  110.  
  111. """ PART II """
  112. fc_count = 0  # count of fully connected layers. Do not remove.
  113.  
  114.  
  115. def input_placeholder():
  116.     return tf.placeholder(dtype=tf.float32, shape=[None, 784],
  117.                           name="image_input")
  118.  
  119.  
  120. def target_placeholder():
  121.     return tf.placeholder(dtype=tf.float32, shape=[None, 10],
  122.                           name="image_target_onehot")
  123.  
  124.  
  125.  
  126.  
  127. def onelayer(X, Y, layersize=10):
  128.     """
  129.    Create a Tensorflow model for logistic regression (i.e. single layer NN)
  130.  
  131.    :param X: The input placeholder for images from the MNIST dataset
  132.    :param Y: The output placeholder for image labels
  133.    :return: The following variables should be returned  (variables in the
  134.    python sense, not in the Tensorflow sense, although some may be
  135.    Tensorflow variables). They must be returned in the following order.
  136.        w: Connection weights
  137.        b: Biases
  138.        logits: The input to the activation function
  139.        preds: The output of the activation function (a probability
  140.        distribution over the 10 digits)
  141.        batch_xentropy: The cross-entropy loss for each image in the batch
  142.        batch_loss: The average cross-entropy loss of the batch
  143.    """
  144.     #Set up variables
  145.     w = tf.Variable(tf.zeros([X.get_shape().as_list()[1],layersize]),dtype=tf.float32)
  146.     b = tf.Variable(tf.zeros([layersize]),dtype=tf.float32)
  147.     #Multiply input by weights + bias
  148.     logits = tf.matmul(X,w) + b
  149.     #Use softmax activation function
  150.     preds = tf.nn.softmax(logits)
  151.     #Calculate loss for each image
  152.     batch_xentropy = -tf.reduce_sum(Y*tf.log(preds))
  153.     #Average each loss
  154.     batch_loss = tf.reduce_mean(batch_xentropy)
  155.  
  156.     return w, b, logits, preds, batch_xentropy, batch_loss
  157.  
  158.  
  159. def twolayer(X, Y, hiddensize=30, outputsize=10):
  160.     """
  161.    Create a Tensorflow model for a Neural Network with one hidden layer
  162.  
  163.    :param X: The  input placeholder for images from the MNIST dataset
  164.    :param Y: The output placeholder for image labels
  165.    :return: The following variables should be returned in the following order.
  166.        W1: Connection weights for the first layer
  167.        b1: Biases for the first layer
  168.        W2: Connection weights for the second layer
  169.        b2: Biases for the second layer
  170.        logits: The inputs to the activation function
  171.        preds: The outputs of the activation function (a probability
  172.        distribution over the 10 digits)
  173.        batch_xentropy: The cross-entropy loss for each image in the batch
  174.        batch_loss: The average cross-entropy loss of the batch
  175.    """
  176.     #Set up variables
  177.     w1 = tf.Variable(tf.truncated_normal([X.get_shape().as_list()[1], hiddensize]),dtype=tf.float32)
  178.     b1 = tf.Variable(tf.truncated_normal([hiddensize]),dtype=tf.float32)
  179.     #Set up variables 1
  180.     w2 = tf.Variable(tf.zeros([hiddensize, outputsize]),dtype=tf.float32)
  181.     b2 = tf.Variable(tf.zeros([outputsize]),dtype=tf.float32)
  182.  
  183.  
  184.     #Calculate first layer
  185.     logits1 = tf.matmul(X,w1) + b1
  186.     preds1 = tf.nn.relu(logits1)
  187.     #Calculate second layer
  188.     logits = tf.matmul(preds1,w2) + b2
  189.     preds = tf.nn.softmax(logits)
  190.  
  191.     #Calculate loss for each image
  192.     batch_xentropy = -tf.reduce_sum(Y*tf.log(preds))
  193.     #Average each loss
  194.     batch_loss = tf.reduce_mean(batch_xentropy)
  195.  
  196.     return w1, b1, w2, b2, logits, preds, batch_xentropy, batch_loss
  197.  
  198.  
  199. def convnet(X, Y, convlayer_sizes=[10, 10], \
  200.             filter_shape=[3, 3], outputsize=10, padding="same"):
  201.     """
  202.    Create a Tensorflow model for a Convolutional Neural Network. The network
  203.    should be of the following structure:
  204.    conv_layer1 -> conv_layer2 -> fully-connected -> output
  205.  
  206.    :param X: The  input placeholder for images from the MNIST dataset
  207.    :param Y: The output placeholder for image labels
  208.    :return: The following variables should be returned in the following order.
  209.        conv1: A convolutional layer of convlayer_sizes[0] filters of shape filter_shape
  210.        conv2: A convolutional layer of convlayer_sizes[1] filters of shape filter_shape
  211.        w: Connection weights for final layer
  212.        b: biases for final layer
  213.        logits: The inputs to the activation function
  214.        preds: The outputs of the activation function (a probability
  215.        distribution over the 10 digits)
  216.        batch_xentropy: The cross-entropy loss for each image in the batch
  217.        batch_loss: The average cross-entropy loss of the batch
  218.  
  219.    hints:
  220.    1) consider tf.layer.conv2d
  221.    2) the final layer is very similar to the onelayer network. Only the input
  222.    will be from the conv2 layer. If you reshape the conv2 output using tf.reshape,
  223.    you should be able to call onelayer() to get the final layer of your network
  224.    """
  225.  
  226.     X = tf.reshape(X,shape=[-1,28,28,1])
  227.  
  228.     conv1 = tf.layers.conv2d(X,convlayer_sizes[0],filter_shape,padding=padding,activation=tf.nn.relu)
  229.  
  230.     conv2 = tf.layers.conv2d(conv1,convlayer_sizes[1],filter_shape,padding=padding,activation=tf.nn.relu)
  231.  
  232.     X = tf.reshape(conv2,shape=[-1,28*28*10])
  233.     '''
  234.    #Set up variables
  235.    w = tf.Variable(tf.zeros([7840, 10]),dtype=tf.float32)
  236.    b = tf.Variable(tf.zeros([10]),dtype=tf.float32)
  237.    #Multiply input by weights + bias
  238.    logits = tf.matmul(X,w) + b
  239.    #Use softmax activation function
  240.    preds = tf.nn.softmax(logits)
  241.    #Calculate loss for each image
  242.    batch_xentropy = -tf.reduce_sum(Y*tf.log(preds))
  243.    #Average each loss
  244.    batch_loss = tf.reduce_mean(batch_xentropy)
  245.    '''
  246.  
  247.     w, b, logits, preds, batch_xentropy, batch_loss = onelayer(X,Y)
  248.  
  249.     return conv1, conv2, w, b, logits, preds, batch_xentropy, batch_loss
  250.  
  251.  
  252. def train_step(sess, batch, X, Y, train_op, loss_op, summaries_op):
  253.     """
  254.    Run one step of training.
  255.    
  256.    :param sess: the current session
  257.    :param batch: holds the inputs and target outputs for the current minibatch
  258.    batch[0] - array of shape [minibatch_size, 784] with each row holding the
  259.    input images
  260.    batch[1] - array of shape [minibatch_size, 10] with each row holding the
  261.    one-hot encoded targets
  262.    :param X: the input placeholder
  263.    :param Y: the output target placeholder
  264.    :param train_op: the tensorflow operation that will run one step of training
  265.    :param loss_op: the tensorflow operation that will return the loss of your
  266.    model on the batch input/output
  267.  
  268.    :return: a 3-tuple: train_op_result, loss, summary
  269.    which are the results of running the train_op, loss_op and summaries_op
  270.    respectively.
  271.    """
  272.     train_result, loss, summary = \
  273.         sess.run([train_op, loss_op, summaries_op], feed_dict={X: batch[0], Y: batch[1]})
  274.     return train_result, loss, summary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement