Skip to content

Commit

Permalink
added keras section
Browse files Browse the repository at this point in the history
  • Loading branch information
littleblackfish committed Apr 6, 2018
1 parent b88936a commit 6b65bc7
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 358 deletions.
153 changes: 153 additions & 0 deletions Keras/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
We don't do a rigorous treatment of neural nets (or any other model for the most part) in this course but you don't necessarily have to know all the math to make use of them.
[Keras](https://keras.io/) is a high-level Python library for deep learning that runs on high performance back-ends such as Theano and TensorFlow.

## Installing Keras

Installing Keras is pretty similar to any other python package; and can be done using pip or conda.
It requires that either one of Theano or TensorFlow is also installed, but pip or conda should handle that too.

## Using Keras

Building and training models with Keras is only slightly more complex than scikit-learn.
Although there are many different architectures that can be implemented in Keras, the simplest ones go a long way.

### Perceptron

In Keras, these are called **Sequential models**.

```
from keras.models import Sequential
model = Sequential()
```
We can construct a perceptron, we need a layer of fully connected units.
In Keras, this is called a **Dense layer**.

```
from keras.layers import Dense
# Input layer
model.add(Dense(10, input_dim=5, activation='relu'))
# Hidden layer
model.add(Dense(10, activation='relu'))
# Output layer
model.add(Dense(1, activation='sigmoid'))
```

Unlike scikit-learn, we then have to **Compile** our model so the backend knows how to calculate efficiently.

```
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
```

### Multilayer perceptron

A multilayer perceptron is simply a perceptron with multiple hidden layers.
We therefore already know how to build it

```
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
# Input layer
model.add(Dense(10, input_dim=5, activation='relu'))
# Hidden layers
model.add(Dense(10, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(10, activation='relu'))
# Output layer
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
```
### Multilayer perceptron with dropout

Dropout is an elegant way to avoid overfitting in neural networks, as described in [this paper](https://www.cs.toronto.edu/~hinton/absps/JMLRdropout.pdf).
Luckily, it is already implemented in Keras and available as a layer.
We can simply add **Dropout** layers to our multilayer perceptron.

```
from keras.models import Sequential
from keras.layers import Dense,Dropout
model = Sequential()
# Input layer
model.add(Dense(10, input_dim=5, activation='relu'))
# Hidden layers with dropout
model.add(Dropout(0.5))
model.add(Dense(10, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='relu'))
# Output layer
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
```

### Using Keras models with scikit-learn

Keras conveniently ships with wrappers to make scikit-compatible models.
This way we can test our Keras models just like our scikit-learn models, using for example **cross_validate** or **GridSearchCV** methods.
All we need to do is to wrap our model-building into a function with appropriate parameters.

```
from keras.models import Sequential
from keras.layers import Dense,Dropout
from keras.wrappers.scikit_learn import KerasClassifier
def genmodel(depth, width, dropout=False):
model = Sequential()
# Input layer
model.add(Dense(width, input_dim=61, activation='relu'))
# Optional dropout
if dropout : model.add(Dropout(0.5))
# Hidden layers with dropout
for i in range(depth) :
model.add(Dense(width, activation='relu'))
# Optional dropout
if dropout : model.add(Dropout(0.5))
# Output layer
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model
model = KerasClassifier(build_fn=genmodel, verbose=0)
```
The rest of the workflow is identical to a scikit-learn model.

```
from sklearn.model_selection import cross_validate
s = cross_validate(model, X, y, cv=10, 'scoring':['accuracy', 'precision', 'recall', 'f1', 'roc_auc'])
print (s)
```
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This is the repository for the lab module of the **Machine Learning for Bioinformatics** course as thought in the Spring of 2017.
This is the repository for the lab module of the **Machine Learning for Bioinformatics** course as thought in the Spring of 2018.
It contains the materials covered in the lab sessions.

* [Main course webpage](http://homes.soic.indiana.edu/classes/spring2018/info/i529-yye/index.php)
Expand All @@ -17,5 +17,5 @@ It contains the materials covered in the lab sessions.
* [Profile-HMMS with HMMER](HMMER/) *(week 6)*
* **Expectation Maximization**
* [Motif Discovery with MEME](MEME/) *(week 7)*
* [Classification exercises with R](R-MachineLearning/) *(week 11)*
* [Classification exercises with Python](Python-MachineLearning/) *(week 12 - 13)*
* [Supervised learning with scikit-learn](Python-MachineLearning/) *(week 11)*
* [Deep learning with Keras](Keras/) *(week 13)*
Loading

0 comments on commit 6b65bc7

Please sign in to comment.