forked from google-research/google-research
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.h
115 lines (96 loc) · 4.05 KB
/
generator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2020 The Google Research Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GENERATOR_H_
#define GENERATOR_H_
#include <memory>
#include <random>
#include "algorithm.h"
#include "definitions.h"
#include "instruction.pb.h"
#include "generator.pb.h"
#include "instruction.h"
#include "randomizer.h"
namespace automl_zero {
class RandomGenerator;
constexpr double kDefaultLearningRate = 0.01;
constexpr double kDefaultInitScale = 0.1;
// A class to generate Algorithms.
class Generator {
public:
Generator(
// The model used to initialize the population. See HardcodedAlgorithmID
// enum. Used by TheInitModel() and ignored by other methods.
HardcodedAlgorithmID init_model,
// The sizes of the component functions. Can be zero if only using
// deterministic models without padding.
IntegerT setup_size_init,
IntegerT predict_size_init,
IntegerT learn_size_init,
// Ops that can be introduced into the setup component function. Can be
// empty if only deterministic models will be generated.
const std::vector<Op>& allowed_setup_ops,
// Ops that can be introduced into the predict component function. Can be
// empty if only deterministic models will be generated.
const std::vector<Op>& allowed_predict_ops,
// Ops that can be introduced into the learn component function. Can be
// empty if deterministic models will be generated.
const std::vector<Op>& allowed_learn_ops,
// Can be a nullptr if only deterministic models will be generated.
std::mt19937* bit_gen,
// Can be a nullptr if only deterministic models will be generated.
RandomGenerator* rand_gen);
Generator(const Generator&) = delete;
Generator& operator=(const Generator&) = delete;
// Returns Algorithm for initialization.
Algorithm TheInitModel();
// Returns Algorithm of the given model type. This will be one of the ones
// below.
Algorithm ModelByID(HardcodedAlgorithmID model);
// A Algorithm with no-op instructions.
Algorithm NoOp();
// Returns Algorithm with fixed-size component functions with random
// instructions.
Algorithm Random();
// A linear model with learning by gradient descent.
static constexpr AddressT LINEAR_ALGORITHMWeightsAddress = 1;
Algorithm LinearModel(double learning_rate);
// A 2-layer neural network with one nonlinearity, where both layers implement
// learning by gradient descent. The weights are initialized randomly.
Algorithm NeuralNet(
double learning_rate, double first_init_scale, double final_init_scale);
// A 2-layer neural network without bias and no learning.
static constexpr AddressT
kUnitTestNeuralNetNoBiasNoGradientFinalLayerWeightsAddress = 1;
static constexpr AddressT
kUnitTestNeuralNetNoBiasNoGradientFirstLayerWeightsAddress = 0;
Algorithm UnitTestNeuralNetNoBiasNoGradient(const double learning_rate);
// Used to create a simple generator for tests. See Generator.
Generator();
private:
friend Generator Generator();
const HardcodedAlgorithmID init_model_;
const IntegerT setup_size_init_;
const IntegerT predict_size_init_;
const IntegerT learn_size_init_;
const std::vector<Op> allowed_setup_ops_;
const std::vector<Op> allowed_predict_ops_;
const std::vector<Op> allowed_learn_ops_;
std::unique_ptr<std::mt19937> bit_gen_owned_;
std::unique_ptr<RandomGenerator> rand_gen_owned_;
RandomGenerator* rand_gen_;
Randomizer randomizer_;
std::shared_ptr<const Instruction> no_op_instruction_;
};
} // namespace automl_zero
#endif // GENERATOR_H_