Tuesday, June 6, 2017

Simple keras trial

Simple keras trial

Overview

By making simple newral network, I try to use keras.



Why keras?

I usually use tensorflow to write newral network. Although it is very good tool for that, the code easily becomes long and seemingly confusing even when the network is simple.
It is said that keras solved the problem. The code becomes short and complex way of variables does not appear.

Code I used

from sklearn import datasets
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.utils import np_utils
import numpy as np

# prepare for data
iris = datasets.load_iris()

features = iris.data
targets = np_utils.to_categorical(iris.target)

# check data
print("explain variables\n{}".format(features[:10]))
print("explained variables\n{}".format(targets[:10]))

# split data into train and test
x_train, x_test, y_train, y_test = train_test_split(features, targets, train_size=0.7)

# set model
model = Sequential()
model.add(Dense(8, input_dim=4))
model.add(Activation('relu'))
model.add(Dense(3, input_dim=8))
model.add(Activation('softmax'))
model.compile(optimizer='SGD', loss='categorical_crossentropy', metrics=['accuracy'])

# train
model.fit(x_train, y_train, nb_epoch=50, batch_size=4)

# predict the labels of test data
pred = model.predict_proba(x_test)
pred_list = pd.Series([np.argmax(value) for value in pred])
true_list = pd.Series([np.argmax(value) for value in y_test])
print(pd.DataFrame({'predict': pred_list,
                   'True_category': true_list}))

Point

data

This time, I used iris data as multi categorical data. The task is to make model to predict iris’s class by 4 explaining variables.
# prepare for data
iris = datasets.load_iris()

features = iris.data
targets = np_utils.to_categorical(iris.target)
sklearn library has some popular and useful data set. iris data is one of those.
By ‘np_utils.to_categorical()’, the explained variable was hot-encoded.

modeling

On this part, I set model, using keras.
# set model
model = Sequential()
model.add(Dense(8, input_dim=4))
model.add(Activation('relu'))
model.add(Dense(3, input_dim=8))
model.add(Activation('softmax'))
model.compile(optimizer='SGD', loss='categorical_crossentropy', metrics=['accuracy'])
The model object by Sequential() adds layers by add() method and each layer is separated into Dense and Activation(in this case).
On the Dense part, we choose the number of nodes which the layer has and input layer’s node number.
On the Activation part, we choose functions to adapt to input’s linear combinations.
So, the model written avobe is as following.
- Input layer has 4 nodes(meaning the data has 4 variables)
- One hidden layer has 8 nodes
- The hidden layer’s each node receives linear combination of the layer one before and applies the activation function
- Output layer has 3 nodes(meaning explained variable has 3 unique value)
- The output layer’s each node receives linear combination of the layer one before and applies the activation function

Summary

Newral network code can be very short by keras. How to write in keras corresponds with newral network graphical model.
We don’t need to use different variable settings depending on data and parameters. This is good point.