Example #1
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import numpy as np
from PSNN import model, layers, activation

netmodel = model([
    layers.dense(2, activation.sigmoid()),
    layers.dense(1, activation.sigmoid())
])

netmodel.debug = True

netmodel.create(inputs=(3, ))

inputs = np.array([
    [0, 0, 1],  #0
    [0, 0, 0],  #0
    [1, 0, 0],  #1
    [1, 1, 1],  #1
    [0, 1, 0],  #1
    [0, 1, 1]  #1
])

targets = np.array([
    [0],
    [0],
    [1],
    [1],
    [1],
    [1],
Example #2
0
    imgs = glob.glob(directory + "/*.png")
    out = ([], [])
    for loc in imgs:
        img = Image.open(loc).convert('L')  # convert image to 8-bit grayscale
        WIDTH, HEIGHT = img.size
        data = list(img.getdata())  # convert image data to a list of integers
        out[0].append([
            data[offset:offset + WIDTH]
            for offset in range(0, WIDTH * HEIGHT, WIDTH)
        ])
        out[1].append(target)

    return out


netmodel = model()
netmodel.get("PSNN/mnist")

# load images for test
test = np.array(getITArray("mnist/testing", None)[0])

# we dont need train model. The model is already trained
print("try predict")
print("\n")
for sample in test:
    # print image
    print('\n'.join(
        [''.join(['{:4}'.format(item) for item in row]) for row in sample]))
    print("\n")
    # predict with image as inputs
    predict = netmodel.predict(sample)
Example #3
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import numpy as np
from PSNN import model, layers, activation

netmodel = model([
    layers.convolve((2, 2)),
    layers.flatten(),
    layers.dense(1, activation.sigmoid())
])

netmodel.debug = True

netmodel.create(inputs=(3, 3))

inputs = np.array([
    [  # 1
        [1, 0, 1],
        [0, 1, 0],
        [1, 0, 1],
    ],
    [  # 0
        [0, 0, 1],
        [0, 1, 0],
        [1, 0, 0],
    ],
    [  # 0
        [1, 0, 0],
        [0, 1, 0],
        [0, 0, 1],
Example #4
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#use evolutionFit to solve math problem

import numpy as np
from PSNN import model, layers

netmodel = model([layers.dense(1)])

inaccuracy = 1e-14


def mycubic(model):
    # 2*x*x*x + 5*x*x = 10

    x = model.predict([])[0]
    loss = abs(10 - (2 * x * x * x + 5 * x * x))

    return loss


netmodel.create(inputs=(0, ))

drate = 1
lastlost = 0

while True:
    loss = netmodel.evolutionFit(rate=drate,
                                 replication=200,
                                 lossfunc=mycubic,
Example #5
0
def getITArray(directory, target):
  imgs = glob.glob(directory + "/*.png")
  out = ([], [])
  for loc in imgs:
    img = Image.open(loc).convert('L')  # convert image to 8-bit grayscale
    WIDTH, HEIGHT = img.size
    data = list(img.getdata()) # convert image data to a list of integers
    out[0].append([data[offset:offset+WIDTH] for offset in range(0, WIDTH*HEIGHT, WIDTH)])
    out[1].append(target)

  return out

netmodel = model([
  layers.flatten(),
  layers.activation(activation.tanh()),
  layers.dense(30, activation.tanh()),
  layers.dense(20, activation.tanh()),
  layers.dense(10, activation.sigmoid())
])

netmodel.debug = True

netmodel.create(
  inputs=(28,28)
)

# get learn data from images
learn = {
  "inputs": [],
  "targets": []
}