提交 3e303fc9 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Fixup tutorial/* and remove matching tests.

上级 a5e70754
...@@ -11,9 +11,6 @@ To get us started with Theano and get a feel of what we're working with, ...@@ -11,9 +11,6 @@ To get us started with Theano and get a feel of what we're working with,
let's make a simple function: add two numbers together. Here is how you do let's make a simple function: add two numbers together. Here is how you do
it: it:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_adding.test_adding_1
>>> import theano.tensor as T >>> import theano.tensor as T
>>> from theano import function >>> from theano import function
>>> x = T.dscalar('x') >>> x = T.dscalar('x')
...@@ -150,9 +147,6 @@ You might already have guessed how to do this. Indeed, the only change ...@@ -150,9 +147,6 @@ You might already have guessed how to do this. Indeed, the only change
from the previous example is that you need to instantiate *x* and from the previous example is that you need to instantiate *x* and
*y* using the matrix Types: *y* using the matrix Types:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_adding.test_adding_2
>>> x = T.dmatrix('x') >>> x = T.dmatrix('x')
>>> y = T.dmatrix('y') >>> y = T.dmatrix('y')
>>> z = x + y >>> z = x + y
...@@ -207,7 +201,7 @@ Exercise ...@@ -207,7 +201,7 @@ Exercise
a = theano.tensor.vector() # declare variable a = theano.tensor.vector() # declare variable
out = a + a ** 10 # build symbolic expression out = a + a ** 10 # build symbolic expression
f = theano.function([a], out) # compile function f = theano.function([a], out) # compile function
print f([0, 1, 2]) print(f([0, 1, 2]))
.. testoutput:: .. testoutput::
......
...@@ -55,10 +55,6 @@ Borrowing when Creating Shared Variables ...@@ -55,10 +55,6 @@ Borrowing when Creating Shared Variables
A ``borrow`` argument can be provided to the shared-variable constructor. A ``borrow`` argument can be provided to the shared-variable constructor.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_aliasing.test_aliasing_1
.. testcode:: borrow .. testcode:: borrow
import numpy, theano import numpy, theano
...@@ -124,9 +120,6 @@ A ``borrow`` argument can also be used to control how a ``shared`` variable's va ...@@ -124,9 +120,6 @@ A ``borrow`` argument can also be used to control how a ``shared`` variable's va
retrieved. retrieved.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_aliasing.test_aliasing_2
.. testcode:: borrow .. testcode:: borrow
s = theano.shared(np_array) s = theano.shared(np_array)
...@@ -185,6 +178,11 @@ that Theano *may* reuse the buffer you provide as the internal storage for the v ...@@ -185,6 +178,11 @@ that Theano *may* reuse the buffer you provide as the internal storage for the v
A standard pattern for manually updating the value of a ``shared`` variable is as A standard pattern for manually updating the value of a ``shared`` variable is as
follows: follows:
.. testsetup:: borrow
def some_inplace_fn(v):
return v
.. testcode:: borrow .. testcode:: borrow
s.set_value( s.set_value(
...@@ -231,9 +229,6 @@ Borrowing when Constructing Function Objects ...@@ -231,9 +229,6 @@ Borrowing when Constructing Function Objects
A ``borrow`` argument can also be provided to the ``In`` and ``Out`` objects A ``borrow`` argument can also be provided to the ``In`` and ``Out`` objects
that control how ``theano.function`` handles its argument[s] and return value[s]. that control how ``theano.function`` handles its argument[s] and return value[s].
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_aliasing.test_aliasing_3
.. testcode:: .. testcode::
import theano, theano.tensor import theano, theano.tensor
...@@ -268,7 +263,7 @@ graph. ...@@ -268,7 +263,7 @@ graph.
For GPU graphs, this borrowing can have a major speed impact. See the following code: For GPU graphs, this borrowing can have a major speed impact. See the following code:
.. testcode:: .. code-block:: python
from theano import function, config, shared, sandbox, tensor, Out from theano import function, config, shared, sandbox, tensor, Out
import numpy import numpy
......
...@@ -52,15 +52,15 @@ IfElse vs Switch ...@@ -52,15 +52,15 @@ IfElse vs Switch
f_lazyifelse(val1, val2, big_mat1, big_mat2) f_lazyifelse(val1, val2, big_mat1, big_mat2)
print 'time spent evaluating one value %f sec' % (time.clock() - tic) print 'time spent evaluating one value %f sec' % (time.clock() - tic)
In this example, the ``IfElse`` op spends less time (about half as much) than ``Switch``
since it computes only one variable out of the two.
.. testoutput:: .. testoutput::
:hide: :hide:
:options: +ELLIPSIS :options: +ELLIPSIS
time spent evaluating both values ... sec time spent evaluating both values ... sec
time spent evaluating one value ... sec time spent evaluating one value ... sec
In this example, the ``IfElse`` op spends less time (about half as much) than ``Switch``
since it computes only one variable out of the two.
.. code-block:: none .. code-block:: none
......
...@@ -39,23 +39,19 @@ messages. Consider the following faulty code. ...@@ -39,23 +39,19 @@ messages. Consider the following faulty code.
Running the code above we see: Running the code above we see:
.. testoutput:: .. testoutput::
:options: +ELLIPSIS
Traceback (most recent call last): Traceback (most recent call last):
File "test0.py", line 10, in <module> ...
f(np.ones((2,)), np.ones((3,))) ValueError: Input dimension mis-match. (input[0].shape[0] = 3, input[1].shape[0] = 2)
File "/PATH_TO_THEANO/theano/compile/function_module.py", line 605, in __call__ Apply node that caused the error: Elemwise{add,no_inplace}(<TensorType(float64, vector)>, <TensorType(float64, vector)>, <TensorType(float64, vector)>)
self.fn.thunks[self.fn.position_of_error]) Inputs types: [TensorType(float64, vector), TensorType(float64, vector), TensorType(float64, vector)]
File "/PATH_TO_THEANO/theano/compile/function_module.py", line 595, in __call__ Inputs shapes: [(3,), (2,), (2,)]
outputs = self.fn() Inputs strides: [(8,), (8,), (8,)]
ValueError: Input dimension mis-match. (input[0].shape[0] = 3, input[1].shape[0] = 2) Inputs scalar values: ['not scalar', 'not scalar', 'not scalar']
Apply node that caused the error: Elemwise{add,no_inplace}(<TensorType(float64, vector)>, <TensorType(float64, vector)>, <TensorType(float64, vector)>)
Inputs types: [TensorType(float64, vector), TensorType(float64, vector), TensorType(float64, vector)]
Inputs shapes: [(3,), (2,), (2,)]
Inputs strides: [(8,), (8,), (8,)]
Inputs scalar values: ['not scalar', 'not scalar', 'not scalar']
HINT: Re-running with most Theano optimization disabled could give you a back-traces when this node was created. This can be done with by setting the Theano flags 'optimizer=fast_compile'. If that does not work, Theano optimization can be disabled with 'optimizer=None'. HINT: Re-running with most Theano optimization disabled could give you a back-traces when this node was created. This can be done with by setting the Theano flags 'optimizer=fast_compile'. If that does not work, Theano optimization can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint of this apply node. HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint of this apply node.
Arguably the most useful information is approximately half-way through Arguably the most useful information is approximately half-way through
the error message, where the kind of error is displayed along with its the error message, where the kind of error is displayed along with its
...@@ -71,7 +67,7 @@ the faulty line, while ``exception_verbosity=high`` will display a ...@@ -71,7 +67,7 @@ the faulty line, while ``exception_verbosity=high`` will display a
debugprint of the apply node. Using these hints, the end of the error debugprint of the apply node. Using these hints, the end of the error
message becomes : message becomes :
.. testoutput:: .. code-block:: none
Backtrace when the node is created: Backtrace when the node is created:
File "test0.py", line 8, in <module> File "test0.py", line 8, in <module>
...@@ -101,7 +97,7 @@ following example. Here, we use ``exception_verbosity=high`` and ...@@ -101,7 +97,7 @@ following example. Here, we use ``exception_verbosity=high`` and
``optimizer=None`` would and it could therefore be used instead of test values. ``optimizer=None`` would and it could therefore be used instead of test values.
.. testcode:: testvalues .. testcode:: testvalue
import numpy import numpy
import theano import theano
...@@ -137,7 +133,7 @@ following example. Here, we use ``exception_verbosity=high`` and ...@@ -137,7 +133,7 @@ following example. Here, we use ``exception_verbosity=high`` and
Running the above code generates the following error message: Running the above code generates the following error message:
.. testoutput:: testvalues .. testoutput:: testvalue
Traceback (most recent call last): Traceback (most recent call last):
File "test1.py", line 31, in <module> File "test1.py", line 31, in <module>
...@@ -166,7 +162,7 @@ Running the above code generates the following error message: ...@@ -166,7 +162,7 @@ Running the above code generates the following error message:
If the above is not informative enough, by instrumenting the code ever If the above is not informative enough, by instrumenting the code ever
so slightly, we can get Theano to reveal the exact source of the error. so slightly, we can get Theano to reveal the exact source of the error.
.. testcode:: testvalues .. code-block:: python
# enable on-the-fly graph computations # enable on-the-fly graph computations
theano.config.compute_test_value = 'warn' theano.config.compute_test_value = 'warn'
...@@ -185,7 +181,7 @@ of error can thus be identified with much more precision and much earlier in ...@@ -185,7 +181,7 @@ of error can thus be identified with much more precision and much earlier in
the compilation pipeline. For example, running the above code yields the the compilation pipeline. For example, running the above code yields the
following error message, which properly identifies *line 24* as the culprit. following error message, which properly identifies *line 24* as the culprit.
.. testoutput:: testvalues .. code-block:: node
Traceback (most recent call last): Traceback (most recent call last):
File "test2.py", line 24, in <module> File "test2.py", line 24, in <module>
...@@ -393,12 +389,13 @@ can be achieved as follows: ...@@ -393,12 +389,13 @@ can be achieved as follows:
f(0) # log(0) * 0 = -inf * 0 = NaN f(0) # log(0) * 0 = -inf * 0 = NaN
.. testoutput:: compiled .. testoutput:: compiled
:options: +NORMALIZE_WHITESPACE
*** NaN detected *** *** NaN detected ***
Elemwise{Composite{(log(i0) * i0)}} [@A] '' Elemwise{Composite{(log(i0) * i0)}} [@A] ''
|x [@B] |x [@B]
Inputs : [array(0.0)] Inputs : [array(0.0)]
Outputs: [array(nan)] Outputs: [array(nan)]
To help understand what is happening in your graph, you can To help understand what is happening in your graph, you can
disable the ``local_elemwise_fusion`` and all ``inplace`` disable the ``local_elemwise_fusion`` and all ``inplace``
...@@ -430,12 +427,11 @@ the execution of the node can garbage collect its inputs that aren't ...@@ -430,12 +427,11 @@ the execution of the node can garbage collect its inputs that aren't
needed anymore by the Theano function. This can be done with the Theano needed anymore by the Theano function. This can be done with the Theano
flag: flag:
.. testcode:: compiled .. code-block:: python
allow_gc=False allow_gc=False
.. TODO: documentation for link.WrapLinkerMany .. TODO: documentation for link.WrapLinkerMany
...@@ -453,26 +449,47 @@ Consider this example script ("ex.py"): ...@@ -453,26 +449,47 @@ Consider this example script ("ex.py"):
.. testcode:: .. testcode::
import theano import theano
import numpy import numpy
import theano.tensor as T import theano.tensor as T
a = T.dmatrix('a') a = T.dmatrix('a')
b = T.dmatrix('b') b = T.dmatrix('b')
f = theano.function([a, b], [a * b]) f = theano.function([a, b], [a * b])
# matrices chosen so dimensions are unsuitable for multiplication # matrices chosen so dimensions are unsuitable for multiplication
mat1 = numpy.arange(12).reshape((3, 4)) mat1 = numpy.arange(12).reshape((3, 4))
mat2 = numpy.arange(25).reshape((5, 5)) mat2 = numpy.arange(25).reshape((5, 5))
f(mat1, mat2) f(mat1, mat2)
.. testoutput::
:hide:
:options: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: Input dimension mis-match. (input[0].shape[0] = 3, input[1].shape[0] = 5)
Apply node that caused the error: Elemwise{mul,no_inplace}(a, b)
Toposort index: 0
Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)]
Inputs shapes: [(3, 4), (5, 5)]
Inputs strides: [(32, 8), (40, 8)]
Inputs values: ['not shown', 'not shown']
Outputs clients: [['output']]
Backtrace when the node is created:
File "<doctest default[0]>", line 8, in <module>
f = theano.function([a, b], [a * b])
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.
This is actually so simple the debugging could be done easily, but it's for This is actually so simple the debugging could be done easily, but it's for
illustrative purposes. As the matrices can't be multiplied element-wise illustrative purposes. As the matrices can't be multiplied element-wise
(unsuitable shapes), we get the following exception: (unsuitable shapes), we get the following exception:
.. testoutput:: .. code-block:: none
File "ex.py", line 14, in <module> File "ex.py", line 14, in <module>
f(mat1, mat2) f(mat1, mat2)
......
...@@ -412,54 +412,46 @@ corresponding to the random number generation process (i.e. RandomFunction{unifo ...@@ -412,54 +412,46 @@ corresponding to the random number generation process (i.e. RandomFunction{unifo
An example of how "random states" can be transferred from one theano function An example of how "random states" can be transferred from one theano function
to another is shown below. to another is shown below.
.. testcode:: >>> from __future__ import print_function
>>> import theano
from __future__ import print_function >>> import numpy
import theano >>> import theano.tensor as T
import numpy >>> from theano.sandbox.rng_mrg import MRG_RandomStreams
import theano.tensor as T >>> from theano.tensor.shared_randomstreams import RandomStreams
from theano.sandbox.rng_mrg import MRG_RandomStreams
from theano.tensor.shared_randomstreams import RandomStreams
class Graph():
def __init__(self, seed=123):
self.rng = RandomStreams(seed)
self.y = self.rng.uniform(size=(1,))
g1 = Graph(seed=123)
f1 = theano.function([], g1.y)
g2 = Graph(seed=987) >>> class Graph():
f2 = theano.function([], g2.y) ... def __init__(self, seed=123):
... self.rng = RandomStreams(seed)
... self.y = self.rng.uniform(size=(1,))
print('By default, the two functions are out of sync.') >>> g1 = Graph(seed=123)
print("f1() returns ", end=" "); print(f1()) >>> f1 = theano.function([], g1.y)
print("f2() returns ", end=" "); print(f2())
def copy_random_state(g1, g2): >>> g2 = Graph(seed=987)
if isinstance(g1.rng, MRG_RandomStreams): >>> f2 = theano.function([], g2.y)
g2.rng.rstate = g1.rng.rstate
for (su1, su2) in zip(g1.rng.state_updates, g2.rng.state_updates):
su2[0].set_value(su1[0].get_value())
print('We now copy the state of the theano random number generators.') >>> # By default, the two functions are out of sync.
copy_random_state(g1, g2) >>> f1()
print("f1() returns ", end=" "); print(f1()) array([ 0.72803009])
print("f2() returns ", end=" "); print(f2()) >>> f2()
array([ 0.55056769])
This gives the following output: >>> def copy_random_state(g1, g2):
... if isinstance(g1.rng, MRG_RandomStreams):
... g2.rng.rstate = g1.rng.rstate
... for (su1, su2) in zip(g1.rng.state_updates, g2.rng.state_updates):
... su2[0].set_value(su1[0].get_value())
.. testoutput:: >>> # We now copy the state of the theano random number generators.
>>> copy_random_state(g1, g2)
>>> f1()
array([ 0.59044123])
>>> f2()
array([ 0.59044123])
By default, the two functions are out of sync.
f1() returns [ 0.72803009]
f2() returns [ 0.55056769]
We now copy the state of the theano random number generators.
f1() returns [ 0.59044123]
f2() returns [ 0.59044123]
Other Random Distributions Other Random Distributions
--------------------------- --------------------------
There are :ref:`other distributions implemented <libdoc_tensor_raw_random>`. There are :ref:`other distributions implemented <libdoc_tensor_raw_random>`.
...@@ -543,13 +535,12 @@ It will be used repeatedly. ...@@ -543,13 +535,12 @@ It will be used repeatedly.
:hide: :hide:
:options: +ELLIPSIS :options: +ELLIPSIS
Initial model: Initial model:
... ...
... 0.0
Final model: Final model:
... ...
... target values for D:
target values for D: ...
... prediction on D:
prediction on D: ...
...
...@@ -73,9 +73,9 @@ possibilities you may encounter or need. For that refer to ...@@ -73,9 +73,9 @@ possibilities you may encounter or need. For that refer to
# Other type of implementation # Other type of implementation
# C implementation: [see theano web site for other functions] # C implementation: [see theano web site for other functions]
def c_code(...): def c_code(self, node, inputs, outputs, sub):
# ...
pass pass
# Other implementations (pycuda, ...): # Other implementations (pycuda, ...):
def make_thunk(self, node, storage_map, _, _2): def make_thunk(self, node, storage_map, _, _2):
pass pass
...@@ -83,7 +83,7 @@ possibilities you may encounter or need. For that refer to ...@@ -83,7 +83,7 @@ possibilities you may encounter or need. For that refer to
# optional: # optional:
check_input = True check_input = True
def __init__(self, ...): def __init__(self, *args):
pass pass
def grad(self, inputs, g): def grad(self, inputs, g):
...@@ -92,7 +92,7 @@ possibilities you may encounter or need. For that refer to ...@@ -92,7 +92,7 @@ possibilities you may encounter or need. For that refer to
def R_op(self, inputs, eval_points): def R_op(self, inputs, eval_points):
pass pass
def infer_shape(node, (i0_shapes, ...)): def infer_shape(node, input_shapes):
pass pass
.. ../extending/op.txt .. ../extending/op.txt
...@@ -684,8 +684,8 @@ You can try it as follows: ...@@ -684,8 +684,8 @@ You can try it as follows:
x = theano.tensor.fmatrix() x = theano.tensor.fmatrix()
y = theano.tensor.fmatrix() y = theano.tensor.fmatrix()
f = function([x, y], numpy_dot(x, y)) f = function([x, y], numpy_dot(x, y))
inp1 = numpy.random.rand(5, 4) inp1 = numpy.random.rand(5, 4).astype('float32')
inp2 = numpy.random.rand(4, 7) inp2 = numpy.random.rand(4, 7).astype('float32')
out = f(inp1, inp2) out = f(inp1, inp2)
......
...@@ -895,7 +895,7 @@ defined to False. In these descrptions 'i' refers to the position ...@@ -895,7 +895,7 @@ defined to False. In these descrptions 'i' refers to the position
corresponds to ``npy_float32`` and can directly be used to declare a corresponds to ``npy_float32`` and can directly be used to declare a
new variable of the same dtype as the data in the array : new variable of the same dtype as the data in the array :
.. testcode:: .. code-block:: c
DTYPE_INPUT_0 myVar = someValue; DTYPE_INPUT_0 myVar = someValue;
...@@ -914,11 +914,11 @@ In addition to these macros, the ``init_code_struct``, ``code``, and ...@@ -914,11 +914,11 @@ In addition to these macros, the ``init_code_struct``, ``code``, and
* ``FAIL`` : Code to insert at error points. A python exception * ``FAIL`` : Code to insert at error points. A python exception
should be set prior to this code. An invocation look like this: should be set prior to this code. An invocation look like this:
.. testcode:: .. code-block:: c
if (error) { if (error) {
// Set python exception // Set python exception
FAIL FAIL
} }
You can add a semicolon after the macro if it makes your editor You can add a semicolon after the macro if it makes your editor
......
...@@ -36,7 +36,7 @@ Compiling with PyCUDA ...@@ -36,7 +36,7 @@ Compiling with PyCUDA
You can use PyCUDA to compile CUDA functions that work directly on You can use PyCUDA to compile CUDA functions that work directly on
CudaNdarrays. Here is an example from the file ``theano/misc/tests/test_pycuda_theano_simple.py``: CudaNdarrays. Here is an example from the file ``theano/misc/tests/test_pycuda_theano_simple.py``:
.. testcode:: .. code-block:: python
import sys import sys
import numpy import numpy
...@@ -78,7 +78,7 @@ Theano Op using a PyCUDA function ...@@ -78,7 +78,7 @@ Theano Op using a PyCUDA function
You can use a GPU function compiled with PyCUDA in a Theano op: You can use a GPU function compiled with PyCUDA in a Theano op:
.. testcode:: .. code-block:: python
import numpy, theano import numpy, theano
import theano.misc.pycuda_init import theano.misc.pycuda_init
......
...@@ -38,6 +38,10 @@ The two modules ``pickle`` and ``cPickle`` have the same functionalities, but ...@@ -38,6 +38,10 @@ The two modules ``pickle`` and ``cPickle`` have the same functionalities, but
You can serialize (or *save*, or *pickle*) objects to a file with You can serialize (or *save*, or *pickle*) objects to a file with
``cPickle.dump``: ``cPickle.dump``:
.. testsetup::
my_obj = object()
>>> f = file('obj.save', 'wb') >>> f = file('obj.save', 'wb')
>>> cPickle.dump(my_obj, f, protocol=cPickle.HIGHEST_PROTOCOL) >>> cPickle.dump(my_obj, f, protocol=cPickle.HIGHEST_PROTOCOL)
>>> f.close() >>> f.close()
...@@ -64,6 +68,12 @@ To de-serialize (or *load*, or *unpickle*) a pickled file, use ...@@ -64,6 +68,12 @@ To de-serialize (or *load*, or *unpickle*) a pickled file, use
You can pickle several objects into the same file, and load them all (in the You can pickle several objects into the same file, and load them all (in the
same order): same order):
.. testsetup::
obj1 = object()
obj2 = object()
obj3 = object()
>>> f = file('objects.save', 'wb') >>> f = file('objects.save', 'wb')
>>> for obj in [obj1, obj2, obj3]: >>> for obj in [obj1, obj2, obj3]:
... cPickle.dump(obj, f, protocol=cPickle.HIGHEST_PROTOCOL) ... cPickle.dump(obj, f, protocol=cPickle.HIGHEST_PROTOCOL)
...@@ -127,7 +137,7 @@ The main advantage of this approach is that you don't even need Theano installed ...@@ -127,7 +137,7 @@ The main advantage of this approach is that you don't even need Theano installed
in order to look at the values of shared variables that you pickled. You can in order to look at the values of shared variables that you pickled. You can
just load the parameters manually with `numpy`. just load the parameters manually with `numpy`.
.. testcode:: .. code-block:: python
import numpy import numpy
numpy.load('model.zip') numpy.load('model.zip')
......
...@@ -63,8 +63,6 @@ Consider the logistic regression: ...@@ -63,8 +63,6 @@ Consider the logistic regression:
b = theano.shared(numpy.asarray(0., dtype=theano.config.floatX), name="b") b = theano.shared(numpy.asarray(0., dtype=theano.config.floatX), name="b")
x.tag.test_value = D[0] x.tag.test_value = D[0]
y.tag.test_value = D[1] y.tag.test_value = D[1]
#print "Initial model:"
#print w.get_value(), b.get_value()
# Construct Theano expression graph # Construct Theano expression graph
p_1 = 1 / (1 + T.exp(-T.dot(x, w)-b)) # Probability of having a one p_1 = 1 / (1 + T.exp(-T.dot(x, w)-b)) # Probability of having a one
...@@ -77,7 +75,7 @@ Consider the logistic regression: ...@@ -77,7 +75,7 @@ Consider the logistic regression:
train = theano.function( train = theano.function(
inputs=[x,y], inputs=[x,y],
outputs=[prediction, xent], outputs=[prediction, xent],
updates={w:w-0.01*gw, b:b-0.01*gb}, updates=[(w, w-0.01*gw), (b, b-0.01*gb)],
name = "train") name = "train")
predict = theano.function(inputs=[x], outputs=prediction, predict = theano.function(inputs=[x], outputs=prediction,
name = "predict") name = "predict")
...@@ -94,8 +92,6 @@ Consider the logistic regression: ...@@ -94,8 +92,6 @@ Consider the logistic regression:
for i in range(training_steps): for i in range(training_steps):
pred, err = train(D[0], D[1]) pred, err = train(D[0], D[1])
#print "Final model:"
#print w.get_value(), b.get_value()
print("target values for D") print("target values for D")
print(D[1]) print(D[1])
...@@ -107,52 +103,11 @@ Consider the logistic regression: ...@@ -107,52 +103,11 @@ Consider the logistic regression:
:hide: :hide:
:options: +ELLIPSIS :options: +ELLIPSIS
Used the cpu Used the cpu
targe values for D target values for D
... ...
prediction on D prediction on D
... ...
.. code-block:: none
Used the cpu
target values for D
[ 0. 0. 1. 0. 1. 1. 0. 1. 0. 1. 0. 1. 0. 0. 0. 0. 1. 0.
1. 0. 1. 1. 0. 1. 0. 0. 1. 1. 1. 0. 1. 0. 0. 1. 1. 1.
0. 0. 0. 0. 0. 1. 1. 1. 0. 0. 1. 1. 1. 1. 1. 0. 0. 1.
0. 1. 0. 0. 1. 1. 0. 1. 1. 1. 1. 0. 0. 1. 1. 0. 1. 1.
1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0.
1. 0. 1. 0. 1. 0. 0. 0. 1. 1. 0. 1. 0. 1. 0. 1. 0. 1.
0. 1. 0. 0. 0. 1. 0. 0. 1. 1. 1. 0. 1. 1. 0. 0. 1. 0.
1. 1. 1. 0. 1. 1. 0. 0. 1. 0. 1. 0. 0. 1. 0. 0. 0. 1.
0. 0. 0. 0. 0. 1. 1. 1. 1. 1. 1. 0. 0. 1. 1. 0. 1. 0.
0. 0. 0. 1. 1. 1. 0. 0. 0. 1. 1. 1. 0. 1. 0. 0. 0. 0.
1. 1. 1. 1. 1. 0. 1. 1. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.
0. 1. 1. 1. 0. 1. 1. 0. 0. 0. 1. 1. 1. 0. 0. 0. 1. 0.
0. 1. 0. 1. 1. 1. 0. 1. 1. 1. 0. 0. 0. 1. 1. 0. 1. 0.
0. 1. 1. 0. 1. 1. 1. 0. 0. 1. 1. 1. 0. 1. 1. 1. 1. 0.
1. 0. 1. 0. 0. 0. 1. 0. 0. 1. 0. 0. 1. 0. 1. 0. 0. 0.
1. 0. 0. 0. 0. 0. 1. 1. 0. 1. 0. 0. 0. 0. 1. 0. 0. 0.
1. 0. 0. 0. 1. 1. 0. 1. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1.
1. 1. 1. 1. 0. 0. 0. 1. 1. 1. 0. 1. 1. 1. 0. 1. 1. 0.
1. 1. 1. 0. 1. 1. 0. 0. 1. 1. 0. 1. 0. 1. 1. 1. 1. 1.
0. 0. 0. 1. 1. 0. 0. 1. 1. 1. 0. 0. 0. 0. 1. 0. 0. 0.
0. 1. 0. 0. 0. 0. 0. 1. 1. 0. 0. 1. 1. 1. 0. 1. 1. 0.
0. 0. 1. 0. 1. 1. 1. 1. 1. 1. 0. 1. 1. 1. 1. 0. 0. 1.
1. 1. 1. 1.]
prediction on D
[0 0 1 0 1 1 0 1 0 1 0 1 0 0 0 0 1 0 1 0 1 1 0 1 0 0 1 1 1 0 1 0 0 1 1 1 0
0 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 1 0 1 0 0 1 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 1 1 0 1 0 1 0 1 0 1 0 1 0
0 0 1 0 0 1 1 1 0 1 1 0 0 1 0 1 1 1 0 1 1 0 0 1 0 1 0 0 1 0 0 0 1 0 0 0 0
0 1 1 1 1 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 0 0 1 1 1 0 1 0 0 0 0 1 1 1 1 1
0 1 1 0 0 0 0 0 1 1 1 1 1 0 1 1 1 0 1 1 0 0 0 1 1 1 0 0 0 1 0 0 1 0 1 1 1
0 1 1 1 0 0 0 1 1 0 1 0 0 1 1 0 1 1 1 0 0 1 1 1 0 1 1 1 1 0 1 0 1 0 0 0 1
0 0 1 0 0 1 0 1 0 0 0 1 0 0 0 0 0 1 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 1 0 1
0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 1 1 1 0 1 1 1 0 1 1 0 1 1 1 0 1 1 0 0 1
1 0 1 0 1 1 1 1 1 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 1 0
0 1 1 1 0 1 1 0 0 0 1 0 1 1 1 1 1 1 0 1 1 1 1 0 0 1 1 1 1 1]
Modify and execute this example to run on CPU (the default) with floatX=float32 and Modify and execute this example to run on CPU (the default) with floatX=float32 and
time the execution using the command line ``time python file.py``. Save your code time the execution using the command line ``time python file.py``. Save your code
...@@ -346,8 +301,9 @@ Compiling your Graph with ProfileMode ...@@ -346,8 +301,9 @@ Compiling your Graph with ProfileMode
Once the ProfileMode instance is created, simply compile your graph as you Once the ProfileMode instance is created, simply compile your graph as you
would normally, by specifying the mode parameter. would normally, by specifying the mode parameter.
>>> # with functions >>> v1, v2 = T.vectors(2)
>>> f = theano.function([input1,input2],[output1], mode=profmode) >>> o = v1 + v2
>>> f = theano.function([v1,v2],[o], mode=profmode)
Retrieving Timing Information Retrieving Timing Information
----------------------------- -----------------------------
...@@ -361,7 +317,7 @@ regression example. ...@@ -361,7 +317,7 @@ regression example.
Compiling the module with ``ProfileMode`` and calling ``profmode.print_summary()`` Compiling the module with ``ProfileMode`` and calling ``profmode.print_summary()``
generates the following output: generates the following output:
.. testcode:: .. code-block:: python
""" """
ProfileMode.print_summary() ProfileMode.print_summary()
......
...@@ -160,9 +160,9 @@ as we apply it. Consider the following example of optimization: ...@@ -160,9 +160,9 @@ as we apply it. Consider the following example of optimization:
>>> f = theano.function([a], b) # compile function >>> f = theano.function([a], b) # compile function
>>> print f([0, 1, 2]) # prints `array([0,2,1026])` >>> print f([0, 1, 2]) # prints `array([0,2,1026])`
[ 0. 2. 1026.] [ 0. 2. 1026.]
>>> theano.printing.pydotprint(b, outfile="./pics/symbolic_graph_unopt.png", var_with_name_simple=True) >>> theano.printing.pydotprint(b, outfile="./pics/symbolic_graph_unopt.png", var_with_name_simple=True) # doctest: +SKIP
The output file is available at ./pics/symbolic_graph_unopt.png The output file is available at ./pics/symbolic_graph_unopt.png
>>> theano.printing.pydotprint(f, outfile="./pics/symbolic_graph_opt.png", var_with_name_simple=True) >>> theano.printing.pydotprint(f, outfile="./pics/symbolic_graph_opt.png", var_with_name_simple=True) # doctest: +SKIP
The output file is available at ./pics/symbolic_graph_opt.png The output file is available at ./pics/symbolic_graph_opt.png
......
...@@ -54,8 +54,8 @@ file and run it. ...@@ -54,8 +54,8 @@ file and run it.
for i in xrange(iters): for i in xrange(iters):
r = f() r = f()
t1 = time.time() t1 = time.time()
print("Looping %d times took" % iters, t1 - t0, "seconds") print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is", r) print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]): if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
print('Used the cpu') print('Used the cpu')
else: else:
...@@ -75,18 +75,10 @@ same floating-point numbers as the CPU. As a benchmark, a loop that calls ``nump ...@@ -75,18 +75,10 @@ same floating-point numbers as the CPU. As a benchmark, a loop that calls ``nump
:hide: :hide:
:options: +ELLIPSIS :options: +ELLIPSIS
$ THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 python check1.py [Elemwise{exp,no_inplace}(<TensorType(float64, vector)>)]
[Elemwise{exp,no_inplace}(<TensorType(float32, vector)>)] Looping 1000 times took ... seconds
Looping 1000 times took ... seconds Result is ...
Result is ... Used the cpu
Used the cpu
$ THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python check1.py
Using gpu device 0: GeForce GTX 580
[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>), HostFromGpu(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took ... seconds
Result is ...
Used the gpu
.. code-block:: none .. code-block:: none
...@@ -134,16 +126,16 @@ after the ``T.exp(x)`` is replaced by a GPU version of ``exp()``. ...@@ -134,16 +126,16 @@ after the ``T.exp(x)`` is replaced by a GPU version of ``exp()``.
iters = 1000 iters = 1000
rng = numpy.random.RandomState(22) rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX)) x = shared(numpy.asarray(rng.rand(vlen), 'float32'))
f = function([], sandbox.cuda.basic_ops.gpu_from_host(T.exp(x))) f = function([], sandbox.cuda.basic_ops.gpu_from_host(T.exp(x)))
print(f.maker.fgraph.toposort()) print(f.maker.fgraph.toposort())
t0 = time.time() t0 = time.time()
for i in xrange(iters): for i in xrange(iters):
r = f() r = f()
t1 = time.time() t1 = time.time()
print("Looping %d times took" % iters, t1 - t0, "seconds") print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is", r) print("Result is %s" % (r,))
print("Numpy result is", numpy.asarray(r)) print("Numpy result is %s" % (numpy.asarray(r),))
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]): if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
print('Used the cpu') print('Used the cpu')
else: else:
...@@ -153,15 +145,14 @@ The output from this program is ...@@ -153,15 +145,14 @@ The output from this program is
.. testoutput:: .. testoutput::
:hide: :hide:
:options: +ELLIPSIS :options: +ELLIPSIS, +SKIP
$ THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python check2.py Using gpu device 0: GeForce GTX 580
Using gpu device 0: GeForce GTX 580 [GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>)]
[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>)] Looping 1000 times took ... seconds
Looping 1000 times took ... seconds Result is <CudaNdarray object at 0x...>
Result is <CudaNdarray object at 0x6a7a5f0> Numpy result is ...
Numpy result is ... Used the gpu
Used the gpu
.. code-block:: none .. code-block:: none
...@@ -302,8 +293,6 @@ Consider again the logistic regression: ...@@ -302,8 +293,6 @@ Consider again the logistic regression:
b = theano.shared(numpy.asarray(0., dtype=theano.config.floatX), name="b") b = theano.shared(numpy.asarray(0., dtype=theano.config.floatX), name="b")
x.tag.test_value = D[0] x.tag.test_value = D[0]
y.tag.test_value = D[1] y.tag.test_value = D[1]
#print "Initial model:"
#print w.get_value(), b.get_value()
# Construct Theano expression graph # Construct Theano expression graph
p_1 = 1 / (1 + T.exp(-T.dot(x, w)-b)) # Probability of having a one p_1 = 1 / (1 + T.exp(-T.dot(x, w)-b)) # Probability of having a one
...@@ -316,7 +305,7 @@ Consider again the logistic regression: ...@@ -316,7 +305,7 @@ Consider again the logistic regression:
train = theano.function( train = theano.function(
inputs=[x,y], inputs=[x,y],
outputs=[prediction, xent], outputs=[prediction, xent],
updates={w:w-0.01*gw, b:b-0.01*gb}, updates=[(w, w-0.01*gw), (b, b-0.01*gb)],
name = "train") name = "train")
predict = theano.function(inputs=[x], outputs=prediction, predict = theano.function(inputs=[x], outputs=prediction,
name = "predict") name = "predict")
...@@ -333,8 +322,6 @@ Consider again the logistic regression: ...@@ -333,8 +322,6 @@ Consider again the logistic regression:
for i in range(training_steps): for i in range(training_steps):
pred, err = train(D[0], D[1]) pred, err = train(D[0], D[1])
#print "Final model:"
#print w.get_value(), b.get_value()
print("target values for D") print("target values for D")
print(D[1]) print(D[1])
...@@ -346,52 +333,11 @@ Consider again the logistic regression: ...@@ -346,52 +333,11 @@ Consider again the logistic regression:
:hide: :hide:
:options: + ELLIPSIS :options: + ELLIPSIS
Used the cpu Used the cpu
target values for D target values for D
... ...
prediction on D prediction on D
... ...
.. code-block:: none
Used the cpu
target values for D
[ 0. 1. 0. 0. 1. 1. 1. 0. 1. 1. 1. 1. 1. 1. 1. 0. 1. 0.
0. 0. 0. 0. 1. 1. 0. 1. 1. 0. 0. 1. 1. 1. 1. 0. 1. 1.
0. 0. 0. 0. 0. 0. 1. 1. 1. 0. 1. 1. 0. 0. 0. 0. 1. 0.
0. 1. 0. 0. 0. 0. 1. 1. 0. 1. 0. 0. 1. 1. 0. 1. 0. 0.
1. 1. 0. 0. 0. 0. 0. 1. 1. 1. 0. 1. 0. 0. 0. 0. 1. 0.
0. 0. 0. 1. 0. 1. 1. 0. 0. 0. 1. 1. 1. 1. 1. 1. 0. 0.
0. 1. 0. 1. 0. 1. 1. 0. 0. 1. 0. 0. 1. 0. 1. 1. 0. 1.
1. 1. 0. 1. 0. 1. 1. 0. 1. 1. 1. 0. 0. 0. 0. 0. 1. 0.
0. 0. 0. 0. 1. 0. 1. 0. 0. 0. 1. 1. 0. 0. 0. 1. 0. 0.
0. 1. 1. 0. 1. 1. 1. 1. 1. 1. 0. 0. 1. 1. 1. 1. 1. 0.
1. 0. 1. 1. 1. 0. 0. 0. 1. 0. 1. 1. 0. 1. 1. 0. 1. 1.
1. 1. 0. 0. 0. 0. 0. 1. 0. 1. 0. 0. 1. 1. 1. 0. 0. 0.
1. 0. 0. 0. 0. 1. 1. 1. 0. 1. 0. 1. 1. 0. 1. 0. 0. 1.
0. 0. 0. 1. 1. 0. 0. 0. 1. 1. 0. 1. 0. 1. 0. 0. 1. 0.
1. 1. 1. 1. 1. 1. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 1. 1.
0. 0. 0. 0. 0. 0. 1. 0. 1. 1. 1. 0. 1. 1. 0. 1. 1. 1.
0. 1. 0. 1. 0. 0. 0. 1. 1. 1. 0. 1. 0. 1. 1. 1. 0. 0.
0. 1. 0. 1. 1. 0. 0. 0. 1. 0. 1. 0. 1. 0. 1. 0. 0. 0.
1. 1. 0. 1. 0. 1. 1. 1. 1. 1. 0. 1. 1. 0. 0. 0. 0. 1.
1. 1. 0. 1. 1. 1. 0. 1. 0. 1. 1. 0. 1. 0. 0. 1. 0. 1.
0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1. 1. 0. 0. 1. 1. 1. 0.
0. 0. 1. 1. 0. 0. 0. 0. 0. 0. 1. 1. 0. 0. 0. 0. 1. 1.
1. 1. 0. 1.]
prediction on D
[0 1 0 0 1 1 1 0 1 1 1 1 1 1 1 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 1 1 1 0 1 1 0
0 0 0 0 0 1 1 1 0 1 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 0 1 0 0 1 1 0 1 0 0 1 1
0 0 0 0 0 1 1 1 0 1 0 0 0 0 1 0 0 0 0 1 0 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1 0
1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 1 1 0 1 0 1 1 0 1 1 1 0 0 0 0 0 1 0 0 0 0 0
1 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1 1 1 0 0 1 1 1 1 1 0 1 0 1 1 1
0 0 0 1 0 1 1 0 1 1 0 1 1 1 1 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 0 1 0 0 0 0 1
1 1 0 1 0 1 1 0 1 0 0 1 0 0 0 1 1 0 0 0 1 1 0 1 0 1 0 0 1 0 1 1 1 1 1 1 0
0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 1 0 1 1 1 0 1 1 0 1 1 1 0 1 0 1 0 0 0 1
1 1 0 1 0 1 1 1 0 0 0 1 0 1 1 0 0 0 1 0 1 0 1 0 1 0 0 0 1 1 0 1 0 1 1 1 1
1 0 1 1 0 0 0 0 1 1 1 0 1 1 1 0 1 0 1 1 0 1 0 0 1 0 1 0 0 0 0 0 0 1 1 1 1
1 1 0 0 1 1 1 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 1 1 1 1 0 1]
Modify and execute this example to run on GPU with ``floatX=float32`` and Modify and execute this example to run on GPU with ``floatX=float32`` and
time it using the command line ``time python file.py``. (Of course, you may use some of your answer time it using the command line ``time python file.py``. (Of course, you may use some of your answer
...@@ -461,15 +407,15 @@ into a file and run it. ...@@ -461,15 +407,15 @@ into a file and run it.
iters = 1000 iters = 1000
rng = numpy.random.RandomState(22) rng = numpy.random.RandomState(22)
x = shared(numpy.asarrayx = shared(numpy.asarray(rng.rand(vlen), config.floatX)) x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], tensor.exp(x)) f = function([], tensor.exp(x))
print(f.maker.fgraph.toposort()) print(f.maker.fgraph.toposort())
t0 = time.time() t0 = time.time()
for i in xrange(iters): for i in xrange(iters):
r = f() r = f()
t1 = time.time() t1 = time.time()
print("Looping %d times took" % iters, t1 - t0, "seconds") print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is", r) print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, tensor.Elemwise) and if numpy.any([isinstance(x.op, tensor.Elemwise) and
('Gpu' not in type(x.op).__name__) ('Gpu' not in type(x.op).__name__)
for x in f.maker.fgraph.toposort()]): for x in f.maker.fgraph.toposort()]):
...@@ -485,18 +431,10 @@ input *x* is stored on the GPU. ...@@ -485,18 +431,10 @@ input *x* is stored on the GPU.
:hide: :hide:
:options: +ELLIPSIS :options: +ELLIPSIS
$ THEANO_FLAGS=device=cpu python check1.py [Elemwise{exp,no_inplace}(<TensorType(float64, vector)>)]
[Elemwise{exp,no_inplace}(<TensorType(float64, vector)>)] Looping 1000 times took ... seconds
Looping 1000 times took ... seconds Result is ...
Result is ... Used the cpu
Used the cpu
$ THEANO_FLAGS=device=cuda0 python check1.py
Using device cuda0: GeForce GTX 275
[GpuElemwise{exp,no_inplace}(<GpuArray<float64>>), HostFromGpu(gpuarray)(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took ... seconds
Result is ...
Used the gpu
.. code-block:: none .. code-block:: none
...@@ -545,8 +483,8 @@ the GPU object directly. The following code is modifed to do just that. ...@@ -545,8 +483,8 @@ the GPU object directly. The following code is modifed to do just that.
for i in xrange(iters): for i in xrange(iters):
r = f() r = f()
t1 = time.time() t1 = time.time()
print("Looping %d times took" % iters, t1 - t0, "seconds") print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is", numpy.asarray(r)) print("Result is %s" % (numpy.asarray(r),))
if numpy.any([isinstance(x.op, tensor.Elemwise) and if numpy.any([isinstance(x.op, tensor.Elemwise) and
('Gpu' not in type(x.op).__name__) ('Gpu' not in type(x.op).__name__)
for x in f.maker.fgraph.toposort()]): for x in f.maker.fgraph.toposort()]):
...@@ -742,7 +680,7 @@ you feel competent enough, you may try yourself on the corresponding exercises. ...@@ -742,7 +680,7 @@ you feel competent enough, you may try yourself on the corresponding exercises.
**Example: PyCUDA** **Example: PyCUDA**
.. testcode:: .. code-block:: python
# (from PyCUDA's documentation) # (from PyCUDA's documentation)
import pycuda.autoinit import pycuda.autoinit
...@@ -786,7 +724,7 @@ Modify and execute to work for a matrix of shape (20, 10). ...@@ -786,7 +724,7 @@ Modify and execute to work for a matrix of shape (20, 10).
**Example: Theano + PyCUDA** **Example: Theano + PyCUDA**
.. testcode:: .. code-block:: python
import numpy, theano import numpy, theano
import theano.misc.pycuda_init import theano.misc.pycuda_init
...@@ -828,10 +766,10 @@ Modify and execute to work for a matrix of shape (20, 10). ...@@ -828,10 +766,10 @@ Modify and execute to work for a matrix of shape (20, 10).
Use this code to test it: Use this code to test it:
>>> x = theano.tensor.fmatrix() >>> x = theano.tensor.fmatrix()
>>> f = theano.function([x], PyCUDADoubleOp()(x)) >>> f = theano.function([x], PyCUDADoubleOp()(x)) # doctest: +SKIP
>>> xv = numpy.ones((4, 5), dtype="float32") >>> xv = numpy.ones((4, 5), dtype="float32")
>>> assert numpy.allclose(f(xv), xv*2) >>> assert numpy.allclose(f(xv), xv*2) # doctest: +SKIP
>>> print(numpy.asarray(f(xv))) >>> print(numpy.asarray(f(xv))) # doctest: +SKIP
Exercise Exercise
......
...@@ -491,412 +491,6 @@ class T_extending(unittest.TestCase): ...@@ -491,412 +491,6 @@ class T_extending(unittest.TestCase):
return numpy.sub(a, b) return numpy.sub(a, b)
class T_introduction(unittest.TestCase):
# All tests here belong to
# http://deeplearning.net/software/theano/tutorial/introduction.html
# Theano/doc/tutorial/introduction.txt
# Any change you do here also add it to the tutorial !
def test_introduction_1(self):
import theano
from theano import tensor
# declare two symbolic floating-point scalars
a = tensor.dscalar()
b = tensor.dscalar()
# create a simple expression
c = a + b
# convert the expression into a callable object that takes (a,b)
# values as input and computes a value for c
f = theano.function([a, b], c)
# bind 1.5 to 'a', 2.5 to 'b', and evaluate 'c'
assert 4.0 == f(1.5, 2.5)
class T_adding(unittest.TestCase):
# All tests here belong to
# http://deeplearning.net/software/theano/tutorial/adding.html
# Theano/doc/tutorial/adding.txt
# Any change you do here also add it to the tutorial !
def test_adding_1(self):
import theano.tensor as T
from theano import function
x = T.dscalar('x')
y = T.dscalar('y')
z = x + y
f = function([x, y], z)
assert f(2, 3) == numpy.array(5.0)
assert f(16.3, 12.1) == numpy.array(28.4)
def test_adding_2(self):
x = T.dmatrix('x')
y = T.dmatrix('y')
z = x + y
f = function([x, y], z)
assert numpy.all(f([[1, 2], [3, 4]], [[10, 20], [30, 40]]) ==
numpy.array([[ 11., 22.], [ 33., 44.]]))
assert numpy.all(f(numpy.array([[1, 2], [3, 4]])
, numpy.array([[10, 20], [30, 40]])) ==
numpy.array([[ 11., 22.], [ 33., 44.]]))
class T_examples(unittest.TestCase):
# All tests here belog to
# http://deeplearning.net/software/theano/tutorial/examples.html
# Theano/doc/tutorial/examples.txt
# Any change you do here also add it to the tutorial !
def test_examples_1(self):
x = T.dmatrix('x')
s = 1 / (1 + T.exp(-x))
logistic = function([x], s)
assert numpy.allclose( logistic([[0, 1], [-1, -2]]),
array([[ 0.5 , 0.73105858],
[ 0.26894142, 0.11920292]]))
def test_examples_2(self):
x = T.dmatrix('x')
s2 = (1 + T.tanh(x / 2)) / 2
logistic2 = function([x], s2)
assert numpy.allclose(logistic2([[0, 1], [-1, -2]]),
array([[ 0.5 , 0.73105858],
[ 0.26894142, 0.11920292]]))
def test_examples_3(self):
a, b = T.dmatrices('a', 'b')
diff = a - b
abs_diff = abs(diff)
diff_squared = diff**2
f = function([a, b], [diff, abs_diff, diff_squared])
elems = f([[1, 1], [1, 1]], [[0, 1], [2, 3]])
assert numpy.all( elems[0] == array([[ 1., 0.], [-1., -2.]]))
assert numpy.all( elems[1] == array([[ 1., 0.], [ 1., 2.]]))
assert numpy.all( elems[2] == array([[ 1., 0.], [ 1., 4.]]))
def test_examples_4(self):
from theano import pp
x = T.dscalar('x')
y = x**2
gy = T.grad(y, x)
pp(gy) # print out the gradient prior to optimization
'((fill((x ** 2), 1.0) * 2) * (x ** (2 - 1)))'
f = function([x], gy)
assert f(4) == array(8.0)
assert f(94.2) == array(188.40000000000001)
def test_examples_5(self):
x = T.dmatrix('x')
s = T.sum(1 / (1 + T.exp(-x)))
gs = T.grad(s, x)
dlogistic = function([x], gs)
assert numpy.allclose( dlogistic([[0, 1], [-1, -2]]),
array([[ 0.25 , 0.19661193],
[ 0.19661193, 0.10499359]]))
def test_examples_6(self):
from theano import Param
x, y = T.dscalars('x', 'y')
z = x + y
f = function([x, Param(y, default=1)], z)
assert f(33) == array(34.0)
assert f(33, 2) == array(35.0)
def test_examples_7(self):
from theano import Param
x, y, w = T.dscalars('x', 'y', 'w')
z = (x + y) * w
f = function([x, Param(y, default=1), Param(w, default=2, name='w_by_name')], z)
assert f(33) == array(68.0)
assert f(33, 2) == array(70.0)
assert f(33, 0, 1) == array(33.0)
assert f(33, w_by_name=1) == array(34.0)
assert f(33, w_by_name=1, y=0) == array(33.0)
def test_examples_8(self):
from theano import shared
# Force the dtype to int64 to work correctly on 32 bit computer.
# Otherwise, it create by default a int32 on 32 bit computer.
state = shared(0)
inc = T.iscalar('inc')
accumulator = function([inc], state, updates=[(state, state+inc)])
assert state.get_value() == array(0)
assert accumulator(1) == array(0)
assert state.get_value() == array(1)
assert accumulator(300) == array(1)
assert state.get_value() == array(301)
state.set_value(-1)
assert accumulator(3) == array(-1)
assert state.get_value() == array(2)
decrementor = function([inc], state, updates=[(state, state-inc)])
assert decrementor(2) == array(2)
assert state.get_value() == array(0)
fn_of_state = state * 2 + inc
# The type of foo must match the shared variable we are replacing
# with the ``givens``
foo = T.scalar(dtype=state.dtype)
skip_shared = function([inc, foo], fn_of_state,
givens=[(state, foo)])
assert skip_shared(1, 3) == array(7)
assert state.get_value() == array(0)
def test_examples_9(self):
from theano.tensor.shared_randomstreams import RandomStreams
srng = RandomStreams(seed=234)
rv_u = srng.uniform((2, 2))
rv_n = srng.normal((2, 2))
f = function([], rv_u)
g = function([], rv_n, no_default_updates=True) #Not updating rv_n.rng
nearly_zeros = function([], rv_u + rv_u - 2 * rv_u)
f_val0 = f()
f_val1 = f() #different numbers from f_val0
assert numpy.all(f_val0 != f_val1)
g_val0 = g() # different numbers from f_val0 and f_val1
g_val1 = g() # same numbers as g_val0 !!!
assert numpy.all(g_val0 == g_val1)
assert numpy.all(g_val0 != f_val0)
assert numpy.all(g_val0 != f_val1)
nearly_zeros = function([], rv_u + rv_u - 2 * rv_u)
assert numpy.allclose(nearly_zeros(), [[0., 0.], [0., 0.]])
rng_val = rv_u.rng.get_value(borrow=True) # Get the rng for rv_u
rng_val.seed(89234) # seeds the generator
rv_u.rng.set_value(rng_val, borrow=True) # Assign back seeded rng
srng.seed(902340) # seeds rv_u and rv_n with different seeds each
state_after_v0 = rv_u.rng.get_value().get_state()
nearly_zeros() # this affects rv_u's generator
v1 = f()
rng = rv_u.rng.get_value(borrow=True)
rng.set_state(state_after_v0)
rv_u.rng.set_value(rng, borrow=True)
v2 = f() # v2 != v1
v3 = f() # v3 == v1
assert numpy.all(v1 != v2)
assert numpy.all(v1 == v3)
def test_copy_random_state(self):
class Graph():
def __init__(self, seed=123):
self.rng = RandomStreams(seed)
self.y = self.rng.uniform(size=(1,))
g1 = Graph(seed=123)
f1 = theano.function([], g1.y)
g2 = Graph(seed=987)
f2 = theano.function([], g2.y)
# print 'By default, the two functions are out of sync.'
v1 = f1()
v2 = f2()
def copy_random_state(g1, g2):
if isinstance(g1.rng, MRG_RandomStreams):
g2.rng.rstate = g1.rng.rstate
for (su1, su2) in zip(g1.rng.state_updates, g2.rng.state_updates):
su2[0].set_value(su1[0].get_value())
# print 'We now copy the state of the theano random number generators.'
copy_random_state(g1, g2)
v3 = f1()
v4 = f2()
assert numpy.allclose(v1, 0.72803009)
assert numpy.allclose(v2, 0.55056769)
assert numpy.allclose(v3, 0.59044123)
assert numpy.allclose(v4, 0.59044123)
@attr('slow')
def test_examples_real_example(self):
rng = numpy.random
N = 400
feats = 784
D = (rng.randn(N, feats).astype(config.floatX),
rng.randint(size=N, low=0, high=2).astype(config.floatX))
training_steps = 10000
if config.mode in ["DebugMode", "DEBUG_MODE", "FAST_COMPILE"]:
training_steps = 10
# Declare Theano symbolic variables
x = T.matrix("x")
y = T.vector("y")
# The *.03 have been added to have DebugMode don't complain
w = theano.shared(rng.randn(feats).astype(config.floatX) * .03,
name="w")
b = theano.shared(numpy.asarray(0., dtype=config.floatX),
name="b")
print("Initial model:")
print(w.get_value(), b.get_value())
# Construct Theano expression graph
p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b)) # Probability that target = 1
prediction = p_1 > 0.5 # The prediction thresholded
xent = -y * T.log(p_1) - (1-y) * T.log(1-p_1) # Cross-entropy loss function
cost = xent.mean() + 0.01 * (w ** 2).sum() # The cost to minimize
gw, gb = T.grad(cost, [w, b]) # Compute the gradient of the cost
# (we shall return to this in a
# following section of this tutorial)
# Compile
train = theano.function(
inputs=[x, y],
outputs=[prediction, xent],
updates=((w, w - 0.1 * gw), (b, b - 0.1 * gb)))
predict = theano.function(inputs=[x], outputs=prediction)
# Train
for i in range(training_steps):
pred, err = train(D[0], D[1])
print("Final model:")
print(w.get_value(), b.get_value())
print("target values for D:", D[1])
print("prediction on D:", predict(D[0]))
# A user reported that this happened on the mailig list.
assert not numpy.isnan(b.get_value()).any()
assert not numpy.isnan(w.get_value()).any()
class T_aliasing(unittest.TestCase):
# All tests here belog to
# http://deeplearning.net/software/theano/tutorial/aliasing.html
# Theano/doc/tutorial/aliasing.txt
# Any change you do here also add it to the tutorial !
def test_aliasing_1(self):
import numpy, theano
np_array = numpy.ones(2, dtype='float32')
s_default = theano.shared(np_array)
s_false = theano.shared(np_array, borrow=False)
s_true = theano.shared(np_array, borrow=True)
np_array += 1 # now it is an array of 2.0 s
assert numpy.all(s_default.get_value() == array([1.0, 1.0]))
assert numpy.all(s_false.get_value() == array([1.0, 1.0]))
assert numpy.all(s_true.get_value() == array([2.0, 2.0]))
def test_aliasing_2(self):
import numpy, theano
np_array = numpy.ones(2, dtype='float32')
s = theano.shared(np_array)
v_false = s.get_value(borrow=False) # N.B. borrow default is False
v_true = s.get_value(borrow=True)
v_internal = s.get_value(borrow=True, return_internal_type=True)
s.set_value(
# some_inplace_fn
s.get_value(borrow=True).__imul__(2),
borrow=True)
def test_aliasing_3(self):
import theano, theano.tensor
x = theano.tensor.matrix()
y = 2*x
f = theano.function([theano.In(x, borrow=True)], theano.Out(y, borrow=True))
class T_loading_and_saving(unittest.TestCase):
# All tests here belong to
# http://deeplearning.net/software/theano/tutorial/loading_and_saving.html
# Theano/doc/tutorial/loading_and_saving.txt
# Any change you do here also add it to the tutorial !
def test_loading_and_saving_1(self):
import six.moves.cPickle as pickle
import theano, theano.tensor
x = theano.tensor.matrix()
y = 2*x
my_obj = theano.function([theano.In(x, borrow=True)]
, theano.Out(y, borrow=True))
mode_instance = theano.compile.mode.get_mode(None)
if not isinstance(mode_instance, theano.compile.debugmode.DebugMode):
# Here, we work in a temporary directory in order not to clutter
# the Theano repository. Code relative to creating that dir and
# removing it afterwards should _not_ be backported to the tutorial.
from tempfile import mkdtemp
origdir = os.getcwd()
tmpdir = None
try:
tmpdir = mkdtemp()
os.chdir(tmpdir)
f = open('obj.save', 'wb')
pickle.dump(my_obj, f, protocol=pickle.HIGHEST_PROTOCOL)
f.close()
f = open('obj.save', 'rb')
loaded_obj = pickle.load(f)
f.close()
obj1 = my_obj
obj2 = my_obj
obj3 = my_obj
f = open('objects.save', 'wb')
for obj in [obj1, obj2, obj3]:
pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL)
f.close()
f = open('objects.save', 'rb')
loaded_objects = []
for i in range(3):
loaded_objects.append(pickle.load(f))
f.close()
finally:
# Get back to the original dir, and delete the temporary one.
os.chdir(origdir)
if tmpdir is not None:
shutil.rmtree(tmpdir)
class T_modes(unittest.TestCase):
# All tests here belong to
# http://deeplearning.net/software/theano/tutorial/modes.html
# Theano/doc/tutorial/modes.txt
# Any change you do here also add it to the tutorial !
def test_modes_1(self):
x = T.dvector('x')
f = theano.function([x], 10*x, mode='DEBUG_MODE')
assert numpy.all(f([5]) == [50.])
assert numpy.all(f([0]) == [0.])
assert numpy.all(f([7]) == [70.])
class T_using_gpu(unittest.TestCase): class T_using_gpu(unittest.TestCase):
# All tests here belog to # All tests here belog to
# http://deeplearning.net/software/theano/tutorial/using_gpu.html # http://deeplearning.net/software/theano/tutorial/using_gpu.html
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论