+ All Categories
Home > Documents > Technical University of Munich - Rostlab...Technical University of Munich Chair for Bioinformatics...

Technical University of Munich - Rostlab...Technical University of Munich Chair for Bioinformatics...

Date post: 23-Jun-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
5
Technical University of Munich Chair for Bioinformatics and Computational Biology SoSe 2018 Protein Prediction I for Computer Scientists SoSe 2018 Exercise sheet 7 Prof. B. Rost 21.06.2018 – 28.06.2018 M. Bernhofer, M. Heinzinger, D. Nechaev, L. Richter Exercise 7: Neural Network Basics Important: Before writing an email to us asking questions regarding the input or output speci- fications, please read the information provided on the exercise sheet and in the student template carefully! Additionally, we encourage you to discuss potential issues with your colleagues before sending an email to us. We are only able to provide you with quick help in case of a real problem (technical issue or bug) if you avoid asking unnecessary questions. Also, please do not ask about the slides for the lecture or the exercise. We will upload them as soon as we have time. Introduction In the previous exercise you used SciKit to classify residues which are part of a transmembrane helix from all other residues in a protein. These high-level functions allow you a quick and easy-to- use access to many classifiers and associated methods. However, these high-level functions require you to meet pre-defined input and output specifications and give fixed boundaries for possible architectures. Not all real-life problems will fit into these specifications or might be better solved by a handcrafted architecture. Therefore, you will implement the most basic building block of feed-forward neural networks in this exercise: the perceptron. A single perceptron calculates the dot product between inputs and trainable weights. If the result of this operation is greater than a certain threshold, the perceptron gives an output. This behaviour is derived from neurons which receive inputs from various other neurons and ’fire’ (output) as soon as the incoming signals pass a certain threshold. This very simple architecture can be extended to much more complex architectures. For further explanation, please see the slides. In the first part of the exercise you will implement some helper functions which will be needed for all subsequent tasks. These functions include: adding a bias term to a given layer of the network and calculating the error (loss) between your prediction and the actual target. In the second part you will train a single perceptron which receives two boolean inputs for predicting the OR gate. This architecture will be implemented without and with an additional bias term. In the third part of the exercise you will stack several perceptrons to a multi-layer neural network. Again, this architecture takes two boolean inputs ( and one bias term ) but this time you will find a decision border for separating the different states of the XOR gate. This multi-layer architecture will be implemented once without an activation function and once with an activation function. 1
Transcript
Page 1: Technical University of Munich - Rostlab...Technical University of Munich Chair for Bioinformatics and Computational Biology SoSe 2018 Protein Prediction I for Computer Scientists

Technical University of Munich

Chair for Bioinformatics and Computational Biology SoSe 2018

Protein Prediction I for Computer Scientists SoSe 2018 Exercise sheet 7

Prof. B. Rost 21.06.2018 – 28.06.2018M. Bernhofer, M. Heinzinger, D. Nechaev, L. Richter

Exercise 7: Neural Network Basics

Important: Before writing an email to us asking questions regarding the input or output speci-fications, please read the information provided on the exercise sheet and in the student templatecarefully! Additionally, we encourage you to discuss potential issues with your colleagues beforesending an email to us. We are only able to provide you with quick help in case of a real problem(technical issue or bug) if you avoid asking unnecessary questions. Also, please do not ask aboutthe slides for the lecture or the exercise. We will upload them as soon as we have time.

Introduction

In the previous exercise you used SciKit to classify residues which are part of a transmembranehelix from all other residues in a protein. These high-level functions allow you a quick and easy-to-use access to many classifiers and associated methods. However, these high-level functions requireyou to meet pre-defined input and output specifications and give fixed boundaries for possiblearchitectures. Not all real-life problems will fit into these specifications or might be better solvedby a handcrafted architecture. Therefore, you will implement the most basic building block offeed-forward neural networks in this exercise: the perceptron. A single perceptron calculates the dotproduct between inputs and trainable weights. If the result of this operation is greater than a certainthreshold, the perceptron gives an output. This behaviour is derived from neurons which receiveinputs from various other neurons and ’fire’ (output) as soon as the incoming signals pass a certainthreshold. This very simple architecture can be extended to much more complex architectures. Forfurther explanation, please see the slides.

