Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
a022d421
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
a022d421
编写于
6月 11, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
6月 11, 2020
浏览文件
操作
浏览文件
下载
差异文件
!1992 fix lstm weight initializer
Merge pull request !1992 from baihuawei/cpulstm
上级
8de8289c
3bd03dde
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
21 addition
and
6 deletion
+21
-6
mindspore/ccsrc/kernel/cpu/mkldnn/lstm_cpu_kernel.cc
mindspore/ccsrc/kernel/cpu/mkldnn/lstm_cpu_kernel.cc
+7
-0
mindspore/ccsrc/kernel/cpu/mkldnn/lstm_grad_cpu_kernel.cc
mindspore/ccsrc/kernel/cpu/mkldnn/lstm_grad_cpu_kernel.cc
+7
-0
mindspore/nn/layer/lstm.py
mindspore/nn/layer/lstm.py
+7
-6
未找到文件。
mindspore/ccsrc/kernel/cpu/mkldnn/lstm_cpu_kernel.cc
浏览文件 @
a022d421
...
@@ -27,6 +27,7 @@ void LstmCPUKernel::InitKernel(const CNodePtr &kernel_node) {
...
@@ -27,6 +27,7 @@ void LstmCPUKernel::InitKernel(const CNodePtr &kernel_node) {
using
dim
=
dnnl
::
memory
::
dims
;
using
dim
=
dnnl
::
memory
::
dims
;
std
::
vector
<
size_t
>
src_shape
=
AnfAlgo
::
GetInputDeviceShape
(
kernel_node
,
0
);
std
::
vector
<
size_t
>
src_shape
=
AnfAlgo
::
GetInputDeviceShape
(
kernel_node
,
0
);
std
::
vector
<
size_t
>
src_h_shape
=
AnfAlgo
::
GetInputDeviceShape
(
kernel_node
,
1
);
std
::
vector
<
size_t
>
src_h_shape
=
AnfAlgo
::
GetInputDeviceShape
(
kernel_node
,
1
);
std
::
vector
<
size_t
>
src_c_shape
=
AnfAlgo
::
GetInputDeviceShape
(
kernel_node
,
2
);
bidirectional_
=
AnfAlgo
::
GetNodeAttr
<
bool
>
(
kernel_node
,
"bidirectional"
);
bidirectional_
=
AnfAlgo
::
GetNodeAttr
<
bool
>
(
kernel_node
,
"bidirectional"
);
input_size_
=
AnfAlgo
::
GetNodeAttr
<
int
>
(
kernel_node
,
"input_size"
);
input_size_
=
AnfAlgo
::
GetNodeAttr
<
int
>
(
kernel_node
,
"input_size"
);
hidden_size_
=
AnfAlgo
::
GetNodeAttr
<
int
>
(
kernel_node
,
"hidden_size"
);
hidden_size_
=
AnfAlgo
::
GetNodeAttr
<
int
>
(
kernel_node
,
"hidden_size"
);
...
@@ -41,6 +42,12 @@ void LstmCPUKernel::InitKernel(const CNodePtr &kernel_node) {
...
@@ -41,6 +42,12 @@ void LstmCPUKernel::InitKernel(const CNodePtr &kernel_node) {
if
(
num_directions_
*
num_layers_
!=
SizeToInt
(
src_h_shape
[
0
]))
{
if
(
num_directions_
*
num_layers_
!=
SizeToInt
(
src_h_shape
[
0
]))
{
MS_LOG
(
EXCEPTION
)
<<
"error iteration shape!"
;
MS_LOG
(
EXCEPTION
)
<<
"error iteration shape!"
;
}
}
if
(
num_layers_
<=
0
)
{
MS_LOG
(
EXCEPTION
)
<<
"layers must be greater than zero!"
;
}
if
(
src_shape
.
size
()
!=
3
||
src_h_shape
.
size
()
!=
3
||
src_c_shape
.
size
()
!=
3
)
{
MS_LOG
(
EXCEPTION
)
<<
"conv2d only support 3-D input!"
;
}
const
int
gate_size
=
4
*
hidden_size_
;
const
int
gate_size
=
4
*
hidden_size_
;
for
(
int
i
=
0
;
i
<
num_layers_
;
++
i
)
{
for
(
int
i
=
0
;
i
<
num_layers_
;
++
i
)
{
weight_size_
+=
gate_size
*
(
i
==
0
?
input_size_
:
hidden_size_
*
num_directions_
);
weight_size_
+=
gate_size
*
(
i
==
0
?
input_size_
:
hidden_size_
*
num_directions_
);
...
...
mindspore/ccsrc/kernel/cpu/mkldnn/lstm_grad_cpu_kernel.cc
浏览文件 @
a022d421
...
@@ -31,6 +31,7 @@ void LSTMGradCPUKernel::InitKernel(const CNodePtr &kernel_node) {
...
@@ -31,6 +31,7 @@ void LSTMGradCPUKernel::InitKernel(const CNodePtr &kernel_node) {
auto
eng
=
MKLKernelEngine
::
Get
().
engine
();
auto
eng
=
MKLKernelEngine
::
Get
().
engine
();
std
::
vector
<
size_t
>
src_shape
=
AnfAlgo
::
GetInputDeviceShape
(
kernel_node
,
0
);
std
::
vector
<
size_t
>
src_shape
=
AnfAlgo
::
GetInputDeviceShape
(
kernel_node
,
0
);
std
::
vector
<
size_t
>
src_h_shape
=
AnfAlgo
::
GetInputDeviceShape
(
kernel_node
,
1
);
std
::
vector
<
size_t
>
src_h_shape
=
AnfAlgo
::
GetInputDeviceShape
(
kernel_node
,
1
);
std
::
vector
<
size_t
>
src_c_shape
=
AnfAlgo
::
GetInputDeviceShape
(
kernel_node
,
2
);
bidirectional_
=
AnfAlgo
::
GetNodeAttr
<
bool
>
(
kernel_node
,
"bidirectional"
);
bidirectional_
=
AnfAlgo
::
GetNodeAttr
<
bool
>
(
kernel_node
,
"bidirectional"
);
input_size_
=
AnfAlgo
::
GetNodeAttr
<
int
>
(
kernel_node
,
"input_size"
);
input_size_
=
AnfAlgo
::
GetNodeAttr
<
int
>
(
kernel_node
,
"input_size"
);
hidden_size_
=
AnfAlgo
::
GetNodeAttr
<
int
>
(
kernel_node
,
"hidden_size"
);
hidden_size_
=
AnfAlgo
::
GetNodeAttr
<
int
>
(
kernel_node
,
"hidden_size"
);
...
@@ -45,6 +46,12 @@ void LSTMGradCPUKernel::InitKernel(const CNodePtr &kernel_node) {
...
@@ -45,6 +46,12 @@ void LSTMGradCPUKernel::InitKernel(const CNodePtr &kernel_node) {
if
(
num_directions_
*
num_layers_
!=
SizeToInt
(
src_h_shape
[
0
]))
{
if
(
num_directions_
*
num_layers_
!=
SizeToInt
(
src_h_shape
[
0
]))
{
MS_LOG
(
EXCEPTION
)
<<
"error iteration shape!"
;
MS_LOG
(
EXCEPTION
)
<<
"error iteration shape!"
;
}
}
if
(
num_layers_
<=
0
)
{
MS_LOG
(
EXCEPTION
)
<<
"layers must be greater than zero!"
;
}
if
(
src_shape
.
size
()
!=
3
||
src_h_shape
.
size
()
!=
3
||
src_c_shape
.
size
()
!=
3
)
{
MS_LOG
(
EXCEPTION
)
<<
"conv2d only support 3-D input!"
;
}
const
int
gate_size
=
4
*
hidden_size_
;
const
int
gate_size
=
4
*
hidden_size_
;
for
(
int
i
=
0
;
i
<
num_layers_
;
++
i
)
{
for
(
int
i
=
0
;
i
<
num_layers_
;
++
i
)
{
weight_size_
+=
gate_size
*
(
i
==
0
?
input_size_
:
hidden_size_
*
num_directions_
);
weight_size_
+=
gate_size
*
(
i
==
0
?
input_size_
:
hidden_size_
*
num_directions_
);
...
...
mindspore/nn/layer/lstm.py
浏览文件 @
a022d421
...
@@ -13,8 +13,8 @@
...
@@ -13,8 +13,8 @@
# limitations under the License.
# limitations under the License.
# ============================================================================
# ============================================================================
"""lstm"""
"""lstm"""
import
math
import
numpy
as
np
import
numpy
as
np
import
mindspore.nn
as
nn
import
mindspore.nn
as
nn
from
mindspore
import
context
from
mindspore
import
context
from
mindspore._checkparam
import
Validator
as
validator
from
mindspore._checkparam
import
Validator
as
validator
...
@@ -148,7 +148,9 @@ class LSTM(Cell):
...
@@ -148,7 +148,9 @@ class LSTM(Cell):
if
self
.
has_bias
:
if
self
.
has_bias
:
increment_size
+=
2
*
gate_size
increment_size
+=
2
*
gate_size
weight_size
+=
increment_size
*
num_directions
weight_size
+=
increment_size
*
num_directions
self
.
weight
=
Parameter
(
initializer
(
0.0
,
[
weight_size
,
1
,
1
]),
name
=
'weight'
)
stdv
=
1
/
math
.
sqrt
(
hidden_size
)
w_np
=
np
.
random
.
uniform
(
-
stdv
,
stdv
,
(
weight_size
,
1
,
1
)).
astype
(
np
.
float32
)
self
.
weight
=
Parameter
(
initializer
(
Tensor
(
w_np
),
[
weight_size
,
1
,
1
]),
name
=
'weight'
)
else
:
else
:
input_size_list
=
[]
input_size_list
=
[]
input_size_list
.
append
(
self
.
input_size
)
input_size_list
.
append
(
self
.
input_size
)
...
@@ -157,14 +159,13 @@ class LSTM(Cell):
...
@@ -157,14 +159,13 @@ class LSTM(Cell):
weights
=
[]
weights
=
[]
layers
=
[]
layers
=
[]
bias_size
=
0
if
not
self
.
has_bias
else
num_directions
*
self
.
hidden_size
*
4
bias_size
=
0
if
not
self
.
has_bias
else
num_directions
*
self
.
hidden_size
*
4
stdv
=
1
/
math
.
sqrt
(
hidden_size
)
for
i
in
range
(
num_layers
):
for
i
in
range
(
num_layers
):
weight_size
=
(
input_size_list
[
i
]
+
self
.
hidden_size
)
*
num_directions
*
self
.
hidden_size
*
4
weight_size
=
(
input_size_list
[
i
]
+
self
.
hidden_size
)
*
num_directions
*
self
.
hidden_size
*
4
w_np
=
np
.
ones
([
weight_size
,
1
,
1
]).
astype
(
np
.
float32
)
*
0.01
if
has_bias
:
if
has_bias
:
bias_np
=
np
.
zeros
([
bias_size
,
1
,
1
]).
astype
(
np
.
float32
)
weight_size
=
weight_size
+
bias_size
w_np
=
np
.
concatenate
([
w_np
,
bias_np
],
axis
=
0
)
w_np
=
np
.
random
.
uniform
(
-
stdv
,
stdv
,
(
weight_size
,
1
,
1
)).
astype
(
np
.
float32
)
weights
.
append
(
Parameter
(
initializer
(
Tensor
(
w_np
),
w_np
.
shape
),
name
=
'weight'
+
str
(
i
)))
weights
.
append
(
Parameter
(
initializer
(
Tensor
(
w_np
),
w_np
.
shape
),
name
=
'weight'
+
str
(
i
)))
layers
.
append
(
nn
.
LSTMCell
(
input_size
=
input_size_list
[
i
],
layers
.
append
(
nn
.
LSTMCell
(
input_size
=
input_size_list
[
i
],
hidden_size
=
self
.
hidden_size
,
hidden_size
=
self
.
hidden_size
,
has_bias
=
self
.
has_bias
,
has_bias
=
self
.
has_bias
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录