Ejemplo n.º 1
0
# import mnist
import skl
import utils

imgs1 = skl.get_imgs_by_number(0)
imgs2 = skl.get_imgs_by_number(3)

strength_matrix = utils.read_pickle('pkl/nn_mnist_jellyfish_0.pkl')

# for label, img in imgs1:
propagated_1 = []
propagated_2 = []
r = range(np.min([len(imgs1), len(imgs2)]))
index = random.choice(r)
for i in range(10000):
    propagated_1.append(
        NeuralNetwork.validate(imgs1[index][1], strength_matrix))
    propagated_2.append(
        NeuralNetwork.validate(imgs2[index][1], strength_matrix))
propagated_1 = np.array(propagated_1)
propagated_2 = np.array(propagated_2)

fig, axes = plt.subplots(2, 1, figsize=(3, 3))
axes[0].hist(propagated_1, bins=50, alpha=.8)
axes[0].hist(propagated_2, bins=50, alpha=.8)
axes[0].tick_params(labelsize=8)
axes[1].tick_params(labelsize=8)
print(propagated_1.mean(), propagated_2.mean())
plt.savefig('./minst_z_distribution.png')
plt.show()
parser = argparse.ArgumentParser()
parser.add_argument('-n', action="store", dest="number")
parser.add_argument('-i',
                    action="store",
                    dest="iterations",
                    default=25000,
                    help="default: 25000")
args = parser.parse_args()
number = int(args.number)
iterations = int(args.iterations)

best_images = utils.read_pickle('best_images.pkl')

pf = lambda x: (1 / (1 + np.exp(-1 * 10 * x)) - .5) * 1.8 + .05
nn = NeuralNetwork(strength_function=pf, image_scale=8)

img = best_images[number]
print('%s' % number)

start_time = datetime.datetime.now()

for i in range(iterations):
    nn.propagate_once(img, gray_max=16)

end_time = datetime.datetime.now()
print('start time:', start_time, 'stop time: ', end_time)

utils.write_pickle(nn.connections_matrix,
                   './pkl/nn_mnist_jellyfish_%s.pkl' % number)
import strengthen_functions

parser = argparse.ArgumentParser()
parser.add_argument('-n', action="store", dest="number")
parser.add_argument('-i',
                    action="store",
                    dest="iterations",
                    default=30000,
                    help="default: 30000")
args = parser.parse_args()
number = int(args.number)
iterations = int(args.iterations)

pf = strengthen_functions.PF80
nn = NeuralNetwork(strength_function=pf,
                   image_scale=8,
                   transmission_history_len=10**4,
                   propagation_depth=1)

imgs = skl.get_imgs_by_number(number)
size = float(len(imgs))
img = np.zeros([8, 8])
for label, i in imgs:
    img += i
img = img / size

start_time = datetime.datetime.now()
for i in range(iterations):
    nn.propagate_once(img, gray_max=16)
end_time = datetime.datetime.now()

print('%s: ' % number, 'start time:', start_time, 'stop time: ', end_time)
Ejemplo n.º 4
0
from nn_mnist_jellyfish import NeuralNetwork
import mnist
import utils
import sys

parser = argparse.ArgumentParser()
parser.add_argument('-n', action="store", dest="mnist_number")
parser.add_argument('-i', action="store", dest="iterations", default=25000, help="default: 25000")
args = parser.parse_args()
mnist_number = int(args.mnist_number)
iterations = int(args.iterations)

# images choice indexes = [181, 100, 333, 100, 3282, 5239, 5070, 893, 2117, 5712]

pf = lambda x: (1 / (1 + np.exp(-1 * 10 * x)) - .5) * 1.8 + .05
nn = NeuralNetwork(strength_function=pf)

imgs = mnist.get_imgs_by_number(mnist_number)
i = random.choice(range(len(imgs)))
img = imgs[i][1]
print('%s: %s' % (mnist_number, i))

start_time = datetime.datetime.now()

plotting_strength = True
if plotting_strength: strength_stats = []

for i in range(iterations):
    nn.propagate_once(img)
    if plotting_strength:
        if i % 10 == 0: strength_stats.append(nn.stats()['strength'])
Ejemplo n.º 5
0
parser.add_argument('-i', action="store", dest="iterations", default=30000)
parser.add_argument('-j', action="store", dest="num", default=-1)
args = parser.parse_args()
iterations = int(args.iterations)
num = int(args.num)

if num >= 0:
    imgs = mnist.get_imgs_by_number(num)
else:
    imgs = mnist.get_imgs_by_number()

