Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
7458114b
P
Paddle
项目概览
PaddlePaddle
/
Paddle
12 个月 前同步成功
通知
2292
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
7458114b
编写于
3月 18, 2019
作者:
X
Xin Pan
提交者:
GitHub
3月 18, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #16228 from panyx0718/imperative
graph neural network for imperative mode
上级
efca4de7
3be7e971
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
176 addition
and
2 deletion
+176
-2
paddle/fluid/operators/squeeze_op.cc
paddle/fluid/operators/squeeze_op.cc
+1
-0
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+5
-0
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+5
-2
python/paddle/fluid/tests/unittests/test_imperative_gnn.py
python/paddle/fluid/tests/unittests/test_imperative_gnn.py
+144
-0
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+21
-0
未找到文件。
paddle/fluid/operators/squeeze_op.cc
浏览文件 @
7458114b
...
...
@@ -94,6 +94,7 @@ class SqueezeOpInferShape : public framework::InferShapeBase {
}
};
// TODO(paddle-dev): Should use OpKernel.
class
SqueezeOp
:
public
framework
::
OperatorBase
{
public:
using
OperatorBase
::
OperatorBase
;
...
...
python/paddle/fluid/framework.py
浏览文件 @
7458114b
...
...
@@ -430,6 +430,11 @@ class Variable(object):
Returns:
str: The debug string.
"""
if
_in_imperative_mode
():
# TODO(panyx0718): add more imperative debug info.
return
'name %s, dtype: %s shape: %s'
%
(
self
.
name
,
self
.
dtype
,
self
.
shape
)
assert
isinstance
(
throw_on_error
,
bool
)
and
isinstance
(
with_details
,
bool
)
protostr
=
self
.
desc
.
serialize_to_string
()
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
7458114b
...
...
@@ -23,7 +23,7 @@ import os
import
inspect
from
..layer_helper
import
LayerHelper
from
..initializer
import
Normal
,
Constant
,
NumpyArrayInitializer
from
..framework
import
Variable
,
OpProtoHolder
from
..framework
import
Variable
,
OpProtoHolder
,
_in_imperative_mode
from
..param_attr
import
ParamAttr
from
.layer_function_generator
import
autodoc
,
templatedoc
,
_generate_doc_string_
from
.tensor
import
concat
,
assign
...
...
@@ -4895,7 +4895,8 @@ def matmul(x, y, transpose_x=False, transpose_y=False, alpha=1.0, name=None):
if
transpose_y
:
y_shape
[
-
2
],
y_shape
[
-
1
]
=
y_shape
[
-
1
],
y_shape
[
-
2
]
if
x_shape
[
-
1
]
!=
y_shape
[
-
2
]:
raise
ValueError
(
"Invalid inputs for matmul."
)
raise
ValueError
(
"Invalid inputs for matmul. x: %s, y: %s
\n
"
%
(
x_shape
,
y_shape
))
if
len
(
y_shape
)
>
2
and
len
(
x_shape
)
>
2
:
for
i
,
dim_x
in
enumerate
(
x_shape
[:
-
2
]):
...
...
@@ -6398,6 +6399,8 @@ def squeeze(input, axes, name=None):
x = layers.data(name='x', shape=[5, 1, 10])
y = layers.sequeeze(input=x, axes=[1])
"""
assert
not
_in_imperative_mode
(),
(
"squeeze layer is not supported in imperative mode yet."
)
helper
=
LayerHelper
(
"squeeze"
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
input
.
dtype
)
x_shape
=
helper
.
create_variable_for_type_inference
(
dtype
=
input
.
dtype
)
...
...
python/paddle/fluid/tests/unittests/test_imperative_gnn.py
0 → 100644
浏览文件 @
7458114b
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
contextlib
import
unittest
import
numpy
as
np
import
six
import
sys
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.core
as
core
from
paddle.fluid.optimizer
import
AdamOptimizer
from
paddle.fluid.imperative.nn
import
Conv2D
,
Pool2D
,
FC
from
test_imperative_base
import
new_program_scope
from
paddle.fluid.imperative.base
import
to_variable
def
gen_data
():
pass
class
GraphConv
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
,
name_scope
,
in_features
,
out_features
):
super
(
GraphConv
,
self
).
__init__
(
name_scope
)
self
.
_in_features
=
in_features
self
.
_out_features
=
out_features
self
.
weight
=
self
.
create_parameter
(
attr
=
None
,
dtype
=
'float32'
,
shape
=
[
self
.
_in_features
,
self
.
_out_features
])
self
.
bias
=
self
.
create_parameter
(
attr
=
None
,
dtype
=
'float32'
,
shape
=
[
self
.
_out_features
])
def
forward
(
self
,
features
,
adj
):
support
=
fluid
.
layers
.
matmul
(
features
,
self
.
weight
)
# TODO(panyx0718): sparse matmul?
return
fluid
.
layers
.
matmul
(
adj
,
support
)
+
self
.
bias
class
GCN
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
,
name_scope
,
num_hidden
):
super
(
GCN
,
self
).
__init__
(
name_scope
)
self
.
gc
=
GraphConv
(
self
.
full_name
(),
num_hidden
,
32
)
self
.
gc2
=
GraphConv
(
self
.
full_name
(),
32
,
10
)
def
forward
(
self
,
x
,
adj
):
x
=
fluid
.
layers
.
relu
(
self
.
gc
(
x
,
adj
))
return
self
.
gc2
(
x
,
adj
)
class
TestImperativeGNN
(
unittest
.
TestCase
):
def
test_gnn_float32
(
self
):
seed
=
90
startup
=
fluid
.
Program
()
startup
.
random_seed
=
seed
main
=
fluid
.
Program
()
main
.
random_seed
=
seed
scope
=
fluid
.
core
.
Scope
()
with
new_program_scope
(
main
=
main
,
startup
=
startup
,
scope
=
scope
):
features
=
fluid
.
layers
.
data
(
name
=
'features'
,
shape
=
[
1
,
100
,
50
],
dtype
=
'float32'
,
append_batch_size
=
False
)
# Use selected rows when it's supported.
adj
=
fluid
.
layers
.
data
(
name
=
'adj'
,
shape
=
[
1
,
100
,
100
],
dtype
=
'float32'
,
append_batch_size
=
False
)
labels
=
fluid
.
layers
.
data
(
name
=
'labels'
,
shape
=
[
100
,
1
],
dtype
=
'int64'
,
append_batch_size
=
False
)
model
=
GCN
(
'test_gcn'
,
50
)
logits
=
model
(
features
,
adj
)
logits
=
fluid
.
layers
.
reshape
(
logits
,
logits
.
shape
[
1
:])
# In other example, it's nll with log_softmax. However, paddle's
# log_loss only supports binary classification now.
loss
=
fluid
.
layers
.
softmax_with_cross_entropy
(
logits
,
labels
)
loss
=
fluid
.
layers
.
reduce_sum
(
loss
)
adam
=
AdamOptimizer
(
learning_rate
=
1e-3
)
adam
.
minimize
(
loss
)
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
(
)
if
not
core
.
is_compiled_with_cuda
()
else
fluid
.
CUDAPlace
(
0
))
exe
.
run
(
startup
)
static_loss
=
exe
.
run
(
feed
=
{
'features'
:
np
.
zeros
(
[
1
,
100
,
50
],
dtype
=
np
.
float32
),
'adj'
:
np
.
zeros
(
[
1
,
100
,
100
],
dtype
=
np
.
float32
),
'labels'
:
np
.
zeros
(
[
100
,
1
],
dtype
=
np
.
int64
)
},
fetch_list
=
[
loss
])[
0
]
static_weight
=
np
.
array
(
scope
.
find_var
(
model
.
gc
.
weight
.
name
).
get_tensor
())
with
fluid
.
imperative
.
guard
():
fluid
.
default_startup_program
().
random_seed
=
seed
fluid
.
default_main_program
().
random_seed
=
seed
features
=
np
.
zeros
([
1
,
100
,
50
],
dtype
=
np
.
float32
)
# Use selected rows when it's supported.
adj
=
np
.
zeros
([
1
,
100
,
100
],
dtype
=
np
.
float32
)
labels
=
np
.
zeros
([
100
,
1
],
dtype
=
np
.
int64
)
model
=
GCN
(
'test_gcn'
,
50
)
logits
=
model
(
to_variable
(
features
),
to_variable
(
adj
))
logits
=
fluid
.
layers
.
reshape
(
logits
,
logits
.
shape
[
1
:])
# In other example, it's nll with log_softmax. However, paddle's
# log_loss only supports binary classification now.
loss
=
fluid
.
layers
.
softmax_with_cross_entropy
(
logits
,
to_variable
(
labels
))
loss
=
fluid
.
layers
.
reduce_sum
(
loss
)
adam
=
AdamOptimizer
(
learning_rate
=
1e-3
)
adam
.
minimize
(
loss
)
self
.
assertEqual
(
static_loss
,
loss
.
_numpy
())
self
.
assertTrue
(
np
.
allclose
(
static_weight
,
model
.
gc
.
weight
.
_numpy
()))
sys
.
stderr
.
write
(
'%s %s
\n
'
%
(
static_loss
,
loss
.
_numpy
()))
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
7458114b
...
...
@@ -84,6 +84,27 @@ class TestLayer(LayerTest):
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
dy_ret
.
_numpy
()))
def
test_matmul
(
self
):
with
self
.
static_graph
():
t
=
layers
.
data
(
name
=
't'
,
shape
=
[
3
,
3
],
dtype
=
'float32'
)
t2
=
layers
.
data
(
name
=
't2'
,
shape
=
[
3
,
3
],
dtype
=
'float32'
)
ret
=
layers
.
matmul
(
t
,
t2
)
static_ret
=
self
.
get_static_graph_result
(
feed
=
{
't'
:
np
.
ones
(
[
3
,
3
],
dtype
=
'float32'
),
't2'
:
np
.
ones
(
[
3
,
3
],
dtype
=
'float32'
)
},
fetch_list
=
[
ret
])[
0
]
with
self
.
dynamic_graph
():
t
=
np
.
ones
([
3
,
3
],
dtype
=
'float32'
)
t2
=
np
.
ones
([
3
,
3
],
dtype
=
'float32'
)
dy_ret
=
layers
.
matmul
(
base
.
to_variable
(
t
),
base
.
to_variable
(
t2
))
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
dy_ret
.
_numpy
()))
def
test_conv2d
(
self
):
with
self
.
static_graph
():
images
=
layers
.
data
(
name
=
'pixel'
,
shape
=
[
3
,
5
,
5
],
dtype
=
'float32'
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录