提交 d7fa47aa authored 作者: David Warde-Farley's avatar David Warde-Farley

Merged with the last thing before Fred's updates which conflict.

...@@ -34,7 +34,7 @@ theano.config. For example, to see a list of all active configuration ...@@ -34,7 +34,7 @@ theano.config. For example, to see a list of all active configuration
variables, type this from the command-line: variables, type this from the command-line:
.. code-block:: bash .. code-block:: bash
python -c 'import theano; print theano.config' | less python -c 'import theano; print theano.config' | less
Environment Variables Environment Variables
...@@ -52,7 +52,7 @@ Environment Variables ...@@ -52,7 +52,7 @@ Environment Variables
.. code-block:: bash .. code-block:: bash
THEANO_FLAGS='floatX=float32,device=gpu0,nvcc.fastmath' python <myscript>.py THEANO_FLAGS='floatX=float32,device=gpu0,nvcc.fastmath=True' python <myscript>.py
If a value is defined several times in ``THEANO_FLAGS``, If a value is defined several times in ``THEANO_FLAGS``,
the right-most definition is used. So, for instance, if the right-most definition is used. So, for instance, if
...@@ -86,7 +86,7 @@ that you might want to use. For the complete list (including documentation), ...@@ -86,7 +86,7 @@ that you might want to use. For the complete list (including documentation),
import theano and print the config variable, as in: import theano and print the config variable, as in:
.. code-block:: bash .. code-block:: bash
python -c 'import theano; print theano.config' | less python -c 'import theano; print theano.config' | less
...@@ -164,7 +164,7 @@ Config Attributes ...@@ -164,7 +164,7 @@ Config Attributes
This flag allows a new user not to get warnings about old bugs, that were fixed This flag allows a new user not to get warnings about old bugs, that were fixed
before their first checkout of Theano. before their first checkout of Theano.
You can set its value to the first version of Theano You can set its value to the first version of Theano
that you used (probably 0.3 or higher) that you used (probably 0.3 or higher)
`None` mean that all warnings will be displayed. `None` mean that all warnings will be displayed.
......
差异被折叠。
#!/usr/bin/env python
__docformat__ = 'restructuredtext en'
import difflib
import operator
import os
import string
from StringIO import StringIO
from subprocess import Popen, PIPE
import sys
import tabnanny
import tokenize
import argparse
import reindent
def get_parse_error(code):
"""
Checks code for ambiguous tabs or other basic parsing issues.
:param code: a string containing a file's worth of Python code
:returns: a string containing a description of the first parse error encountered,
or None if the code is ok
"""
# note that this uses non-public elements from stdlib's tabnanny, because tabnanny
# is (very frustratingly) written only to be used as a script, but using it that way
# in this context requires writing temporarily files, running subprocesses, blah blah blah
code_buffer = StringIO(code)
try:
tabnanny.process_tokens(tokenize.generate_tokens(code_buffer.readline))
except tokenize.TokenError as err:
return "Could not parse code: {err}".format(err=err)
except IndentationError as err:
return "Indentation error: {err}".format(err=err)
except tabnanny.NannyNag as err:
return "Ambiguous tab at line {line_number}; line is '{line}'.".format(line_number=err.get_lineno(),
line=err.get_line())
return None
def get_correct_indentation_diff(code, filename):
"""
Generate a diff to make code correctly indented.
:param code: a string containing a file's worth of Python code
:param filename: the filename being considered (used in diff generation only)
:returns: a unified diff to make code correctly indented, or
None if code is already correctedly indented
"""
code_buffer = StringIO(code)
output_buffer = StringIO()
reindenter = reindent.Reindenter(code_buffer)
reindenter.run()
reindenter.write(output_buffer)
reindent_output = output_buffer.getvalue()
output_buffer.close()
if code != reindent_output:
diff_generator = difflib.unified_diff(code.splitlines(True), reindent_output.splitlines(True),
fromfile=filename, tofile=filename + " (reindented)")
# work around http://bugs.python.org/issue2142
diff_tuple = [diff_line if diff_line.endswith("\n") else diff_line + "\n\\ No newline at end of file\n"
for diff_line in diff_generator]
diff = "".join(diff_tuple)
return diff
else:
return None
def is_merge():
parent2 = os.environ.get("HG_PARENT2", None)
return parent2 is not None and len(parent2) > 0
def parent_commit():
parent1 = os.environ.get("HG_PARENT1", None)
return parent1
class MercurialRuntimeError(Exception):
pass
def run_mercurial_command(hg_command):
hg_subprocess = Popen(hg_command.split(), stdout=PIPE, stderr=PIPE)
hg_out, hg_err = hg_subprocess.communicate()
if len(hg_err) > 0:
raise MercurialRuntimeError(hg_err)
return hg_out
def parse_stdout_filelist(hg_out_filelist):
files = hg_out_filelist.split()
files = [f.strip(string.whitespace + "'") for f in files]
files = filter(operator.truth, files) # get rid of empty entries
return files
def changed_files():
hg_out = run_mercurial_command("hg tip --template '{file_mods}'")
return parse_stdout_filelist(hg_out)
def added_files():
hg_out = run_mercurial_command("hg tip --template '{file_adds}'")
return parse_stdout_filelist(hg_out)
def is_python_file(filename):
return filename.endswith(".py")
def get_file_contents(filename, revision="tip"):
hg_out = run_mercurial_command("hg cat -r {revision} {filename}".format(filename=filename, revision=revision))
return hg_out
def save_commit_message(filename):
commit_message = run_mercurial_command("hg tip --template '{desc}'")
with open(filename, "w") as save_file:
save_file.write(commit_message)
def save_diffs(diffs, filename):
diff = "\n\n".join(diffs)
with open(filename, "w") as diff_file:
diff_file.write(diff)
def main(argv=None):
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser(description="Pretxncommit hook for Mercurial to check for whitespace issues")
parser.add_argument("-n", "--no-indentation",
action="store_const",
default=False,
const=True,
help="don't check indentation, just basic parsing"
)
parser.add_argument("-i", "--incremental",
action="store_const",
default=False,
const=True,
help="only check indentation if the file was previously correctly indented (or is new)"
)
args = parser.parse_args(argv)
if is_merge():
# don't inspect merges: (a) they're complex and (b) they don't really introduce new code
return 0
block_commit = False
diffs = []
added_filenames = added_files()
changed_filenames = changed_files()
for filename in filter(is_python_file, added_filenames + changed_filenames):
code = get_file_contents(filename)
parse_error = get_parse_error(code)
if parse_error is not None:
print >> sys.stderr, "*** {filename} has parse error: {err}".format(filename=filename, err=parse_error)
block_commit = True
else:
# parsing succeeded, it is safe to check indentation
if not args.no_indentation:
if args.incremental and filename in changed_filenames:
# only check it if it was clean before
old_file_contents = get_file_contents(filename, revision=parent_commit())
check_indentation = get_correct_indentation_diff(old_file_contents, "") is None
else:
check_indentation = True
if check_indentation:
indentation_diff = get_correct_indentation_diff(code, filename)
if indentation_diff is not None:
block_commit = True
diffs.append(indentation_diff)
print >> sys.stderr, "{filename} is not correctly indented".format(filename=filename)
if len(diffs) > 0:
diffs_filename = ".hg/indentation_fixes.patch"
save_diffs(diffs, diffs_filename)
print >> sys.stderr, "*** To fix all indentation issues, run: cd `hg root` && patch -p0 < {filename}".format(filename=diffs_filename)
if block_commit:
save_filename = ".hg/commit_message.saved"
save_commit_message(save_filename)
print >> sys.stderr, "*** Commit message saved to {filename}".format(filename=save_filename)
return int(block_commit)
if __name__ == '__main__':
sys.exit(main())
差异被折叠。
import sys, time import sys, time
from theano import shared
from theano.compile.pfunc import pfunc from theano.compile.pfunc import pfunc
from theano import tensor from theano import tensor
...@@ -17,7 +16,7 @@ if cuda_ndarray.cuda_available == False: ...@@ -17,7 +16,7 @@ if cuda_ndarray.cuda_available == False:
import theano.sandbox.cuda as tcn import theano.sandbox.cuda as tcn
import theano.sandbox.cuda as cuda import theano.sandbox.cuda as cuda
import theano.sandbox.cuda.basic_ops as B import theano.sandbox.cuda.basic_ops as B
import theano.compile.mode from theano.tensor.basic import _allclose
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
if theano.config.mode=='FAST_COMPILE': if theano.config.mode=='FAST_COMPILE':
...@@ -98,7 +97,7 @@ def test_sum(): ...@@ -98,7 +97,7 @@ def test_sum():
if val.size==0: if val.size==0:
assert f2(val)==f(val), ('shape', shape, 'pattern', pattern) assert f2(val)==f(val), ('shape', shape, 'pattern', pattern)
else: else:
assert numpy.allclose(f2(val),f(val)), ('shape', shape, 'pattern', pattern) assert _allclose(f2(val),f(val)), ('shape', shape, 'pattern', pattern, sum([shape[i] for i in pattern]))
#test with dimshuffle #test with dimshuffle
...@@ -121,7 +120,7 @@ def test_sum(): ...@@ -121,7 +120,7 @@ def test_sum():
f2 = theano.function([a],b, mode=mode_without_gpu) f2 = theano.function([a],b, mode=mode_without_gpu)
assert tcn.GpuSum in [x.op.__class__ for x in f.maker.env.toposort()] assert tcn.GpuSum in [x.op.__class__ for x in f.maker.env.toposort()]
assert T.Sum in [x.op.__class__ for x in f2.maker.env.toposort()] assert T.Sum in [x.op.__class__ for x in f2.maker.env.toposort()]
assert numpy.allclose(f2(val),f(val)) assert _allclose(f2(val),f(val)), ('shape', shape, 'pattern', pattern, sum([shape[i] for i in pattern]))
#test with broadcast #test with broadcast
...@@ -155,7 +154,7 @@ def test_sum(): ...@@ -155,7 +154,7 @@ def test_sum():
f2 = theano.function([a2],b2, mode=mode_with_gpu) f2 = theano.function([a2],b2, mode=mode_with_gpu)
assert tcn.GpuSum in [x.op.__class__ for x in f2.maker.env.toposort()] assert tcn.GpuSum in [x.op.__class__ for x in f2.maker.env.toposort()]
assert T.Sum in [x.op.__class__ for x in f.maker.env.toposort()] assert T.Sum in [x.op.__class__ for x in f.maker.env.toposort()]
assert numpy.allclose(f2(val2),f(val)) assert _allclose(f2(val2),f(val)), ('shape', shape, 'pattern', pattern, sum([shape[i] for i in pattern]))
def test_flatten(): def test_flatten():
x = cuda.fmatrix('x') x = cuda.fmatrix('x')
......
...@@ -15,7 +15,7 @@ def test_host_to_device(): ...@@ -15,7 +15,7 @@ def test_host_to_device():
c = numpy.asarray(b) c = numpy.asarray(b)
assert numpy.all(a == c) assert numpy.all(a == c)
def test_add(): def test_add_iadd_idiv():
for shape in ((), (0,), (3,), (2,3), (1,10000000),(10,1000000), (100,100000), for shape in ((), (0,), (3,), (2,3), (1,10000000),(10,1000000), (100,100000),
(1000,10000),(10000,1000), (1000,10000),(10000,1000),
(4100,33,34),(33,4100,34),(33,34,4100), (4100,33,34),(33,4100,34),(33,34,4100),
...@@ -51,6 +51,11 @@ def test_add(): ...@@ -51,6 +51,11 @@ def test_add():
assert numpy.allclose(a0, numpy.asarray(b0)) assert numpy.allclose(a0, numpy.asarray(b0))
assert numpy.allclose(a0,a1*2) assert numpy.allclose(a0,a1*2)
b0 /= b1
a0 /= a1
assert numpy.allclose(a0, numpy.asarray(b0))
assert numpy.allclose(a0,numpy.ones(a0.shape)*2)
if len(shape)==2: if len(shape)==2:
#test not contiguous version. #test not contiguous version.
#should raise not implemented. #should raise not implemented.
......
...@@ -367,18 +367,22 @@ class T_RandomStreams(unittest.TestCase): ...@@ -367,18 +367,22 @@ class T_RandomStreams(unittest.TestCase):
made = m.make() made = m.make()
made.random.initialize() made.random.initialize()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) #seed_rng is generator for generating *seeds* for RandomStates
numpy_rng = numpy.random.RandomState(int(rng_seed)) seed_rng = numpy.random.RandomState(utt.fetch_seed())
uniform_rng = numpy.random.RandomState(int(seed_rng.randint(2**30)))
multinomial_rng = numpy.random.RandomState(int(seed_rng.randint(2**30)))
val0 = made.f() val0 = made.f()
val1 = made.f() val1 = made.f()
numpy_val0 = numpy_rng.uniform() numpy_val0 = uniform_rng.uniform()
numpy_val1 = numpy_rng.uniform() numpy_val1 = uniform_rng.uniform()
assert numpy.allclose(val0, numpy_val0) assert numpy.allclose(val0, numpy_val0)
assert numpy.allclose(val1, numpy_val1) assert numpy.allclose(val1, numpy_val1)
val2 = made.g() for i in range(10): # every test has 50% chance of passing even with non-matching random states
numpy_val2 = numpy_rng.multinomial(n=1, pvals=[.5, .5]) val2 = made.g()
assert numpy.all(val2 == numpy_val2) numpy_val2 = multinomial_rng.multinomial(n=1, pvals=[.5, .5])
assert numpy.all(val2 == numpy_val2)
def test_vector_arguments(self): def test_vector_arguments(self):
m = Module() m = Module()
......
...@@ -6,7 +6,7 @@ import numpy ...@@ -6,7 +6,7 @@ import numpy
from theano.tensor import raw_random from theano.tensor import raw_random
from theano.tensor.shared_randomstreams import RandomStreams from theano.tensor.shared_randomstreams import RandomStreams
from theano import function from theano import function, shared
from theano import tensor from theano import tensor
from theano import compile, config, gof from theano import compile, config, gof
...@@ -336,19 +336,23 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -336,19 +336,23 @@ class T_SharedRandomStreams(unittest.TestCase):
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
f = function([], random.uniform()) f = function([], random.uniform())
g = function([], random.multinomial()) g = function([], random.multinomial())
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
numpy_rng = numpy.random.RandomState(int(rng_seed)) #seed_rng is generator for generating *seeds* for RandomStates
seed_rng = numpy.random.RandomState(utt.fetch_seed())
uniform_rng = numpy.random.RandomState(int(seed_rng.randint(2**30)))
multinomial_rng = numpy.random.RandomState(int(seed_rng.randint(2**30)))
val0 = f() val0 = f()
val1 = f() val1 = f()
numpy_val0 = numpy_rng.uniform() numpy_val0 = uniform_rng.uniform()
numpy_val1 = numpy_rng.uniform() numpy_val1 = uniform_rng.uniform()
assert numpy.allclose(val0, numpy_val0) assert numpy.allclose(val0, numpy_val0)
assert numpy.allclose(val1, numpy_val1) assert numpy.allclose(val1, numpy_val1)
val2 = g() for i in range(10): # every test has 50% chance of passing even with non-matching random states
numpy_val2 = numpy_rng.multinomial(n=1, pvals=[.5, .5]) val2 = g()
assert numpy.all(val2 == numpy_val2) numpy_val2 = multinomial_rng.multinomial(n=1, pvals=[.5, .5])
assert numpy.all(val2 == numpy_val2)
def test_vector_arguments(self): def test_vector_arguments(self):
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
...@@ -607,6 +611,85 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -607,6 +611,85 @@ class T_SharedRandomStreams(unittest.TestCase):
assert numpy.all(abs(val1) <= 1) assert numpy.all(abs(val1) <= 1)
def test_shared_constructor_borrow(self):
rng = numpy.random.RandomState(123)
s_rng_default = shared(rng)
s_rng_True = shared(rng, borrow=True)
s_rng_False = shared(rng, borrow=False)
# test borrow contract: that False means a copy must have been made
assert s_rng_default.container.storage[0] is not rng
assert s_rng_False.container.storage[0] is not rng
# test current implementation: that True means a copy was not made
assert s_rng_True.container.storage[0] is rng
# ensure that all the random number generators are in the same state
v = rng.randn()
v0 = s_rng_default.container.storage[0].randn()
v1 = s_rng_False.container.storage[0].randn()
assert v == v0 == v1
def test_get_value_borrow(self):
rng = numpy.random.RandomState(123)
s_rng = shared(rng)
r_ = s_rng.container.storage[0]
r_T = s_rng.get_value(borrow=True)
r_F = s_rng.get_value(borrow=False)
#the contract requires that borrow=False returns a copy
assert r_ is not r_F
# the current implementation allows for True to return the real thing
assert r_ is r_T
#either way, the rngs should all be in the same state
assert r_.rand() == r_F.rand()
def test_get_value_internal_type(self):
rng = numpy.random.RandomState(123)
s_rng = shared(rng)
# there is no special behaviour required of return_internal_type
# this test just ensures that the flag doesn't screw anything up
# by repeating the get_value_borrow test.
r_ = s_rng.container.storage[0]
r_T = s_rng.get_value(borrow=True, return_internal_type=True)
r_F = s_rng.get_value(borrow=False, return_internal_type=True)
#the contract requires that borrow=False returns a copy
assert r_ is not r_F
# the current implementation allows for True to return the real thing
assert r_ is r_T
#either way, the rngs should all be in the same state
assert r_.rand() == r_F.rand()
def test_set_value_borrow(self):
rng = numpy.random.RandomState(123)
s_rng = shared(rng)
new_rng = numpy.random.RandomState(234234)
# Test the borrow contract is respected:
# assigning with borrow=False makes a copy
s_rng.set_value(new_rng, borrow=False)
assert new_rng is not s_rng.container.storage[0]
assert new_rng.randn() == s_rng.container.storage[0].randn()
# Test that the current implementation is actually borrowing when it can.
rr = numpy.random.RandomState(33)
s_rng.set_value(rr, borrow=True)
assert rr is s_rng.container.storage[0]
if __name__ == '__main__': if __name__ == '__main__':
from theano.tests import main from theano.tests import main
main("test_shared_randomstreams") main("test_shared_randomstreams")
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论