pypy / demo /

The branch 'array_equal' does not exist.
#!/usr/bin/env python
    Translator Demo

    To analyse and type-annotate the functions and class defined in
    this module, starting from the entry point function demo(),
    use the following command line:


    Insert '--help' before '' for a list of translation options,
    or see the Overview of Command Line Options for translation at
# Back-Propagation Neural Networks
# Written in Python.  See
# Neil Schemenauer <>
# Modifications to the original (Armin Rigo):
#   * import random from PyPy's lib, which is Python 2.2's plain
#     Python implementation
#   * print a doc about how to start the Translator

import sys
import math
import time

import autopath
from pypy.rlib import rrandom


random = rrandom.Random(1)

# calculate a random number where:  a <= rand < b
def rand(a, b):
    return (b-a)*random.random() + a

# Make a matrix (we could use NumPy to speed this up)
def makeMatrix(I, J, fill=0.0):
    m = []
    for i in range(I):
    return m

class NN:
    def __init__(self, ni, nh, no):
        # number of input, hidden, and output nodes = ni + 1 # +1 for bias node
        self.nh = nh = no

        # activations for nodes = [1.0]*
        self.ah = [1.0]*self.nh = [1.0]*
        # create weights
        self.wi = makeMatrix(, self.nh)
        self.wo = makeMatrix(self.nh,
        # set them to random vaules
        for i in range(
            for j in range(self.nh):
                self.wi[i][j] = rand(-2.0, 2.0)
        for j in range(self.nh):
            for k in range(
                self.wo[j][k] = rand(-2.0, 2.0)

        # last change in weights for momentum   = makeMatrix(, self.nh) = makeMatrix(self.nh,

    def update(self, inputs):
        if len(inputs) !=
            raise ValueError, 'wrong number of inputs'

        # input activations
        for i in range(
  [i] = 1.0/(1.0+math.exp(-inputs[i]))
  [i] = inputs[i]

        # hidden activations
        for j in range(self.nh):
            sum = 0.0
            for i in range(
                sum = sum +[i] * self.wi[i][j]
            self.ah[j] = 1.0/(1.0+math.exp(-sum))

        # output activations
        for k in range(
            sum = 0.0
            for j in range(self.nh):
                sum = sum + self.ah[j] * self.wo[j][k]
  [k] = 1.0/(1.0+math.exp(-sum))


    def backPropagate(self, targets, N, M):
        if len(targets) !=
            raise ValueError, 'wrong number of target values'

        # calculate error terms for output
        output_deltas = [0.0] *
        for k in range(
            ao =[k]
            output_deltas[k] = ao*(1-ao)*(targets[k]-ao)

        # calculate error terms for hidden
        hidden_deltas = [0.0] * self.nh
        for j in range(self.nh):
            sum = 0.0
            for k in range(
                sum = sum + output_deltas[k]*self.wo[j][k]
            hidden_deltas[j] = self.ah[j]*(1-self.ah[j])*sum

        # update output weights
        for j in range(self.nh):
            for k in range(
                change = output_deltas[k]*self.ah[j]
                self.wo[j][k] = self.wo[j][k] + N*change + M*[j][k]
      [j][k] = change
                #print N*change, M*[j][k]

        # update input weights
        for i in range(
            for j in range(self.nh):
                change = hidden_deltas[j]*[i]
                self.wi[i][j] = self.wi[i][j] + N*change + M*[i][j]
      [i][j] = change

        # calculate error
        error = 0.0
        for k in range(len(targets)):
            delta = targets[k][k]
            error = error + 0.5*delta*delta
        return error

    def test(self, patterns):
        for p in patterns:
            if PRINT_IT:
                print p[0], '->', self.update(p[0])

    def weights(self):
        if PRINT_IT:
            print 'Input weights:'
            for i in range(
                print self.wi[i]
            print 'Output weights:'
            for j in range(self.nh):
                print self.wo[j]

    def train(self, patterns, iterations=2000, N=0.5, M=0.1):
        # N: learning rate
        # M: momentum factor
        for i in xrange(iterations):
            error = 0.0
            for p in patterns:
                inputs = p[0]
                targets = p[1]
                error = error + self.backPropagate(targets, N, M)
            if PRINT_IT and i % 100 == 0:
                print 'error', error

def demo():
    # Teach network XOR function
    pat = [
        [[0,0], [0]],
        [[0,1], [1]],
        [[1,0], [1]],
        [[1,1], [0]]

    # create a network with two input, two hidden, and two output nodes
    n = NN(2, 3, 1)
    # train it with some patterns
    n.train(pat, 2000)
    # test it

# __________  Entry point for stand-alone builds __________

import time

def entry_point(argv):
    if len(argv) > 1:
        N = int(argv[1])
        N = 200
    T = time.time()
    for i in range(N):
    t1 = time.time() - T
    print "%d iterations, %s milliseconds per iteration" % (N, 1000.0*t1/N)
    return 0

# _____ Define and setup target ___

def target(*args):
    return entry_point, None

if __name__ == '__main__':
    if len(sys.argv) == 1:
    print __doc__
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.