Deep Learning

Neural Networks

Back Propagation

Activation Functions

Regularization in Deep Learning

Optimizers



Keras and Example

Sequential Model Example:

from keras.models import Sequential model = Sequential()

Then add layers to the model one by one as follows: You may want to pass the data through StandardScalar() before passing through NN .

from keras.layers import Dense, Activation #first hidden layer with 4 neurons #.. input_dim is only need at the first hidden layer model.add(Dense(units=4, input_dim=3)) model.add(Activation('sigmoid')) #next hidden layer model.add(Dense(units=1)) model.add(Activation('sigmoid')) #or both lines can be combined as follows model.add(Dense(1,activation='sigmoid')) #print model summary here to see the number of parameters model.summary() #compile the model as #defining the learning rate, loss function and metrics #use categorical_crossentropy for categorical output model.compile(SGD(lr=0.003),'binary_crossentropy',metrics=['accuracy']) #fit the data, and save the history for analysis history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=500) #-------------------- #two kinds of predictions are done #.. the class predictions y_pred_classes = model.predict_classes(x_test) #.. the probability predictions y_pred_prob = model.predict(x_test)

You can draw a ROC curve using following:

roc_curve(y_test, y_pred_classes)


  • Scaling Inputs

  • Convolutional Neural Networks

    Creating a CNN Using Keras

    #import libraries from keras.preprocessing.image import ImageDataGenerator from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers import Conv2D, MaxPooling2D #image data usually are 3 dims, x-dim, y-dim and 3-channels # .. so training data is going to be 4-dims including the no of samples

    Here we also need to mend the y-values so that it fits the NN. We will convert the values to a vector using one-hot encoding.

    y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes)

    Let us create the mode.

    #create a model model = Sequential() #.. add a Convolutional layer model.add(Conv2D(32, (5,5), strides=(2,2), padding='same'),input_shape=x_train.shape[1:]) model.add(Activation('relu')) # add another convolution layer model.add(Conv2D(32, (5,5), strides=(2,2))) model.add(Activation('relu')) # add a max pool layer, add dropout for some regularization model.add(MaxPooling2D(pool_size=(2,2))) model.add(Dropout(0.25)) # flatten to 1D data model.add(Flatten()) # add 1 more dense layer model.add(Dense(512)) model.add(Activation('relu')) model.add(Dropout(0.5)) # add another dense layer model.add(Dense(num_classes)) model.add(Activation('softmax')) model.summary()

    Now that the model definition is complete, let us define optimizer and compile and fit the model.

    batch_size = 32 my_optimizer = keras.optimizers.RMSprop(lr=0.0005, decay=1e-6) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) model.fit(x_train, y_train, batch_size=batch_size, epochs=15, validation_data=(x_test, y_test), shuffle=True)

    After the model is trained, we can call predict as follows:

    model.predict_classes(x_test)

    The accuracy of the model can be determined as follows:

    from sklearn.metrics import accuracy_score accuracy_score(np.argmax(y_test, axis=1), model.predict_classes(x_test))

    Since we had one-hot encoded the y_test, value we used the np.argmax to convert the data back. We can add more convulation layers with different depth to increase model accuracy.

    We can also freeze training on a layer by setting trainable parameter to false for the layer.


    Transfer Learning


    Many Famous CNNs


    Recurrent NN

    LSTM

    GRU: Gated Recurrent Unit

    Seq2Seq

    Uses:


    Autoencoders

    Variational Autoencoders

    Auto Encoder Code


    Generative Adversarial Networks (GAN)s


    LIME: Locally Interpretable Model Agnostic Explanations


    Reinforcement Learning

    Implementation in Python

    Using gym library from openAI

    import gym