diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..7487e1f35 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +wandb-debug.log diff --git a/README.md b/README.md index 5af7bedd2..422d267a3 100644 --- a/README.md +++ b/README.md @@ -33,38 +33,37 @@ If you've never used pandas or numpy - they are great tools and I use them heavi ## Installation Before the class, please run the following commands to install the prerequisite code. -### Linux and Mac OS X -#### Install python +### Windows -You can download python from https://www.python.org/downloads/. There are more detailed instructions for windows installation at https://www.howtogeek.com/197947/how-to-install-python-on-windows/. +#### Git -The material should work with python 2 or 3. *On Windows, you need to install thre 64 bit version of python 3.5 or 3.6 in order to install tensorflow*. +Install git: https://git-scm.com/download/win + +#### Anaconda + +Install [anaconda](https://repo.continuum.io/archive/Anaconda3-4.4.0-Windows-x86_64.exe) + +Try running the following from the command prompt: -#### Clone this github repository ``` -git clone https://github.com/lukas/ml-class.git -cd ml-class +python --version ``` -If you get an error message here, most likely you don't have git installed. Go to https://www.atlassian.com/git/tutorials/install-git for intructions on installing git. +You should see something like -#### Install necessary pip libraries ``` -pip install pandas -pip install scikit-learn -pip install tensorflow -pip install keras +Python 3.6.1 :: Anaconda 4.4.0 (64-bit) ``` -#### Install python libraries for optional material +If don't see "Anaconda" in the output, search for "anaconda prompt" from the start menu and enter your command prompt this way. It's also best to use a virtual environment to keep your packages silo'ed. Do so with: + ``` -pip install h5py -pip install flask -pip install scikit-image -pip install scipy -pip install pillow +conda create -n ml-class python=3.6 +activate ml-class ``` +Whenever you start a new terminal, you will need to call `activate ml-class`. + #### Common problems The most common problem is an old version of python. Its easy to have multiple versions of python installed at once and Macs in particular come with a default version of python that is too old to install tensorflow. @@ -77,42 +76,53 @@ python --version If your version is less than 2.7.12, you have a version issue. Try reinstalling python 2. -### Windows - -#### Git - -Install git from https://git-for-windows.github.io/ - -#### Anaconda - -Install anaconda from https://www.anaconda.com/download/#download - -Add Anaconda3 and Anaconda3/Scripts to your path environmental variable - -try running +#### Clone this github repository ``` -python --version +git clone https://github.com/lukas/ml-class.git +cd ml-class ``` -You should see something like +#### libraries ``` -Python 3.6.1 :: Anaconda 4.4.0 (64-bit) +pip install wandb +conda install -c conda-forge scikit-learn +conda install -c conda-forge tensorflow +conda install -c conda-forge keras ``` +### Linux and Mac OS X +#### Install python + +You can download python from https://www.python.org/downloads/. There are more detailed instructions for windows installation at https://www.howtogeek.com/197947/how-to-install-python-on-windows/. + +The material should work with python 2 or 3. *On Windows, you need to install thre 64 bit version of python 3.5 or 3.6 in order to install tensorflow*. + #### Clone this github repository ``` git clone https://github.com/lukas/ml-class.git cd ml-class ``` -#### libraries +If you get an error message here, most likely you don't have git installed. Go to https://www.atlassian.com/git/tutorials/install-git for intructions on installing git. +#### Install necessary pip libraries ``` -conda install -c conda-forge scikit-learn -conda install -c conda-forge tensorflow -conda install -c conda-forge keras +pip install wandb +pip install pandas +pip install scikit-learn +pip install tensorflow +pip install keras +``` + +#### Install python libraries for optional material +``` +pip install h5py +pip install flask +pip install scikit-image +pip install scipy +pip install pillow ``` @@ -192,16 +202,16 @@ Order of presentation of files, if you want to follow along - keras-scikit-learn.py - keras-one-hot.py - log-loss.py -- keras-perceptron-1.py -- keras-perceptron-2.py -- keras-perceptron-3.py -- keras-perceptron-4.py -- keras-perceptron-checkpoint.py -- keras-perceptron-save.py -- keras-perceptron-load.py -- keras-perceptron-regression.py -- keras-mlp.py -- keras-dropout.py +- keras-perceptron/perceptron-1.py +- keras-perceptron/perceptron-2.py +- keras-perceptron/perceptron-3.py +- keras-perceptron/perceptron-4.py +- keras-perceptron/perceptron-checkpoint.py +- keras-perceptron/perceptron-save.py +- keras-perceptron/perceptron-load.py +- keras-perceptron/perceptron-regression.py +- keras-mlp/mlp.py +- keras-mlp/dropout.py ### tensorflow - tensorflow-mult.py @@ -214,9 +224,9 @@ Order of presentation of files, if you want to follow along ### conv neural nets - convolution-demo.py - maxpool-demo.py -- keras-cnn-1.py -- keras-cnn-2.py -- keras-cnn-inspect.py +- keras-cnn/cnn-1.py +- keras-cnn/cnn-2.py +- keras-cnn/cnn-inspect.py ### deep dream - keras-deep-dream.py diff --git a/keras-cnn-2.py b/keras-cnn-2.py deleted file mode 100644 index a831a327a..000000000 --- a/keras-cnn-2.py +++ /dev/null @@ -1,41 +0,0 @@ -from keras.datasets import mnist -from keras.models import Sequential -from keras.layers import Conv2D, MaxPooling2D, Dropout, Dense, Flatten -from keras.utils import np_utils - -(X_train, y_train), (X_test, y_test) = mnist.load_data() - -img_width=28 -img_height=28 - -X_train = X_train.astype('float32') -X_train /= 255. -X_test = X_test.astype('float32') -X_test /= 255. - -#reshape input data -X_train = X_train.reshape(X_train.shape[0], img_width, img_height, 1) -X_test = X_test.reshape(X_test.shape[0], img_width, img_height, 1) - -# one hot encode outputs -y_train = np_utils.to_categorical(y_train) -y_test = np_utils.to_categorical(y_test) -num_classes = y_test.shape[1] - -# build model - -model = Sequential() - -model.add(Conv2D(8, (5, 5), input_shape=(img_width, img_height,1), activation='relu')) -model.add(MaxPooling2D(pool_size=(2, 2))) -model.add(Conv2D(16, (5, 5), input_shape=(img_width, img_height,1), activation='relu')) -model.add(MaxPooling2D(pool_size=(2, 2))) -model.add(Dropout(0.2)) -model.add(Flatten()) -model.add(Dense(32, activation='relu')) -model.add(Dense(num_classes, activation='softmax')) - -model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) -model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=1) - -model.save("convnet.h5") diff --git a/keras-cnn-wandb.py b/keras-cnn/cnn-1.py similarity index 76% rename from keras-cnn-wandb.py rename to keras-cnn/cnn-1.py index fb0f7d31c..a2ecddb0f 100644 --- a/keras-cnn-wandb.py +++ b/keras-cnn/cnn-1.py @@ -8,18 +8,8 @@ run = wandb.init() config = run.config - (X_train, y_train), (X_test, y_test) = mnist.load_data() -config = wandb.run.config -config.first_layer_convs = 32 -config.first_layer_conv_width = 3 -config.first_layer_conv_height = 3 -config.dropout = 0.2 -config.dense_layer_size = 128 -config.img_width=28 -config.img_height=28 - X_train = X_train.astype('float32') X_train /= 255. X_test = X_test.astype('float32') @@ -29,14 +19,12 @@ X_train = X_train.reshape(X_train.shape[0], config.img_width, config.img_height, 1) X_test = X_test.reshape(X_test.shape[0], config.img_width, config.img_height, 1) - # one hot encode outputs y_train = np_utils.to_categorical(y_train) y_test = np_utils.to_categorical(y_test) num_classes = y_test.shape[1] # build model - model = Sequential() model.add(Conv2D(32, (config.first_layer_conv_width, config.first_layer_conv_height), @@ -48,5 +36,5 @@ model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) -model.fit(X_train, y_train, validation_data=(X_test, y_test), - callbacks=[WandbKerasCallback()], epochs=config.epochs) +model.fit(X_train, y_train, validation_data=(X_test, y_test), + callbacks=[WandbKerasCallback()], epochs=config.epochs) \ No newline at end of file diff --git a/keras-cnn-1.py b/keras-cnn/cnn-2.py similarity index 56% rename from keras-cnn-1.py rename to keras-cnn/cnn-2.py index 6bba883db..20fc0cb34 100644 --- a/keras-cnn-1.py +++ b/keras-cnn/cnn-2.py @@ -2,42 +2,41 @@ from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D, Dropout, Dense, Flatten from keras.utils import np_utils - from wandb.wandb_keras import WandbKerasCallback import wandb run = wandb.init() config = run.config - (X_train, y_train), (X_test, y_test) = mnist.load_data() -img_width=28 -img_height=28 - X_train = X_train.astype('float32') X_train /= 255. X_test = X_test.astype('float32') X_test /= 255. #reshape input data -X_train = X_train.reshape(X_train.shape[0], img_width, img_height, 1) -X_test = X_test.reshape(X_test.shape[0], img_width, img_height, 1) +X_train = X_train.reshape(X_train.shape[0], config.img_width, config.img_height, 1) +X_test = X_test.reshape(X_test.shape[0], config.img_width, config.img_height, 1) # one hot encode outputs y_train = np_utils.to_categorical(y_train) y_test = np_utils.to_categorical(y_test) num_classes = y_test.shape[1] -# build model - model = Sequential() -model.add(Conv2D(32, (3, 3), input_shape=(img_width, img_height,1), activation='relu')) +model.add(Conv2D(8, (5, 5), input_shape=(config.img_width, config.img_height,1), activation='relu')) +model.add(MaxPooling2D(pool_size=(2, 2))) +model.add(Conv2D(16, (5, 5), input_shape=(config.img_width, config.img_height,1), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.2)) model.add(Flatten()) -model.add(Dense(128, activation='relu')) +model.add(Dense(32, activation='relu')) model.add(Dense(num_classes, activation='softmax')) -model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) -model.fit(X_train, y_train) +model.compile(loss='categorical_crossentropy', optimizer='adam', + metrics=['accuracy']) +model.fit(X_train, y_train, validation_data=(X_test, y_test), + callbacks=[WandbKerasCallback()], epochs=config.epochs) + + diff --git a/keras-cnn-inspect.py b/keras-cnn/cnn-inspect.py similarity index 100% rename from keras-cnn-inspect.py rename to keras-cnn/cnn-inspect.py diff --git a/keras-cnn/config-defaults.yaml b/keras-cnn/config-defaults.yaml new file mode 100644 index 000000000..3ae0b38a3 --- /dev/null +++ b/keras-cnn/config-defaults.yaml @@ -0,0 +1,26 @@ +wandb_version: 1 + +first_layer_convs: + desc: Number of convolutions in the first layer + value: 32 +first_layer_conv_width: + desc: The width of the convolution + value: 3 +first_layer_conv_height: + desc: The height of the convolution + value: 3 +dropout: + desc: Percentage of neurons to dropout + value: 0.2 +dense_layer_size: + desc: The size of the dense layer + value: 128 +img_width: + desc: The width of the input image + value: 28 +img_height: + desc: The height of the input image + value: 28 +epochs: + desc: The number of epochs to run + value: 10 diff --git a/keras-cnn/wandb/settings b/keras-cnn/wandb/settings new file mode 100644 index 000000000..6f98177a5 --- /dev/null +++ b/keras-cnn/wandb/settings @@ -0,0 +1,3 @@ +[default] +entity: qualcomm +project: cnn diff --git a/keras-mlp.py b/keras-mlp.py deleted file mode 100644 index b468deeb5..000000000 --- a/keras-mlp.py +++ /dev/null @@ -1,35 +0,0 @@ -import numpy -from keras.datasets import mnist -from keras.models import Sequential -from keras.layers import Dense -from keras.layers import Flatten - -from keras.layers import Dropout -from keras.utils import np_utils - -# load data -(X_train, y_train), (X_test, y_test) = mnist.load_data() -img_width = X_train.shape[1] -img_height = X_train.shape[2] - -X_train = X_train.astype('float32') -X_train /= 255. -X_test = X_test.astype('float32') -X_test /= 255. - -# one hot encode outputs -y_train = np_utils.to_categorical(y_train) -num_classes = y_train.shape[1] - -y_test = np_utils.to_categorical(y_test) - -# create model -model=Sequential() -model.add(Flatten(input_shape=(img_width,img_height))) -model.add(Dense(100, activation='relu')) -model.add(Dense(num_classes, activation='softmax')) -model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) - -# Fit the model -model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10) -model.save('two-layer.h5') diff --git a/keras-mlp/config-defaults.yaml b/keras-mlp/config-defaults.yaml new file mode 100644 index 000000000..398164ed7 --- /dev/null +++ b/keras-mlp/config-defaults.yaml @@ -0,0 +1,11 @@ +wandb_version: 1 + +# Example variables below. Uncomment (remove leading '# ') to use them, or just +# delete and create your own. + +epochs: + desc: Number of epochs to train over + value: 10 +hidden_nodes: + desc: Size of the hidden layer + value: 100 diff --git a/keras-dropout.py b/keras-mlp/dropout.py similarity index 75% rename from keras-dropout.py rename to keras-mlp/dropout.py index be4f6ec28..0b9ec6c61 100644 --- a/keras-dropout.py +++ b/keras-mlp/dropout.py @@ -7,6 +7,12 @@ from keras.layers import Dropout from keras.utils import np_utils +from wandb.wandb_keras import WandbKerasCallback +import wandb + +run = wandb.init() +config = run.config + # load data (X_train, y_train), (X_test, y_test) = mnist.load_data() img_width = X_train.shape[1] @@ -27,10 +33,11 @@ model=Sequential() model.add(Flatten(input_shape=(img_width,img_height))) model.add(Dropout(0.9)) -model.add(Dense(30, activation='relu')) +model.add(Dense(config.hidden_nodes, activation='relu')) model.add(Dropout(0.4)) model.add(Dense(num_classes, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # Fit the model -model.fit(X_train, y_train, validation_data=(X_test, y_test)) +model.fit(X_train, y_train, validation_data=(X_test, y_test), + callbacks=[WandbKerasCallback()], epcohs=config.epochs) \ No newline at end of file diff --git a/keras-mlp-wandb.py b/keras-mlp/mlp.py similarity index 86% rename from keras-mlp-wandb.py rename to keras-mlp/mlp.py index b504c43bc..e08fc6080 100644 --- a/keras-mlp-wandb.py +++ b/keras-mlp/mlp.py @@ -40,5 +40,5 @@ model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # Fit the model -model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=config.epochs) -model.save('two-layer.h5') +model.fit(X_train, y_train, validation_data=(X_test, y_test), callbacks=[WandbKerasCallback()], epochs=config.epochs) +model.save(run.dir+'/two-layer.h5') diff --git a/keras-mlp/wandb/settings b/keras-mlp/wandb/settings new file mode 100644 index 000000000..cfeb948f2 --- /dev/null +++ b/keras-mlp/wandb/settings @@ -0,0 +1,3 @@ +[default] +entity: qualcomm +project: mlp diff --git a/keras-perceptron/config-defaults.yaml b/keras-perceptron/config-defaults.yaml new file mode 100644 index 000000000..80374dce0 --- /dev/null +++ b/keras-perceptron/config-defaults.yaml @@ -0,0 +1,14 @@ +wandb_version: 1 + +batch_size: + desc: Size of each mini-batch + value: 32 +img_width: + desc: The width of the input image + value: 28 +img_height: + desc: The height of the input image + value: 28 +epochs: + desc: The number of epochs to run + value: 10 diff --git a/keras-perceptron-1.py b/keras-perceptron/perceptron-1.py similarity index 100% rename from keras-perceptron-1.py rename to keras-perceptron/perceptron-1.py diff --git a/keras-perceptron-2.py b/keras-perceptron/perceptron-2.py similarity index 100% rename from keras-perceptron-2.py rename to keras-perceptron/perceptron-2.py diff --git a/keras-perceptron-3.py b/keras-perceptron/perceptron-3.py similarity index 100% rename from keras-perceptron-3.py rename to keras-perceptron/perceptron-3.py diff --git a/keras-perceptron-4.py b/keras-perceptron/perceptron-4.py similarity index 96% rename from keras-perceptron-4.py rename to keras-perceptron/perceptron-4.py index 6d73ab4e1..e66e55067 100644 --- a/keras-perceptron-4.py +++ b/keras-perceptron/perceptron-4.py @@ -9,6 +9,9 @@ from keras.layers import Dropout from keras.utils import np_utils from keras.optimizers import SGD +import wandb +run = wandb.init() + # load data (X_train, y_train), (X_test, y_test) = mnist.load_data() img_width = X_train.shape[1] diff --git a/keras-perceptron-checkpoint.py b/keras-perceptron/perceptron-checkpoint.py similarity index 100% rename from keras-perceptron-checkpoint.py rename to keras-perceptron/perceptron-checkpoint.py diff --git a/keras-perceptron-load.py b/keras-perceptron/perceptron-load.py similarity index 99% rename from keras-perceptron-load.py rename to keras-perceptron/perceptron-load.py index 074095253..d55d45c32 100644 --- a/keras-perceptron-load.py +++ b/keras-perceptron/perceptron-load.py @@ -2,7 +2,6 @@ from keras.models import load_model from keras.utils import np_utils - (X_train, y_train), (X_test, y_test) = mnist.load_data() model = load_model("perceptron.h5") diff --git a/keras-perceptron-wandb.py b/keras-perceptron/perceptron-log.py similarity index 89% rename from keras-perceptron-wandb.py rename to keras-perceptron/perceptron-log.py index 5ce808c92..05b5a90b1 100644 --- a/keras-perceptron-wandb.py +++ b/keras-perceptron/perceptron-log.py @@ -16,7 +16,6 @@ run = wandb.init() config = run.config - # load data (X_train, y_train), (X_test, y_test) = mnist.load_data() img_width = X_train.shape[1] @@ -41,25 +40,15 @@ model.add(Flatten(input_shape=(img_width,img_height))) model.add(Dense(num_classes, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam') +model.summary() -print("Running") # Fit the model history = model.fit(X_train, y_train, epochs=config.epochs, batch_size=config.batch_size, validation_data=(X_test, y_test), callbacks=[tensorboard, WandbKerasCallback()]) -print("Done") -print(history.history) - - -with open('history.json', 'w') as outfile: - json.dump(history.history, outfile) - # Final evaluation of the model scores = model.evaluate(X_test, y_test, verbose=0) with open('metrics.json', 'w') as outfile: json.dump(scores, outfile) - - -model.save("model.h5") diff --git a/keras-perceptron-normalize.py b/keras-perceptron/perceptron-normalize.py similarity index 100% rename from keras-perceptron-normalize.py rename to keras-perceptron/perceptron-normalize.py diff --git a/keras-perceptron-parallel.py b/keras-perceptron/perceptron-parallel.py similarity index 100% rename from keras-perceptron-parallel.py rename to keras-perceptron/perceptron-parallel.py diff --git a/keras-perceptron-predict.py b/keras-perceptron/perceptron-predict.py similarity index 100% rename from keras-perceptron-predict.py rename to keras-perceptron/perceptron-predict.py diff --git a/keras-perceptron-regression.py b/keras-perceptron/perceptron-regression.py similarity index 100% rename from keras-perceptron-regression.py rename to keras-perceptron/perceptron-regression.py diff --git a/keras-perceptron-save.py b/keras-perceptron/perceptron-save.py similarity index 100% rename from keras-perceptron-save.py rename to keras-perceptron/perceptron-save.py diff --git a/keras-perceptron/wandb/settings b/keras-perceptron/wandb/settings new file mode 100644 index 000000000..5ec79be30 --- /dev/null +++ b/keras-perceptron/wandb/settings @@ -0,0 +1,3 @@ +[default] +entity: qualcomm +project: perceptron