def Fill(self, type, **kwargs): """Fill self with the specific type of filler. Parameters ---------- type : str The type of the filler. Returns ------- Tensor Self, with filler registered implicitly in the backend. """ filler = pb.TensorFillerProto() filler.tensor = self.name filler.type = type.lower() if filler.type in ['placeholder', 'variable']: pass elif filler.type == 'constant': filler.value = kwargs['value'] if 'value' in kwargs else 0 elif filler.type in ['normal', 'gaussian']: filler.mean = kwargs['mean'] if 'mean' in kwargs else 0 filler.std = kwargs['std'] if 'std' in kwargs else 1 filler.type = 'normal' elif filler.type == 'uniform': filler.low = kwargs['low'] if 'low' in kwargs else 0 filler.high = kwargs['high'] if 'high' in kwargs else 1 filler.type = 'uniform' elif filler.type in ['truncated_normal', 'truncatednormal']: filler.mean = kwargs['mean'] if 'mean' in kwargs else 0 filler.std = kwargs['std'] if 'std' in kwargs else 1 filler.low = filler.mean - 2.0 * filler.std filler.high = filler.mean + 2.0 * filler.std filler.type = 'truncated_normal' elif filler.type == 'parameterized_truncated_normal': filler.mean = kwargs['mean'] if 'mean' in kwargs else 0 filler.std = kwargs['std'] if 'std' in kwargs else 1 filler.low = kwargs['low'] if 'low' in kwargs else -2.0 filler.high = kwargs['high'] if 'high' in kwargs else 2.0 elif filler.type in ['glorot_uniform', 'xavier']: filler.scale = kwargs['scale'] if 'scale' in kwargs else 3.0 elif filler.type in ['glorot_normal', 'msra']: filler.scale = kwargs['scale'] if 'scale' in kwargs else 2.0 else: raise ValueError('Unknown filler type: {}'.format(filler.type)) ws.CreateFiller(filler) return self
def Constant(self, value=0): """Register as a variable with constant initializer. Parameters ---------- value : basic numerical type The constant value. """ filler = pb.TensorFiller() filler.tensor = self.name filler.type = 'constant' filler.value = value ws.CreateFiller(filler) return self
def Uniform(self, low=-1, high=1): """Register as a variable with uniform initializer. Parameters ---------- low : basic numerical type The lower bound of uniform distribution. high : basic numerical type The higher bound of uniform distribution. """ filler = pb.TensorFiller() filler.tensor = self.name filler.type = 'uniform' filler.low = low filler.high = high ws.CreateFiller(filler) return self
def Normal(self, mu=0, sigma=1): """Register as a variable with normal initializer. Parameters ---------- mu : basic numerical type The mu of normal distribution. sigma : basic numerical type The sigma of normal distribution. """ filler = pb.TensorFiller() filler.tensor = self.name filler.type = 'normal' filler.mean = mu filler.std = sigma ws.CreateFiller(filler) return self
def Fill(self, type, **kwargs): """Fill self with the specific type of filler. Parameters ---------- type : str The type of the filler. Returns ------- Tensor Self, with filler registered implicitly in the backend. """ filler = pb.TensorFiller() filler.tensor = self._name filler.type = type.lower() if filler.type == 'constant': filler.value = kwargs['value'] if 'value' in kwargs else 0 elif filler.type == 'normal' or filler.type == 'gaussian': filler.mean = kwargs['mean'] if 'mean' in kwargs else 0 filler.std = kwargs['std'] if 'std' in kwargs else 1 filler.type = 'normal' elif filler.type == 'uniform': filler.low = kwargs['low'] if 'low' in kwargs else 0 filler.high = kwargs['high'] if 'high' in kwargs else 1 filler.type = 'uniform' elif filler.type == 'truncated_normal' or filler.type == 'truncatednormal': filler.mean = kwargs['mean'] if 'mean' in kwargs else 0 filler.std = kwargs['std'] if 'std' in kwargs else 1 filler.low = filler.mean - 2.0 * filler.std filler.high = filler.mean + 2.0 * filler.std filler.type = 'truncated_normal' elif filler.type == 'parameterized_truncated_normal': filler.mean = kwargs['mean'] if 'mean' in kwargs else 0 filler.std = kwargs['std'] if 'std' in kwargs else 1 filler.low = kwargs['low'] if 'low' in kwargs else -2.0 filler.high = kwargs['high'] if 'high' in kwargs else 2.0 ws.CreateFiller(filler) return self
def _no_parameter_filler(self, type): filler = pb.TensorFiller() filler.tensor = self.name filler.type = type ws.CreateFiller(filler) return self