提交 f747933f authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Add testcode for typed_list and get rid of test_tutorial.py

上级 87025fca
...@@ -15,32 +15,29 @@ ...@@ -15,32 +15,29 @@
tensor. tensor.
This is a type that represents a list in Theano. All elements must have This is a type that represents a list in Theano. All elements must have
the same Theano type. Here is an example:: the same Theano type. Here is an example:
import theano.typed_list >>> import theano.typed_list
>>> tl = theano.typed_list.TypedListType(theano.tensor.fvector)()
tl = theano.typed_list.TypedListType(theano.tensor.fvector)() >>> v = theano.tensor.fvector()
v = theano.tensor.fvector() >>> o = theano.typed_list.append(tl, v)
o = theano.typed_list.append(tl, v) >>> f = theano.function([tl, v], o)
f = theano.function([tl, v], o) >>> f([[1, 2, 3], [4, 5]], [2])
print f([[1, 2, 3], [4, 5]], [2]) [array([ 1., 2., 3.], dtype=float32), array([ 4., 5.], dtype=float32), array([ 2.], dtype=float32)]
#[array([ 1., 2., 3.], dtype=float32), array([ 4., 5.], dtype=float32), array([ 2.], dtype=float32)]
A second example with Scan. Scan doesn't yet have direct support of A second example with Scan. Scan doesn't yet have direct support of
TypedList, so you can only use it as non_sequences (not in sequences or TypedList, so you can only use it as non_sequences (not in sequences or
as outputs):: as outputs):
import theano.typed_list >>> import theano.typed_list
>>> a = theano.typed_list.TypedListType(theano.tensor.fvector)()
a = theano.typed_list.TypedListType(theano.tensor.fvector)() >>> l = theano.typed_list.length(a)
l = theano.typed_list.length(a) >>> s, _ = theano.scan(fn=lambda i, tl: tl[i].sum(),
s, _ = theano.scan(fn=lambda i, tl: tl[i].sum(), ... non_sequences=[a],
non_sequences=[a], ... sequences=[theano.tensor.arange(l, dtype='int64')])
sequences=[theano.tensor.arange(l, dtype='int64')]) >>> f = theano.function([a], s)
>>> f([[1, 2, 3], [4, 5]])
f = theano.function([a], s) array([ 6., 9.], dtype=float32)
f([[1, 2, 3], [4, 5]])
#array([ 6., 9.], dtype=float32)
.. automodule:: theano.typed_list.basic .. automodule:: theano.typed_list.basic
:members: :members:
...@@ -37,7 +37,6 @@ whitelist_flake8 = [ ...@@ -37,7 +37,6 @@ whitelist_flake8 = [
"tests/test_pickle_unpickle_theano_fn.py", "tests/test_pickle_unpickle_theano_fn.py",
"tests/test_determinism.py", "tests/test_determinism.py",
"tests/record.py", "tests/record.py",
"tests/test_tutorial.py",
"tests/unittest_tools.py", "tests/unittest_tools.py",
"compile/__init__.py", "compile/__init__.py",
"compile/profiling.py", "compile/profiling.py",
......
""" test code snippet in the Theano tutorials.
"""
from __future__ import print_function
import os
import shutil
import unittest
from nose.plugins.attrib import attr
from nose.plugins.skip import SkipTest
import numpy
from numpy import array
import theano
import theano.tensor as T
from theano import function, compat
from six.moves import xrange
from theano import config
from theano.tests import unittest_tools as utt
from theano.sandbox.rng_mrg import MRG_RandomStreams
from theano.tensor.shared_randomstreams import RandomStreams
class T_typedlist(unittest.TestCase):
# All tests here belong to
# http://deeplearning.net/software/theano/library/typed_list.html
# Theano/doc/library/typed_list.txt
# Any change you do here must also be done in the documentation !
def test_typedlist_basic(self):
import theano.typed_list
tl = theano.typed_list.TypedListType(theano.tensor.fvector)()
v = theano.tensor.fvector()
o = theano.typed_list.append(tl, v)
f = theano.function([tl, v], o)
output = f([[1, 2, 3], [4, 5]], [2])
# Validate ouput is as expected
expected_output = [numpy.array([1, 2, 3], dtype="float32"),
numpy.array([4, 5], dtype="float32"),
numpy.array([2], dtype="float32")]
assert len(output) == len(expected_output)
for i in range(len(output)):
utt.assert_allclose(output[i], expected_output[i])
def test_typedlist_with_scan(self):
import theano.typed_list
a = theano.typed_list.TypedListType(theano.tensor.fvector)()
l = theano.typed_list.length(a)
s, _ = theano.scan(fn=lambda i, tl: tl[i].sum(),
non_sequences=[a],
sequences=[theano.tensor.arange(l, dtype='int64')])
f = theano.function([a], s)
output = f([[1, 2, 3], [4, 5]])
# Validate ouput is as expected
expected_output = numpy.array([6, 9], dtype="float32")
utt.assert_allclose(output, expected_output)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论