Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
070ae21e
提交
070ae21e
authored
3月 06, 2009
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactored tensor.constant and tensor.value to include an ndim argument. also…
refactored tensor.constant and tensor.value to include an ndim argument. also added ndim argument to as_tensor
上级
50030aa8
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
36 行增加
和
27 行删除
+36
-27
basic.py
theano/tensor/basic.py
+36
-27
没有找到文件。
theano/tensor/basic.py
浏览文件 @
070ae21e
...
...
@@ -59,7 +59,7 @@ def __oplist_tag(thing, tag):
thing
.
__oplist_tags
=
tags
def
as_tensor
(
x
,
name
=
None
):
def
as_tensor
(
x
,
name
=
None
,
ndim
=
None
):
"""Return `x`, transformed into a `Tensor`
This function is often used by `make_node` methods of `Op` subclasses to
...
...
@@ -73,6 +73,8 @@ def as_tensor(x, name = None):
to make an ndarray.
- `name`: str or None
If a new `Result` instance is created, it will be named with this string.
- `ndim`: None or integer
Return a Result with this many dimensions. Raise TypeError if it's not possible.
:Exceptions:
- `ValueError`: raised if an `Apply` with no default output is fetched
...
...
@@ -88,12 +90,23 @@ def as_tensor(x, name = None):
x
=
x
.
outputs
[
0
]
if
isinstance
(
x
,
Result
):
if
isinstance
(
x
.
type
,
scal
.
Scalar
):
return
tensor_from_scalar
(
x
)
x
=
tensor_from_scalar
(
x
)
if
not
isinstance
(
x
.
type
,
Tensor
):
raise
TypeError
(
"Result type field must be a Tensor."
,
x
,
x
.
type
)
return
x
if
ndim
is
None
:
return
x
else
:
if
(
x
.
type
.
ndim
>
ndim
):
#TODO: strip off leading broadcastable dimensions
raise
ValueError
(
'Tensor could not be cast to have
%
i dimensions'
%
ndim
,
x
.
type
)
elif
(
x
.
type
.
ndim
<
ndim
):
return
shape_padleft
(
x
,
n_ones
=
(
ndim
-
x
.
type
.
ndim
))
else
:
return
x
try
:
return
constant
(
x
)
return
constant
(
x
,
name
=
name
,
ndim
=
ndim
)
except
TypeError
:
try
:
str_x
=
str
(
x
)
...
...
@@ -105,43 +118,39 @@ def as_tensor(x, name = None):
# to upcast their arguments... this internal-use function is a good place to put debugging stuff, better than the global astensor.
_as_tensor
=
as_tensor
def
constant
(
x
,
name
=
None
):
def
constant_or_value
(
x
,
rtype
,
name
=
None
,
ndim
=
None
):
"""Return a symbolic `Constant` with value `x`
:Exceptions:
- `TypeError`: `x` could not be converted to a numpy.ndarray
"""
if
isinstance
(
x
,
numpy
.
ndarray
):
x_
=
x
else
:
x_
=
numpy
.
asarray
(
x
)
try
:
return
TensorConstant
(
Tensor
(
dtype
=
x_
.
dtype
,
broadcastable
=
[
d
==
1
for
d
in
x_
.
shape
]),
x_
,
name
=
name
)
except
:
raise
TypeError
(
"Could not convert
%
s to Tensor"
%
x
,
type
(
x
))
- `ValueError`: `x` could not be expanded to have ndim dimensions
def
value
(
x
,
name
=
None
):
"""Return a symbolic `Value` with default value `x`
:Exceptions:
- `TypeError`: `x` could not be converted to a numpy.ndarray
"""
if
isinstance
(
x
,
numpy
.
ndarray
):
x_
=
x
else
:
x_
=
numpy
.
asarray
(
x
)
bcastable
=
[
d
==
1
for
d
in
x_
.
shape
]
if
ndim
is
not
None
:
if
len
(
bcastable
)
<
ndim
:
bcastable
=
[
True
]
*
(
ndim
-
len
(
bcastable
))
+
bcastable
elif
len
(
bcastable
)
>
ndim
:
#TODO: strip off dimensions of size 1
raise
ValueError
(
'ndarray could not be cast to constant with
%
i dimensions'
%
ndim
)
assert
len
(
bcastable
)
==
ndim
try
:
if
name
is
None
:
return
TensorValue
(
Tensor
(
dtype
=
x_
.
dtype
,
broadcastable
=
[
d
==
1
for
d
in
x_
.
shape
]),
x_
)
else
:
return
TensorValue
(
Tensor
(
dtype
=
x_
.
dtype
,
broadcastable
=
[
d
==
1
for
d
in
x_
.
shape
]),
x_
,
name
=
name
)
return
rtype
(
Tensor
(
dtype
=
x_
.
dtype
,
broadcastable
=
bcastable
),
x_
,
name
=
name
)
except
:
raise
TypeError
(
"Could not convert
%
s to Tensor"
%
x
,
type
(
x
))
def
constant
(
x
,
name
=
None
,
ndim
=
None
):
return
constant_or_value
(
x
,
rtype
=
TensorConstant
,
name
=
name
,
ndim
=
ndim
)
def
value
(
x
,
name
=
None
,
ndim
=
None
):
return
constant_or_value
(
x
,
rtype
=
TensorValue
,
name
=
name
,
ndim
=
ndim
)
class
Tensor
(
Type
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论