Day 134: Sunday Coding

It is hot and sunny today. I now have air conditioning this year. I decided that I had other interesting things to do than to melt outside. I stayed inside most of the day. I have to admit I did not get dressed until mid-afternoon.

And what created this lack of attention to the same Sunday process for months, you ask? Back to some Python coding and writing more machine learning code. So just pounding away on my Apple laptop for hours and hours.

Breakfast was the end of the Einstein bagels (they are good for about three days) and a banana. While I worked on getting my Apple back to top class, the morning and part of the afternoon vanished. First, PyCharm, my goto editor for Python, needs to be upgraded. I then needed to use Anaconda, Python update software, to upgrade all of my Python libraries. Not surprisingly, Anaconda needed an upgrade. Then I discovered that Anaconda would not load the machine learning libraries I wanted to use, keras, to handle the machine learning. This is from the examples, and I am not really ready for keras.  Of course, tensor flow is needed as keras uses tensor flow to perform the process.

I have to load most of the changes one at a time. I do manage to have Anaconda update my science libraries. I finally get to coding.

I harvest from my last big python program from eighteen-months ago my standard header and set-up. I like to use a header with all the system values that name versions and developers set. I also like to printout the versions, and the time a program takes to run. So I harvest all of that.

I find a warning that one of my libraries is out-of-date and growl at Anaconda and fix that too.

The day before I attended a Meet-up via Zoom with Knowledge Mavens that had Darren show us a basic machine learning example of how to make a neural network predict a number from hand-written numbers. I wanted to get that working.

As usual, with Python machine learning, it all went sideways. I could not get the libraries to load that had the examples, mnist is a set of files of images of handwritten digits. I searched Stack Overflow, the source of all knowledge for writing Python on the Internet, and found a hint. Yes, my new version of keras had overwritten mnist with a special keras version. So I found a new example and mixed and matched Darren’s code from yesterday with another example using keras version of mnist. At about 3:30PM, I had a working example.

My example code can predict handwritten digits to 97%. I was happy to get back to Python and faced down all the challenges. I did play with the setting in the neural network and managed better and worse results. My program runs in about thirty seconds and runs ten epochs.

#!/usr/bin/env python
"""
    Copyright 2020 by Michael Wild (alohawild)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

==============================================================================


"""
__author__ = 'michaelwild'
__copyright__ = "Copyright (C) 2020 Michael Wild"
__license__ = "Apache License, Version 2.0"
__version__ = "0.0.1"
__credits__ = "Michael Wild"
__maintainer__ = "Michael Wild"
__email__ = "alohawild@mac.com"
__status__ = "Initial"

from time import process_time
import sys
import numpy as np
import keras
from keras.datasets import mnist
from keras.layers import Dense
from keras.models import Sequential
from keras.preprocessing.image import array_to_img
from keras.utils.vis_utils import plot_model
from keras.utils import np_utils

# ######################## shared ##############################

def run_time(start):
    """
    Just takes in previous time and returns elapsed time
    :param start: start time
    :return: elapsed time
    """
    return process_time() - start


def view_image(img):
    img1 = np.expand_dims(img, 2)
    pil_img = array_to_img(img1)
    pil_img.show()

# ****************************************************************

# =============================================================
# Main program begins here


if __name__ == "__main__":

    begin_time = process_time()

    print("Program Numl.py")
    print("Version ", __version__, " ", __copyright__, " ", __license__)
    print("Running on ", sys.version)
    print("Version numpy     :", np.__version__)
    print("Version keras     :", keras.__version__)

    # Load images from models
    (X_train, y_train), (X_test, y_test) = mnist.load_data()

    # flatten 28*28 images to a 784 vector for each image
    num_pixels = X_train.shape[1] * X_train.shape[2]
    X_train = X_train.reshape((X_train.shape[0], num_pixels)).astype('float32')
    X_test = X_test.reshape((X_test.shape[0], num_pixels)).astype('float32')

    # Normalize the model images.
    train_images = (X_train / 255) - 0.5
    test_images = (X_test / 255) - 0.5

    # Flatten the images.
    train_images = train_images.reshape((-1, 784))
    test_images = test_images.reshape((-1, 784))

    # get labels
    train_labels = np_utils.to_categorical(y_train)
    test_labels = np_utils.to_categorical(y_test)
    num_classes = test_labels.shape[1]

    print(train_images.shape) # (60000, 784)
    print(test_images.shape)  # (10000, 784)

    # Build keras model
    model = Sequential([
        Dense(64, activation='relu', name='Input', input_shape=(784,)),
        Dense(64, activation='relu', name='Hidden1'),
        Dense(64, activation='relu', name='Hidden2'),
        Dense(10, activation='softmax', name='Output'),
    ])

    model_time = process_time()
    print(" ")
    print("Begin modeling...")

    # Compile the model
    model.compile(
        optimizer=keras.optimizers.Adam(),
        loss=keras.losses.categorical_crossentropy,
        metrics=['accuracy'],
    )

    # Train the model.
    model.fit(
        train_images, train_labels,
        validation_data=(test_images, test_labels),
        epochs=10,
        batch_size=200,
        verbose=2
    )
    scores = model.evaluate(test_images, test_labels, verbose=0)
    print("Baseline Error: %.2f%%" % (100-scores[1]*100))

    print(" ")
    print("Model Run time:", run_time(model_time))

    # Predict on the first 5 test images.
    predictions = model.predict(test_images[:5])
    # Print our model's predictions.
    print(" ")
    print("First five values")
    print(np.argmax(predictions, axis=1)) # [7, 2, 1, 0, 4]
    print("Compare the labels")
    # Check our predictions against the ground truths.
    print(test_labels[:5]) # [7, 2, 1, 0, 4]

    print(" ")
    print("Run time:", run_time(begin_time))
    print("...Finished...End of Line")

I then read some of the rules I picked up from the gaming stores to unwind. It is hard to change gears.

Dinner was grilled teriyaki chicken with green beans. I did have to go outside for the grilling–oh my! I served Susie and myself.

I had boiled the chicken before teriyaki sauce and grilling to ensure it was cooked and remove the flame-ups. Often raw chicken with teriyaki sauce is a fire hazard with a burned outside and uncooked in the middle. I avoided that.

I kept some chicken out of the flames and teriyaki and made chicken salad for tomorrow.

I cleaned the kitchen and then poured myself a gin and tonic before writing this. It is the right kind of day for gin and tonic. It is made from Oregon Gin, Canda tonic, and the lime was from Mexico. A perfect liberal drink!

IMG_1301

Today more than four-hundred fifty people in the USA died from the virus, according to reports. The week’s totals are ten percent higher than the previous week.

With all the attacks in Portland and the problems and the terrible losses, I picked The Star Spangle Banner from here in liberal Oregon and Portland Greater Area. This is followed by “American the Beautiful” in the video.

The drink is empty so it is time to stop. Good night!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s