Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
qq_38905368
tensorflow
提交
250ab666
T
tensorflow
项目概览
qq_38905368
/
tensorflow
与 Fork 源项目一致
从无法访问的项目Fork
通知
5
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
tensorflow
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
250ab666
编写于
12月 12, 2018
作者:
F
Francois Chollet
提交者:
TensorFlower Gardener
12月 12, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make core layers tests run in graph and eager mode.
PiperOrigin-RevId: 225231668
上级
16069bf8
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
155 addition
and
170 deletion
+155
-170
tensorflow/python/keras/BUILD
tensorflow/python/keras/BUILD
+1
-1
tensorflow/python/keras/layers/core_test.py
tensorflow/python/keras/layers/core_test.py
+154
-169
未找到文件。
tensorflow/python/keras/BUILD
浏览文件 @
250ab666
...
...
@@ -401,7 +401,7 @@ py_test(
name
=
"core_test"
,
size
=
"medium"
,
srcs
=
[
"layers/core_test.py"
],
shard_count
=
2
,
shard_count
=
3
,
srcs_version
=
"PY2AND3"
,
deps
=
[
":keras"
,
...
...
tensorflow/python/keras/layers/core_test.py
浏览文件 @
250ab666
...
...
@@ -22,43 +22,36 @@ import numpy as np
from
tensorflow.python
import
keras
from
tensorflow.python.eager
import
context
from
tensorflow.python.
framework
import
test_util
as
tf_test_util
from
tensorflow.python.
keras
import
keras_parameterized
from
tensorflow.python.keras
import
testing_utils
from
tensorflow.python.ops
import
math_ops
from
tensorflow.python.platform
import
test
class
CoreLayersTest
(
test
.
TestCase
):
def
test_masking
(
self
):
with
self
.
cached_session
():
testing_utils
.
layer_test
(
keras
.
layers
.
Masking
,
kwargs
=
{},
input_shape
=
(
3
,
2
,
3
))
@
keras_parameterized
.
run_all_keras_modes
class
DropoutLayersTest
(
keras_parameterized
.
TestCase
):
def
test_dropout
(
self
):
with
self
.
cached_session
():
testing_utils
.
layer_test
(
keras
.
layers
.
Dropout
,
kwargs
=
{
'rate'
:
0.5
},
input_shape
=
(
3
,
2
))
with
self
.
cached_session
():
testing_utils
.
layer_test
(
keras
.
layers
.
Dropout
,
kwargs
=
{
'rate'
:
0.5
,
'noise_shape'
:
[
3
,
1
]},
input_shape
=
(
3
,
2
))
# https://github.com/tensorflow/tensorflow/issues/14819
with
self
.
cached_session
():
def
test_dropout_supports_masking
(
self
):
dropout
=
keras
.
layers
.
Dropout
(
0.5
)
self
.
assertEqual
(
True
,
dropout
.
supports_masking
)
@
tf_test_util
.
run_in_graph_and_eager_modes
def
test_spatial_dropout
(
self
):
def
test_spatial_dropout_1d
(
self
):
testing_utils
.
layer_test
(
keras
.
layers
.
SpatialDropout1D
,
kwargs
=
{
'rate'
:
0.5
},
input_shape
=
(
2
,
3
,
4
))
def
test_spatial_dropout_2d
(
self
):
testing_utils
.
layer_test
(
keras
.
layers
.
SpatialDropout2D
,
kwargs
=
{
'rate'
:
0.5
},
...
...
@@ -69,6 +62,7 @@ class CoreLayersTest(test.TestCase):
kwargs
=
{
'rate'
:
0.5
,
'data_format'
:
'channels_first'
},
input_shape
=
(
2
,
3
,
4
,
5
))
def
test_spatial_dropout_3d
(
self
):
testing_utils
.
layer_test
(
keras
.
layers
.
SpatialDropout3D
,
kwargs
=
{
'rate'
:
0.5
},
...
...
@@ -79,7 +73,122 @@ class CoreLayersTest(test.TestCase):
kwargs
=
{
'rate'
:
0.5
,
'data_format'
:
'channels_first'
},
input_shape
=
(
2
,
3
,
4
,
4
,
5
))
@
tf_test_util
.
run_in_graph_and_eager_modes
@
keras_parameterized
.
run_all_keras_modes
class
LambdaLayerTest
(
keras_parameterized
.
TestCase
):
def
test_lambda
(
self
):
testing_utils
.
layer_test
(
keras
.
layers
.
Lambda
,
kwargs
=
{
'function'
:
lambda
x
:
x
+
1
},
input_shape
=
(
3
,
2
))
testing_utils
.
layer_test
(
keras
.
layers
.
Lambda
,
kwargs
=
{
'function'
:
lambda
x
,
a
,
b
:
x
*
a
+
b
,
'arguments'
:
{
'a'
:
0.6
,
'b'
:
0.4
}
},
input_shape
=
(
3
,
2
))
# test serialization with function
def
f
(
x
):
return
x
+
1
ld
=
keras
.
layers
.
Lambda
(
f
)
config
=
ld
.
get_config
()
ld
=
keras
.
layers
.
deserialize
({
'class_name'
:
'Lambda'
,
'config'
:
config
})
# test with lambda
ld
=
keras
.
layers
.
Lambda
(
lambda
x
:
keras
.
backend
.
concatenate
([
math_ops
.
square
(
x
),
x
]))
config
=
ld
.
get_config
()
ld
=
keras
.
layers
.
Lambda
.
from_config
(
config
)
def
test_lambda_multiple_inputs
(
self
):
ld
=
keras
.
layers
.
Lambda
(
lambda
x
:
x
[
0
],
output_shape
=
lambda
x
:
x
[
0
])
x1
=
np
.
ones
([
3
,
2
],
np
.
float32
)
x2
=
np
.
ones
([
3
,
5
],
np
.
float32
)
out
=
ld
([
x1
,
x2
])
self
.
assertAllEqual
(
out
.
shape
,
[
3
,
2
])
def
test_lambda_output_shape
(
self
):
l
=
keras
.
layers
.
Lambda
(
lambda
x
:
x
+
1
,
output_shape
=
(
1
,
1
))
l
(
keras
.
backend
.
variable
(
np
.
ones
((
1
,
1
))))
self
.
assertEqual
((
1
,
1
),
l
.
get_config
()[
'output_shape'
])
def
test_lambda_output_shape_function
(
self
):
def
get_output_shape
(
input_shape
):
return
1
*
input_shape
l
=
keras
.
layers
.
Lambda
(
lambda
x
:
x
+
1
,
output_shape
=
get_output_shape
)
l
(
keras
.
backend
.
variable
(
np
.
ones
((
1
,
1
))))
self
.
assertEqual
(
'lambda'
,
l
.
get_config
()[
'output_shape_type'
])
def
test_lambda_output_shape_autocalculate_multiple_inputs
(
self
):
def
lambda_fn
(
x
):
return
math_ops
.
matmul
(
x
[
0
],
x
[
1
])
l
=
keras
.
layers
.
Lambda
(
lambda_fn
)
output_shape
=
l
.
compute_output_shape
([(
10
,
10
),
(
10
,
20
)])
self
.
assertAllEqual
((
10
,
20
),
output_shape
)
def
test_lambda_output_shape_list_multiple_outputs
(
self
):
def
lambda_fn
(
x
):
return
x
l
=
keras
.
layers
.
Lambda
(
lambda_fn
,
output_shape
=
[(
10
,),
(
20
,)])
output_shape
=
l
.
compute_output_shape
([(
10
,
10
),
(
10
,
20
)])
self
.
assertAllEqual
([(
10
,
10
),
(
10
,
20
)],
output_shape
)
def
test_lambda_output_shape_tuple_with_none
(
self
):
def
lambda_fn
(
x
):
return
x
l
=
keras
.
layers
.
Lambda
(
lambda_fn
,
output_shape
=
(
None
,
10
))
output_shape
=
l
.
compute_output_shape
((
5
,
10
,
20
))
self
.
assertAllEqual
([
5
,
None
,
10
],
output_shape
.
as_list
())
def
test_lambda_output_shape_function_multiple_outputs
(
self
):
def
lambda_fn
(
x
):
return
x
def
output_shape_fn
(
input_shape
):
return
input_shape
l
=
keras
.
layers
.
Lambda
(
lambda_fn
,
output_shape
=
output_shape_fn
)
output_shape
=
l
.
compute_output_shape
([(
10
,
10
),
(
10
,
20
)])
self
.
assertAllEqual
([(
10
,
10
),
(
10
,
20
)],
output_shape
)
def
test_lambda_config_serialization
(
self
):
# Test serialization with output_shape and output_shape_type
layer
=
keras
.
layers
.
Lambda
(
lambda
x
:
x
+
1
,
output_shape
=
(
1
,
1
))
layer
(
keras
.
backend
.
variable
(
np
.
ones
((
1
,
1
))))
config
=
layer
.
get_config
()
layer
=
keras
.
layers
.
deserialize
({
'class_name'
:
'Lambda'
,
'config'
:
config
})
layer
=
keras
.
layers
.
Lambda
.
from_config
(
config
)
@
keras_parameterized
.
run_all_keras_modes
class
CoreLayersTest
(
keras_parameterized
.
TestCase
):
def
test_masking
(
self
):
testing_utils
.
layer_test
(
keras
.
layers
.
Masking
,
kwargs
=
{},
input_shape
=
(
3
,
2
,
3
))
def
test_activation
(
self
):
# with string argument
testing_utils
.
layer_test
(
...
...
@@ -93,7 +202,6 @@ class CoreLayersTest(test.TestCase):
kwargs
=
{
'activation'
:
keras
.
backend
.
relu
},
input_shape
=
(
3
,
2
))
@
tf_test_util
.
run_in_graph_and_eager_modes
def
test_reshape
(
self
):
testing_utils
.
layer_test
(
keras
.
layers
.
Reshape
,
...
...
@@ -115,26 +223,22 @@ class CoreLayersTest(test.TestCase):
kwargs
=
{
'target_shape'
:
(
-
1
,
1
)},
input_shape
=
(
None
,
None
,
2
))
@
tf_test_util
.
run_in_graph_and_eager_modes
def
test_permute
(
self
):
testing_utils
.
layer_test
(
keras
.
layers
.
Permute
,
kwargs
=
{
'dims'
:
(
2
,
1
)},
input_shape
=
(
3
,
2
,
4
))
@
tf_test_util
.
run_in_graph_and_eager_modes
def
test_permute_errors_on_invalid_starting_dims_index
(
self
):
with
self
.
assertRaisesRegexp
(
ValueError
,
r
'Invalid permutation .*dims.*'
):
testing_utils
.
layer_test
(
keras
.
layers
.
Permute
,
kwargs
=
{
'dims'
:
(
0
,
1
,
2
)},
input_shape
=
(
3
,
2
,
4
))
@
tf_test_util
.
run_in_graph_and_eager_modes
def
test_permute_errors_on_invalid_set_of_dims_indices
(
self
):
with
self
.
assertRaisesRegexp
(
ValueError
,
r
'Invalid permutation .*dims.*'
):
testing_utils
.
layer_test
(
keras
.
layers
.
Permute
,
kwargs
=
{
'dims'
:
(
1
,
4
,
2
)},
input_shape
=
(
3
,
2
,
4
))
@
tf_test_util
.
run_in_graph_and_eager_modes
def
test_flatten
(
self
):
testing_utils
.
layer_test
(
keras
.
layers
.
Flatten
,
kwargs
=
{},
input_shape
=
(
3
,
2
,
4
))
...
...
@@ -149,7 +253,6 @@ class CoreLayersTest(test.TestCase):
np
.
transpose
(
inputs
,
(
0
,
2
,
3
,
1
)),
(
-
1
,
5
*
5
*
3
))
self
.
assertAllClose
(
outputs
,
target_outputs
)
@
tf_test_util
.
run_in_graph_and_eager_modes
def
test_flatten_scalar_channels
(
self
):
testing_utils
.
layer_test
(
keras
.
layers
.
Flatten
,
kwargs
=
{},
input_shape
=
(
3
,))
...
...
@@ -163,54 +266,10 @@ class CoreLayersTest(test.TestCase):
target_outputs
=
np
.
expand_dims
(
inputs
,
-
1
)
self
.
assertAllClose
(
outputs
,
target_outputs
)
@
tf_test_util
.
run_in_graph_and_eager_modes
def
test_repeat_vector
(
self
):
testing_utils
.
layer_test
(
keras
.
layers
.
RepeatVector
,
kwargs
=
{
'n'
:
3
},
input_shape
=
(
3
,
2
))
def
test_lambda
(
self
):
testing_utils
.
layer_test
(
keras
.
layers
.
Lambda
,
kwargs
=
{
'function'
:
lambda
x
:
x
+
1
},
input_shape
=
(
3
,
2
))
testing_utils
.
layer_test
(
keras
.
layers
.
Lambda
,
kwargs
=
{
'function'
:
lambda
x
,
a
,
b
:
x
*
a
+
b
,
'arguments'
:
{
'a'
:
0.6
,
'b'
:
0.4
}
},
input_shape
=
(
3
,
2
))
# test serialization with function
def
f
(
x
):
return
x
+
1
ld
=
keras
.
layers
.
Lambda
(
f
)
config
=
ld
.
get_config
()
ld
=
keras
.
layers
.
deserialize
({
'class_name'
:
'Lambda'
,
'config'
:
config
})
# test with lambda
ld
=
keras
.
layers
.
Lambda
(
lambda
x
:
keras
.
backend
.
concatenate
([
math_ops
.
square
(
x
),
x
]))
config
=
ld
.
get_config
()
ld
=
keras
.
layers
.
Lambda
.
from_config
(
config
)
@
tf_test_util
.
run_in_graph_and_eager_modes
def
test_lambda_multiple_inputs
(
self
):
ld
=
keras
.
layers
.
Lambda
(
lambda
x
:
x
[
0
],
output_shape
=
lambda
x
:
x
[
0
])
x1
=
np
.
ones
([
3
,
2
],
np
.
float32
)
x2
=
np
.
ones
([
3
,
5
],
np
.
float32
)
out
=
ld
([
x1
,
x2
])
self
.
assertAllEqual
(
out
.
shape
,
[
3
,
2
])
@
tf_test_util
.
run_in_graph_and_eager_modes
def
test_dense
(
self
):
testing_utils
.
layer_test
(
keras
.
layers
.
Dense
,
kwargs
=
{
'units'
:
3
},
input_shape
=
(
3
,
2
))
...
...
@@ -225,7 +284,6 @@ class CoreLayersTest(test.TestCase):
keras
.
layers
.
Dense
,
kwargs
=
{
'units'
:
3
},
input_shape
=
(
3
,
4
,
5
,
2
))
def
test_dense_regularization
(
self
):
with
self
.
cached_session
():
layer
=
keras
.
layers
.
Dense
(
3
,
kernel_regularizer
=
keras
.
regularizers
.
l1
(
0.01
),
...
...
@@ -236,7 +294,6 @@ class CoreLayersTest(test.TestCase):
self
.
assertEqual
(
3
,
len
(
layer
.
losses
))
def
test_dense_constraints
(
self
):
with
self
.
cached_session
():
k_constraint
=
keras
.
constraints
.
max_norm
(
0.01
)
b_constraint
=
keras
.
constraints
.
max_norm
(
0.01
)
layer
=
keras
.
layers
.
Dense
(
...
...
@@ -246,84 +303,12 @@ class CoreLayersTest(test.TestCase):
self
.
assertEqual
(
layer
.
bias
.
constraint
,
b_constraint
)
def
test_activity_regularization
(
self
):
with
self
.
cached_session
():
layer
=
keras
.
layers
.
ActivityRegularization
(
l1
=
0.1
)
layer
(
keras
.
backend
.
variable
(
np
.
ones
((
2
,
4
))))
self
.
assertEqual
(
1
,
len
(
layer
.
losses
))
_
=
layer
.
get_config
()
def
test_lambda_output_shape
(
self
):
with
self
.
cached_session
():
l
=
keras
.
layers
.
Lambda
(
lambda
x
:
x
+
1
,
output_shape
=
(
1
,
1
))
l
(
keras
.
backend
.
variable
(
np
.
ones
((
1
,
1
))))
self
.
assertEqual
((
1
,
1
),
l
.
get_config
()[
'output_shape'
])
def
test_lambda_output_shape_function
(
self
):
def
get_output_shape
(
input_shape
):
return
1
*
input_shape
with
self
.
cached_session
():
l
=
keras
.
layers
.
Lambda
(
lambda
x
:
x
+
1
,
output_shape
=
get_output_shape
)
l
(
keras
.
backend
.
variable
(
np
.
ones
((
1
,
1
))))
self
.
assertEqual
(
'lambda'
,
l
.
get_config
()[
'output_shape_type'
])
@
tf_test_util
.
run_in_graph_and_eager_modes
def
test_lambda_output_shape_autocalculate_multiple_inputs
(
self
):
def
lambda_fn
(
x
):
return
math_ops
.
matmul
(
x
[
0
],
x
[
1
])
l
=
keras
.
layers
.
Lambda
(
lambda_fn
)
output_shape
=
l
.
compute_output_shape
([(
10
,
10
),
(
10
,
20
)])
self
.
assertAllEqual
((
10
,
20
),
output_shape
)
@
tf_test_util
.
run_in_graph_and_eager_modes
def
test_lambda_output_shape_list_multiple_outputs
(
self
):
def
lambda_fn
(
x
):
return
x
l
=
keras
.
layers
.
Lambda
(
lambda_fn
,
output_shape
=
[(
10
,),
(
20
,)])
output_shape
=
l
.
compute_output_shape
([(
10
,
10
),
(
10
,
20
)])
self
.
assertAllEqual
([(
10
,
10
),
(
10
,
20
)],
output_shape
)
@
tf_test_util
.
run_in_graph_and_eager_modes
def
test_lambda_output_shape_tuple_with_none
(
self
):
def
lambda_fn
(
x
):
return
x
l
=
keras
.
layers
.
Lambda
(
lambda_fn
,
output_shape
=
(
None
,
10
))
output_shape
=
l
.
compute_output_shape
((
5
,
10
,
20
))
self
.
assertAllEqual
([
5
,
None
,
10
],
output_shape
.
as_list
())
@
tf_test_util
.
run_in_graph_and_eager_modes
def
test_lambda_output_shape_function_multiple_outputs
(
self
):
def
lambda_fn
(
x
):
return
x
def
output_shape_fn
(
input_shape
):
return
input_shape
l
=
keras
.
layers
.
Lambda
(
lambda_fn
,
output_shape
=
output_shape_fn
)
output_shape
=
l
.
compute_output_shape
([(
10
,
10
),
(
10
,
20
)])
self
.
assertAllEqual
([(
10
,
10
),
(
10
,
20
)],
output_shape
)
def
test_lambda_config_serialization
(
self
):
with
self
.
cached_session
():
# test serialization with output_shape and output_shape_type
layer
=
keras
.
layers
.
Lambda
(
lambda
x
:
x
+
1
,
output_shape
=
(
1
,
1
))
layer
(
keras
.
backend
.
variable
(
np
.
ones
((
1
,
1
))))
config
=
layer
.
get_config
()
layer
=
keras
.
layers
.
deserialize
({
'class_name'
:
'Lambda'
,
'config'
:
config
})
layer
=
keras
.
layers
.
Lambda
.
from_config
(
config
)
self
.
assertEqual
(
config
.
pop
(
'l1'
),
0.1
)
@
tf_test_util
.
run_in_graph_and_eager_modes
def
test_numpy_inputs
(
self
):
if
context
.
executing_eagerly
():
layer
=
keras
.
layers
.
RepeatVector
(
2
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录