Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a4646f2c
提交
a4646f2c
authored
4月 21, 2010
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
revised handling of casting of constant and shared scalars.
The basic idea is to apply autocasting to constants but not to shared scalars. Joint work with Fred.
上级
1ea851e5
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
56 行增加
和
42 行删除
+56
-42
test_pfunc.py
theano/compile/tests/test_pfunc.py
+1
-1
test_shared.py
theano/compile/tests/test_shared.py
+12
-5
safe_asarray.py
theano/misc/safe_asarray.py
+1
-1
basic.py
theano/tensor/basic.py
+32
-17
sharedvar.py
theano/tensor/sharedvar.py
+10
-18
没有找到文件。
theano/compile/tests/test_pfunc.py
浏览文件 @
a4646f2c
...
...
@@ -445,7 +445,7 @@ class Test_pfunc(unittest.TestCase):
def
test_givens_replaces_shared_variable
(
self
):
a
=
shared
(
1.
,
'a'
)
a
.
default_update
=
a
+
3.
b
=
tensor
.
scalar
(
'b'
)
b
=
tensor
.
d
scalar
(
'b'
)
c
=
a
+
10
f
=
pfunc
([
b
],
c
,
givens
=
{
a
:
b
})
...
...
theano/compile/tests/test_shared.py
浏览文件 @
a4646f2c
...
...
@@ -16,9 +16,9 @@ class Test_SharedVariable(unittest.TestCase):
assert
shared
(
7
,
dtype
=
'float64'
)
.
type
==
Scalar
(
'float64'
)
else
:
assert
shared
(
7
)
.
type
==
theano
.
tensor
.
lscalar
assert
shared
(
7.0
)
.
type
==
theano
.
tensor
.
scalar
()
.
type
assert
shared
(
7
,
dtype
=
'float64'
)
.
type
==
theano
.
tensor
.
d
scalar
assert
shared
(
7
)
.
type
==
theano
.
tensor
.
lscalar
,
shared
(
7
)
.
type
assert
shared
(
7.0
)
.
type
==
theano
.
tensor
.
dscalar
assert
shared
(
numpy
.
float32
(
7
))
.
type
==
theano
.
tensor
.
f
scalar
# test tensor constructor
b
=
shared
(
numpy
.
zeros
((
5
,
5
),
dtype
=
'int32'
))
...
...
@@ -169,6 +169,13 @@ class Test_SharedVariable(unittest.TestCase):
def
test_scalar_floatX
(
self
):
#
# the test should assure that floatX is not used in the shared constructor for scalars
# Shared values can change, and since we don't know the range they might take, we
# should keep the same bit width / precision as the original value used to create the
# shared variable.
#
def
f
(
var
,
val
):
var
.
value
=
val
b
=
shared
(
numpy
.
int64
(
7
))
...
...
@@ -184,11 +191,11 @@ class Test_SharedVariable(unittest.TestCase):
f
(
b
,
8
)
b
=
shared
(
numpy
.
float
(
7.234
))
assert
b
.
dtype
==
theano
.
config
.
floatX
assert
b
.
type
==
theano
.
tensor
.
dscalar
f
(
b
,
8
)
b
=
shared
(
7.234
)
assert
b
.
dtype
==
theano
.
config
.
floatX
assert
b
.
type
==
theano
.
tensor
.
dscalar
f
(
b
,
8
)
c
=
shared
(
numpy
.
zeros
((
5
,
5
),
dtype
=
'float32'
))
...
...
theano/misc/safe_asarray.py
浏览文件 @
a4646f2c
...
...
@@ -6,7 +6,7 @@ __docformat__ = "restructuredtext en"
import
numpy
def
_asarray
(
a
,
dtype
=
None
,
order
=
None
):
def
_asarray
(
a
,
dtype
,
order
=
None
):
"""Convert the input to a Numpy array.
This function is almost identical to ``numpy.asarray``, but it should be
...
...
theano/tensor/basic.py
浏览文件 @
a4646f2c
...
...
@@ -154,11 +154,18 @@ as_tensor = as_tensor_variable
class
NumpyAutocaster
(
object
):
""" This class is used to cast python ints and floats to numpy arrays.
The behaviour for numpy scalars is a bit tricky... but tends to work in practice.
If the dtype of a numpy scalar is in the self.dtypes list, then this 'cast' is a no-op.
When config.floatX is float32 (at the time of calling), then this function downcasts float
and numpy.float arguments to numpy.float32, if float32 is in the self.dtypes list.
Python ints are always 64bit and floats are always double precision.
This class uses the algorithm in __call__ to use a narrower dtype when no precision would
be lost, and to even lose precision when this is demanded (e.g. to automatically cast all
floats to single-precision).
be lost, and to even lose precision when this is demanded by the list of dtypes (e.g. to
automatically cast all floats to single-precision if self.dtypes does not include full
precision floats).
"""
def
__init__
(
self
,
dtypes
):
...
...
@@ -166,15 +173,17 @@ class NumpyAutocaster(object):
def
__call__
(
self
,
x
):
# change the default casting behaviour for python floats to always cast to float32
dtype
=
None
if
isinstance
(
x
,
numpy
.
float64
)
and
'float64'
in
self
.
dtypes
:
dtype
=
'float64'
elif
isinstance
(
x
,
numpy
.
float32
)
and
'float32'
in
self
.
dtypes
:
dtype
=
'float32'
elif
isinstance
(
x
,
float
)
and
config
.
floatX
in
self
.
dtypes
and
config
.
floatX
==
'float32'
:
dtype
=
config
.
floatX
if
dtype
:
return
theano
.
_asarray
(
x
,
dtype
=
dtype
)
try
:
# pass through numpy scalars, since they are already typed on purpose typically.
if
str
(
x
.
dtype
)
in
self
.
dtypes
:
return
theano
.
_asarray
(
x
,
dtype
=
x
.
dtype
)
#leave dtype alone
except
AttributeError
:
pass
# unsafe downcast of float64 variables when config.floatX == 'float32'
# recall: float is numpy.float
if
isinstance
(
x
,
float
)
and
config
.
floatX
in
self
.
dtypes
and
config
.
floatX
==
'float32'
:
return
theano
.
_asarray
(
x
,
dtype
=
'float32'
)
for
dtype
in
self
.
dtypes
:
x_
=
theano
.
_asarray
(
x
,
dtype
=
dtype
)
...
...
@@ -1857,15 +1866,21 @@ class Alloc(gof.Op):
def
grad
(
self
,
inputs
,
(
gout
,)):
return
[
None
for
i
in
inputs
]
def
__call__
(
self
,
*
inputs
,
**
kwarg
s
):
def
__call__
(
self
,
val
,
*
shape
s
):
"""
Don't generate alloc that do nothing
.
If the alloc would be useless, this function returns val
.
If you always want an Alloc node, call make_node.
"""
ret
=
super
(
Alloc
,
self
)
.
__call__
(
*
inputs
,
**
kwargs
)
if
inputs
[
0
]
.
type
==
ret
.
type
:
return
inputs
[
0
]
else
:
return
ret
ret
=
super
(
Alloc
,
self
)
.
__call__
(
val
,
*
shapes
)
try
:
#It makes optimization difficult when useless allocs are thrown into the graph at every
#stage of optimization. This little logic tries to help at least in some cases.
if
val
.
type
==
ret
.
type
:
return
val
except
AttributeError
:
pass
return
ret
alloc
=
Alloc
()
pprint
.
assign
(
alloc
,
printing
.
FunctionPrinter
(
'alloc'
))
...
...
theano/tensor/sharedvar.py
浏览文件 @
a4646f2c
import
traceback
import
numpy
import
theano.tensor.basic
from
basic
import
TensorType
,
_tensor_py_operators
from
basic
import
TensorType
,
_tensor_py_operators
,
autocast_int
,
autocast_float
from
theano.compile
import
shared_constructor
,
SharedVariable
from
theano
import
config
...
...
@@ -35,29 +35,22 @@ class ScalarSharedVariable(SharedVariable, _tensor_py_operators):
pass
@shared_constructor
def
scalar_constructor
(
value
,
name
=
None
,
strict
=
False
,
dtype
=
None
):
def
scalar_constructor
(
value
,
name
=
None
,
strict
=
False
):
"""SharedVariable constructor for scalar values. Default: int64 or float64.
:note: We implement this using 0-d tensors for now.
"""
if
not
isinstance
(
value
,
(
numpy
.
number
,
float
,
int
)):
if
not
isinstance
(
value
,
(
numpy
.
number
,
float
,
int
,
complex
)):
raise
TypeError
()
if
dtype
is
None
:
if
isinstance
(
value
,
numpy
.
float64
):
dtype
=
'float64'
elif
isinstance
(
value
,
numpy
.
float32
):
dtype
=
'float32'
elif
isinstance
(
value
,
float
)
and
not
strict
:
dtype
=
config
.
floatX
elif
isinstance
(
value
,
float
):
dtype
=
'float64'
elif
isinstance
(
value
,
int
):
dtype
=
'int64'
else
:
dtype
=
type
(
value
)
.
__name__
try
:
dtype
=
value
.
dtype
except
:
dtype
=
numpy
.
asarray
(
value
)
.
dtype
tensor_type
=
TensorType
(
dtype
=
dtype
,
broadcastable
=
[])
dtype
=
str
(
dtype
)
value
=
theano
.
_asarray
(
value
,
dtype
=
dtype
)
tensor_type
=
TensorType
(
dtype
=
str
(
value
.
dtype
),
broadcastable
=
[])
try
:
# Do not pass the dtype to asarray because we want this to fail if
...
...
@@ -69,4 +62,3 @@ def scalar_constructor(value, name=None, strict=False, dtype=None):
except
:
traceback
.
print_exc
()
raise
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论