Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
ujs_wantz
Paddle
提交
0ca10d31
P
Paddle
项目概览
ujs_wantz
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
0ca10d31
编写于
8月 22, 2020
作者:
S
ShenLiang
提交者:
GitHub
8月 22, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add paddle.static.data (#26525)
* add static data
上级
81c127d4
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
152 addition
and
2 deletion
+152
-2
python/paddle/fluid/data.py
python/paddle/fluid/data.py
+2
-0
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+1
-0
python/paddle/fluid/tests/unittests/test_data.py
python/paddle/fluid/tests/unittests/test_data.py
+46
-0
python/paddle/static/input.py
python/paddle/static/input.py
+103
-2
未找到文件。
python/paddle/fluid/data.py
浏览文件 @
0ca10d31
...
@@ -18,10 +18,12 @@ import six
...
@@ -18,10 +18,12 @@ import six
from
paddle.fluid
import
core
from
paddle.fluid
import
core
from
paddle.fluid.layer_helper
import
LayerHelper
from
paddle.fluid.layer_helper
import
LayerHelper
from
paddle.fluid.data_feeder
import
check_dtype
,
check_type
from
paddle.fluid.data_feeder
import
check_dtype
,
check_type
from
..utils
import
deprecated
__all__
=
[
'data'
]
__all__
=
[
'data'
]
@
deprecated
(
since
=
"2.0.0"
,
update_to
=
"paddle.static.data"
)
def
data
(
name
,
shape
,
dtype
=
'float32'
,
lod_level
=
0
):
def
data
(
name
,
shape
,
dtype
=
'float32'
,
lod_level
=
0
):
"""
"""
**Data Layer**
**Data Layer**
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
0ca10d31
...
@@ -8353,6 +8353,7 @@ def gather_nd(input, index, name=None):
...
@@ -8353,6 +8353,7 @@ def gather_nd(input, index, name=None):
return output
return output
@deprecated(since="2.0.0", update_to="paddle.scatter")
def scatter(input, index, updates, name=None, overwrite=True):
def scatter(input, index, updates, name=None, overwrite=True):
"""
"""
:alias_main: paddle.scatter
:alias_main: paddle.scatter
...
...
python/paddle/fluid/tests/unittests/test_data.py
浏览文件 @
0ca10d31
...
@@ -16,9 +16,11 @@ from __future__ import print_function
...
@@ -16,9 +16,11 @@ from __future__ import print_function
import
unittest
import
unittest
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
import
paddle.fluid.layers
as
layers
import
paddle.fluid.layers
as
layers
from
paddle.fluid
import
Program
,
program_guard
from
paddle.fluid
import
Program
,
program_guard
import
paddle.fluid.core
as
core
class
TestApiDataError
(
unittest
.
TestCase
):
class
TestApiDataError
(
unittest
.
TestCase
):
...
@@ -53,5 +55,49 @@ class TestApiDataError(unittest.TestCase):
...
@@ -53,5 +55,49 @@ class TestApiDataError(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
test_shape_type
)
self
.
assertRaises
(
TypeError
,
test_shape_type
)
class
TestApiStaticDataError
(
unittest
.
TestCase
):
def
test_fluid_dtype
(
self
):
with
program_guard
(
Program
(),
Program
()):
x1
=
paddle
.
static
.
data
(
name
=
"x1"
,
shape
=
[
2
,
25
])
self
.
assertEqual
(
x1
.
dtype
,
core
.
VarDesc
.
VarType
.
FP32
)
x2
=
paddle
.
static
.
data
(
name
=
"x2"
,
shape
=
[
2
,
25
],
dtype
=
"bool"
)
self
.
assertEqual
(
x2
.
dtype
,
core
.
VarDesc
.
VarType
.
BOOL
)
paddle
.
set_default_dtype
(
"float64"
)
x3
=
paddle
.
static
.
data
(
name
=
"x3"
,
shape
=
[
2
,
25
])
self
.
assertEqual
(
x3
.
dtype
,
core
.
VarDesc
.
VarType
.
FP64
)
def
test_fluid_data
(
self
):
with
program_guard
(
Program
(),
Program
()):
# 1. The type of 'name' in fluid.data must be str.
def
test_name_type
():
paddle
.
static
.
data
(
name
=
1
,
shape
=
[
2
,
25
],
dtype
=
"bool"
)
self
.
assertRaises
(
TypeError
,
test_name_type
)
# 2. The type of 'shape' in fluid.data must be list or tuple.
def
test_shape_type
():
paddle
.
static
.
data
(
name
=
'data1'
,
shape
=
2
,
dtype
=
"bool"
)
self
.
assertRaises
(
TypeError
,
test_shape_type
)
def
test_layers_data
(
self
):
with
program_guard
(
Program
(),
Program
()):
# 1. The type of 'name' in layers.data must be str.
def
test_name_type
():
paddle
.
static
.
data
(
name
=
1
,
shape
=
[
2
,
25
],
dtype
=
"bool"
)
self
.
assertRaises
(
TypeError
,
test_name_type
)
# 2. The type of 'shape' in layers.data must be list or tuple.
def
test_shape_type
():
paddle
.
static
.
data
(
name
=
'data1'
,
shape
=
2
,
dtype
=
"bool"
)
self
.
assertRaises
(
TypeError
,
test_shape_type
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
unittest
.
main
()
unittest
.
main
()
python/paddle/static/input.py
浏览文件 @
0ca10d31
...
@@ -12,11 +12,112 @@
...
@@ -12,11 +12,112 @@
# See the License for the specific language governing permissions and
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.
from
..fluid.data
import
data
import
paddle
import
numpy
as
np
import
six
from
paddle.fluid
import
core
from
paddle.fluid.layer_helper
import
LayerHelper
from
paddle.fluid.data_feeder
import
check_dtype
,
check_type
__all__
=
[
'data'
,
'InputSpec'
]
__all__
=
[
'data'
,
'InputSpec'
]
def
data
(
name
,
shape
,
dtype
=
None
,
lod_level
=
0
):
"""
**Data Layer**
This function creates a variable on the global block. The global variable
can be accessed by all the following operators in the graph. The variable
is a placeholder that could be fed with input, such as Executor can feed
input into the variable. When `dtype` is None, the dtype
will get from the global dtype by `paddle.get_default_dtype()`.
Args:
name (str): The name/alias of the variable, see :ref:`api_guide_Name`
for more details.
shape (list|tuple): List|Tuple of integers declaring the shape. You can
set "None" or -1 at a dimension to indicate the dimension can be of any
size. For example, it is useful to set changeable batch size as "None" or -1.
dtype (np.dtype|str, optional): The type of the data. Supported
dtype: bool, float16, float32, float64, int8, int16, int32, int64,
uint8. Default: None. When `dtype` is not set, the dtype will get
from the global dtype by `paddle.get_default_dtype()`.
lod_level (int, optional): The LoD level of the LoDTensor. Usually users
don't have to set this value. For more details about when and how to
use LoD level, see :ref:`user_guide_lod_tensor` . Default: 0.
Returns:
Variable: The global variable that gives access to the data.
Examples:
.. code-block:: python
import numpy as np
import paddle.fluid as fluid
import paddle
# Creates a variable with fixed size [3, 2, 1]
# User can only feed data of the same shape to x
# the dtype is not set, so it will set "float32" by
# paddle.get_default_dtype(). You can use paddle.get_default_dtype() to
# change the global dtype
x = paddle.static.data(name='x', shape=[3, 2, 1])
# Creates a variable with changeable batch size -1.
# Users can feed data of any batch size into y,
# but size of each data sample has to be [2, 1]
y = paddle.static.data(name='y', shape=[-1, 2, 1], dtype='float32')
z = x + y
# In this example, we will feed x and y with np-ndarray "1"
# and fetch z, like implementing "1 + 1 = 2" in PaddlePaddle
feed_data = np.ones(shape=[3, 2, 1], dtype=np.float32)
exe = fluid.Executor(fluid.CPUPlace())
out = exe.run(fluid.default_main_program(),
feed={
'x': feed_data,
'y': feed_data
},
fetch_list=[z.name])
# np-ndarray of shape=[3, 2, 1], dtype=float32, whose elements are 2
print(out)
"""
helper
=
LayerHelper
(
'data'
,
**
locals
())
check_type
(
name
,
'name'
,
(
six
.
binary_type
,
six
.
text_type
),
'data'
)
check_type
(
shape
,
'shape'
,
(
list
,
tuple
),
'data'
)
shape
=
list
(
shape
)
for
i
in
six
.
moves
.
range
(
len
(
shape
)):
if
shape
[
i
]
is
None
:
shape
[
i
]
=
-
1
if
dtype
:
return
helper
.
create_global_variable
(
name
=
name
,
shape
=
shape
,
dtype
=
dtype
,
type
=
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
stop_gradient
=
True
,
lod_level
=
lod_level
,
is_data
=
True
,
need_check_feed
=
True
)
else
:
return
helper
.
create_global_variable
(
name
=
name
,
shape
=
shape
,
dtype
=
paddle
.
get_default_dtype
(),
type
=
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
stop_gradient
=
True
,
lod_level
=
lod_level
,
is_data
=
True
,
need_check_feed
=
True
)
class
InputSpec
(
object
):
class
InputSpec
(
object
):
"""
"""
Define input specification of the model.
Define input specification of the model.
...
@@ -28,7 +129,7 @@ class InputSpec(object):
...
@@ -28,7 +129,7 @@ class InputSpec(object):
declaring the shape. You can set "None" or -1 at a dimension
declaring the shape. You can set "None" or -1 at a dimension
to indicate the dimension can be of any size. For example,
to indicate the dimension can be of any size. For example,
it is useful to set changeable batch size as "None" or -1.
it is useful to set changeable batch size as "None" or -1.
dtype (np.dtype|
VarType|
str, optional): The type of the data. Supported
dtype (np.dtype|str, optional): The type of the data. Supported
dtype: bool, float16, float32, float64, int8, int16, int32, int64,
dtype: bool, float16, float32, float64, int8, int16, int32, int64,
uint8. Default: float32.
uint8. Default: float32.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录