|
| 1 | +import paddle.v2.framework.network as network |
| 2 | +import paddle.v2.framework.core as core |
| 3 | + |
| 4 | +__all__ = ['Model', 'g_model', 'ParameterAttribute'] |
| 5 | + |
| 6 | + |
| 7 | +class ParameterAttribute(object): |
| 8 | + def __init__(self, |
| 9 | + name=None, |
| 10 | + initial_max=None, |
| 11 | + initial_min=None, |
| 12 | + initial_mean=None, |
| 13 | + initial_std=None, |
| 14 | + initial_seed=0): |
| 15 | + self.name = name |
| 16 | + |
| 17 | + if initial_min is None and initial_max is None and initial_mean is None and initial_std is None: |
| 18 | + initial_min = -1.0 |
| 19 | + initial_max = 1.0 |
| 20 | + |
| 21 | + if initial_max is not None and initial_min is not None: |
| 22 | + self.init_strategy = ('uniform_random', { |
| 23 | + 'min': initial_min, |
| 24 | + 'max': initial_max, |
| 25 | + 'seed': initial_seed |
| 26 | + }) |
| 27 | + elif initial_mean is not None and initial_std is not None: |
| 28 | + self.init_strategy = ('gauss_random', { |
| 29 | + 'mean': initial_mean, |
| 30 | + 'std': initial_std, |
| 31 | + 'seed': initial_seed |
| 32 | + }) |
| 33 | + else: |
| 34 | + raise ValueError() |
| 35 | + |
| 36 | + @staticmethod |
| 37 | + def default_weight_attr(): |
| 38 | + return ParameterAttribute() |
| 39 | + |
| 40 | + @staticmethod |
| 41 | + def default_bias_attr(): |
| 42 | + # TODO(yy): Change it to FillZero. |
| 43 | + return ParameterAttribute(initial_min=-0.0001, initial_max=0.0001) |
| 44 | + |
| 45 | + |
| 46 | +class Model(object): |
| 47 | + def __init__(self, place=None): |
| 48 | + self.init_network = network.Network() |
| 49 | + self.network = network.Network() |
| 50 | + |
| 51 | + if place is None: |
| 52 | + self.device_context = None |
| 53 | + self.place = None |
| 54 | + else: |
| 55 | + self.device_context = core.DeviceContext.create(place) |
| 56 | + self.place = None |
| 57 | + |
| 58 | + self.global_scope = core.Scope() |
| 59 | + self.cur_scope = self.global_scope |
| 60 | + self.name_counter = 0 |
| 61 | + self.all_param_names = set() |
| 62 | + self.has_been_run = False |
| 63 | + |
| 64 | + def next_name(self, prefix): |
| 65 | + name = prefix + str(self.name_counter) |
| 66 | + self.name_counter += 1 |
| 67 | + return name |
| 68 | + |
| 69 | + def create_parameter(self, name_prefix, param_attr, dims): |
| 70 | + if not isinstance(param_attr, ParameterAttribute): |
| 71 | + raise TypeError() |
| 72 | + if param_attr.name is None: |
| 73 | + param_attr.name = self.next_name(name_prefix) |
| 74 | + |
| 75 | + if self.cur_scope.find_var(param_attr.name) is not None: |
| 76 | + raise ValueError("Parameter {} has been created before", |
| 77 | + param_attr.name) |
| 78 | + |
| 79 | + self.cur_scope.new_var(param_attr.name).get_tensor() |
| 80 | + |
| 81 | + op_type, attrs = param_attr.init_strategy |
| 82 | + attrs['dims'] = dims |
| 83 | + attrs['Out'] = param_attr.name |
| 84 | + |
| 85 | + op_func = getattr(self.init_network, op_type) |
| 86 | + pname = op_func(**attrs) |
| 87 | + self.all_param_names.add(pname) |
| 88 | + |
| 89 | + self.init_network.infer_shape( |
| 90 | + len(self.init_network) - 1, self.cur_scope) |
| 91 | + |
| 92 | + return pname |
| 93 | + |
| 94 | + def add_op_and_infer_shape(self, op_type, **kwargs): |
| 95 | + out = self.network.create_and_add_op(op_type, **kwargs) |
| 96 | + return_value = out |
| 97 | + if out is None: |
| 98 | + return |
| 99 | + |
| 100 | + if isinstance(out, unicode) or isinstance(out, str): |
| 101 | + out = [out] |
| 102 | + |
| 103 | + for o in out: |
| 104 | + v = self.cur_scope.find_var(o) |
| 105 | + if v is None: |
| 106 | + v = self.cur_scope.new_var(o) |
| 107 | + v.get_tensor() |
| 108 | + |
| 109 | + op_idx = len(self.network) - 1 |
| 110 | + self.network.infer_shape(op_idx, self.cur_scope) |
| 111 | + |
| 112 | + return return_value |
| 113 | + |
| 114 | + def set_place(self, place): |
| 115 | + if self.has_been_run: |
| 116 | + raise ValueError("Cannot set place to model after run") |
| 117 | + |
| 118 | + self.device_context = core.DeviceContext.create(place) |
| 119 | + self.place = place |
| 120 | + |
| 121 | + def init_parameters(self): |
| 122 | + if self.device_context is None: |
| 123 | + # TODO(yy): Log warning here |
| 124 | + self.set_place(core.CPUPlace()) |
| 125 | + if not self.has_been_run: |
| 126 | + self.has_been_run = True |
| 127 | + |
| 128 | + self.init_network.run(self.global_scope, self.device_context) |
| 129 | + |
| 130 | + def run(self): |
| 131 | + if not self.has_been_run: |
| 132 | + self.has_been_run = True |
| 133 | + self.network.infer_shape(self.global_scope) |
| 134 | + self.network.run(self.global_scope, self.device_context) |
| 135 | + |
| 136 | + def feed_data(self, data): |
| 137 | + for key in data: |
| 138 | + tensor = self.global_scope.find_var(key).get_tensor() |
| 139 | + d = data[key] |
| 140 | + tensor.set_dims(d.shape) |
| 141 | + tensor.set(d, self.place) |
| 142 | + |
| 143 | + def find_tensor(self, var_name): |
| 144 | + return self.global_scope.find_var(var_name).get_tensor() |
| 145 | + |
| 146 | + |
| 147 | +g_model = Model() |
0 commit comments