Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
41678c83
提交
41678c83
authored
9月 23, 2009
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added Real and Imag Ops to tensor.basic.
上级
fe6a777b
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
100 行增加
和
0 行删除
+100
-0
basic.py
theano/tensor/basic.py
+77
-0
test_complex.py
theano/tensor/tests/test_complex.py
+23
-0
没有找到文件。
theano/tensor/basic.py
浏览文件 @
41678c83
...
...
@@ -1214,6 +1214,83 @@ def sinh(a):
def
tanh
(
a
):
"""hyperbolic tangent of a"""
class
Real
(
Op
):
"""Extract the real elements of a complex ndarray"""
view_map
=
{
0
:[
0
]}
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
def
__hash__
(
self
):
return
hash
(
type
(
self
))
def
make_node
(
self
,
x
):
_x
=
as_tensor
(
x
)
y_dtype
=
_x
.
type
.
dtype
if
y_dtype
==
'complex64'
:
y_dtype
=
'float32'
if
y_dtype
==
'complex128'
:
y_dtype
=
'float64'
_y
=
Tensor
(
y_dtype
,
_x
.
type
.
broadcastable
)()
return
Apply
(
self
,
[
_x
],
[
_y
])
def
perform
(
self
,
node
,
(
x
,),
(
y
,)):
if
str
(
x
.
dtype
)
.
startswith
(
'complex'
):
y
[
0
]
=
x
.
real
else
:
y
[
0
]
=
x
def
grad
(
self
,
inputs
,
(
g_y
,)):
#TODO: waiting on a Complex(real=, imag=) op that can merge
#things back into a complex tensor
raise
NotImplementedError
()
_real
=
Real
()
@constructor
def
real
(
x
):
"""Return the real part of real or complex-valued `x`
For real-valued `x`, `x` itself is returned.
"""
_x
=
as_tensor_variable
(
x
)
if
_x
.
type
.
dtype
.
startswith
(
'complex'
):
return
_real
(
x
)
else
:
return
_x
class
Imag
(
Op
):
"""Extract the imaginary elements of a complex ndarray"""
view_map
=
{
0
:[
0
]}
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
def
__hash__
(
self
):
return
hash
(
type
(
self
))
def
make_node
(
self
,
x
):
_x
=
as_tensor_variable
(
x
)
if
not
_x
.
type
.
dtype
.
startswith
(
'complex'
):
raise
TypeError
(
'Imag(x) requires complex x'
,
x
)
if
_x
.
type
.
dtype
==
'complex64'
:
y_dtype
=
'float32'
elif
_x
.
type
.
dtype
==
'complex128'
:
y_dtype
=
'float64'
else
:
raise
NotImplementedError
(
'what is this?'
,
y_dtype
)
_y
=
Tensor
(
y_dtype
,
_x
.
type
.
broadcastable
)()
return
Apply
(
self
,
[
_x
],
[
_y
])
def
perform
(
self
,
node
,
(
x
,),
(
y
,)):
if
str
(
x
.
dtype
)
.
startswith
(
'complex'
):
y
[
0
]
=
x
.
imag
else
:
y
[
0
]
=
x
*
0
def
grad
(
self
,
inputs
,
(
g_y
,)):
# TODO: waiting on a complex(real=, imag=) op that can merge
# things back into a complex tensor
raise
NotImplementedError
()
_imag
=
Imag
()
@constructor
def
imag
(
x
):
"""Return the imaginary part of real or complex-valued `x`
For real-valued 'x' this returns `zeros_like(x)`.
"""
_x
=
as_tensor_variable
(
x
)
if
_x
.
type
.
dtype
.
startswith
(
'complex'
):
return
_imag
(
x
)
else
:
return
zeros_like
(
x
)
##########################
# Misc
...
...
theano/tensor/tests/test_complex.py
0 → 100644
浏览文件 @
41678c83
import
unittest
import
theano
from
theano.tensor
import
*
class
TestRealImag
(
unittest
.
TestCase
):
def
test0
(
self
):
x
=
zvector
()
rng
=
numpy
.
random
.
RandomState
(
23
)
xval
=
numpy
.
asarray
(
list
(
numpy
.
complex
(
rng
.
randn
(),
rng
.
randn
())
for
i
in
xrange
(
10
)))
assert
numpy
.
all
(
xval
.
real
==
theano
.
function
([
x
],
real
(
x
))(
xval
))
assert
numpy
.
all
(
xval
.
imag
==
theano
.
function
([
x
],
imag
(
x
))(
xval
))
def
test_on_real_input
(
self
):
x
=
dvector
()
rng
=
numpy
.
random
.
RandomState
(
23
)
xval
=
rng
.
randn
(
10
)
assert
numpy
.
all
(
0
==
theano
.
function
([
x
],
imag
(
x
))(
xval
))
assert
numpy
.
all
(
xval
==
theano
.
function
([
x
],
real
(
x
))(
xval
))
def
test_cast
(
self
):
x
=
zvector
()
self
.
failUnlessRaises
(
TypeError
,
cast
,
x
,
'int32'
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论