Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
曾经的那一瞬间
Models
提交
0788a23c
M
Models
项目概览
曾经的那一瞬间
/
Models
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
Models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0788a23c
编写于
12月 13, 2019
作者:
H
Hongkun Yu
提交者:
A. Unique TensorFlower
12月 13, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Internal change
PiperOrigin-RevId: 285533511
上级
357f30f4
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
51 addition
and
62 deletion
+51
-62
official/r1/boosted_trees/train_higgs.py
official/r1/boosted_trees/train_higgs.py
+1
-2
official/r1/mnist/mnist_eager.py
official/r1/mnist/mnist_eager.py
+8
-9
official/r1/mnist/mnist_tpu.py
official/r1/mnist/mnist_tpu.py
+12
-11
official/r1/resnet/resnet_run_loop.py
official/r1/resnet/resnet_run_loop.py
+1
-2
official/r1/utils/tpu.py
official/r1/utils/tpu.py
+4
-5
official/recommendation/neumf_model.py
official/recommendation/neumf_model.py
+2
-3
official/transformer/transformer_main.py
official/transformer/transformer_main.py
+16
-23
official/utils/misc/distribution_utils.py
official/utils/misc/distribution_utils.py
+7
-7
未找到文件。
official/r1/boosted_trees/train_higgs.py
浏览文件 @
0788a23c
...
...
@@ -53,7 +53,6 @@ import tensorflow as tf
# pylint: enable=g-bad-import-order
from
official.utils.flags
import
core
as
flags_core
from
tensorflow.contrib
import
estimator
as
contrib_estimator
from
official.utils.flags._conventions
import
help_wrap
from
official.utils.logs
import
logger
...
...
@@ -230,7 +229,7 @@ def train_boosted_trees(flags_obj):
# Though BoostedTreesClassifier is under tf.estimator, faster in-memory
# training is yet provided as a contrib library.
classifier
=
contrib_
estimator
.
boosted_trees_classifier_train_in_memory
(
classifier
=
tf
.
contrib
.
estimator
.
boosted_trees_classifier_train_in_memory
(
train_input_fn
,
feature_columns
,
model_dir
=
flags_obj
.
model_dir
or
None
,
...
...
official/r1/mnist/mnist_eager.py
浏览文件 @
0788a23c
...
...
@@ -33,7 +33,6 @@ import time
from
absl
import
app
as
absl_app
from
absl
import
flags
import
tensorflow
as
tf
from
tensorflow.contrib
import
summary
as
contrib_summary
from
tensorflow.python
import
eager
as
tfe
# pylint: enable=g-bad-import-order
...
...
@@ -62,7 +61,7 @@ def train(model, optimizer, dataset, step_counter, log_interval=None):
start
=
time
.
time
()
for
(
batch
,
(
images
,
labels
))
in
enumerate
(
dataset
):
with
contrib_
summary
.
record_summaries_every_n_global_steps
(
with
tf
.
contrib
.
summary
.
record_summaries_every_n_global_steps
(
10
,
global_step
=
step_counter
):
# Record the operations used to compute the loss given the input,
# so that the gradient of the loss with respect to the variables
...
...
@@ -70,8 +69,8 @@ def train(model, optimizer, dataset, step_counter, log_interval=None):
with
tf
.
GradientTape
()
as
tape
:
logits
=
model
(
images
,
training
=
True
)
loss_value
=
loss
(
logits
,
labels
)
contrib_
summary
.
scalar
(
'loss'
,
loss_value
)
contrib_
summary
.
scalar
(
'accuracy'
,
compute_accuracy
(
logits
,
labels
))
tf
.
contrib
.
summary
.
scalar
(
'loss'
,
loss_value
)
tf
.
contrib
.
summary
.
scalar
(
'accuracy'
,
compute_accuracy
(
logits
,
labels
))
grads
=
tape
.
gradient
(
loss_value
,
model
.
variables
)
optimizer
.
apply_gradients
(
zip
(
grads
,
model
.
variables
),
global_step
=
step_counter
)
...
...
@@ -94,9 +93,9 @@ def test(model, dataset):
tf
.
cast
(
labels
,
tf
.
int64
))
print
(
'Test set: Average loss: %.4f, Accuracy: %4f%%
\n
'
%
(
avg_loss
.
result
(),
100
*
accuracy
.
result
()))
with
contrib_
summary
.
always_record_summaries
():
contrib_
summary
.
scalar
(
'loss'
,
avg_loss
.
result
())
contrib_
summary
.
scalar
(
'accuracy'
,
accuracy
.
result
())
with
tf
.
contrib
.
summary
.
always_record_summaries
():
tf
.
contrib
.
summary
.
scalar
(
'loss'
,
avg_loss
.
result
())
tf
.
contrib
.
summary
.
scalar
(
'accuracy'
,
accuracy
.
result
())
def
run_mnist_eager
(
flags_obj
):
...
...
@@ -138,9 +137,9 @@ def run_mnist_eager(flags_obj):
else
:
train_dir
=
None
test_dir
=
None
summary_writer
=
contrib_
summary
.
create_file_writer
(
summary_writer
=
tf
.
contrib
.
summary
.
create_file_writer
(
train_dir
,
flush_millis
=
10000
)
test_summary_writer
=
contrib_
summary
.
create_file_writer
(
test_summary_writer
=
tf
.
contrib
.
summary
.
create_file_writer
(
test_dir
,
flush_millis
=
10000
,
name
=
'test'
)
# Create and restore checkpoint (if one exists on the path)
...
...
official/r1/mnist/mnist_tpu.py
浏览文件 @
0788a23c
...
...
@@ -33,8 +33,6 @@ import tensorflow as tf
# For open source environment, add grandparent directory for import
sys
.
path
.
append
(
os
.
path
.
dirname
(
os
.
path
.
dirname
(
os
.
path
.
abspath
(
sys
.
path
[
0
]))))
from
tensorflow.contrib
import
cluster_resolver
as
contrib_cluster_resolver
from
tensorflow.contrib
import
tpu
as
contrib_tpu
from
official.r1.mnist
import
dataset
# pylint: disable=wrong-import-position
from
official.r1.mnist
import
mnist
# pylint: disable=wrong-import-position
...
...
@@ -100,7 +98,7 @@ def model_fn(features, labels, mode, params):
'class_ids'
:
tf
.
argmax
(
logits
,
axis
=
1
),
'probabilities'
:
tf
.
nn
.
softmax
(
logits
),
}
return
contrib_
tpu
.
TPUEstimatorSpec
(
mode
,
predictions
=
predictions
)
return
tf
.
contrib
.
tpu
.
TPUEstimatorSpec
(
mode
,
predictions
=
predictions
)
logits
=
model
(
image
,
training
=
(
mode
==
tf
.
estimator
.
ModeKeys
.
TRAIN
))
loss
=
tf
.
losses
.
sparse_softmax_cross_entropy
(
labels
=
labels
,
logits
=
logits
)
...
...
@@ -113,14 +111,14 @@ def model_fn(features, labels, mode, params):
decay_rate
=
0.96
)
optimizer
=
tf
.
train
.
GradientDescentOptimizer
(
learning_rate
=
learning_rate
)
if
FLAGS
.
use_tpu
:
optimizer
=
contrib_
tpu
.
CrossShardOptimizer
(
optimizer
)
return
contrib_
tpu
.
TPUEstimatorSpec
(
optimizer
=
tf
.
contrib
.
tpu
.
CrossShardOptimizer
(
optimizer
)
return
tf
.
contrib
.
tpu
.
TPUEstimatorSpec
(
mode
=
mode
,
loss
=
loss
,
train_op
=
optimizer
.
minimize
(
loss
,
tf
.
train
.
get_global_step
()))
if
mode
==
tf
.
estimator
.
ModeKeys
.
EVAL
:
return
contrib_
tpu
.
TPUEstimatorSpec
(
return
tf
.
contrib
.
tpu
.
TPUEstimatorSpec
(
mode
=
mode
,
loss
=
loss
,
eval_metrics
=
(
metric_fn
,
[
labels
,
logits
]))
...
...
@@ -155,18 +153,21 @@ def main(argv):
del
argv
# Unused.
tf
.
logging
.
set_verbosity
(
tf
.
logging
.
INFO
)
tpu_cluster_resolver
=
contrib_cluster_resolver
.
TPUClusterResolver
(
FLAGS
.
tpu
,
zone
=
FLAGS
.
tpu_zone
,
project
=
FLAGS
.
gcp_project
)
tpu_cluster_resolver
=
tf
.
contrib
.
cluster_resolver
.
TPUClusterResolver
(
FLAGS
.
tpu
,
zone
=
FLAGS
.
tpu_zone
,
project
=
FLAGS
.
gcp_project
)
run_config
=
contrib_
tpu
.
RunConfig
(
run_config
=
tf
.
contrib
.
tpu
.
RunConfig
(
cluster
=
tpu_cluster_resolver
,
model_dir
=
FLAGS
.
model_dir
,
session_config
=
tf
.
ConfigProto
(
allow_soft_placement
=
True
,
log_device_placement
=
True
),
tpu_config
=
contrib_
tpu
.
TPUConfig
(
FLAGS
.
iterations
,
FLAGS
.
num_shards
),
tpu_config
=
tf
.
contrib
.
tpu
.
TPUConfig
(
FLAGS
.
iterations
,
FLAGS
.
num_shards
),
)
estimator
=
contrib_
tpu
.
TPUEstimator
(
estimator
=
tf
.
contrib
.
tpu
.
TPUEstimator
(
model_fn
=
model_fn
,
use_tpu
=
FLAGS
.
use_tpu
,
train_batch_size
=
FLAGS
.
batch_size
,
...
...
official/r1/resnet/resnet_run_loop.py
浏览文件 @
0788a23c
...
...
@@ -30,7 +30,6 @@ import os
from
absl
import
flags
import
tensorflow
as
tf
from
tensorflow.contrib
import
opt
as
contrib_opt
from
official.r1.resnet
import
imagenet_preprocessing
from
official.r1.resnet
import
resnet_model
...
...
@@ -446,7 +445,7 @@ def resnet_model_fn(features, labels, mode, model_class,
tf
.
compat
.
v1
.
summary
.
scalar
(
'learning_rate'
,
learning_rate
)
if
flags
.
FLAGS
.
enable_lars
:
optimizer
=
contrib_
opt
.
LARSOptimizer
(
optimizer
=
tf
.
contrib
.
opt
.
LARSOptimizer
(
learning_rate
,
momentum
=
momentum
,
weight_decay
=
weight_decay
,
...
...
official/r1/utils/tpu.py
浏览文件 @
0788a23c
...
...
@@ -15,7 +15,6 @@
"""Functions specific to running TensorFlow on TPUs."""
import
tensorflow
as
tf
from
tensorflow.contrib
import
summary
as
contrib_summary
# "local" is a magic word in the TPU cluster resolver; it informs the resolver
...
...
@@ -59,13 +58,13 @@ def construct_scalar_host_call(metric_dict, model_dir, prefix=""):
List of summary ops to run on the CPU host.
"""
step
=
global_step
[
0
]
with
contrib_
summary
.
create_file_writer
(
with
tf
.
contrib
.
summary
.
create_file_writer
(
logdir
=
model_dir
,
filename_suffix
=
".host_call"
).
as_default
():
with
contrib_
summary
.
always_record_summaries
():
with
tf
.
contrib
.
summary
.
always_record_summaries
():
for
i
,
name
in
enumerate
(
metric_names
):
contrib_
summary
.
scalar
(
prefix
+
name
,
args
[
i
][
0
],
step
=
step
)
tf
.
contrib
.
summary
.
scalar
(
prefix
+
name
,
args
[
i
][
0
],
step
=
step
)
return
contrib_
summary
.
all_summary_ops
()
return
tf
.
contrib
.
summary
.
all_summary_ops
()
# To log the current learning rate, and gradient norm for Tensorboard, the
# summary op needs to be run on the host CPU via host_call. host_call
...
...
official/recommendation/neumf_model.py
浏览文件 @
0788a23c
...
...
@@ -37,7 +37,6 @@ import sys
from
six.moves
import
xrange
# pylint: disable=redefined-builtin
import
tensorflow
as
tf
from
tensorflow.contrib
import
tpu
as
contrib_tpu
from
official.recommendation
import
constants
as
rconst
from
official.recommendation
import
movielens
...
...
@@ -117,7 +116,7 @@ def neumf_model_fn(features, labels, mode, params):
epsilon
=
params
[
"epsilon"
])
if
params
[
"use_tpu"
]:
# TODO(seemuch): remove this contrib import
optimizer
=
contrib_
tpu
.
CrossShardOptimizer
(
optimizer
)
optimizer
=
tf
.
contrib
.
tpu
.
CrossShardOptimizer
(
optimizer
)
mlperf_helper
.
ncf_print
(
key
=
mlperf_helper
.
TAGS
.
MODEL_HP_LOSS_FN
,
value
=
mlperf_helper
.
TAGS
.
BCE
)
...
...
@@ -275,7 +274,7 @@ def _get_estimator_spec_with_metrics(logits, # type: tf.Tensor
use_tpu_spec
)
if
use_tpu_spec
:
return
contrib_
tpu
.
TPUEstimatorSpec
(
return
tf
.
contrib
.
tpu
.
TPUEstimatorSpec
(
mode
=
tf
.
estimator
.
ModeKeys
.
EVAL
,
loss
=
cross_entropy
,
eval_metrics
=
(
metric_fn
,
[
in_top_k
,
ndcg
,
metric_weights
]))
...
...
official/transformer/transformer_main.py
浏览文件 @
0788a23c
...
...
@@ -33,9 +33,6 @@ import tensorflow as tf
# pylint: enable=g-bad-import-order
from
official.r1.utils
import
export
from
tensorflow.contrib
import
cluster_resolver
as
contrib_cluster_resolver
from
tensorflow.contrib
import
opt
as
contrib_opt
from
tensorflow.contrib
import
tpu
as
contrib_tpu
from
official.r1.utils
import
tpu
as
tpu_util
from
official.transformer
import
compute_bleu
from
official.transformer
import
translate
...
...
@@ -118,10 +115,8 @@ def model_fn(features, labels, mode, params):
metric_fn
=
lambda
logits
,
labels
:
(
metrics
.
get_eval_metrics
(
logits
,
labels
,
params
=
params
))
eval_metrics
=
(
metric_fn
,
[
logits
,
labels
])
return
contrib_tpu
.
TPUEstimatorSpec
(
mode
=
mode
,
loss
=
loss
,
predictions
=
{
"predictions"
:
logits
},
return
tf
.
contrib
.
tpu
.
TPUEstimatorSpec
(
mode
=
mode
,
loss
=
loss
,
predictions
=
{
"predictions"
:
logits
},
eval_metrics
=
eval_metrics
)
return
tf
.
estimator
.
EstimatorSpec
(
mode
=
mode
,
loss
=
loss
,
predictions
=
{
"predictions"
:
logits
},
...
...
@@ -133,14 +128,12 @@ def model_fn(features, labels, mode, params):
# in TensorBoard.
metric_dict
[
"minibatch_loss"
]
=
loss
if
params
[
"use_tpu"
]:
return
contrib_tpu
.
TPUEstimatorSpec
(
mode
=
mode
,
loss
=
loss
,
train_op
=
train_op
,
return
tf
.
contrib
.
tpu
.
TPUEstimatorSpec
(
mode
=
mode
,
loss
=
loss
,
train_op
=
train_op
,
host_call
=
tpu_util
.
construct_scalar_host_call
(
metric_dict
=
metric_dict
,
model_dir
=
params
[
"model_dir"
],
prefix
=
"training/"
)
)
metric_dict
=
metric_dict
,
model_dir
=
params
[
"model_dir"
],
prefix
=
"training/"
)
)
record_scalars
(
metric_dict
)
return
tf
.
estimator
.
EstimatorSpec
(
mode
=
mode
,
loss
=
loss
,
train_op
=
train_op
)
...
...
@@ -180,14 +173,14 @@ def get_train_op_and_metrics(loss, params):
# Create optimizer. Use LazyAdamOptimizer from TF contrib, which is faster
# than the TF core Adam optimizer.
optimizer
=
contrib_
opt
.
LazyAdamOptimizer
(
optimizer
=
tf
.
contrib
.
opt
.
LazyAdamOptimizer
(
learning_rate
,
beta1
=
params
[
"optimizer_adam_beta1"
],
beta2
=
params
[
"optimizer_adam_beta2"
],
epsilon
=
params
[
"optimizer_adam_epsilon"
])
if
params
[
"use_tpu"
]
and
params
[
"tpu"
]
!=
tpu_util
.
LOCAL
:
optimizer
=
contrib_
tpu
.
CrossShardOptimizer
(
optimizer
)
optimizer
=
tf
.
contrib
.
tpu
.
CrossShardOptimizer
(
optimizer
)
# Uses automatic mixed precision FP16 training if on GPU.
if
params
[
"dtype"
]
==
"fp16"
:
...
...
@@ -535,31 +528,31 @@ def construct_estimator(flags_obj, params, schedule_manager):
model_fn
=
model_fn
,
model_dir
=
flags_obj
.
model_dir
,
params
=
params
,
config
=
tf
.
estimator
.
RunConfig
(
train_distribute
=
distribution_strategy
))
tpu_cluster_resolver
=
contrib_
cluster_resolver
.
TPUClusterResolver
(
tpu_cluster_resolver
=
tf
.
contrib
.
cluster_resolver
.
TPUClusterResolver
(
tpu
=
flags_obj
.
tpu
,
zone
=
flags_obj
.
tpu_zone
,
project
=
flags_obj
.
tpu_gcp_project
)
project
=
flags_obj
.
tpu_gcp_project
)
tpu_config
=
contrib_
tpu
.
TPUConfig
(
tpu_config
=
tf
.
contrib
.
tpu
.
TPUConfig
(
iterations_per_loop
=
schedule_manager
.
single_iteration_train_steps
,
num_shards
=
flags_obj
.
num_tpu_shards
)
run_config
=
contrib_
tpu
.
RunConfig
(
run_config
=
tf
.
contrib
.
tpu
.
RunConfig
(
cluster
=
tpu_cluster_resolver
,
model_dir
=
flags_obj
.
model_dir
,
session_config
=
tf
.
ConfigProto
(
allow_soft_placement
=
True
,
log_device_placement
=
True
),
tpu_config
=
tpu_config
)
return
contrib_
tpu
.
TPUEstimator
(
return
tf
.
contrib
.
tpu
.
TPUEstimator
(
model_fn
=
model_fn
,
use_tpu
=
params
[
"use_tpu"
]
and
flags_obj
.
tpu
!=
tpu_util
.
LOCAL
,
train_batch_size
=
schedule_manager
.
batch_size
,
eval_batch_size
=
schedule_manager
.
batch_size
,
params
=
{
# TPUEstimator needs to populate batch_size itself due to sharding.
key
:
value
for
key
,
value
in
params
.
items
()
if
key
!=
"batch_size"
},
key
:
value
for
key
,
value
in
params
.
items
()
if
key
!=
"batch_size"
},
config
=
run_config
)
...
...
official/utils/misc/distribution_utils.py
浏览文件 @
0788a23c
...
...
@@ -23,7 +23,6 @@ import os
import
random
import
string
import
tensorflow
as
tf
from
tensorflow.contrib
import
distribute
as
contrib_distribute
from
official.utils.misc
import
tpu_lib
...
...
@@ -286,9 +285,10 @@ def set_up_synthetic_data():
tf
.
distribute
.
experimental
.
MultiWorkerMirroredStrategy
)
# TODO(tobyboyd): Remove when contrib.distribute is all in core.
if
hasattr
(
tf
,
'contrib'
):
_monkey_patch_dataset_method
(
contrib_distribute
.
MirroredStrategy
)
_monkey_patch_dataset_method
(
contrib_distribute
.
OneDeviceStrategy
)
_monkey_patch_dataset_method
(
contrib_distribute
.
CollectiveAllReduceStrategy
)
_monkey_patch_dataset_method
(
tf
.
contrib
.
distribute
.
MirroredStrategy
)
_monkey_patch_dataset_method
(
tf
.
contrib
.
distribute
.
OneDeviceStrategy
)
_monkey_patch_dataset_method
(
tf
.
contrib
.
distribute
.
CollectiveAllReduceStrategy
)
else
:
print
(
'Contrib missing: Skip monkey patch tf.contrib.distribute.*'
)
...
...
@@ -300,10 +300,10 @@ def undo_set_up_synthetic_data():
tf
.
distribute
.
experimental
.
MultiWorkerMirroredStrategy
)
# TODO(tobyboyd): Remove when contrib.distribute is all in core.
if
hasattr
(
tf
,
'contrib'
):
_undo_monkey_patch_dataset_method
(
contrib_
distribute
.
MirroredStrategy
)
_undo_monkey_patch_dataset_method
(
contrib_
distribute
.
OneDeviceStrategy
)
_undo_monkey_patch_dataset_method
(
tf
.
contrib
.
distribute
.
MirroredStrategy
)
_undo_monkey_patch_dataset_method
(
tf
.
contrib
.
distribute
.
OneDeviceStrategy
)
_undo_monkey_patch_dataset_method
(
contrib_
distribute
.
CollectiveAllReduceStrategy
)
tf
.
contrib
.
distribute
.
CollectiveAllReduceStrategy
)
else
:
print
(
'Contrib missing: Skip remove monkey patch tf.contrib.distribute.*'
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录