0% found this document useful (0 votes)
143 views1 page

Keras CheatSheet PGAA

This document provides a cheat sheet on Keras, a deep learning library built on TensorFlow and Theano. It summarizes key Keras features including basic model architecture, preprocessing techniques like padding and one-hot encoding, common neural network types supported like convolutional and recurrent neural networks, and how to compile, train, evaluate and use models for prediction.

Uploaded by

Pooja Bhushan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views1 page

Keras CheatSheet PGAA

This document provides a cheat sheet on Keras, a deep learning library built on TensorFlow and Theano. It summarizes key Keras features including basic model architecture, preprocessing techniques like padding and one-hot encoding, common neural network types supported like convolutional and recurrent neural networks, and how to compile, train, evaluate and use models for prediction.

Uploaded by

Pooja Bhushan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

ANALYTICS INDIA MAGAZINE

KERAS CHEAT SHEET


Keras is a deep learning library that is built on top of
TensorFlow and Theano. It provides APIs to develop deep neural
networks. We present here the various features of Keras
framework.
Basic Architecture of a model Keras Preprocessing Convolutional Neural Network

Here is an overview of the basic 1.Sequence padding CNN is a deep learning network
model architecture you can build Used to ensure all sequences in the list have
the same length used for image classification.
using Keras.

Code: from keras.preprocessing import Code:


from keras.datasets import mnist sequence from keras.layers import
from keras.models import Sequential x1=pad_sequences([[1, 2, 3], [3, 4, 5, 6], [7, Activation,Conv2D,MaxPooling2D,Flatt
from keras.layers import Dense 8]]) en
(x_train,y_train),(x_test,y_test) = #output: array([[0, 1, 2, 3], model2.add(Conv2D(32,(3,3),padding='s
mnist.load_data() [3, 4, 5, 6], ame',input_shape=x_train.shape[1:]))
model = Sequential() [0, 0, 7, 8]], dtype=int32) model.add(Activation('relu'))
model.add(Dense(32,activation='relu', model.add(Conv2D(32,(3,3)))
input_dim=x_train[1].shape)) 2.One-hot encoding model.add(Activation('relu'))
model.add(Dense(10, Representation of categorical variables into model.add(MaxPooling2D(pool_size=(2,
activation='softmax')) binary vectors. 2)))
model.compile(optimizer='SGD',loss= model.add(Dropout(0.25))
'categorical_crossentropy',metrics=['a Code: from keras.utils import to_categorical model.add(Dense(num_classes))
ccuracy']) data=np.array([5,4,6]) model.add(Activation('softmax'))
model.fit(x_train,y_train,epochs=10,b encoded = to_categorical(data)
atch_size=32) #output:
predictions = model.predict(x_test) [[0. 0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 1.]]

Recurrent Neural Network Model compilation and training Model evaluation and prediction.
This is a type of deep neural network Compilation Evaluation
where the output from the previous After building the model it is required to To evaluate the performance of the
step is fed as input to the present step. compile the model model after training.
model.compile(optimizer='rmsprop', score = model.evaluate(x_test,
Code: loss='mse', metrics=['accuracy']) y_test,batch_size=32)
from keras.layers import The optimizers supported are
Embedding,LSTM Sgd,adam,adagrad,rmsprop,adaboost. Prediction
model.add(Embedding(200,128)) Losses supported are After evaluating the model accuracy, to
model.add(LSTM(64,dropout=0.2,rec mse,rmse,binary_crossentropy and predict the output on unseen data
urrent_dropout=0.2)) categorical_crossentropy. model.predict(x_test, batch_size=32)
model.add(Dense(10,activation='sigm
oid')) Training
To train the model:
model.fit(x_train, y_train,batch_size=32,
epochs=15,
verbose=1,validation_data=(x_test,y_test))

You might also like