Tefla: deep learning on the top of tensorflow.
Tefla is a high level API developed on the top of tensorflow to facilitate faster model building and deployment on the cloud/datacenter. It is also packaged with state-of-the-art classification, segmentation, generative and semi supervised learning models. Apart from that Tefla is also packaged with state-of-the-art image-processing, text processing algorithms.
One of the major improvement over existing deep learning engine is that Tefla makes it easy for everyone to develop deep learning application faster. It’s modular design enable integration of new module seamless.
Tefla is equipped to work with image, text, videos, volumetric data. It can also be used to develop reinforcement learning algorithms on game-environment.
Installations:
pip install teflaInstallations from source: For more detailed instructions visit https://github.com/openAGI/tefla
git clone https://github.com/openagi/tefla.git
cd tefla
pip install -r requirements.txt
export PYTHONPATH=.Let’s develop a model for IMDB dataset to classify movie reviews into positive and negative classes using Tefla.
All the code used in this example is available in the OpenAGI models github repository.
1. Prepare and load the dataset:
use the following script to process and load the dataset
https://github.com/openAGI/models/blob/master/datasets/imdb.pyimport imdb
train, test, _ = imdb.load_data(path='imdb.pkl', n_words=10000, valid_portion=0.1)
Dataset need to be processed, since it has variable length sequences, we need to use padding
trainX, trainY = train
testX, testY = test trainX = pad_sequences(trainX, maxlen=100, value=0.) testX = pad_sequences(testX, maxlen=100, value=0.) trainY = np.asarray(trainY)
testY = np.asarray(testY)
# use Dataset class from Tefla
data_set = DataSet(trainX, trainY, testX, testY)Here we consider vocabulary size of the dataset as 10000.
2. Define LSTM based RNN model to classify sentences.
You can use normal Conv layers also to build classification models for text data. In this example I’m using lstm.
Tefla has high level layer definitions built on the top of tensorflow for many RNN cell such as LSTM, GRU, Attention etc.
Here we will define a model with one embedding layer, one lstm layer and one final classification layer. The embedding layer projects the input data into an embedding space of feature dimension 128.
In general for deep learning models most of the layers share same arguments. TO avoid writing same parameters for all the layers we define a function to prepare the common layers parameters using
common_layer_argsandmake_args.
Common arguments such as is_training or variable reuse are same for all the layers and can be defined using common_layer_args, Arguments for convolutional/RNN/fully connected layers can be deined on the top of common arguments using make_args function.
def model(x, is_training, reuse, num_classes=2):
common_args = common_layer_args(is_training, reuse)
fc_args = make_args(activation=relu, **common_args)
logit_args = make_args(activation=None, **common_args)
x = embedding(x, 10000, 128, reuse)
x = lstm(x, 34, reuse, is_training)
logits = fc(x, num_classes, name='logits', **logit_args) predictions = softmax(logits, name='predictions', **common_args)
return end_points(is_training)
3. Write the training loop
We also need to define configurations for training
training_cnf = {
'classification': True,
'batch_size_train': 32,
'batch_size_test': 32,
'validation_scores': [('accuracy',tf.metrics.accuracy)],
'num_epochs': 50,
'input_size': (100, ),
'lr_policy': StepDecayPolicy(schedule={
0: 0.01,
30: 0.001,
})
}Finally create the op to train the model
learner = SupervisedLearner(
model,
training_cnf,
classification=training_cnf['classification'],
is_summary=False,
num_classes=2)Train the model
learner.fit(data_set, weights_from=None, start_epoch=1)Thats all, hope you will find Tefla useful for deep learning development.