def _sample(self, n_samples=1, **kwargs): if n_samples > 1: _shape = fluid.layers.shape(self._mean) _shape = fluid.layers.concat([paddle.to_tensor([n_samples], dtype="int32"), _shape]) _len = len(self._std.shape) _std = paddle.tile(self._std, repeat_times=[n_samples, *_len*[1]]) _mean = paddle.tile(self._mean, repeat_times=[n_samples, *_len*[1]]) else: _shape = fluid.layers.shape(self._mean) _std = self._std + 0. _mean = self._mean + 0. if self.is_reparameterized: epsilon = paddle.normal(name='sample', shape=_shape, mean=0.0, std=1.0) sample_ = _mean + _std * epsilon else: _mean.stop_gradient = True _std.stop_gradient = True epsilon = paddle.normal(name='sample', shape=_shape, mean=0.0, std=1.0) sample_ = _mean + _std * epsilon sample_.stop_gradient = False self.sample_cache = sample_ if n_samples > 1: assert(sample_.shape[0] == n_samples) return sample_
def static_api(self): shape = self.get_shape() ret_all_shape = copy.deepcopy(shape) ret_all_shape.insert(0, self.repeat_num) ret_all = np.zeros(ret_all_shape, self.dtype) if isinstance(self.mean, np.ndarray) \ and isinstance(self.std, np.ndarray): with paddle.static.program_guard(paddle.static.Program()): mean = paddle.fluid.data('Mean', self.mean.shape, self.mean.dtype) std = paddle.fluid.data('Std', self.std.shape, self.std.dtype) out = paddle.normal(mean, std, self.shape) exe = paddle.static.Executor(self.place) for i in range(self.repeat_num): ret = exe.run(feed={ 'Mean': self.mean, 'Std': self.std.reshape(shape) }, fetch_list=[out]) ret_all[i] = ret[0] return ret_all elif isinstance(self.mean, np.ndarray): with paddle.static.program_guard(paddle.static.Program()): mean = paddle.fluid.data('Mean', self.mean.shape, self.mean.dtype) out = paddle.normal(mean, self.std, self.shape) exe = paddle.static.Executor(self.place) for i in range(self.repeat_num): ret = exe.run(feed={'Mean': self.mean}, fetch_list=[out]) ret_all[i] = ret[0] return ret_all elif isinstance(self.std, np.ndarray): with paddle.static.program_guard(paddle.static.Program()): std = paddle.fluid.data('Std', self.std.shape, self.std.dtype) out = paddle.normal(self.mean, std, self.shape) exe = paddle.static.Executor(self.place) for i in range(self.repeat_num): ret = exe.run(feed={'Std': self.std}, fetch_list=[out]) ret_all[i] = ret[0] return ret_all else: with paddle.static.program_guard(paddle.static.Program()): out = paddle.normal(self.mean, self.std, self.shape) exe = paddle.static.Executor(self.place) for i in range(self.repeat_num): ret = exe.run(fetch_list=[out]) ret_all[i] = ret[0] return ret_all
def variance_scaling_init_(tensor, scale=1, mode="fan_avg", distribution="uniform"): stop_gradient = tensor.stop_gradient tensor.stop_gradient = True fan_in, fan_out = _calculate_fan_in_and_fan_out(tensor) if mode == "fan_in": scale /= fan_in elif mode == "fan_out": scale /= fan_out else: scale /= (fan_in + fan_out) / 2 if distribution == "normal": std = math.sqrt(scale) tensor[:] = paddle.normal(0, std, shape=tensor.shape).astype(tensor.dtype) tensor.stop_gradient = stop_gradient return tensor else: bound = math.sqrt(3 * scale) tensor[:] = paddle.uniform(tensor.shape, dtype=tensor.dtype, min=-bound, max=bound) tensor.stop_gradient = stop_gradient return tensor
def test_alias(self): paddle.disable_static() shape = [1, 2, 3] out1 = paddle.normal(shape=shape) out2 = paddle.tensor.normal(shape=shape) out3 = paddle.tensor.random.normal(shape=shape) paddle.enable_static()
def kaiming_normal_(x, a=0, mode='fan_in', nonlinearity='leaky_relu'): """Fills the input `Tensor` with values according to the method described in `Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification` - He, K. et al. (2015), using a normal distribution. The resulting tensor will have values sampled from :math:`\mathcal{N}(0, \text{std}^2)` where .. math:: \text{std} = \frac{\text{gain}}{\sqrt{\text{fan\_mode}}} Also known as He initialization. Args: x: an n-dimensional `paddle.Tensor` a: the negative slope of the rectifier used after this layer (only used with ``'leaky_relu'``) mode: either ``'fan_in'`` (default) or ``'fan_out'``. Choosing ``'fan_in'`` preserves the magnitude of the variance of the weights in the forward pass. Choosing ``'fan_out'`` preserves the magnitudes in the backwards pass. nonlinearity: the non-linear function (`nn.functional` name), recommended to use only with ``'relu'`` or ``'leaky_relu'`` (default). """ fan = _calculate_correct_fan(x, mode) gain = calculate_gain(nonlinearity, a) std = gain / math.sqrt(fan) temp_value = paddle.normal(0, std, shape=x.shape) x.set_value(temp_value) return x
def normal_init_(param, mean=0.0, std=1.0): replaced_param = paddle.create_parameter( shape=param.shape, dtype=param.dtype, default_initializer=paddle.nn.initializer.Assign( paddle.normal( mean=mean, std=std, shape=param.shape))) paddle.assign(replaced_param, param)
def reparameterize(self, mu, logvar): """ tbd """ eps = paddle.normal(mean=0, std=self.model_config['eps_std'], shape=mu.shape) return mu + (logvar / 2).exp() * eps
def hook(layer, input, output): global embedding global alpha output += paddle.normal(std=self.noise_amount, shape=output.shape) output = alpha * output embedding = output return output
def sampling(self, z_mean, z_log_var): """ Reparameterization trick """ # By default, random_normal has mean=0 and std=1.0 epsilon = paddle.normal(shape=(z_mean.shape[0], self.latent_size)) epsilon.stop_gradient = True return z_mean + paddle.exp(0.5 * z_log_var) * epsilon
def hook(layer, input, output): if noise_amount is not None: bias = paddle.normal(std=noise_amount * output.mean(), shape=output.shape) output = output + bias if scale is not None: output = scale * output target_feature_map.append(output) return output
def dygraph_api(self): paddle.disable_static(self.place) shape = self.get_shape() ret_all_shape = copy.deepcopy(shape) ret_all_shape.insert(0, self.repeat_num) ret_all = np.zeros(ret_all_shape, self.dtype) mean = paddle.to_tensor(self.mean) \ if isinstance(self.mean, np.ndarray) else self.mean std = paddle.to_tensor(self.std) \ if isinstance(self.std, np.ndarray) else self.std for i in range(self.repeat_num): out = paddle.normal(mean, std, self.shape) ret_all[i] = out.numpy() paddle.enable_static() return ret_all
def forward(self, bn, observed, resample=False, step=1): if resample: self.t = 0 bn.forward(observed) self.t += 1 self._latent = {k:v.tensor for k,v in bn.nodes.items() if k not in observed.keys()} self._latent_k = self._latent.keys() self._var_list = [self._latent[k] for k in self._latent_k] #self._var_list = [fluid.layers.zeros(self._latent[k].shape, dtype='float32') #for k in self._latent_k] sample_ = dict(zip(self._latent_k, self._var_list)) for i in range(len(self._var_list)): self._var_list[i] = self._var_list[i].detach() self._var_list[i].stop_gradient = False return sample_ for s in range(step): observed_ = {**dict(zip(self._latent_k, self._var_list)), **observed} bn.forward(observed_) log_joint_ = bn.log_joint() grad = paddle.grad(log_joint_, self._var_list) for i,_ in enumerate(grad): _lr = max(self.lr_min,self.lr / math.sqrt(self.t)) #_lr = self.lr / math.sqrt(self.t) epsilon = paddle.normal(shape=self._var_list[i].shape, mean=0.0, std=paddle.sqrt(_lr)) self._var_list[i] = self._var_list[i] + 0.5 * _lr * grad[i] + epsilon self._var_list[i] = self._var_list[i].detach() self._var_list[i].stop_gradient = False self.t += 1 sample_ = dict(zip(self._latent_k, self._var_list)) return sample_
def forward(self, trg): # Encoder latent_z = paddle.normal(shape=(trg.shape[0], self.latent_size)) dec_first_hidden_cell = self.fc(latent_z) dec_first_hidden, dec_first_cell = paddle.split( dec_first_hidden_cell, 2, axis=-1) if self.num_layers > 1: dec_first_hidden = paddle.split(dec_first_hidden, self.num_layers) dec_first_cell = paddle.split(dec_first_cell, self.num_layers) else: dec_first_hidden = [dec_first_hidden] dec_first_cell = [dec_first_cell] dec_initial_states = [[h, c] for h, c in zip(dec_first_hidden, dec_first_cell)] output_fc = lambda x: F.one_hot( paddle.multinomial( F.softmax(paddle.squeeze( self.decoder.output_fc(x),[1]))),num_classes=self.vocab_size) latent_z = nn.BeamSearchDecoder.tile_beam_merge_with_batch( latent_z, self.beam_size) decoder = nn.BeamSearchDecoder( cell=self.decoder.lstm.cell, start_token=self.start_token, end_token=self.end_token, beam_size=self.beam_size, embedding_fn=self.decoder.trg_embedder, output_fn=output_fc) outputs, _ = nn.dynamic_decode( decoder, inits=dec_initial_states, max_step_num=self.max_out_len, latent_z=latent_z) return outputs
def normal_(x, mean=0., std=1.): temp_value = paddle.normal(mean, std, shape=x.shape) x.set_value(temp_value) return x
def synthetic_data(w, b, num_examples): #@save """生成y=Xw+b+噪声""" X = paddle.normal(0, 1, (num_examples, len(w))) y = paddle.matmul(X, w) + b y += paddle.normal(0, 0.01, y.shape) return X, y.reshape((-1, 1))
def __init__(self, rank, local_rank, world_size, batch_size, resume, margin_softmax, num_classes, sample_rate=1.0, embedding_size=512, prefix="./"): super(PartialFC, self).__init__() self.num_classes: int = num_classes self.rank: int = rank self.local_rank: int = local_rank self.world_size: int = world_size self.batch_size: int = batch_size self.margin_softmax: callable = margin_softmax self.sample_rate: float = sample_rate self.embedding_size: int = embedding_size self.prefix: str = prefix self.num_local: int = num_classes // world_size + int( rank < num_classes % world_size) self.class_start: int = num_classes // world_size * rank + min( rank, num_classes % world_size) self.num_sample: int = int(self.sample_rate * self.num_local) self.weight_name = os.path.join( self.prefix, "rank:{}_softmax_weight.pkl".format(self.rank)) self.weight_mom_name = os.path.join( self.prefix, "rank:{}_softmax_weight_mom.pkl".format(self.rank)) if resume: try: self.weight: paddle.Tensor = paddle.load(self.weight_name) print("softmax weight resume successfully!") except (FileNotFoundError, KeyError, IndexError): self.weight = paddle.normal( 0, 0.01, (self.num_local, self.embedding_size)) print("softmax weight resume fail!") try: self.weight_mom: paddle.Tensor = paddle.load( self.weight_mom_name) print("softmax weight mom resume successfully!") except (FileNotFoundError, KeyError, IndexError): self.weight_mom: paddle.Tensor = paddle.zeros_like(self.weight) print("softmax weight mom resume fail!") else: self.weight = paddle.normal(0, 0.01, (self.num_local, self.embedding_size)) self.weight_mom: paddle.Tensor = paddle.zeros_like(self.weight) print("softmax weight init successfully!") print("softmax weight mom init successfully!") self.index = None if int(self.sample_rate) == 1: self.update = lambda: 0 self.sub_weight = paddle.create_parameter( shape=self.weight.shape, dtype='float32', default_initializer=paddle.nn.initializer.Assign(self.weight)) self.sub_weight_mom = self.weight_mom else: self.sub_weight = paddle.create_parameter( shape=[1, 1], dtype='float32', default_initializer=paddle.nn.initializer.Assign( paddle.empty((1, 1))))
def _no_grad_normal_(tensor, mean=0., std=1.): with paddle.no_grad(): tensor.set_value(paddle.normal(mean=mean, std=std, shape=tensor.shape)) return tensor
def train(): global epoch total_reward = 0 # 重置游戏状态 state = env.reset() while True: action = actor.select_action(state) noisy = paddle.normal(0, exploration_noise, shape=[env.action_space.shape[0] ]).clip(env.action_space.low, env.action_space.high) action = (action + noisy).clip(env.action_space.low, env.action_space.high).numpy() next_state, reward, done, info = env.step(action) env.render() rpm.append((state, action, reward, next_state, np.float(done))) state = next_state if done: break total_reward += reward if len(rpm) > batch_size: # 获取训练数据 batch_state, batch_action, batch_reward, batch_next_state, batch_done = rpm.sample( batch_size) # 计算损失函数 best_v_1 = target_critic_1(batch_next_state, target_actor(batch_next_state)) best_v_2 = target_critic_2(batch_next_state, target_actor(batch_next_state)) best_v = paddle.min(paddle.concat([best_v_1, best_v_2], axis=1), axis=1, keepdim=True) best_v = batch_reward + (gamma * best_v * (1 - batch_done)).detach() current_v_1 = critic_1(batch_state, batch_action) critic_loss = F.mse_loss(current_v_1, best_v) critic_1_optimizer.clear_grad() critic_loss.backward() critic_1_optimizer.step() current_v_2 = critic_2(batch_state, batch_action) critic_loss = F.mse_loss(current_v_2, best_v) critic_2_optimizer.clear_grad() critic_loss.backward() critic_2_optimizer.step() if epoch % policy_delay == 0: actor_loss = -critic_1(batch_state, actor(batch_state)).mean() actor_optimizer.clear_grad() actor_loss.backward() actor_optimizer.step() # 指定的训练次数更新一次目标模型的参数 if epoch % 200 == 0: for target_param, param in zip(target_actor.parameters(), actor.parameters()): target_param.set_value(target_param * (1.0 - ratio) + param * ratio) for target_param, param in zip(target_critic_1.parameters(), critic_1.parameters()): target_param.set_value(target_param * (1.0 - ratio) + param * ratio) for target_param, param in zip(target_critic_2.parameters(), critic_2.parameters()): target_param.set_value(target_param * (1.0 - ratio) + param * ratio) epoch += 1 return total_reward
def _no_grad_normal_(tensor, mean, std): with paddle.no_grad(): tensor.set_value(paddle.normal(shape=tensor.shape, mean=mean, std=std)) return tensor
def __init__(self, dataset, depth, alpha, num_classes, bottleneck=False): super(PyramidNet, self).__init__() self.dataset = dataset if self.dataset.startswith('cifar'): self.inplanes = 16 if bottleneck == True: n = int((depth - 2) / 9) block = Bottleneck else: n = int((depth - 2) / 6) block = BasicBlock self.addrate = alpha / (3 * n * 1.0) self.input_featuremap_dim = self.inplanes self.conv1 = nn.Conv2D(3, self.input_featuremap_dim, kernel_size=3, stride=1, padding=1, bias_attr=False) self.bn1 = nn.BatchNorm2D(self.input_featuremap_dim) self.featuremap_dim = self.input_featuremap_dim self.layer1 = self.pyramidal_make_layer(block, n) self.layer2 = self.pyramidal_make_layer(block, n, stride=2) self.layer3 = self.pyramidal_make_layer(block, n, stride=2) self.final_featuremap_dim = self.input_featuremap_dim self.bn_final = nn.BatchNorm2D(self.final_featuremap_dim) self.relu_final = nn.ReLU() self.avgpool = nn.AvgPool2D(8) self.fc = nn.Linear(self.final_featuremap_dim, num_classes) elif dataset == 'imagenet': blocks = { 18: BasicBlock, 34: BasicBlock, 50: Bottleneck, 101: Bottleneck, 152: Bottleneck, 200: Bottleneck } layers = { 18: [2, 2, 2, 2], 34: [3, 4, 6, 3], 50: [3, 4, 6, 3], 101: [3, 4, 23, 3], 152: [3, 8, 36, 3], 200: [3, 24, 36, 3] } if layers.get(depth) is None: if bottleneck == True: blocks[depth] = Bottleneck temp_cfg = int((depth - 2) / 12) else: blocks[depth] = BasicBlock temp_cfg = int((depth - 2) / 8) layers[depth] = [temp_cfg, temp_cfg, temp_cfg, temp_cfg] print('=> the layer configuration for each stage is set to', layers[depth]) self.inplanes = 64 self.addrate = alpha / (sum(layers[depth]) * 1.0) self.input_featuremap_dim = self.inplanes self.conv1 = nn.Conv2D(3, self.input_featuremap_dim, kernel_size=7, stride=2, padding=3, bias_attr=False) self.bn1 = nn.BatchNorm2D(self.input_featuremap_dim) self.relu = nn.ReLU() self.maxpool = nn.MaxPool2D(kernel_size=3, stride=2, padding=1) self.featuremap_dim = self.input_featuremap_dim self.layer1 = self.pyramidal_make_layer(blocks[depth], layers[depth][0]) self.layer2 = self.pyramidal_make_layer(blocks[depth], layers[depth][1], stride=2) self.layer3 = self.pyramidal_make_layer(blocks[depth], layers[depth][2], stride=2) self.layer4 = self.pyramidal_make_layer(blocks[depth], layers[depth][3], stride=2) self.final_featuremap_dim = self.input_featuremap_dim self.bn_final = nn.BatchNorm2D(self.final_featuremap_dim) self.relu_final = nn.ReLU() self.avgpool = nn.AvgPool2D(7) self.fc = nn.Linear(self.final_featuremap_dim, num_classes) for m in self.sublayers(): if isinstance(m, nn.Conv2D): n = m._kernel_size[0] * m._kernel_size[1] * m._out_channels m.weight.set_value( paddle.normal(mean=0, std=math.sqrt(2. / n), shape=m.weight.shape)) elif isinstance(m, nn.BatchNorm2D): m.weight.set_value(paddle.ones(m.weight.shape)) m.bias.set_value(paddle.zeros(m.bias.shape))
def hook(layer, input, output): output += paddle.normal(std=self.noise_amount, shape=output.shape) output = self._alpha * output self._embedding = output return output