Class06 Answer:

Enhance iris_model

I enhanced the iris_model, and now my model creation script looks like this

"""
learn_iris_enh.py

This script should should learn from iris.csv and then create files which describe a model.
Later, that model should be served to browsers.
Then, the browsers should use the model to make predictions.
Ref:
blog.fastforwardlabs.com/post/139921712388/hello-world-in-keras-or-scikit-learn-versusa
https://github.com/transcranial/keras-js#usage

Demo:
./keras_tensorflow.bash learn_iris.py
"""

import pandas as pd
import numpy  as np
from keras.models      import Sequential
from keras.layers.core import Dense, Activation

# I should specify params which affect fit().
# I like powers of 2 for these:
batch_size_i = 2   # Smaller is better but slower
epochs_i     = 256 # Larger  is better but slower

iris_df = pd.read_csv('iris.csv', header=None, names=['f1','f2','f3','f4','iristype'])

print('Here is some data I am learning from:')
print(iris_df.head())

xtrain_a = iris_df.values[:, 0:4]
y = iris_df.values[:, 4]

# I should one-hot-encode setosa, virginica, versicolor:
ytrain_l = []
for y_s in y:
  if y_s == 'setosa':
    ytrain_l.append([1,0,0])
  elif y_s == 'virginica':
    ytrain_l.append([0,1,0])
  else:
    ytrain_l.append([0,0,1])

# Keras wants each y-val to be np.array:
ytrain_a_l = [np.array(some_l) for some_l in ytrain_l]
# Keras wants those arrays to be in a np.array:
ytrain_a   = np.array(ytrain_a_l)
# I should use Keras API to create a 2 layer neural network model:
iris_model = Sequential()
iris_model.add(Dense(4, input_shape=(4,)))
iris_model.add(Activation('relu'))

# I should enhance by inserting a hidden layer of 5 neurons:
iris_model.add(Dense(5))
iris_model.add(Activation('relu'))
# Enhancement finished.

iris_model.add(Dense(3))  
iris_model.add(Activation('softmax'))
iris_model.compile(loss='categorical_crossentropy', optimizer='adam')
iris_model.fit(xtrain_a, ytrain_a, batch_size=batch_size_i, epochs=epochs_i)
# Above call should push loss to below 0.10

# It should be able to predict now:
print(iris_model.predict(xtrain_a[5:8]),     ytrain_a[5:8])     # setosa
print(iris_model.predict(xtrain_a[55:58]),   ytrain_a[55:58])   # virginica
print(iris_model.predict(xtrain_a[105:108]), ytrain_a[105:108]) # versicolor
print('The above predictions should be very accurate.')
print('Why? Because I am predicting 9 observations from training data.')

# I should save the iris_model:
# ref:
# https://github.com/transcranial/keras-js#usage
iris_model.save_weights('iris_model.hdf5')
with open('iris_model.json', 'w') as f:
  f.write(iris_model.to_json())
print('iris_model saved as: iris_model.hdf5 and iris_model.json')

# I should run this shell command:
# python encoder.py iris_model.hdf5
#from subprocess import call
#call(["python","encoder.py","iris_model.hdf5"])

# I should create iris_model_weights.buf, iris_model_metadata.json:
import encoder
enc = encoder.Encoder('iris_model.hdf5')
enc.serialize()
enc.save()

'bye'

Class06 Lab


ml4.us About Blog Contact Class01 Class02 Class03 Class04 Class05 Class06 Class07 Class08 Class09 Class10 dan101 Forum Google Hangout Vboxen