In the first part of the exercise you will implement some helper functions which will be needed for allsubsequent tasks. These functions include: adding a bias term to a given layer of the network andcalculating the error (loss) between your prediction and the actual target. In the second part youwill train a single perceptron which receives two boolean inputs for predicting the OR gate. Thisarchitecture will be implemented without and with an additional bias term. In the third part of theexercise you will stack several perceptrons to a multi-layer neural network. Again, this architecturetakes two boolean inputs ( and one bias term ) but this time you will find a decision border forseparating the different states of the XOR gate. This multi-layer architecture will be implementedonce without an activation function and once with an activation function.

1

Page 2: Technical University of Munich - Rostlab...Technical University of Munich Chair for Bioinformatics and Computational Biology SoSe 2018 Protein Prediction I for Computer Scientists

Helper Functions (∑

=6P)

All neural networks are trained by measuring the difference between the predicted label ( or value )and the true label ( or value ). All weights in the network are adjusted during the training processto minimize this difference. In the first part of the exercise you will implement two different lossfunctions which allow you to measure the difference between your predictions and the ground truth.Additionally, you have to implement the derivatives of these loss functions as the weights are notupdated based on raw error but on their negative gradient (see gradient descent). Additionally, youwill implement an activation function (sigmoid) and a function which allows you to add a bias termfor each layer. A bias term is an additional node in a layer which has a constant value (here: 1),meaning it receives no inputs from previous layers.

7.1 (H) Adding a Bias Term (1)

Complete the add bias function which takes a 1D or 2D numpy array as an input and returns anew array which includes the bias term. For the 1D array, the bias is added as last element while forthe 2D array the bias term is added to each row. In case of a 1D numpy array (e.g. [ 0, 0, 0 ]) thereturned array should look like this: [ 0, 0, 0, 1 ]. In case of a 2D numpy array (e.g. [ [0,1],[2,3],[4,5]]) the returned array should look like this: [ [0,1,1], [2,3,1], [4,5,1] ]

7.2 (H) Hinge Loss (1)

Complete the function hinge loss so that it returns the hinge loss based on a given predictionand the actual label. Keep in mind that this loss requires the classes to be within [-1, 1] instead of[0, 1].

7.3 (H) Gradient of Hinge Loss (1)

Complete the function delta hinge in such a way that it returns the gradient of the hinge lossfunction for a given prediction and the corresponding ground truth label. Again, keep in mind thatthe class labels are within [-1, 1] for the hinge loss instead of [0,1].

7.4 (H) L2 Loss (1)

Complete the function l2 loss which returns the l2 loss for a given ground truth and the corre-sponding prediction. Here, a constant factor of 0.5 is multiplied with the loss in order to cancel outthe exponent during derivation.

7.5 (H) Gradient of L2 Loss (1)

Complete the function delta l2 which returns the gradient of the l2 loss for a given ground truthand the corresponding prediction. Remember that we’ve added a constant factor of 0.5 previouslyto our L2 loss function in order to cancel out the exponent during derivation.

7.6 (H) Sigmoid Activation Function (1)

Complete the function sigmoid which transforms a given input number using the sigmoid function.Return the sigmoid value for the given input.

2

Page 3: Technical University of Munich - Rostlab...Technical University of Munich Chair for Bioinformatics and Computational Biology SoSe 2018 Protein Prediction I for Computer Scientists

Single Layer Perceptrons (∑

=4P)

After preparing the required helper functions we can start implementing our first perceptron. Forthe sake of simplicity we approximate the OR gate:

x1 x2 OR

0 0 -10 1 11 0 11 1 1

OR gate Single perceptron

Single perceptron with bias

