# Overview

On this article, I rewrote the Keras code by tf.keras. From TensorFlow, we can use Keras by tf.keras. But I had never used this. So I checked and it was very simple and easy.# Re-write-target

On the article below, I made deep neural network for classification of iris data by Keras.## Simple keras trial

By making simple newral network, I try to use 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.

From the article, the whole code is as followings. I noticed I forgot to import pandas, one of the necessary libraries, on the article. About that, I’ll fix later.

```
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 pandas as pd
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, epochs=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}))
```

The purpose of this article is to rewrite this by tf.keras. Actually, it is easy and simple.

# Re-write by tf.keras

Rewriting the code by tf.keras is very easy. We need to import the equivalent methods from tensorflow. And that’s it.

About model part, there is nothing you need to change.

```
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Activation
import pandas as pd
import numpy as np
# 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, epochs=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})
```

It is really simple.

## No comments:

## Post a Comment