def var(x, axis=None, unbiased=True, keepdim=False, name=None): """ Computes the variance of ``x`` along ``axis`` . Args: x (Tensor): The input Tensor with data type float32, float64. axis (int|list|tuple, optional): The axis along which to perform variance calculations. ``axis`` should be int, list(int) or tuple(int). If ``axis`` is a list/tuple of dimension(s), variance is calculated along all element(s) of ``axis`` . ``axis`` or element(s) of ``axis`` should be in range [-D, D), where D is the dimensions of ``x`` . If ``axis`` or element(s) of ``axis`` is less than 0, it works the same way as :math:`axis + D` . If ``axis`` is None, variance is calculated over all elements of ``x``. Default is None. unbiased (bool, optional): Whether to use the unbiased estimation. If ``unbiased`` is True, the divisor used in the computation is :math:`N - 1`, where :math:`N` represents the number of elements along ``axis`` , otherwise the divisor is :math:`N`. Default is True. keepdim (bool, optional): Whether to reserve the reduced dimension(s) in the output Tensor. If ``keepdim`` is True, the dimensions of the output Tensor is the same as ``x`` except in the reduced dimensions(it is of size 1 in this case). Otherwise, the shape of the output Tensor is squeezed in ``axis`` . Default is False. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor, results of variance along ``axis`` of ``x``, with the same data type as ``x``. Examples: .. code-block:: python import paddle import numpy as np paddle.disable_static() x = np.array([[1.0, 2.0, 3.0], [1.0, 4.0, 5.0]]) x = paddle.to_tensor(x) out1 = paddle.var(x) # [2.66666667] out2 = paddle.var(x, axis=1) # [1. 4.33333333] """ if not in_dygraph_mode(): check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'var') u = mean(x, axis, True, name) out = paddle.sum((x - u)**2, axis, keepdim=keepdim, name=name) n = paddle.cast(paddle.numel(x), x.dtype) \ / paddle.cast(paddle.numel(out), x.dtype) if unbiased: one_const = paddle.ones([1], x.dtype) n = where(n > one_const, n - 1., one_const) out /= n return out
def test_numel_imperative(self): paddle.disable_static(paddle.CPUPlace()) input_1 = np.random.random([2, 1, 4, 5]).astype("int32") input_2 = np.random.random([1, 4, 5]).astype("int32") x_1 = paddle.to_tensor(input_1) x_2 = paddle.to_tensor(input_2) out_1 = paddle.numel(x_1) out_2 = paddle.numel(x_2) assert (np.array_equal(out_1.numpy().item(0), np.size(input_1))) assert (np.array_equal(out_2.numpy().item(0), np.size(input_2))) paddle.enable_static()
def var(x, axis=None, unbiased=True, keepdim=False, name=None): u = paddle.mean(x, axis, True, name) out = paddle.sum((x - u) * (x - u), axis, keepdim=keepdim, name=name) n = paddle.cast(paddle.numel(x), x.dtype) \ / paddle.cast(paddle.numel(out), x.dtype) if unbiased: one_const = paddle.ones([1], x.dtype) n = paddle.where(n > one_const, n - 1., one_const) out /= n return out
def test_numel_static(self): main_program = fluid.Program() startup_program = fluid.Program() with fluid.program_guard(main_program, startup_program): shape1 = [2, 1, 4, 5] shape2 = [1, 4, 5] x_1 = paddle.fluid.data(shape=shape1, dtype='int32', name='x_1') x_2 = paddle.fluid.data(shape=shape2, dtype='int32', name='x_2') input_1 = np.random.random(shape1).astype("int32") input_2 = np.random.random(shape2).astype("int32") out_1 = paddle.numel(x_1) out_2 = paddle.numel(x_2) exe = paddle.static.Executor(place=paddle.CPUPlace()) res_1, res_2 = exe.run(feed={ "x_1": input_1, "x_2": input_2, }, fetch_list=[out_1, out_2]) assert (np.array_equal( res_1, np.array([np.size(input_1)]).astype("int64"))) assert (np.array_equal( res_2, np.array([np.size(input_2)]).astype("int64")))
def _calculate_fan_in_and_fan_out(tensor): dimensions = len(tensor.shape) if dimensions < 2: raise ValueError( "Fan in and fan out can not be computed for tensor with fewer than 2 dimensions" ) num_input_fmaps = tensor.shape[1] num_output_fmaps = tensor.shape[0] receptive_field_size = 1 if len(tensor.shape) > 2: receptive_field_size = paddle.numel(tensor[0][0]) fan_in = num_input_fmaps * receptive_field_size fan_out = num_output_fmaps * receptive_field_size return fan_in, fan_out
def _split_activation(tensor): global _hcg mp_degree = _hcg.get_model_parallel_world_size() mp_rank = _hcg.get_model_parallel_rank() if mp_degree < 2: return tensor tensor_numel = paddle.numel(tensor) assert tensor_numel != 0, "can't recompute zero element" assert tensor_numel % mp_degree == 0, "The capacity of the activation () cannot be divisible by mp_degree()".format( tensor_numel, mp_degree) # use inplace operation to save memory data = tensor.flatten_() part_size = tensor_numel // mp_degree start = part_size * mp_rank end = start + part_size return data[start:end]
def forward(self, hidden, target, keep_order=False): assert (hidden.shape[0] == target.shape[0]) if self.num_clusters == 0: logit = self._compute_logits(hidden, self.out_layers_weight[0], self.out_layers_bias[0], self.out_projs[0]) nll = -paddle.log(F.softmax(logit, axis=-1)) idx = paddle.concat( [ paddle.arange(0, nll.shape[0]).unsqueeze([1]), target.unsqueeze(1) ], axis=1) nll = paddle.gather_nd(nll, idx) else: weights, biases = [], [] for i in range(len(self.cutoffs)): if self.div_val == 1: l_idx, r_idx = self.cutoff_ends[i], self.cutoff_ends[i + 1] weight_i = self.out_layers_weight[0][l_idx:r_idx] bias_i = self.out_layers_bias[0][l_idx:r_idx] else: weight_i = self.out_layers_weight[i] bias_i = self.out_layers_bias[i] if i == 0: weight_i = paddle.concat( [weight_i, self.cluster_weight], axis=0) bias_i = paddle.concat([bias_i, self.cluster_bias], axis=0) weights.append(weight_i) biases.append(bias_i) head_weight, head_bias, head_proj = weights[0], biases[ 0], self.out_projs[0] head_logit = self._compute_logits(hidden, head_weight, head_bias, head_proj) head_logprob = paddle.log(F.softmax(head_logit, axis=-1)) nll = paddle.zeros_like(target, dtype=hidden.dtype) offset = 0 cutoff_values = [0] + self.cutoffs for i in range(len(cutoff_values) - 1): l_idx, r_idx = cutoff_values[i], cutoff_values[i + 1] mask_i = paddle.cast( target >= l_idx, dtype=paddle.get_default_dtype()) * paddle.cast( target < r_idx, dtype="int64") indices_i = paddle.nonzero(mask_i).squeeze([1]) if paddle.numel(indices_i) == 0: continue target_i = paddle.gather(target, indices_i, axis=0) - l_idx head_logprob_i = paddle.gather(head_logprob, indices_i, axis=0) if i == 0: target_i_idx = paddle.concat( [ paddle.arange(0, head_logprob_i.shape[0]).unsqueeze( [1]), target_i.unsqueeze([1]) ], axis=1) logprob_i = head_logprob_i.gather_nd(target_i_idx) else: weight_i, bias_i, proj_i = weights[i], biases[ i], self.out_projs[i].weight if self.out_projs[ i] is not None else None hidden_i = paddle.gather(hidden, indices_i, axis=0) tail_logit_i = self._compute_logits(hidden_i, weight_i, bias_i, proj_i) tail_logprob_i = paddle.log( F.softmax( tail_logit_i, axis=-1)) target_i_idx = paddle.concat( [ paddle.arange(0, tail_logprob_i.shape[0]).unsqueeze( [1]), target_i.unsqueeze([1]) ], axis=1) logprob_i = tail_logprob_i.gather_nd(target_i_idx) logprob_i = head_logprob_i[:, -i] + logprob_i if self.keep_order or keep_order: nll = paddle.scatter(nll, indices_i, -logprob_i) else: index = paddle.arange(offset, offset + logprob_i.shape[0], 1) nll = paddle.scatter(nll, index, -logprob_i) offset += logprob_i.shape[0] return nll
def forward(self, inputs): """ forward """ x = paddle.numel(inputs) return x
def test_x_type(): shape = [1, 4, 5] input_1 = np.random.random(shape).astype("int32") out_1 = paddle.numel(input_1)