<s> from __future__ import absolute_import , print_function , division <EOL> import numpy <EOL> import unittest <EOL> import theano <EOL> from theano . tensor import Tensor , TensorType <EOL> from theano . compile . sharedvalue import shared <EOL> from theano . compile . sharedvalue import SharedVariable <EOL> from theano . compile . sharedvalue import generic <EOL> class Test_SharedVariable ( unittest . TestCase ) : <EOL> def test_ctors ( self ) : <EOL> if theano . configdefaults . python_int_bitwidth ( ) == <NUM_LIT:32> : <EOL> assert shared ( <NUM_LIT:7> ) . type == theano . tensor . iscalar , shared ( <NUM_LIT:7> ) . type <EOL> else : <EOL> assert shared ( <NUM_LIT:7> ) . type == theano . tensor . lscalar , shared ( <NUM_LIT:7> ) . type <EOL> assert shared ( <NUM_LIT> ) . type == theano . tensor . dscalar <EOL> assert shared ( numpy . float32 ( <NUM_LIT:7> ) ) . type == theano . tensor . fscalar <EOL> b = shared ( numpy . zeros ( ( <NUM_LIT:5> , <NUM_LIT:5> ) , dtype = '<STR_LIT>' ) ) <EOL> assert b . type == TensorType ( '<STR_LIT>' , broadcastable = [ False , False ] ) <EOL> b = shared ( numpy . random . rand ( <NUM_LIT:4> , <NUM_LIT:5> ) ) <EOL> assert b . type == TensorType ( '<STR_LIT>' , broadcastable = [ False , False ] ) <EOL> b = shared ( numpy . random . rand ( <NUM_LIT:5> , <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> assert b . type == TensorType ( '<STR_LIT>' , broadcastable = [ False , False , False ] ) <EOL> assert shared ( [ ] ) . type == generic <EOL> def badfunc ( ) : <EOL> shared ( <NUM_LIT:7> , bad_kw = False ) <EOL> self . assertRaises ( TypeError , badfunc ) <EOL> def test_strict_generic ( self ) : <EOL> u = shared ( '<STR_LIT>' , strict = False ) <EOL> v = shared ( '<STR_LIT>' , strict = True ) <EOL> u . set_value ( <NUM_LIT> ) <EOL> v . set_value ( <NUM_LIT> ) <EOL> def test_create_numpy_strict_false ( self ) : <EOL> SharedVariable ( <EOL> name = '<STR_LIT:u>' , <EOL> type = Tensor ( broadcastable = [ False ] , dtype = '<STR_LIT>' ) , <EOL> value = numpy . asarray ( [ <NUM_LIT:1.> , <NUM_LIT> ] ) , <EOL> strict = False ) <EOL> SharedVariable ( <EOL> name = '<STR_LIT:u>' , <EOL> type = Tensor ( broadcastable = [ False ] , dtype = '<STR_LIT>' ) , <EOL> value = [ <NUM_LIT:1.> , <NUM_LIT> ] , <EOL> strict = False ) <EOL> SharedVariable ( <EOL> name = '<STR_LIT:u>' , <EOL> type = Tensor ( broadcastable = [ False ] , dtype = '<STR_LIT>' ) , <EOL> value = [ <NUM_LIT:1> , <NUM_LIT:2> ] , <EOL> strict = False ) <EOL> try : <EOL> SharedVariable ( <EOL> name = '<STR_LIT:u>' , <EOL> type = Tensor ( broadcastable = [ False ] , dtype = '<STR_LIT>' ) , <EOL> value = dict ( ) , <EOL> strict = False ) <EOL> assert <NUM_LIT:0> <EOL> except TypeError : <EOL> pass <EOL> def test_use_numpy_strict_false ( self ) : <EOL> u = SharedVariable ( <EOL> name = '<STR_LIT:u>' , <EOL> type = Tensor ( broadcastable = [ False ] , dtype = '<STR_LIT>' ) , <EOL> value = numpy . asarray ( [ <NUM_LIT:1.> , <NUM_LIT> ] ) , <EOL> strict = False ) <EOL> u . set_value ( [ <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> assert type ( u . get_value ( ) ) is numpy . ndarray <EOL> assert str ( u . get_value ( borrow = True ) . dtype ) == '<STR_LIT>' <EOL> assert numpy . all ( u . get_value ( ) == [ <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> try : <EOL> u . set_value ( '<STR_LIT>' ) <EOL> assert <NUM_LIT:0> <EOL> except ValueError : <EOL> pass <EOL> uval = theano . _asarray ( [ <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ] , dtype = '<STR_LIT>' ) <EOL> u . set_value ( uval , borrow = True ) <EOL> assert u . get_value ( borrow = True ) is uval <EOL> def test_scalar_strict ( self ) : <EOL> def f ( var , val ) : <EOL> var . set_value ( val ) <EOL> b = shared ( numpy . int64 ( <NUM_LIT:7> ) , strict = True ) <EOL> assert b . type == theano . tensor . lscalar <EOL> self . assertRaises ( TypeError , f , b , <NUM_LIT> ) <EOL> b = shared ( numpy . int32 ( <NUM_LIT:7> ) , strict = True ) <EOL> assert b . type == theano . tensor . iscalar <EOL> self . assertRaises ( TypeError , f , b , <NUM_LIT> ) <EOL> b = shared ( numpy . int16 ( <NUM_LIT:7> ) , strict = True ) <EOL> assert b . type == theano . tensor . wscalar <EOL> self . assertRaises ( TypeError , f , b , <NUM_LIT> ) <EOL> b = shared ( numpy . int8 ( <NUM_LIT:7> ) , strict = True ) <EOL> assert b . type == theano . tensor . bscalar <EOL> self . assertRaises ( TypeError , f , b , <NUM_LIT> ) <EOL> b = shared ( numpy . float64 ( <NUM_LIT> ) , strict = True ) <EOL> assert b . type == theano . tensor . dscalar <EOL> self . assertRaises ( TypeError , f , b , <NUM_LIT:8> ) <EOL> b = shared ( numpy . float32 ( <NUM_LIT> ) , strict = True ) <EOL> assert b . type == theano . tensor . fscalar <EOL> self . assertRaises ( TypeError , f , b , <NUM_LIT:8> ) <EOL> b = shared ( numpy . float ( <NUM_LIT> ) , strict = True ) <EOL> assert b . type == theano . tensor . dscalar <EOL> self . assertRaises ( TypeError , f , b , <NUM_LIT:8> ) <EOL> b = shared ( <NUM_LIT> , strict = True ) <EOL> assert b . type == theano . tensor . dscalar <EOL> self . assertRaises ( TypeError , f , b , <NUM_LIT:8> ) <EOL> b = shared ( numpy . zeros ( ( <NUM_LIT:5> , <NUM_LIT:5> ) , dtype = '<STR_LIT>' ) ) <EOL> self . assertRaises ( TypeError , f , b , numpy . random . rand ( <NUM_LIT:5> , <NUM_LIT:5> ) ) <EOL> def test_tensor_strict ( self ) : <EOL> def f ( var , val ) : <EOL> var . set_value ( val ) <EOL> b = shared ( numpy . int64 ( [ <NUM_LIT:7> ] ) , strict = True ) <EOL> assert b . type == theano . tensor . lvector <EOL> self . assertRaises ( TypeError , f , b , <NUM_LIT> ) <EOL> b = shared ( numpy . int32 ( [ <NUM_LIT:7> ] ) , strict = True ) <EOL> assert b . type == theano . tensor . ivector <EOL> self . assertRaises ( TypeError , f , b , <NUM_LIT> ) <EOL> b = shared ( numpy . int16 ( [ <NUM_LIT:7> ] ) , strict = True ) <EOL> assert b . type == theano . tensor . wvector <EOL> self . assertRaises ( TypeError , f , b , <NUM_LIT> ) <EOL> b = shared ( numpy . int8 ( [ <NUM_LIT:7> ] ) , strict = True ) <EOL> assert b . type == theano . tensor . bvector <EOL> self . assertRaises ( TypeError , f , b , <NUM_LIT> ) <EOL> b = shared ( numpy . float64 ( [ <NUM_LIT> ] ) , strict = True ) <EOL> assert b . type == theano . tensor . dvector <EOL> self . assertRaises ( TypeError , f , b , <NUM_LIT:8> ) <EOL> b = shared ( numpy . float32 ( [ <NUM_LIT> ] ) , strict = True ) <EOL> assert b . type == theano . tensor . fvector <EOL> self . assertRaises ( TypeError , f , b , <NUM_LIT:8> ) <EOL> b = shared ( numpy . zeros ( ( <NUM_LIT:5> , <NUM_LIT:5> ) , dtype = '<STR_LIT>' ) ) <EOL> self . assertRaises ( TypeError , f , b , numpy . random . rand ( <NUM_LIT:5> , <NUM_LIT:5> ) ) <EOL> def test_scalar_floatX ( self ) : <EOL> def f ( var , val ) : <EOL> var . set_value ( val ) <EOL> b = shared ( numpy . int64 ( <NUM_LIT:7> ) , allow_downcast = True ) <EOL> assert b . type == theano . tensor . lscalar <EOL> f ( b , <NUM_LIT> ) <EOL> assert b . get_value ( ) == <NUM_LIT:8> <EOL> b = shared ( numpy . int32 ( <NUM_LIT:7> ) , allow_downcast = True ) <EOL> assert b . type == theano . tensor . iscalar <EOL> f ( b , <NUM_LIT> ) <EOL> assert b . get_value ( ) == <NUM_LIT:8> <EOL> b = shared ( numpy . int16 ( <NUM_LIT:7> ) , allow_downcast = True ) <EOL> assert b . type == theano . tensor . wscalar <EOL> f ( b , <NUM_LIT> ) <EOL> assert b . get_value ( ) == <NUM_LIT:8> <EOL> b = shared ( numpy . int8 ( <NUM_LIT:7> ) , allow_downcast = True ) <EOL> assert b . type == theano . tensor . bscalar <EOL> f ( b , <NUM_LIT> ) <EOL> assert b . get_value ( ) == <NUM_LIT:8> <EOL> b = shared ( numpy . float64 ( <NUM_LIT> ) , allow_downcast = True ) <EOL> assert b . type == theano . tensor . dscalar <EOL> f ( b , <NUM_LIT:8> ) <EOL> assert b . get_value ( ) == <NUM_LIT:8> <EOL> b = shared ( numpy . float32 ( <NUM_LIT> ) , allow_downcast = True ) <EOL> assert b . type == theano . tensor . fscalar <EOL> f ( b , <NUM_LIT:8> ) <EOL> assert b . get_value ( ) == <NUM_LIT:8> <EOL> b = shared ( numpy . float ( <NUM_LIT> ) , allow_downcast = True ) <EOL> assert b . type == theano . tensor . dscalar <EOL> f ( b , <NUM_LIT:8> ) <EOL> assert b . get_value ( ) == <NUM_LIT:8> <EOL> b = shared ( <NUM_LIT> , allow_downcast = True ) <EOL> assert b . type == theano . tensor . dscalar <EOL> f ( b , <NUM_LIT:8> ) <EOL> assert b . get_value ( ) == <NUM_LIT:8> <EOL> b = shared ( numpy . zeros ( ( <NUM_LIT:5> , <NUM_LIT:5> ) , dtype = '<STR_LIT>' ) ) <EOL> self . assertRaises ( TypeError , f , b , numpy . random . rand ( <NUM_LIT:5> , <NUM_LIT:5> ) ) <EOL> def test_tensor_floatX ( self ) : <EOL> def f ( var , val ) : <EOL> var . set_value ( val ) <EOL> b = shared ( numpy . int64 ( [ <NUM_LIT:7> ] ) , allow_downcast = True ) <EOL> assert b . type == theano . tensor . lvector <EOL> f ( b , [ <NUM_LIT> ] ) <EOL> assert b . get_value ( ) == <NUM_LIT:8> <EOL> b = shared ( numpy . int32 ( [ <NUM_LIT:7> ] ) , allow_downcast = True ) <EOL> assert b . type == theano . tensor . ivector <EOL> f ( b , [ <NUM_LIT> ] ) <EOL> assert b . get_value ( ) == <NUM_LIT:8> <EOL> b = shared ( numpy . int16 ( [ <NUM_LIT:7> ] ) , allow_downcast = True ) <EOL> assert b . type == theano . tensor . wvector <EOL> f ( b , [ <NUM_LIT> ] ) <EOL> assert b . get_value ( ) == <NUM_LIT:8> <EOL> b = shared ( numpy . int8 ( [ <NUM_LIT:7> ] ) , allow_downcast = True ) <EOL> assert b . type == theano . tensor . bvector <EOL> f ( b , [ <NUM_LIT> ] ) <EOL> assert b . get_value ( ) == <NUM_LIT:8> <EOL> b = shared ( numpy . float64 ( [ <NUM_LIT> ] ) , allow_downcast = True ) <EOL> assert b . type == theano . tensor . dvector <EOL> f ( b , [ <NUM_LIT:8> ] ) <EOL> assert b . get_value ( ) == <NUM_LIT:8> <EOL> b = shared ( numpy . float32 ( [ <NUM_LIT> ] ) , allow_downcast = True ) <EOL> assert b . type == theano . tensor . fvector <EOL> f ( b , [ <NUM_LIT:8> ] ) <EOL> assert b . get_value ( ) == <NUM_LIT:8> <EOL> b = shared ( numpy . asarray ( [ <NUM_LIT> ] , dtype = theano . config . floatX ) , <EOL> allow_downcast = True ) <EOL> assert b . dtype == theano . config . floatX <EOL> f ( b , [ <NUM_LIT:8> ] ) <EOL> assert b . get_value ( ) == <NUM_LIT:8> <EOL> b = shared ( numpy . zeros ( ( <NUM_LIT:5> , <NUM_LIT:5> ) , dtype = '<STR_LIT>' ) ) <EOL> self . assertRaises ( TypeError , f , b , numpy . random . rand ( <NUM_LIT:5> , <NUM_LIT:5> ) ) <EOL> def test_err_symbolic_variable ( self ) : <EOL> self . assertRaises ( TypeError , shared , theano . tensor . ones ( ( <NUM_LIT:2> , <NUM_LIT:3> ) ) ) <EOL> shared ( numpy . ones ( ( <NUM_LIT:2> , <NUM_LIT:4> ) ) ) </s>
