コード例 #1
0
#Simple Recurrent network performing the MNIST classification.
# Pays attention a temporal pattern in the data by processing a sequence of
#subimages in order to make a classification. Eg a 28x28 sample image from
#the dataset can also be processed as a sequence of 28 vectors, each of size
#28.

#Starting with a sample input t1 and through to input t27, the output of the
#model is ignored; only the context from the hidden layer of the previous input
#is saved and used to augment the input of the next sequence. After input t28,
#the classifcation at the output is measured.
#
# SRN model converts each sample to a set of vectors of n_inputs_srn size.
import dataset_mnist
from srn import srn

x, y, xTest, yTest = dataset_mnist.read();
dataset_mnist.show(input=x, labels=y)


#instantiate an object that implements an SRN hypothesis function
#n_inputs_image:  specifies total number of pixels in a single image
#n_inputs_srn : size of a suub-image vector that gets fed to SRN at a given point
#(ensure it divides by 784 without a remainder)

#When processing a 28x28 image, the model will divide it into a sequence of
#n_inputs_image/n_inputs_srn vectors of size n_inputs_srn, and the set of those
#vectors as a sequence.

#layers: list of dictionaries, each specifying the configuration of a layer
#for the SRN model, only two layers are allowed: one to specify the size of
#the hidden layer and the second the output layer.
コード例 #2
0
import dataset_mnist
from sklearn.cluster import KMeans

x, y, _, _ = dataset_mnist.read(selectedLabels=[0, 2, 7], randomShift=False)

h_kmeans = KMeans(n_clusters=3, random_state=0).fit(x)

dataset_mnist.show(input=x,
                   labels=h_kmeans.predict(x),
                   withVisOfVectorSpace=True)
コード例 #3
0
import dataset_mnist
from sklearn.decomposition import PCA

x, y, _, _ = dataset_mnist.read(randomShift=False)

#show raw data
dataset_mnist.show(input=x, labels=y, withVisOfVectorSpace=True)

h_pca = PCA(n_components=500).fit(x)
#h_pca = PCA(n.components=k).fit(x) 1 up to 784 - the higher the value, the better the reconstruction
#take a look at eigenvalues given by the PCA decomposition and decide
#how many components k you need to keep in order to preserve good
#amounts of information about the data. To get most significant k
#components, where k is number of components you want to keep

#Principal components (eigenvectors of the covariance matrix) are given
#in h_pca.components_. Since these components have the same number of
#attributes/dimensions as x, you can visualise them as images

#show principle components
dataset_mnist.show(input=h_pca.components_)
#components is a kxM matrix where k is the number of components and M is
#the dimensionality of the original dataset. Here, k=M=784

print(h_pca.explained_variance_)
#the components are column vectors ordered from one that preserves most
#information to the one that preserves the least of information. To see the
#eigenvalues, which give the variance of the data projected onto PCA components

x_pca = h_pca.transform(x)
#a Nxk matrix, where N is the number of points in the dataset and k is the num components
コード例 #4
0
import dataset_mnist
import numpy as np
from dbn import dbn

x, y, xTest, yTest = dataset_mnist.read(randomShift=False)
h=dbn(n_inputs=784,
      layers=[{'neurons':12, 'activation':'sigmoid'},
              {'neurons':3, 'activation':'softmax'}]
      )
#you cn first do unsupervised learning layer by layer training of
#the hidden layers like so. This treats the hidden layers as a stack
# of Restricted Boltzmann Machines (RBM) (if you remove this, equivalent to MLP model)
h.learn_unsupervised(input=x, alpha=0.01, num_epochs=50)
#unsupervised training can be followed by supervised training of the entire model
h.learn(input=x, labels=y, cost='ce', alpha=0.01, num_epochs=50)

eTrain = h.error(input=x, labels=y)
eTest = h.error(input=xTest, labels=yTest)
print("Training error: %.2f%% " %eTrain)
print("Test error: %.2f%%" % eTest)

#To compute the output of the model
yhatTest = h.output(input=xTest)

#This output is actual output of the network, which you can interpet as the probability of a given neuron being on.
#To see how this output classifies a sample of the data, you can invoke the following function which shows 16 images
#and their labels
dataset_mnist.show(input=xTest, labels=yhatTest)

#Extract missclassified examples from a dataset and inspect them.
xm,ym= h.extract_misclassified_data(input=xTest, labels=yTest)