Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
569616b3
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
569616b3
编写于
10月 09, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Complete Variable for Python API
上级
1e41a675
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
71 addition
and
8 deletion
+71
-8
python/paddle/v2/framework/graph.py
python/paddle/v2/framework/graph.py
+52
-7
python/paddle/v2/framework/tests/test_variable.py
python/paddle/v2/framework/tests/test_variable.py
+19
-1
未找到文件。
python/paddle/v2/framework/graph.py
浏览文件 @
569616b3
...
...
@@ -12,23 +12,68 @@ class Variable(object):
if
name
is
None
:
name
=
Variable
.
_unique_var_name_
()
self
.
proto
=
self
.
block
.
proto
.
new_var
(
name
)
try
:
self
.
proto
=
self
.
block
.
proto
.
var
(
name
)
is_new_var
=
False
except
core
.
EnforceNotMet
:
self
.
proto
=
self
.
block
.
proto
.
new_var
(
name
)
is_new_var
=
True
if
shape
is
not
None
:
self
.
proto
.
set_shape
(
shape
)
if
is_new_var
:
self
.
proto
.
set_shape
(
shape
)
else
:
old_shape
=
self
.
shape
shape
=
tuple
(
shape
)
if
shape
!=
old_shape
:
raise
ValueError
(
"Variable {0} has been created before. the previous "
"shape is {1}; the new shape is {2}. They are not "
"matched."
.
format
(
self
.
name
,
old_shape
,
shape
))
if
dtype
is
not
None
:
if
not
isinstance
(
dtype
,
core
.
DataType
):
dtype
=
Variable
.
_convert_np_dtype_to_dtype_
(
dtype
)
self
.
proto
.
set_data_type
(
dtype
)
if
is_new_var
:
self
.
proto
.
set_data_type
(
dtype
)
else
:
old_dtype
=
self
.
data_type
()
if
dtype
!=
old_shape
:
raise
ValueError
(
"Variable {0} has been created before. "
"The previous data type is {1}; the new "
"data type is {2}. They are not "
"matched."
.
format
(
self
.
name
,
old_dtype
,
dtype
))
if
lod_level
is
not
None
:
self
.
proto
.
set_lod_level
(
lod_level
)
if
is_new_var
:
self
.
proto
.
set_lod_level
(
lod_level
)
else
:
if
lod_level
!=
self
.
lod_level
:
raise
ValueError
(
"Variable {0} has been created before. "
"The previous lod_level is {1}; the new "
"lod_level is {2}. They are not "
"matched"
.
format
(
self
.
name
,
self
.
lod_level
,
lod_level
))
self
.
block
.
vars
[
name
]
=
self
self
.
op
=
None
# TODO(yuyang18): Get methods
@
property
def
name
(
self
):
return
self
.
proto
.
name
()
@
property
def
shape
(
self
):
# convert to tuple, make it as same as numpy API.
return
tuple
(
self
.
proto
.
shape
())
@
property
def
data_type
(
self
):
return
self
.
proto
.
data_type
()
@
property
def
lod_level
(
self
):
return
self
.
proto
.
lod_level
()
@
staticmethod
def
_unique_var_name_
():
...
...
@@ -79,7 +124,7 @@ class Operator(object):
# TODO
pass
# TODO: Getters
# TODO: Getters
class
Block
(
object
):
...
...
python/paddle/v2/framework/tests/test_variable.py
浏览文件 @
569616b3
import
unittest
from
paddle.v2.framework.graph
import
Variable
from
paddle.v2.framework.graph
import
Variable
,
g_program
import
paddle.v2.framework.core
as
core
import
numpy
as
np
...
...
@@ -17,6 +17,24 @@ class TestVariable(unittest.TestCase):
self
.
assertEqual
(
DT
.
BOOL
,
convert
(
"bool"
))
self
.
assertRaises
(
ValueError
,
lambda
:
convert
(
"int8"
))
def
test_var
(
self
):
b
=
g_program
.
current_block
()
w
=
b
.
create_var
(
dtype
=
"float64"
,
shape
=
[
784
,
100
],
lod_level
=
0
,
name
=
"fc.w"
)
self
.
assertEqual
(
core
.
DataType
.
FP64
,
w
.
data_type
)
self
.
assertEqual
((
784
,
100
),
w
.
shape
)
self
.
assertEqual
(
"fc.w"
,
w
.
name
)
self
.
assertEqual
(
0
,
w
.
lod_level
)
w
=
b
.
create_var
(
name
=
'fc.w'
)
self
.
assertEqual
(
core
.
DataType
.
FP64
,
w
.
data_type
)
self
.
assertEqual
((
784
,
100
),
w
.
shape
)
self
.
assertEqual
(
"fc.w"
,
w
.
name
)
self
.
assertEqual
(
0
,
w
.
lod_level
)
self
.
assertRaises
(
ValueError
,
lambda
:
b
.
create_var
(
name
=
"fc.w"
,
shape
=
(
24
,
100
)))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录