Keras in Python, Backend TensorFlow, with Iris data to Build Deep Learning Model

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0

We talked about Deep Learning Modeling in TensorFlow in Python&R: https://charleshsliao.wordpress.com/2017/06/06/rnn-in-tensorflow-in-pythonr-with-mnist/
We also mentioned Keras application in R: https://charleshsliao.wordpress.com/2017/04/24/cnndnn-of-keras-in-r-backend-tensorflow-for-mnist/

This article covers the basic application of Keras and TensorFlow in Python3, with Sublime text3 and Ipython Notebook as IDE.
More details of the following code can be found in Robert Layton’s book here: https://www.goodreads.com/book/show/26019855-learning-data-mining-with-python?from_search=true

###This is a basic example for tensorflow in python for some constants, 
#which is a start for Keras in Python
import tensorflow as tf 
import numpy as np
from IPython.display import clear_output, Image, display, HTML

a = tf.constant(5.0)
b = tf.constant(4.5)
c = tf.constant(3.0)
x = tf.Variable(0., name='x')
y = (a * x ** 2) + (b * x) + c

def strip_consts(graph_def, max_const_size=32):
    """Strip large constant values from graph_def."""
    strip_def = tf.GraphDef()
    for n0 in graph_def.node:
        n = strip_def.node.add() 
        n.MergeFrom(n0)
        if n.op == 'Const':
            tensor = n.attr['value'].tensor
            size = len(tensor.tensor_content)
            if size > max_const_size:
                tensor.tensor_content = "<stripped %d bytes>"%size
    return strip_def

def show_graph(graph_def, max_const_size=32):
    """Visualize TensorFlow graph."""
    if hasattr(graph_def, 'as_graph_def'):
        graph_def = graph_def.as_graph_def()
    strip_def = strip_consts(graph_def, max_const_size=max_const_size)
    code = """
        <script>
          function load() {{
            document.getElementById("{id}").pbtxt = {data};
          }}
        </script>
        <link rel="import" href="https://tensorboard.appspot.com/tf-graph-basic.build.html" onload=load()>
        <div style="height:600px">
          <tf-graph-basic id="{id}"></tf-graph-basic>
        </div>
    """.format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))

    iframe = """
        <iframe seamless style="width:1200px;height:620px;border:0" srcdoc="{}"></iframe>
    """.format(code.replace('"', '"'))
    display(HTML(iframe))

show_graph(tf.get_default_graph().as_graph_def())
model = tf.global_variables_initializer()
with tf.Session() as session:
    session.run(model)
    result = session.run(y)
print(result)
###3.0

Screen Shot 2017-06-12 at 1.58.38 PM.png

model = tf.global_variables_initializer()
with tf.Session() as session:
    session.run(model)
    result = session.run(y, {x: 5})
print(result)
#150.5
model = tf.global_variables_initializer()
with tf.Session() as session:
    session.run(model)
    session.run(x.assign(10))
    result = session.run(y)
print(result)
#548.0

###KeRas example with iris data
import numpy as np
from sklearn.datasets import load_iris 
iris = load_iris()
X = iris.data.astype(np.float32) 
y = iris.target.astype(np.int32)
#reshape and convert the data
from sklearn.preprocessing import OneHotEncoder
y_onehot = OneHotEncoder().fit_transform(y.reshape(-1, 1))
y_onehot = y_onehot.astype(np.int64).todense()
print(y_onehot[:5])
#[[1 0 0]
 #[1 0 0]
 #[1 0 0]
 #[1 0 0]
 #[1 0 0]]

After this step, I tried to import Keras package in sublime text3 it failed. It worked perfectly in Ipython, though.
Screen Shot 2017-06-12 at 2.23.22 PM.png

Screen Shot 2017-06-12 at 2.23.31 PM.png

from sklearn.model_selection import train_test_split
from keras.layers import Dense
from keras.models import Sequential
X_train, X_test, y_train, y_test = train_test_split(X, y_onehot, random_state=42)
input_layer_size, hidden_layer_size, output_layer_size = 4, 6, 3
hidden_layer = Dense(output_dim=hidden_layer_size, input_dim=input_layer_size, activation='relu')
output_layer = Dense(output_layer_size, activation='sigmoid')
model = Sequential(layers=[hidden_layer, output_layer])
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])

###Fit the model without epoch
history = model.fit(X_train, y_train)
%matplotlib inline
import seaborn as sns
import ipykernel
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
plt.plot(history.epoch, history.history['loss'])
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.savefig("keras_on_iris_one.png")
plt.show()

Screen Shot 2017-06-12 at 2.32.24 PM.png

from sklearn.metrics import classification_report
y_pred = model.predict_classes(X_test)
print(classification_report(y_true=y_test.argmax(axis=1), y_pred=y_pred))
             #precision    recall  f1-score   support
#
          #0       0.00      0.00      0.00        15
          #1       0.48      1.00      0.65        11
          #2       0.00      0.00      0.00        12
#
#avg / total       0.14      0.29      0.19        38

###Fit the model with epoch
history = model.fit(X_train, y_train, nb_epoch=1000, verbose=False)
plt.figure(figsize=(12, 8))
plt.plot(history.epoch, history.history['loss'])
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.savefig("keras_on_iris_2.png")
plt.show()
y_pred = model.predict_classes(X_test)
print(classification_report(y_true=y_test.argmax(axis=1), y_pred=y_pred))
			#precision    recall  f1-score   support
#
          #0       1.00      1.00      1.00        15
          #1       1.00      0.91      0.95        11
          #2       0.92      1.00      0.96        12
#
#avg / total       0.98      0.97      0.97        38

=”286″ class=”alignnone size-full wp-image-1684″/>
Screen Shot 2017-06-12 at 2.43.54 PM.png

Advertisements

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0