Skip to content

Instantly share code, notes, and snippets.

@s2t2
Last active December 10, 2018 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s2t2/ca70d8980c3e898bb335f24ccb119271 to your computer and use it in GitHub Desktop.
Save s2t2/ca70d8980c3e898bb335f24ccb119271 to your computer and use it in GitHub Desktop.
keras setup

Getting started with Machine Learning in Python

Configuring Local Dev Environment

Install anaconda from source: https://www.anaconda.com/download/#macos then restart terminal and check to see if it is installed:

conda --version #> conda 4.5.11
which conda #> /anaconda3/bin/conda

Create a new virtual environment, named something like ml-env-1, with the necessary python packages:

conda create -n ml-env-1 python=3.6 # installing packages here seems to not work, also version 3.7 not yet supported by tensorflow, so use python 3.6
conda env list #> you should see your new environment included

Enter the virtual environemnt:

conda activate ml-env-2  # ... to deactivate: conda deactivate

which python #> /anaconda3/envs/ml-env-2/bin/python
python --version #> Python 3.6.7 :: Anaconda, Inc.

which pip #> /anaconda3/envs/ml-env-2/bin/pip
pip --version #> pip 18.1 from /anaconda3/envs/ml-env-2/lib/python3.6/site-packages/pip (python 3.6)

Install package dependencies inside the virtual environment:

pip install keras tensorflow

pip list #> should see both, with supporting packages

Creating a Model

python my_model.py

Credits, notes and reference

Python / General

Anaconda

Keras

Tensorflow

Datasets

MNIST Handwritten Digits

Numpy / Scipy

N-dimensional Data arrays:

Matplotlib

import pdb
from keras.models import Sequential
from keras.datasets import mnist
# from matplotlib import pyplot as plt #> ImportError: Python is not installed as a framework....
# h/t: https://stackoverflow.com/questions/21784641/installation-issue-with-matplotlib-python#comment56913201_21789908
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
print("--------------------")
print("LOADING DATA...")
print("--------------------")
# h/t: https://keras.io/datasets/#mnist-database-of-handwritten-digits
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(f" + TRAINING/INPUTS: {type(x_train)} {x_train.shape} {x_train.dtype}") #> <class 'numpy.ndarray'> (60000, 28, 28) uint8
print(f" + TRAINING/OUTPUTS: {type(y_train)} {y_train.shape} {y_train.dtype}") #> <class 'numpy.ndarray'> (60000,) uint8
print(f" + TESTING/INPUTS: {type(x_test)} {x_test.shape} {x_test.dtype}") #> <class 'numpy.ndarray'> (10000, 28, 28) uint8
print(f" + TESTING/OUTPUTS: {type(y_test)} {y_test.shape} {y_test.dtype}") #> <class 'numpy.ndarray'> (10000,) uint8
print("--------------------")
print("EXAMPLE TRAINING DATA INAGES...")
print("--------------------")
# h/t: https://topicfly.io/classify-mnist-digits-keras/
plt.subplot(221)
plt.imshow(x_train[0], cmap=plt.get_cmap('gray'))
plt.subplot(222)
plt.imshow(x_train[1], cmap=plt.get_cmap('gray'))
plt.subplot(223)
plt.imshow(x_train[2], cmap=plt.get_cmap('gray'))
plt.subplot(224)
plt.imshow(x_train[3], cmap=plt.get_cmap('gray'))
plt.show() # h/t: https://stackoverflow.com/a/35085705/670433
#pdb.set_trace()
print("--------------------")
print("PROCESSING DATA...")
print("--------------------")
# todo
print("--------------------")
print("CREATING MODEL...")
print("--------------------")
model = Sequential()
print(type(model)) #> <class 'keras.engine.sequential.Sequential'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment