Beispiel #1
0
# NNet rendering of the neural net in Grokking, p. 159
import numpy as np
import sys
from netExamples.grokking.mnist import \
    images, labels, test_images, test_labels
from nnet import NNet

np.random.seed(1)

batch_size = 100
iterations = 301

nn = NNet(sizes=[784, 100, 10], batch_size=batch_size)
nn.setActivations(['brelu', 'linear'])
nn.setMaskPr({1: 2})
nn.setAlpha(0.001)
nn.scale(0.1)

for j in range(iterations):
    error, correct_cnt = (0.0, 0)
    for i in range(int(len(images) / batch_size)):
        batch_start, batch_end = ((i * batch_size), ((i + 1) * batch_size))
        prediction = nn.learn(images[batch_start:batch_end],
                              labels[batch_start:batch_end])
        # vprint(i, nn, suffix='a', quit=True)
        # vprint(i, nn.dropout_masks[1], suffix='m', quit=True)
        # nn.train(labels[batch_start:batch_end])
        # vprint(i, nn, stage='b', quit=True)

        error += np.sum((labels[batch_start:batch_end] - prediction) ** 2)
        for k in range(batch_size):
Beispiel #2
0
def vprintnn(i, quit=False, suffix=''):
    ll = [layer_0, layer_1, layer_2]
    ww = [weights_0_1, weights_1_2]
    nn = NNet(weights=ww, layerList=ll)
    nn.setAlpha(alpha)
    vprint(i, nn, quit=quit, prefix='gro', suffix=suffix)
Beispiel #3
0
from nnet import NNet
from verbosePrint import vprint
import verbosePrint

np.random.seed(1)

batch_size = 128
iterations = 300

cm = ConvolutionMatrix(rows=9, cols=16, shapes=((28, 28), (3, 3)))
hLen = cm.outputLength()
nn = NNet(sizes=[784, hLen, 10], batch_size=batch_size)
nn.replaceLayer(0, cm)
nn.setActivations(['tanh', 'softmax'])
nn.setMaskPr({1: 2})
nn.setAlpha(2)
nn.scale(0.01, 0)
nn.scale(0.1, 1)

# vprint(0, nn, quit=True)
# params = (test_images, test_labels)
# nn.checkup(*params)
for j in range(0, iterations + 1):
    correct_cnt = 0
    for i in range(int(len(images) / batch_size)):
        batch_start, batch_end = ((i * batch_size), ((i + 1) * batch_size))
        prediction = nn.learn(images[batch_start:batch_end],
                              labels[batch_start:batch_end])
        # vprint(i, nn, suffix='a', quit=True)
        # vprint(i, nn.dropout_masks[1], suffix='m', quit=True)
Beispiel #4
0
from nnet import NNet
from verbosePrint import vp, vprint, vprintnn
import verbosePrint

demo = 6
verbosePrint.vIteration = 0
verbosePrint.stage = ''

vp()

if demo == 4:
    # weights = [[[0.1],
    #             [0.2],
    #             [-0.1]]]
    nn = NNet([[[0.1], [0.2], [-0.1]]])
    nn.setAlpha(0.01)

    datain = np.array([[8.5, 0.65, 1.2]])
    goal = np.array([[1]])
    for i in range(4):
        output = nn.fire(datain)
        print('Goal:    ' + str(goal))
        print(nn)
        nn.learn(datain, goal)

if demo == 6:
    nn = NNet([[[0.5], [0.48], [-0.7]]])
    nn.setAlpha(0.1)

    streetlights = np.array([[1, 0, 1], [0, 1, 1], [0, 0, 1], [1, 1, 1],
                             [0, 1, 1], [1, 0, 1]])
Beispiel #5
0
# NNet rendering of the neural net in Grokking, p. 147
import numpy as np
import sys
from netExamples.grokking.mnist import \
    images, labels, test_images, test_labels

from nnet import NNet
np.random.seed(1)

iterations = 351

nn = NNet(sizes=[784, 40, 10])
nn.setActivations(['relu', 'linear'])
nn.setAlpha(0.005)
nn.scale(0.1)

nn.fire(images[0:1])
nn.checkup(images[0:1], labels[0:1])
# vprint(0, nn)

for j in range(iterations):
    error, correct_cnt = (0.0, 0)
    for i in range(len(images)):
        # vprint(i, nn, quit=True)
        prediction = nn.learn(images[i:i + 1], labels[i:i + 1])
        # vprint(i, nn, suffix='b', quit=True)

        error += np.sum((labels[i:i + 1] - prediction)**2)
        correct_cnt += int(np.argmax(prediction) == \
                           np.argmax(labels[i:i + 1]))
Beispiel #6
0
# NNet rendering of the neural net in Grokking, p. 174
import numpy as np
import sys
from netExamples.grokking.mnist import \
    images, labels, test_images, test_labels
from nnet import NNet

np.random.seed(1)

batch_size = 100
iterations = 301

nn = NNet(sizes=[784, 100, 10], batch_size=batch_size)
nn.setActivations(['tanh', 'softmax'])
nn.setMaskPr({1: 2})
nn.setAlpha(0.2)
nn.scale(0.1)

# vprint(0, nn, quit=True)

for j in range(iterations):
    correct_cnt = 0
    for i in range(int(len(images) / batch_size)):
        batch_start, batch_end = ((i * batch_size), ((i + 1) * batch_size))
        prediction = nn.learn(images[batch_start:batch_end],
                              labels[batch_start:batch_end])
        # vprint(i, nn, suffix='a', quit=True)
        # vprint(i, nn.dropout_masks[1], suffix='m', quit=True)

        for k in range(batch_size):
            correct_cnt += int(
Beispiel #7
0
                correct_cnt += int(np.argmax(layer_2) == \
                                   np.argmax(test_labels[i:i + 1]))
            sys.stdout.write(
                " Test-Err:" + str(error / float(len(test_images)))[0:5]
                + " Test-Acc:" + str(correct_cnt / float(len(test_images)))
            )
            print()

if demo == 82:
    np.random.seed(1)

    iterations = 351

    nn = NNet(sizes=[784, 40, 10])
    nn.setActivations(['brelu', 'linear'])
    nn.setAlpha(0.005)
    nn.scale(0.1)

    nn.fire(images[0:1])
    vprint(0, nn)

    for j in range(iterations):
        error, correct_cnt = (0.0, 0)
        for i in range(len(images)):
            vprint(i, nn, quit=True)
            prediction = nn.learn(images[i:i+1], labels[i:i+1])
            vprint(i, nn, suffix='b', quit=True)

            error += np.sum((labels[i:i+1] - prediction) ** 2)
            correct_cnt += int(np.argmax(prediction) == \
                   np.argmax(test_labels[i:i + 1]))
Beispiel #8
0
import numpy as np
from nnet import NNet
from verbosePrint import vprint

nn = NNet([[[0.5],
            [0.48],
            [-0.7]]])
nn.setAlpha(0.1)

streetlights = np.array([[1, 0, 1],
                         [0, 1, 1],
                         [0, 0, 1],
                         [1, 1, 1],
                         [0, 1, 1],
                         [1, 0, 1]])

walk_vs_stop = np.array([[0, 1, 0, 1, 1, 0]]).T

datain = streetlights[0]  # [1,0,1]
goal_prediction = walk_vs_stop[0]  # equals 0... i.e. "stop"

for iteration in range(40):
    vprint(iteration, '~~~~~~~~~~~ Iteration %d ~~~~~~~~~~~' % iteration)
    error_for_all_lights = 0
    for row_index in range(len(walk_vs_stop)):
        datain = streetlights[row_index:row_index + 1]
        goal_prediction = walk_vs_stop[row_index:row_index + 1]
        prediction = nn.fire(datain)
        # print('Prediction:' + str(prediction))
        vprint(iteration, nn)