Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
seminar-breakout
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shashank Suhas
seminar-breakout
Commits
ef1b20f9
Commit
ef1b20f9
authored
Mar 18, 2016
by
Yuxin Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update on regularizer
parent
8c9c61d3
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
17 additions
and
14 deletions
+17
-14
examples/cifar10_resnet.py
examples/cifar10_resnet.py
+2
-5
examples/mnist_convnet.py
examples/mnist_convnet.py
+4
-2
tensorpack/models/model_desc.py
tensorpack/models/model_desc.py
+2
-1
tensorpack/models/regularize.py
tensorpack/models/regularize.py
+9
-6
No files found.
examples/cifar10_resnet.py
View file @
ef1b20f9
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# File: cifar10
-resnet-deeper
.py
# File: cifar10
_resnet
.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com>
import
tensorflow
as
tf
...
...
@@ -100,7 +100,6 @@ class Model(ModelDesc):
cost
=
tf
.
reduce_mean
(
cost
,
name
=
'cross_entropy_loss'
)
tf
.
add_to_collection
(
MOVING_SUMMARY_VARS_KEY
,
cost
)
# compute the number of failed samples, for ValidationError to use at test time
wrong
=
prediction_incorrect
(
logits
,
label
)
nr_wrong
=
tf
.
reduce_sum
(
wrong
,
name
=
'wrong'
)
# monitor training error
...
...
@@ -108,9 +107,7 @@ class Model(ModelDesc):
MOVING_SUMMARY_VARS_KEY
,
tf
.
reduce_mean
(
wrong
,
name
=
'train_error'
))
# weight decay on all W of fc layers
wd_cost
=
tf
.
mul
(
0.0002
,
regularize_cost
(
'.*/W'
,
tf
.
nn
.
l2_loss
),
name
=
'regularize_loss'
)
wd_cost
=
regularize_cost
(
'.*/W'
,
l2_regularizer
(
0.0002
),
name
=
'regularize_loss'
)
tf
.
add_to_collection
(
MOVING_SUMMARY_VARS_KEY
,
wd_cost
)
add_param_summary
([(
'.*/W'
,
[
'histogram'
,
'sparsity'
])])
# monitor W
...
...
examples/mnist_convnet.py
View file @
ef1b20f9
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File:
example_mnis
t.py
# File:
mnist_convne
t.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com>
import
tensorflow
as
tf
...
...
@@ -10,7 +10,7 @@ import numpy as np
import
os
,
sys
import
argparse
from
tensorpack.train
import
TrainConfig
,
SimpleTrainer
from
tensorpack.train
import
*
from
tensorpack.models
import
*
from
tensorpack.utils
import
*
from
tensorpack.utils.symbolic_functions
import
*
...
...
@@ -122,6 +122,8 @@ if __name__ == '__main__':
args
=
parser
.
parse_args
()
if
args
.
gpu
:
os
.
environ
[
'CUDA_VISIBLE_DEVICES'
]
=
args
.
gpu
else
:
os
.
environ
[
'CUDA_VISIBLE_DEVICES'
]
=
'0'
with
tf
.
Graph
()
.
as_default
():
config
=
get_config
()
...
...
tensorpack/models/model_desc.py
View file @
ef1b20f9
...
...
@@ -44,6 +44,7 @@ class ModelDesc(object):
"""
pass
# TODO move this to QueueInputTrainer
def
get_input_queue
(
self
,
input_vars
):
"""
return the queue for input. the dequeued elements will be fed to self.get_cost
...
...
@@ -51,7 +52,7 @@ class ModelDesc(object):
when running with multiGPU, queue cannot be None
"""
assert
input_vars
is
not
None
return
tf
.
FIFOQueue
(
5
0
,
[
x
.
dtype
for
x
in
input_vars
],
name
=
'input_queue'
)
return
tf
.
FIFOQueue
(
10
0
,
[
x
.
dtype
for
x
in
input_vars
],
name
=
'input_queue'
)
def
get_cost
(
self
,
input_vars
,
is_training
):
assert
type
(
is_training
)
==
bool
...
...
tensorpack/models/regularize.py
View file @
ef1b20f9
...
...
@@ -8,13 +8,16 @@ import re
from
..utils
import
logger
from
..utils
import
*
__all__
=
[
'regularize_cost'
]
__all__
=
[
'regularize_cost'
,
'l2_regularizer'
,
'l1_regularizer'
]
@
memoized
def
_log_regularizer
(
name
):
logger
.
info
(
"Apply regularizer for {}"
.
format
(
name
))
def
regularize_cost
(
regex
,
func
):
l2_regularizer
=
tf
.
contrib
.
layers
.
l2_regularizer
l1_regularizer
=
tf
.
contrib
.
layers
.
l1_regularizer
def
regularize_cost
(
regex
,
func
,
name
=
None
):
"""
Apply a regularizer on every trainable variable matching the regex
"""
...
...
@@ -23,11 +26,11 @@ def regularize_cost(regex, func):
costs
=
[]
for
p
in
params
:
name
=
p
.
name
if
re
.
search
(
regex
,
name
):
para_
name
=
p
.
name
if
re
.
search
(
regex
,
para_
name
):
costs
.
append
(
func
(
p
))
_log_regularizer
(
name
)
_log_regularizer
(
para_
name
)
if
not
costs
:
return
0
return
tf
.
add_n
(
costs
)
return
tf
.
add_n
(
costs
,
name
=
name
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment