from tensorflow.keras.datasets import mnist
import random
import matplotlib.pyplot as plt
import numpy as np

traindata, testdata = mnist.load_data()

images = traindata[0]
labels = traindata[1]

def show_image(img):
    plt.imshow(img, cmap=plt.cm.binary)
    plt.show()
  
def random_subset(number, xs, labels):
    n = len(xs)
    indices = list(range(n))
    random.shuffle(indices)
    indices_to_grab = indices[:number]
    return xs[indices_to_grab], labels[indices_to_grab]

def images_to_feature_vectors(images):
    images = images.reshape(len(images), 28*28)
    images = images/255
    return list(map(list,images))

def labels_to_feature_vectors(labels):
    fvs = []
    for label in labels:
        fv = [1.0 if i == label else -1 for i in range(9)]
        fvs.append(fv)
    return fvs

def make_cat_labels(labels, target):
    return [ 1.0 if label == target else -1.0 for label in labels]

sub_images, sub_labels = random_subset(6000, images, labels)

train_input_vecs = images_to_feature_vectors(sub_images[:5000])
train_label_vecs = make_cat_labels(sub_labels[:5000], 5)

test_input_vecs = images_to_feature_vectors(sub_images[5000:])
test_label_vecs = make_cat_labels(sub_labels[5000:], 5)


#-----------------------------------------------------------------
# Example "layer" calculations

def apply_weights(x, w, b):
    # x is a single example, w is a weight matrix, b is bias vector
    return np.dot(w, x) + b

def percept_act(x):
    x1 = x.copy()
    x1[x>=0.0] = 1.0
    x1[x<0.0] = -1.0
    return x1

def soft_max(x):
    exps = np.exp(x)
    return exps/sum(exps)

def example_layer_calculation(x, w, b):
    return soft_max(apply_weights(x, w, b)
