def __init__(self, dataset): num_training = dataset["num_training"] num_unlabeled = dataset["num_unlabeled"] if dataset["name"] == 'mnist': data, labels = mnist.read(dataset) elif dataset["name"] == '20news_group': data, labels = news_group.read(dataset) elif dataset["name"] == 'mnist_back_rand': data, labels = mnist_back_rand.read(dataset) super(ExistingSemiSupervisedDataSet, self).__init__(data, labels, num_training, num_unlabeled) if dataset["noise_scale"] > 0: self._add_noise(dataset["noise_scale"])
path = '/home/lance/git/neuralnetwork/data' def mux(number): result = np.zeros(10) result[number] = 1 return result def demux(numbers): highest_index = 0 for i, number in enumerate(numbers): if number > numbers[highest_index]: highest_index = i return highest_index # Images are 28x28 pixels. Reshapen, they are 784x1 training = [(mux(label), image.reshape(784)/255.0) for label, image in mnist.read(path=path, dataset="training")] testing = [(mux(label), image.reshape(784)/255.0) for label, image in mnist.read(path=path, dataset="testing")] nnet = neuralnetwork.load('saves/mnist_net') success = 0 for t, x in training: y = demux(nnet.evaluate(x)) if y == demux(t): success += 1 print 'On training data ' + str(success*100.0 / len(training)) + '% success' success = 0 for t, x in testing: y = demux(nnet.evaluate(x)) if y == demux(t):
result = np.zeros(10) result[number] = 1 return result def demux(numbers): highest_index = 0 for i, number in enumerate(numbers): if number > numbers[highest_index]: highest_index = i return highest_index # Images are 28x28 pixels. Reshapen, they are 784x1 training = [(mux(label), image.reshape(784) / 255.0) for label, image in mnist.read(path=path, dataset="training")] testing = [(mux(label), image.reshape(784) / 255.0) for label, image in mnist.read(path=path, dataset="testing")] nnet = neuralnetwork.NeuralNetwork([784, 30, 10]) ITR = 30 BTC = 3 p = printer.printer(1) for i in range(ITR): error = 0 for t, x in training: for j in range(BTC): error += nnet.learn(x, t, 1) message = 'Iteration ' + str(i) + ', error ' + str(error) p.overwrite(message) p.clear()
def mux(number): result = [0 for i in range(10)] result[number] = 1 return result def demux(numbers): max_index = 0 for i, n in enumerate(numbers): if n > numbers[max_index]: max_index = i return max_index path = "/home/lance/git/nnetevolution/src/data" # Images are 28x28 pixels. Reshapen, they are 784x1 training = [(mux(label), image.reshape(784)/255.0) for label, image in mnist.read(path=path, dataset="training")] testing = [(mux(label), image.reshape(784)/255.0) for label, image in mnist.read(path=path, dataset="testing")] a = neuralnetwork.makeSquareConvolution(2, 14) # pudb.set_trace() b = neuralnetwork.makeSquareConvolution(2, 7) nnet = neuralnetwork.makeNNet(49, 10, 10, act.sigmoid, act.relu) deepnet = neuralnetwork.DeepNet([a, b, nnet]) l = len(training) k = 1 p = printing.printer(1) for t, x in training: deepnet.learn(x, t) p.reprint("iteration: " + str(k) + "/" + str(l))
#1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. # #2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. # #THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE #DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR #SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF #THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import time import numpy as np import data.mnist as mnist from nnet_toolkit import nnet # Load Data features, labels = mnist.read(range(9), dataset='training') tfeatures, tlabels = mnist.read(range(9), dataset='testing') # Initialize Network layers = [ nnet.layer(features.shape[1]), nnet.layer(128, 'sigmoid'), nnet.layer(labels.shape[1], 'sigmoid') ] net = nnet.net(layers, step_size=.1) # Train Network for N epochs N = 50 mini_batch_size = 1000 t = time.time() print "Starting Training..."
# # 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import time import numpy as np import data.mnist as mnist from nnet_toolkit import nnet # Load Data features, labels = mnist.read(range(9), dataset="training") tfeatures, tlabels = mnist.read(range(9), dataset="testing") # Initialize Network layers = [nnet.layer(features.shape[1]), nnet.layer(128, "sigmoid"), nnet.layer(labels.shape[1], "sigmoid")] net = nnet.net(layers, step_size=0.1) # Train Network for N epochs N = 50 mini_batch_size = 1000 t = time.time() print "Starting Training..." for epoch in range(N): # Randomize Features rix = np.random.permutation(features.shape[0])