Your perceptron will receive two boolean inputs x1 and x2 with each input having a weight asso-ciated. The sum of the weighted inputs should approximate the corresponding OR state (-1 or 1).Use the hinge loss to measure the difference between your prediction and the ground truth. Usethe gradient of the hinge loss to update the weights. Update the weights for each sample. Aftertraining for nEpochs ( given during object initialization), return the weights as one numpy array.Try to understand why this problem is not solvable without adding the bias term.

7.7 (H) Single Perceptron (2)

Complete the function single perceptron by implementing the forward and the backward passfor approximating the OR gate by using the backpropagation algorithm. The architecture is depictedin the figure above (Single Perceptron). While training for nEpochs, update the weights of yournetwork after every sample was processed. As described above use the hinge loss and its gradientto update the weights. After training your network return its weights as numpy array.

7.8 (H) Single Perceptron with bias (2)

Complete the function single perceptron with bias by implementing the forward and the back-ward pass for approximating the OR gate by using the backpropagation algorithm. The differenceto the previous architecture is that we will introduce a constant bias term now. The architecture isdepicted in the figure above (Single Perceptron with bias). While training for nEpochs, update theweights of your network after every sample was processed. As described above use the hinge lossand its gradient to update the weights. After training your network return its weights as numpyarray.

3

Page 4: Technical University of Munich - Rostlab...Technical University of Munich Chair for Bioinformatics and Computational Biology SoSe 2018 Protein Prediction I for Computer Scientists

Multi Layer Perceptrons (∑

=10P)

After solving the OR gate problem in the previous tasks, let’s have a look at a slightly morecomplicated case, the XOR gate:

x1 x2 XOR

0 0 00 1 11 0 11 1 0

XOR gate

Multi-Layer Perceptron

Please understand that the different states in the OR gate were separable by a single decision border.This is not possible for the XOR gate. Therefore, we need to introduce an additional layer to ournetwork which allows us to find more complex decision borders. In the first version of your multi-layer perceptron you will implement two layers without an activation function. Please understandthat this is equivalent to stacking multiple linear transformations which can be replaced by a singlelinear transformation. This means that we do not gain anything by the added complexity. However,this network will require only small modifications (introduction of a non-linearity during the forwardand backward pass) to solve the XOR problem.

In the following we will use the L2 loss for measuring the difference between our predictions andthe actual ground truth. As outlined above we will introduce a non-linearity (here: sigmoid) in thelast part of the exercise. The range of the sigmoid activation function is [0,1] which is why we willnow change our class labels to be also within [0,1]. Again, update the weights for each sample.After training for nEpochs, return the weights from the hidden layer to the output layer as onenumpy array. Also remember to add a bias term to every layer as depicted in the figure above.

7.9 (H) Multi-Layer Perceptron (5)

Complete the function multi perceptron with bias by implementing the forward and the back-ward pass for approximating the XOR gate by using the backpropagation algorithm. The architectureis depicted in the figure above (Multi-Layer Perceptron). While training for nEpochs, update theweights of your network after every sample was processed. As described above, please use the L2loss and its gradient to update the weights now. After training your network return the weightsfrom the hidden to the output layer as a numpy array.

7.10 (H) Multi-Layer Perceptron with bias and non-linearity (5)

Complete the function multi perceptron with bias and nonlinearity by implementing theforward and the backward pass for approximating the XOR gate by using the backpropagationalgorithm. The architecture is depicted in the figure above (Multi-Layer Perceptron). However, thistime you add a sigmoid non-linearity to the hidden-layer. Please note that this also affects the

4

Page 5: Technical University of Munich - Rostlab...Technical University of Munich Chair for Bioinformatics and Computational Biology SoSe 2018 Protein Prediction I for Computer Scientists

backward pass. While training for nEpochs, update the weights of your network after every samplewas processed. As described above use the L2 loss and its gradient to update the weights. Aftertraining your network return the weights of the hidden to the output layer as a numpy array.

5


Recommended