strength_matrix_l = [
    utils.read_pickle('pkl/nn_mnist_jellyfish_' + str(i) + '.pkl')
    for i in range(10)
]

correct = .0
trails = .0
# for label, img in imgs[:iterations]:
for label, img in imgs[:iterations]:
    scores_a = np.array([
        NeuralNetwork.validate(img, strength_matrix)
        for strength_matrix in strength_matrix_l
    ])
    if label == random.choice(np.where(scores_a == scores_a.max())[0]):
        # print(scores_a)
        correct += 1
    trails += 1

print(correct / trails)
Ejemplo n.º 6
0
from __future__ import print_function
import numpy as np
import random
import datetime
import argparse
import matplotlib as mpl
mpl.use('Agg', warn=False)
import matplotlib.pyplot as plt
from nn_mnist_jellyfish import NeuralNetwork
import strengthen_functions
import mnist
import utils

pf = strengthen_functions.PF37
nn = NeuralNetwork(propagation_depth=1, strength_function=pf)

imgs = mnist.get_imgs_by_number(2)
stimulus = (imgs[100][1] / 255.).flatten()

for i in range(30000):
    probs = np.random.rand(28, 28).flatten()
    # stimulated = set(np.where(stimulus > probs)[0])
    nn.propagate_once(imgs[100][1])

strength = nn.connections_matrix.flatten()
strength_target = np.array([pf(x) for x in stimulus * strength])
diff = strength_target - strength
print(strength)
print(diff.mean(), diff.std())
parser = argparse.ArgumentParser()
parser.add_argument('-i', action="store", dest="iterations", default=30000)
parser.add_argument('-j', action="store", dest="num", default=-1)
args = parser.parse_args()
iterations = int(args.iterations)
num = int(args.num)

if num >= 0:
    imgs = skl.get_imgs_by_number(num)
else:
    imgs = skl.get_imgs_by_number()

strength_matrix_l = [
    utils.read_pickle('pkl/nn_mnist_jellyfish_' + str(i) + '.pkl')
    for i in range(10)
]

correct = .0
trails = .0
for label, img in imgs[:iterations]:
    scores_a = np.array([
        NeuralNetwork.validate(img, strength_matrix, gray_max=16)
        for strength_matrix in strength_matrix_l
    ])
    if label == random.choice(np.where(scores_a == scores_a.max())[0]):
        # print(scores_a)
        correct += 1
    trails += 1

print(correct / trails)
Ejemplo n.º 8
0
train_number = int(args.train_number)
test_number = int(args.test_number)
method = int(args.method)
train_pickle_file_name = str(args.train_pickle_file_name)
iterations = int(args.iterations)

imgs_train = mnist.get_imgs_by_number(train_number)
imgs_test = mnist.get_imgs_by_number(test_number)
strength_matrix = utils.read_pickle(train_pickle_file_name)

if strength_matrix is None:
    print('cannot find %s' % train_pickle_file_name)
    sys.exit(1)
score = .0
for _ in range(iterations):
    conns_l, strength_l = NeuralNetwork.validate(
        random.choice(imgs_train)[1], strength_matrix)
    if method == 1:
        stats_train = len(conns_l)
    else:
        stats_train = np.array(strength_l).sum()
    conns_l, strength_l = NeuralNetwork.validate(
        random.choice(imgs_test)[1], strength_matrix)
    if method == 1:
        stats_test = len(conns_l)
    else:
        stats_test = np.array(strength_l).sum()
    if stats_train > stats_test:
        score += 1
print('%s %s %s' % (train_number, test_number, score / iterations))
Ejemplo n.º 9
0
parser = argparse.ArgumentParser()
parser.add_argument('-n', action="store", dest="mnist_number")
parser.add_argument('-i',
                    action="store",
                    dest="iterations",
                    default=25000,
                    help="default: 25000")
args = parser.parse_args()
mnist_number = int(args.mnist_number)
iterations = int(args.iterations)

# images choice indexes = [181, 100, 333, 100, 3282, 5239, 5070, 893, 2117, 5712]

pf = lambda x: (1 / (1 + np.exp(-1 * 10 * x)) - .5) * 1.8 + .05
nn = NeuralNetwork(strength_function=pf, image_scale=8)

imgs = skl.get_imgs_by_number(mnist_number)
i = random.choice(range(len(imgs)))
img = imgs[i][1]
print('%s: #%s' % (mnist_number, i))

start_time = datetime.datetime.now()

plotting_strength = True
if plotting_strength: strength_stats = []

for i in range(iterations):
    nn.propagate_once(img, gray_max=16)
    if plotting_strength:
        if i % 10 == 0: strength_stats.append(nn.stats()['strength'])