google-research

Форк
0
/
generate_samples.py 
68 строк · 2.4 Кб
1
# coding=utf-8
2
# Copyright 2024 The Google Research Authors.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15

16
"""Generate a json file containing a dictionary mapping names to configs.
17

18
This manages sampling configs for both optimizers and tasks.
19

20
The tasks are not directly part of the task dataset of tasks as not all configs
21
will be feasible and/or will train.
22
"""
23
import collections
24
from absl import app
25
from absl import flags
26

27
from task_set import registry
28
from task_set.optimizers import all_optimizers  # pylint: disable=unused-import
29
from task_set.tasks import all_tasks  # pylint: disable=unused-import
30
from task_set.tasks import utils
31
import tensorflow.compat.v1 as tf
32

33
flags.DEFINE_string("task_sampler", None, "Module to use for sampling tasks")
34
flags.DEFINE_string("optimizer_sampler", None,
35
                    "Module to use for sampling optimizers")
36

37
flags.DEFINE_string("output_file", None, "Output location for sampling.")
38
flags.mark_flag_as_required("output_file")
39

40
flags.DEFINE_integer("num_samples", 100, "Number of samples so select.")
41
FLAGS = flags.FLAGS
42

43

44
def main(_):
45
  if FLAGS.task_sampler and FLAGS.optimizer_sampler:
46
    raise ValueError("Only specify one sampler!")
47
  if not FLAGS.task_sampler and not FLAGS.optimizer_sampler:
48
    raise ValueError("Must specify either task_sampler or optimizer_sampler!")
49

50
  if FLAGS.task_sampler:
51
    sampler = registry.task_registry.get_sampler(FLAGS.task_sampler)
52
    sampler_name = FLAGS.task_sampler
53
  else:
54
    sampler = registry.optimizers_registry.get_sampler(FLAGS.optimizer_sampler)
55
    sampler_name = FLAGS.optimizer_sampler
56

57
  samples = collections.OrderedDict()
58
  for i in range(FLAGS.num_samples):
59
    cfg = sampler(i)
60
    task_name = "%s_seed%d" % (sampler_name, i)
61
    samples[task_name] = cfg, sampler_name
62

63
  with tf.gfile.GFile(FLAGS.output_file, "w") as f:
64
    f.write(utils.pretty_json_dumps(samples).encode("utf-8"))
65

66

67
if __name__ == "__main__":
68
  app.run(main)
69

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.