Keras Hello World using Azure Notebooks
I was looking for a simple way to learn Keras and wanted to have a interface which is easy and simple. There are options of using Data Science VM in azure or Jupyter notebook in local machine or a VM. There is also Azure data bricks and also Azure machine learning studio. But there is also a new azure note books available now. Azure machine learning has so many options from some to start and go all the way to be an expert.
My intention here is not to explain the science but to show to get start with hello world. There are lots of documentation on the web to read about commands and what each does.
Azure note books are very simple online based jupyter notebook. I have been using azure notebooks for a while as beta tester or early adaptor but now is it available to every one.
The reason I like it because web based interface and all the dependencies are loaded already. So I decided to do a hello world sample using keras using azure notebooks.
Lets get to do programming now. Let’s go to https://notebooks.azure.com and log in with your Microsoft live id. Once you login it takes you to home page. There lets create a sample project. Once project is created go into the project folder.
Click on New and then select notebook. Azure notebook allows us to program in python 2.7,3.5,3.6 and R and F#. Specify the name of the notebook and will take you to jupyter notebook interface. It does take few minutes to get the kernel up and running. Once the kernel is ready we are ready to do the coding. Default the notebook will run in free computer but you can also give a remote compute IP as well. There is also option to upload your existing jupyter notebook from local computer and other web source.
Let’s go to first cell and import a keras existing data set to test.
from keras.datasets import imdb
top_words = 10000
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=top_words)
Run the cell to setup the environment.
Now on the next cell lets type in x_train[0] and execute the cell and should display the data.
go the a new cell and type
imdb.get_word_index()
the above command should download imdb work index file. These data sources are available for free.
Create a new cell and type:
word_dict = imdb.get_word_index()
word_dict = { key:(value + 3) for key, value in word_dict.items() }
word_dict[‘’] = 0 # Padding
word_dict[‘>’] = 1 # Start
word_dict[‘?’] = 2 # Unknown word
reverse_word_dict = { value:key for key, value in word_dict.items() }
print(‘ ‘.join(reverse_word_dict[id] for id in x_train[0]))
the above will start getting the data ready for keras model. Run the cell.
create a new cell to Set the train and test data set and type:
from keras.preprocessing import sequence
max_review_length = 500
x_train = sequence.pad_sequences(x_train, maxlen=max_review_length)
x_test = sequence.pad_sequences(x_test, maxlen=max_review_length)
run the cell.
Create a new cell and type:
from keras.models import Sequential
from keras.layers import Dense
from keras.layers.embeddings import Embedding
from keras.layers import Flatten
embedding_vector_length = 32
model = Sequential()
model.add(Embedding(top_words, embedding_vector_length, input_length=max_review_length))
model.add(Flatten())
model.add(Dense(16, activation=’relu’))
model.add(Dense(16, activation=’relu’))
model.add(Dense(1, activation=’sigmoid’))
model.compile(loss=’binary_crossentropy’,optimizer=’adam’, metrics=[‘accuracy’])
print(model.summary())
The above code is going to create a simple Deep neural network using keras for text data. It is just a 4 layer deep neural network. Run the cell to get setup the necessary for the keras model.
Now it is time to run the model. Create a new cell and type:
hist = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=5, batch_size=128)
Run this cell and the model will start the training process. This part should take some time to complete depending the number of epochs. in our case it is only 5 and might take few minutes.
Now that we ran the model it is now ready to test the model and see the performance of the model output. So create a new cell and type:
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
sns.set()
acc = hist.history[‘acc’]
val = hist.history[‘val_acc’]
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, ‘-’, label=’Training accuracy’)
plt.plot(epochs, val, ‘:’, label=’Validation accuracy’)
plt.title(‘Training and Validation Accuracy’)
plt.xlabel(‘Epoch’)
plt.ylabel(‘Accuracy’)
plt.legend(loc=’upper left’)
plt.plot()
Execute the above command and see the chart.
That’s it you are set to execute keras in Azure notebooks.
The beauty of Azure notebooks is i didn’t have to install anything and worry about dependencies to get me to start learning. I was able to login and just create a notebook and libraries needed are available for keras model which makes it easier to start to do coding. There are options to download the file and also clone from github as well.