Author: Manuel Jacob Branch: rpython-doc Changeset: r61681:4ae3996cfab2 Date: 2013-02-23 15:29 +0100 http://bitbucket.org/pypy/pypy/changeset/4ae3996cfab2/
Log: hg merge default diff --git a/pypy/doc/cpython_differences.rst b/pypy/doc/cpython_differences.rst --- a/pypy/doc/cpython_differences.rst +++ b/pypy/doc/cpython_differences.rst @@ -134,7 +134,7 @@ ``ReferenceError`` at any place that uses them. (Or, better yet, don't use ``weakref.proxy()`` at all; use ``weakref.ref()``.) -There are a few extra implications for the difference in the GC. Most +There are a few extra implications from the difference in the GC. Most notably, if an object has a ``__del__``, the ``__del__`` is never called more than once in PyPy; but CPython will call the same ``__del__`` several times if the object is resurrected and dies again. The ``__del__`` methods are @@ -156,7 +156,7 @@ .. __: http://bugs.pypy.org/issue736 -Using the default GC called ``minimark``, the built-in function ``id()`` +Using the default GC (called ``minimark``), the built-in function ``id()`` works like it does in CPython. With other GCs it returns numbers that are not real addresses (because an object can move around several times) and calling it a lot can lead to performance problem. @@ -286,7 +286,7 @@ ------------- * Hash randomization (``-R``) is ignored in PyPy. As documented in - http://bugs.python.org/issue14621 , some of us believe it has no + http://bugs.python.org/issue14621, some of us believe it has no purpose in CPython either. * ``sys.setrecursionlimit(n)`` sets the limit only approximately, diff --git a/pypy/doc/faq.rst b/pypy/doc/faq.rst --- a/pypy/doc/faq.rst +++ b/pypy/doc/faq.rst @@ -202,20 +202,20 @@ Be sure to enable it again if you need it! -The PyPy translation tool chain -=============================== +The RPython translation tool chain +=================================== ---------------------------------------------- -Can PyPy compile normal Python programs to C? ---------------------------------------------- +------------------------------------------------ +Can RPython compile normal Python programs to C? +------------------------------------------------ -No, PyPy is not a Python compiler. +No, RPython is not a Python compiler. In Python, it is mostly impossible to *prove* anything about the types that a program will manipulate by doing a static analysis. It should be clear if you are familiar with Python, but if in doubt see [BRETT]_. -If you want a fast Python program, please use our JIT_ instead. +If you want a fast Python program, please use the PyPy JIT_ instead. .. _JIT: jit/index.html @@ -300,8 +300,8 @@ Do I have to rewrite my programs in RPython? -------------------------------------------- -No. And you shouldn't try. First and foremost, RPython is a language -that is designed to write interpreters in. It is a restricted subset of +No, and you shouldn't try. First and foremost, RPython is a language +designed for writing interpreters. It is a restricted subset of Python. If you program is not an interpreter but tries to do "real things", like use *any* part of the standard Python library or *any* 3rd-party library, then it is not RPython to start with. You should @@ -381,8 +381,8 @@ No, you have to rebuild the entire interpreter. This means two things: -* It is imperative to use test-driven development. You have to test - exhaustively your module in pure Python, before even attempting to +* It is imperative to use test-driven development. You have to exhaustively + test your module in pure Python, before even attempting to translate it. Once you translate it, you should have only a few typing issues left to fix, but otherwise the result should work out of the box. diff --git a/pypy/doc/getting-started-dev.rst b/pypy/doc/getting-started-dev.rst --- a/pypy/doc/getting-started-dev.rst +++ b/pypy/doc/getting-started-dev.rst @@ -20,7 +20,7 @@ To start the interactive translator shell do:: - cd pypy + cd rpython python bin/translatorshell.py Test snippets of translatable code are provided in the file diff --git a/pypy/doc/interpreter.rst b/pypy/doc/interpreter.rst --- a/pypy/doc/interpreter.rst +++ b/pypy/doc/interpreter.rst @@ -25,7 +25,7 @@ compact bytecode format as CPython 2.7, with minor differences in the bytecode set. Our bytecode compiler is implemented as a chain of flexible passes (tokenizer, lexer, parser, -abstract syntax tree builder, bytecode generator). The latter passes +abstract syntax tree builder and bytecode generator). The latter passes are based on the ``compiler`` package from the standard library of CPython, with various improvements and bug fixes. The bytecode compiler (living under `pypy/interpreter/astcompiler/`_) is now integrated and is @@ -38,8 +38,8 @@ calling its ``frame.eval()`` method. This main entry point initialize appropriate namespaces and then interprets each bytecode instruction. Python's standard library contains -the `lib-python/2.7/dis.py`_ module which allows to view -the Virtual's machine bytecode instructions:: +the `lib-python/2.7/dis.py`_ module which allows to inspection +of the virtual machine's bytecode instructions:: >>> import dis >>> def f(x): @@ -50,10 +50,10 @@ 6 BINARY_ADD 7 RETURN_VALUE -CPython as well as PyPy are stack-based virtual machines, i.e. -they don't have registers but put object to and pull objects +CPython and PyPy are stack-based virtual machines, i.e. +they don't have registers but instead push object to and pull objects from a stack. The bytecode interpreter is only responsible -for implementing control flow and putting and pulling black +for implementing control flow and pushing and pulling black box objects to and from this value stack. The bytecode interpreter does not know how to perform operations on those black box (`wrapped`_) objects for which it delegates to the `object @@ -75,8 +75,8 @@ on ``OperationErrors``. The interpreter implementation offers mechanisms to allow a -caller to be unaware if a particular function invocation leads -to bytecode interpretation or is executed directly at +caller to be unaware of whether a particular function invocation +leads to bytecode interpretation or is executed directly at interpreter-level. The two basic kinds of `Gateway classes`_ expose either an interpreter-level function to application-level execution (``interp2app``) or allow diff --git a/pypy/module/test_lib_pypy/numpypy/test_numpy.py b/pypy/module/test_lib_pypy/numpypy/test_numpy.py --- a/pypy/module/test_lib_pypy/numpypy/test_numpy.py +++ b/pypy/module/test_lib_pypy/numpypy/test_numpy.py @@ -1,4 +1,6 @@ -class AppTestNumpy: +from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest + +class AppTestNumpy(BaseNumpyAppTest): spaceconfig = dict(usemodules=['micronumpy']) def test_imports(self): diff --git a/rpython/translator/goal/bpnn.py b/rpython/translator/goal/bpnn.py new file mode 100644 --- /dev/null +++ b/rpython/translator/goal/bpnn.py @@ -0,0 +1,214 @@ +#!/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: + + ../pypy/translator/goal/translate.py bpnn.py + + Insert '--help' before 'bpnn.py' for a list of translation options, + or see the Overview of Command Line Options for translation at + http://codespeak.net/pypy/dist/pypy/doc/config/commandline.html +""" +# Back-Propagation Neural Networks +# +# Written in Python. See http://www.python.org/ +# +# Neil Schemenauer <[email protected]> +# +# 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 rpython.rlib import rrandom + +PRINT_IT = True + +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): + m.append([fill]*J) + return m + +class NN: + + def __init__(self, ni, nh, no): + # number of input, hidden, and output nodes + self.ni = ni + 1 # +1 for bias node + self.nh = nh + self.no = no + + # activations for nodes + self.ai = [1.0]*self.ni + self.ah = [1.0]*self.nh + self.ao = [1.0]*self.no + + # create weights + self.wi = makeMatrix(self.ni, self.nh) + self.wo = makeMatrix(self.nh, self.no) + # set them to random vaules + for i in range(self.ni): + 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.no): + self.wo[j][k] = rand(-2.0, 2.0) + + # last change in weights for momentum + self.ci = makeMatrix(self.ni, self.nh) + self.co = makeMatrix(self.nh, self.no) + + def update(self, inputs): + if len(inputs) != self.ni-1: + raise ValueError, 'wrong number of inputs' + + # input activations + for i in range(self.ni-1): + #self.ai[i] = 1.0/(1.0+math.exp(-inputs[i])) + self.ai[i] = inputs[i] + + # hidden activations + for j in range(self.nh): + sum = 0.0 + for i in range(self.ni): + sum = sum + self.ai[i] * self.wi[i][j] + self.ah[j] = 1.0/(1.0+math.exp(-sum)) + + # output activations + for k in range(self.no): + sum = 0.0 + for j in range(self.nh): + sum = sum + self.ah[j] * self.wo[j][k] + self.ao[k] = 1.0/(1.0+math.exp(-sum)) + + return self.ao[:] + + + def backPropagate(self, targets, N, M): + if len(targets) != self.no: + raise ValueError, 'wrong number of target values' + + # calculate error terms for output + output_deltas = [0.0] * self.no + for k in range(self.no): + ao = self.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(self.no): + 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(self.no): + change = output_deltas[k]*self.ah[j] + self.wo[j][k] = self.wo[j][k] + N*change + M*self.co[j][k] + self.co[j][k] = change + #print N*change, M*self.co[j][k] + + # update input weights + for i in range(self.ni): + for j in range(self.nh): + change = hidden_deltas[j]*self.ai[i] + self.wi[i][j] = self.wi[i][j] + N*change + M*self.ci[i][j] + self.ci[i][j] = change + + # calculate error + error = 0.0 + for k in range(len(targets)): + delta = targets[k]-self.ao[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(self.ni): + print self.wi[i] + print + 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] + self.update(inputs) + 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 + n.test(pat) + + +# __________ Entry point for stand-alone builds __________ + +import time + +def entry_point(argv): + if len(argv) > 1: + N = int(argv[1]) + else: + N = 200 + T = time.time() + for i in range(N): + demo() + 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: + sys.argv.append('1') + entry_point(sys.argv) + print __doc__ _______________________________________________ pypy-commit mailing list [email protected] http://mail.python.org/mailman/listinfo/pypy-commit
