jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def test_stochasatic_pool_samples():
    """
    check if the order of frequency of samples from stochastic max pool
    are same as the order of input values.
    """

    batch = 1
    channel = 1
    pool_shape = (2, 2)
    pool_stride = (2, 2)
    image_shape = (2, 2)
    rng = numpy.random.RandomState(12345)
    data = rng.uniform(0, 10, size=(batch, channel, image_shape[0], image_shape[1])).astype('float32')

    x = theano.tensor.tensor4()
    s_max = stochastic_max_pool_bc01(x, pool_shape, pool_stride, image_shape)
    f = theano.function([x], s_max)

    samples = []
    for i in xrange(300):
        samples.append(numpy.asarray(f(data))[0,0,0,0])
    counts = Counter(samples)
    data = data.reshape(numpy.prod(image_shape))
    data.sort()
    data = data[::-1]
    for i in range(len(data) -1):
        assert counts[data[i]] >= counts[data[i+1]]
//  [python]
def _row_sparse_ndarray_from_definition(data, indices, shape=None, ctx=None,
                                        dtype=None, indices_type=None):
    """Create a `RowSparseNDArray` based on data and indices"""
    storage_type = 'row_sparse'
    # context
    ctx = current_context() if ctx is None else ctx
    # types
    dtype = _prepare_default_dtype(data, dtype)
    indices_type = _STORAGE_AUX_TYPES[storage_type][0] if indices_type is None else indices_type
    # prepare src array and types
    data = _prepare_src_array(data, dtype)
    indices = _prepare_src_array(indices, indices_type)

    # TODO(junwu): Convert data, indptr, and indices to mxnet NDArrays
    # if they are not for now. In the future, we should provide a c-api
    # to accept np.ndarray types to copy from to result.data and aux_data
    if not isinstance(data, NDArray):
        data = _array(data, ctx, dtype)
    if not isinstance(indices, NDArray):
        indices = _array(indices, ctx, indices_type)
    if shape is None:
        num_indices = indices.shape[0]
        if num_indices == 0:
            raise ValueError('invalid shape')
        dim0 = indices[num_indices - 1].asscalar() + 1
        shape = (dim0, ) + data.shape[1:]
    # verify shapes
    if data.ndim != len(shape) or indices.ndim != 1 or reduce(lambda x, y: x*y, shape[1:]) == 0:
        raise ValueError("invalid shape")
    result = RowSparseNDArray(_new_alloc_handle(storage_type, shape, ctx, False, dtype,
                                                [indices_type], [indices.shape]))
    check_call(_LIB.MXNDArraySyncCopyFromNDArray(result.handle, data.handle, ctypes.c_int(-1)))
    check_call(_LIB.MXNDArraySyncCopyFromNDArray(result.handle, indices.handle, ctypes.c_int(0)))
    return result
//  [python]
def _row_sparse_ndarray_from_definition(data, indices, shape=None, ctx=None,
                                        dtype=None, indices_type=None):
    """Create a `RowSparseNDArray` based on data and indices"""
    storage_type = 'row_sparse'
    # context
    ctx = current_context() if ctx is None else ctx
    # types
    dtype = _prepare_default_dtype(data, dtype)
    indices_type = _STORAGE_AUX_TYPES[storage_type][0] if indices_type is None else indices_type
    # prepare src array and types
    data = _prepare_src_array(data, dtype)
    indices = _prepare_src_array(indices, indices_type)

    # TODO(junwu): Convert data, indptr, and indices to mxnet NDArrays
    # if they are not for now. In the future, we should provide a c-api
    # to accept np.ndarray types to copy from to result.data and aux_data
    if not isinstance(data, NDArray):
        data = _array(data, ctx, dtype)
    if not isinstance(indices, NDArray):
        indices = _array(indices, ctx, indices_type)
    if shape is None:
        num_indices = indices.shape[0]
        if num_indices == 0:
            raise ValueError('invalid shape')
        dim0 = indices[num_indices - 1].asscalar() + 1
        shape = (dim0, ) + data.shape[1:]
    # verify shapes
    if data.ndim != len(shape) or indices.ndim != 1 or np.prod(shape[1:]) == 0:
        raise ValueError("invalid shape")
    result = RowSparseNDArray(_new_alloc_handle(storage_type, shape, ctx, False, dtype,
                                                [indices_type], [indices.shape]))
    check_call(_LIB.MXNDArraySyncCopyFromNDArray(result.handle, data.handle, ctypes.c_int(-1)))
    check_call(_LIB.MXNDArraySyncCopyFromNDArray(result.handle, indices.handle, ctypes.c_int(0)))
    return result
//  [python]
def test_prod(self):
        x = matrix([[1, 2, 3], [4, 5, 6]])
        assert_equal(x.prod(), 720)
        assert_equal(x.prod(0), matrix([[4, 10, 18]]))
        assert_equal(x.prod(1), matrix([[6], [120]]))

        assert_equal(n.prod(x), 720)
        assert_equal(n.prod(x, axis=0), matrix([[4, 10, 18]]))
        assert_equal(n.prod(x, axis=1), matrix([[6], [120]]))

        y = matrix([0, 1, 3])
        assert_(y.prod() == 0)
//  [python]
def test_prod(self):
        x = matrix([[1, 2, 3], [4, 5, 6]])
        assert_equal(x.prod(), 720)
        assert_equal(x.prod(0), matrix([[4, 10, 18]]))
        assert_equal(x.prod(1), matrix([[6], [120]]))

        assert_equal(np.prod(x), 720)
        assert_equal(np.prod(x, axis=0), matrix([[4, 10, 18]]))
        assert_equal(np.prod(x, axis=1), matrix([[6], [120]]))

        y = matrix([0, 1, 3])
        assert_(y.prod() == 0)
//  [python]
def test_nansum_axis_float16(self):
        # Note that the above test example overflows in float16. We use a
        # smaller array instead, just return if array is too large.
        if numpy.prod(self.shape) > 24:
            return
        a = testing.shaped_arange(self.shape, dtype="e")
        a[:, 1] = cupy.nan
        sa = cupy.nansum(a, axis=1)
        b = testing.shaped_arange(self.shape, numpy, dtype="f")
        b[:, 1] = numpy.nan
        sb = numpy.nansum(b, axis=1)
        testing.assert_allclose(sa, sb.astype("e"))
//  [python]
def tensor_sample(
    tens: Tensor,
    sample_size: int,
    dim: Union[None, int, List[int], Tuple[int, ...]] = None,
) -> Tensor:
    """
    :param tens: the tensor to grab samples from
    :param sample_size: the number of samples to grab overall if dim is not supplied
        or per each dim if it is
    :param dim: the dimension(s) to split the samples over;
        ex, can split over batch, channels, or combos
    :return: the sampled tensor
    """
    if sample_size < 1:
        raise ValueError("improper sample size given of {}".format(sample_size))

    if dim is None:
        indices = tens.new_zeros((sample_size,)).long().random_(0, tens.numel())
        samples = tens.view(-1)[indices]

        return samples

    if isinstance(dim, int):
        dim = [dim]

    if max(dim) >= len(tens.shape):
        raise ValueError(
            "Unsupported dim given of {} in {} for tensor shape {}".format(
                max(dim), dim, tens.shape
            )
        )

    if dim != [ind for ind in range(len(dim))]:
        # put the desired dimension(s) at the front to sample from
        tens = tens.permute(
            *dim, *[ind for ind in range(len(tens.shape)) if ind not in dim]
        )
        dim = [ind for ind in range(len(dim))]

    if not tens.is_contiguous():
        tens = tens.contiguous()

    num_indices = int(numpy.prod([tens.shape[ind] for ind in range(len(dim))]))
    elem_per_ind = int(
        numpy.prod([tens.shape[ind] for ind in range(len(dim), len(tens.shape))])
    )
    # create a new tensor with offsets set for each of our elements that we are indexing
    indices = tens.new_tensor(
        [ind * elem_per_ind for ind in range(num_indices)], dtype=torch.long
    ).unsqueeze(1)
    # now broadcast it across to the total number of elements we should end with
    indices = indices * tens.new_ones((num_indices, sample_size), dtype=torch.long)
    # finally add in a random number within the available range per index
    indices += tens.new_zeros((num_indices, sample_size), dtype=torch.long).random_(
        0, elem_per_ind
    )
    # get our samples
    samples = tens.view(-1)[indices.view(-1)]
    # reshape for the proper dimension
    samples = samples.view(*(tens.shape[ind] for ind in dim), sample_size)

    return samples
//  [python]
def agent_start(self,observation):
        returnAction = qlearning.qlearning_agent.agent_start(self, observation)
        self.pnorm = 2. * max(1, numpy.log10(n.prod(self.weights.shape)))
        self.qnorm = self.pnorm / (self.pnorm - 1.)
        return returnAction
//  [python]
def agent_start(self,observation):
        returnAction = qlearning.qlearning_agent.agent_start(self, observation)
        self.pnorm = 2. * max(1, numpy.log10(numpy.prod(self.weights.shape)))
        self.qnorm = self.pnorm / (self.pnorm - 1.)
        return returnAction
//  [python]
def scramble_pop_ids(self, mask_corners=True):
        """
        Spectrum corresponding to scrambling individuals among populations.
        
        This is useful for assessing how diverged populations are.
        Essentially, it pools all the individuals represented in the fs and
        generates new populations of random individuals (without replacement)
        from that pool. If this fs is significantly different from the
        original, that implies population structure.
        """
        original_folded = self.folded
        # If we started with an folded Spectrum, we need to unfold before
        # projecting.
        if original_folded:
            self = self.unfold()

        total_samp = numpy.sum(self.sample_sizes)
    
        # First generate a 1d sfs for the pooled population.
        combined = numpy.zeros(total_samp+1)
        # For each entry in the fs, this is the total number of derived alleles
        total_per_entry = self._total_per_entry()
        # Sum up to generate the equivalent 1-d spectrum.
        for derived,counts in zip(total_per_entry.ravel(), self.ravel()):
            combined[derived] += counts
    
        # Now resample back into a n-d spectrum
        # For each entry, this is the counts per popuation. 
        #  e.g. counts_per_entry[3,4,5] = [3,4,5]
        counts_per_entry = self._counts_per_entry()
        # Reshape it to be 1-d, so we can iterate over it easily.
        counts_per_entry = counts_per_entry.reshape(numpy.prod(self.shape), 
                                                    self.ndim)
        resamp = numpy.zeros(self.shape)
        for counts, derived in zip(counts_per_entry, total_per_entry.ravel()):
            # The probability here is 
            # (t1 choose d1)*(t2 choose d2)/(ntot choose derived)
            lnprob = sum(_lncomb(t,d) for t,d in zip(self.sample_sizes,counts))
            lnprob -= _lncomb(total_samp, derived)
            prob = numpy.exp(lnprob)
            # Assign result using the appropriate weighting
            resamp[tuple(counts)] += prob*combined[derived]

        resamp = Spectrum(resamp, mask_corners=mask_corners)
        if not original_folded:
            return resamp
        else:
            return resamp.fold()
//  [python]
def agent_start(self,observation):
        returnAction = sarsa_lambda.sarsa_lambda.agent_start(self, observation)
        self.pnorm = 2. * max(1, numpy.log10(n.prod(self.weights.shape)))
        self.qnorm = self.pnorm / (self.pnorm - 1.)
        return returnAction
//  [python]
def agent_start(self,observation):
        returnAction = sarsa_lambda.sarsa_lambda.agent_start(self, observation)
        self.pnorm = 2. * max(1, numpy.log10(numpy.prod(self.weights.shape)))
        self.qnorm = self.pnorm / (self.pnorm - 1.)
        return returnAction
//  [python]
def test_devicearray_broadcast_host_copy(self):
        broadsize = 4
        coreshape = (2, 3)
        coresize = np.multiply.reduce(coreshape)
        core_c = np.arange(coresize).reshape(coreshape, order='C')
        core_f = np.arange(coresize).reshape(coreshape, order='F')
        for dim in range(len(coreshape)):
            newindex = (slice(None),) * dim + (np.newaxis,)
            broadshape = coreshape[:dim] + (broadsize,) + coreshape[dim:]
            broad_c = np.broadcast_to(core_c[newindex], broadshape)
            broad_f = np.broadcast_to(core_f[newindex], broadshape)
            dbroad_c = cuda.to_device(broad_c)
            dbroad_f = cuda.to_device(broad_f)
            np.testing.assert_array_equal(dbroad_c.copy_to_host(), broad_c)
            np.testing.assert_array_equal(dbroad_f.copy_to_host(), broad_f)
            # Also test copying across different core orderings
            dbroad_c.copy_to_device(broad_f)
            dbroad_f.copy_to_device(broad_c)
            np.testing.assert_array_equal(dbroad_c.copy_to_host(), broad_f)
            np.testing.assert_array_equal(dbroad_f.copy_to_host(), broad_c)
//  [python]
def test_devicearray_broadcast_host_copy(self):
        broadsize = 4
        coreshape = (2, 3)
        coresize = np.prod(coreshape)
        core_c = np.arange(coresize).reshape(coreshape, order='C')
        core_f = np.arange(coresize).reshape(coreshape, order='F')
        for dim in range(len(coreshape)):
            newindex = (slice(None),) * dim + (np.newaxis,)
            broadshape = coreshape[:dim] + (broadsize,) + coreshape[dim:]
            broad_c = np.broadcast_to(core_c[newindex], broadshape)
            broad_f = np.broadcast_to(core_f[newindex], broadshape)
            dbroad_c = cuda.to_device(broad_c)
            dbroad_f = cuda.to_device(broad_f)
            np.testing.assert_array_equal(dbroad_c.copy_to_host(), broad_c)
            np.testing.assert_array_equal(dbroad_f.copy_to_host(), broad_f)
            # Also test copying across different core orderings
            dbroad_c.copy_to_device(broad_f)
            dbroad_f.copy_to_device(broad_c)
            np.testing.assert_array_equal(dbroad_c.copy_to_host(), broad_f)
            np.testing.assert_array_equal(dbroad_f.copy_to_host(), broad_c)
//  [python]
def test_zerosize_reduction(self):
        # Test with default dtype and object dtype
        for a in [[], np.array([], dtype=object)]:
            assert_equal(np.sum(a), 0)
            assert_equal(np.prod(a), 1)
            assert_equal(np.any(a), False)
            assert_equal(np.all(a), True)
            assert_raises(ValueError, np.max, a)
            assert_raises(ValueError, np.min, a)
//  [python]
def matmul(a, b):
    """Perform the equivalent of :obj:`numpy.matmul` on two arrays.

    Parameters
    ----------
    a, b : Union[SparseArray, np.ndarray, scipy.sparse.spmatrix]
        The arrays to perform the :code:`matmul` operation on.

    Returns
    -------
    Union[SparseArray, numpy.ndarray]
        The result of the operation.

    Raises
    ------
    ValueError
        If all arguments don't have zero fill-values, or the shape of the two arrays is not broadcastable.

    See Also
    --------
    numpy.matmul : NumPy equivalent function.
    COO.__matmul__ : Equivalent function for COO objects.
    """
    check_zero_fill_value(a, b)
    if not hasattr(a, "ndim") or not hasattr(b, "ndim"):
        raise TypeError(f"Cannot perform dot product on types {type(a)}, {type(b)}")

    if check_class_nan(a) or check_class_nan(b):
        warnings.warn("Nan will not be propagated in matrix multiplication", RuntimeWarning, stacklevel=1)

    # When b is 2-d, it is equivalent to dot
    if b.ndim <= 2:
        return dot(a, b)

    # when a is 2-d, we need to transpose result after dot
    if a.ndim <= 2:
        res = dot(a, b)
        axes = list(range(res.ndim))
        axes.insert(-1, axes.pop(0))
        return res.transpose(axes)

    # If a can be squeeze to a vector, use dot will be faster
    if a.ndim <= b.ndim and n.prod(a.shape[:-1]) == 1:
        res = dot(a.reshape(-1), b)
        shape = list(res.shape)
        shape.insert(-1, 1)
        return res.reshape(shape)

    # If b can be squeeze to a matrix, use dot will be faster
    if b.ndim <= a.ndim and n.prod(b.shape[:-2]) == 1:
        return dot(a, b.reshape(b.shape[-2:]))

    if a.ndim < b.ndim:
        a = a[(None,) * (b.ndim - a.ndim)]
    if a.ndim > b.ndim:
        b = b[(None,) * (a.ndim - b.ndim)]
    for i, j in zip(a.shape[:-2], b.shape[:-2], strict=True):
        if i != 1 and j != 1 and i != j:
            raise ValueError("shapes of a and b are not broadcastable")

    def _matmul_recurser(a, b):
        if a.ndim == 2:
            return dot(a, b)
        res = []
        for i in range(builtins.max(a.shape[0], b.shape[0])):
            a_i = a[0] if a.shape[0] == 1 else a[i]
            b_i = b[0] if b.shape[0] == 1 else b[i]
            res.append(_matmul_recurser(a_i, b_i))
        mask = [isinstance(x, SparseArray) for x in res]
        if builtins.all(mask):
            return stack(res)

        res = [x.todense() if isinstance(x, SparseArray) else x for x in res]
        return np.stack(res)

    return _matmul_recurser(a, b)
//  [python]
def matmul(a, b):
    """Perform the equivalent of :obj:`numpy.matmul` on two arrays.

    Parameters
    ----------
    a, b : Union[SparseArray, np.ndarray, scipy.sparse.spmatrix]
        The arrays to perform the :code:`matmul` operation on.

    Returns
    -------
    Union[SparseArray, numpy.ndarray]
        The result of the operation.

    Raises
    ------
    ValueError
        If all arguments don't have zero fill-values, or the shape of the two arrays is not broadcastable.

    See Also
    --------
    numpy.matmul : NumPy equivalent function.
    COO.__matmul__ : Equivalent function for COO objects.
    """
    check_zero_fill_value(a, b)
    if not hasattr(a, "ndim") or not hasattr(b, "ndim"):
        raise TypeError(f"Cannot perform dot product on types {type(a)}, {type(b)}")

    if check_class_nan(a) or check_class_nan(b):
        warnings.warn("Nan will not be propagated in matrix multiplication", RuntimeWarning, stacklevel=1)

    # When b is 2-d, it is equivalent to dot
    if b.ndim <= 2:
        return dot(a, b)

    # when a is 2-d, we need to transpose result after dot
    if a.ndim <= 2:
        res = dot(a, b)
        axes = list(range(res.ndim))
        axes.insert(-1, axes.pop(0))
        return res.transpose(axes)

    # If a can be squeeze to a vector, use dot will be faster
    if a.ndim <= b.ndim and np.prod(a.shape[:-1]) == 1:
        res = dot(a.reshape(-1), b)
        shape = list(res.shape)
        shape.insert(-1, 1)
        return res.reshape(shape)

    # If b can be squeeze to a matrix, use dot will be faster
    if b.ndim <= a.ndim and np.prod(b.shape[:-2]) == 1:
        return dot(a, b.reshape(b.shape[-2:]))

    if a.ndim < b.ndim:
        a = a[(None,) * (b.ndim - a.ndim)]
    if a.ndim > b.ndim:
        b = b[(None,) * (a.ndim - b.ndim)]
    for i, j in zip(a.shape[:-2], b.shape[:-2], strict=True):
        if i != 1 and j != 1 and i != j:
            raise ValueError("shapes of a and b are not broadcastable")

    def _matmul_recurser(a, b):
        if a.ndim == 2:
            return dot(a, b)
        res = []
        for i in range(builtins.max(a.shape[0], b.shape[0])):
            a_i = a[0] if a.shape[0] == 1 else a[i]
            b_i = b[0] if b.shape[0] == 1 else b[i]
            res.append(_matmul_recurser(a_i, b_i))
        mask = [isinstance(x, SparseArray) for x in res]
        if builtins.all(mask):
            return stack(res)

        res = [x.todense() if isinstance(x, SparseArray) else x for x in res]
        return np.stack(res)

    return _matmul_recurser(a, b)
//  [python]
def test_object_array_reduction(self):
        # Reductions on object arrays
        a = np.array(['a', 'b', 'c'], dtype=object)
        assert_equal(np.sum(a), 'abc')
        assert_equal(np.max(a), 'c')
        assert_equal(np.min(a), 'a')
        a = np.array([True, False, True], dtype=object)
        assert_equal(np.sum(a), 2)
        assert_equal(np.multiply.reduce(a), 0)
        assert_equal(np.any(a), True)
        assert_equal(np.all(a), False)
        assert_equal(np.max(a), True)
        assert_equal(np.min(a), False)
        assert_equal(np.array([[1]], dtype=object).sum(), 1)
        assert_equal(np.array([[[1, 2]]], dtype=object).sum((0, 1)), [1, 2])
        assert_equal(np.array([1], dtype=object).sum(initial=1), 2)
//  [python]
def test_object_array_reduction(self):
        # Reductions on object arrays
        a = np.array(['a', 'b', 'c'], dtype=object)
        assert_equal(np.sum(a), 'abc')
        assert_equal(np.max(a), 'c')
        assert_equal(np.min(a), 'a')
        a = np.array([True, False, True], dtype=object)
        assert_equal(np.sum(a), 2)
        assert_equal(np.prod(a), 0)
        assert_equal(np.any(a), True)
        assert_equal(np.all(a), False)
        assert_equal(np.max(a), True)
        assert_equal(np.min(a), False)
        assert_equal(np.array([[1]], dtype=object).sum(), 1)
        assert_equal(np.array([[[1, 2]]], dtype=object).sum((0, 1)), [1, 2])
        assert_equal(np.array([1], dtype=object).sum(initial=1), 2)
//  [python]
def _scale_per_slice(self, shape, axis, values):
    # Note: repeats the values if the shape is larger than values.
    out = np.take(values, np.remainder(np.arange(np.multiply.reduce(shape)),
                                       len(values))).reshape(shape)
    if axis is not None:
      scale_shape = [1] * len(shape)
      scale_shape[axis] = shape[axis]
      out *= np.arange(1, shape[axis] + 1).reshape(scale_shape)
    return out
//  [python]
def _scale_per_slice(self, shape, axis, values):
    # Note: repeats the values if the shape is larger than values.
    out = np.take(values, np.remainder(np.arange(np.prod(shape)),
                                       len(values))).reshape(shape)
    if axis is not None:
      scale_shape = [1] * len(shape)
      scale_shape[axis] = shape[axis]
      out *= np.arange(1, shape[axis] + 1).reshape(scale_shape)
    return out
//  [python]
def calc_norm(self,x, mode='vec'):
        if mode=='vec':
            return tf.sqrt(tf.reduce_sum(tf.real(x)**2+tf.imag(x)**2, axis=[i for i in range(len(self.d))]))
        else:
            return tf.trace(tf.transpose(tf.reshape(tf.real(x), [n.prod(self.d), n.prod(self.d), self.ntraj]), [2, 0, 1]))
//  [python]
def calc_norm(self,x, mode='vec'):
        if mode=='vec':
            return tf.sqrt(tf.reduce_sum(tf.real(x)**2+tf.imag(x)**2, axis=[i for i in range(len(self.d))]))
        else:
            return tf.trace(tf.transpose(tf.reshape(tf.real(x), [numpy.prod(self.d), numpy.prod(self.d), self.ntraj]), [2, 0, 1]))
//  [python]
def dof_ravel(self, x, mode='vec'):
        if mode == 'vec':
            return tf.reshape(x, (n.prod(self.d), self.ntraj))
        else:
            return tf.reshape(x, (n.prod(self.d), n.prod(self.d), self.ntraj))
//  [python]
def dof_ravel(self, x, mode='vec'):
        if mode == 'vec':
            return tf.reshape(x, (numpy.prod(self.d), self.ntraj))
        else:
            return tf.reshape(x, (numpy.prod(self.d), numpy.prod(self.d), self.ntraj))
//  [python]
def test_all_sparse_utlc():
    skip_if_no_data()
    for name in ['harry', 'terry', 'ule']:
        print("Loading sparse ", name)
        train, valid, test = utlc.load_sparse_dataset(name, normalize=True)
        nb_elem = numpy.prod(train.shape)
        mi = train.data.min()
        ma = train.data.max()
        mi = min(0, mi)
        ma = max(0, ma)
        su = train.data.sum()
        mean = float(su) / nb_elem
        print(name, "dtype, max, min, mean, nb non-zero, nb element, %sparse")
        print(train.dtype, ma, mi, mean, train.nnz, end='')
        print(nb_elem, (nb_elem - float(train.nnz)) / nb_elem)
        print(name, "max, min, mean, std (all stats on non-zero element)")
        print(train.data.max(), train.data.min(), end='')
        print(train.data.mean(), train.data.std())
        assert scipy.sparse.issparse(train)
        assert scipy.sparse.issparse(valid)
        assert scipy.sparse.issparse(test)
        assert train.shape[1] == test.shape[1] == valid.shape[1]
//  [python]
def test_checkerbox_maskgen():
    image = numpy.ones((20, 64, 64, 1))
    batch_size = 10
    mask_size = (3, 3)

    maskgen = maskedgen(image, batch_size, mask_size, replace_by='zero')

    minibatch = next(maskgen)
    actual_mask_ratio = minibatch[0]['input_msk'].sum() / minibatch[0]['input_msk'].size

    assert len(minibatch) == 2
    assert isinstance(minibatch[0], dict)
    assert (
        minibatch[0]['input'].sum()
        == minibatch[0]['input_msk'].size - minibatch[0]['input_msk'].sum()
    )
    assert minibatch[0]['input_msk'].sum() == minibatch[1].sum()
    assert minibatch[1].shape == (batch_size,) + image.shape[1:]
    assert (
        (1 / (n.array(mask_size).prod())) * 0.9
        <= actual_mask_ratio
        <= (1 / (n.array(mask_size).prod())) * 1.1
    )
//  [python]
def test_checkerbox_maskgen():
    image = numpy.ones((20, 64, 64, 1))
    batch_size = 10
    mask_size = (3, 3)

    maskgen = maskedgen(image, batch_size, mask_size, replace_by='zero')

    minibatch = next(maskgen)
    actual_mask_ratio = minibatch[0]['input_msk'].sum() / minibatch[0]['input_msk'].size

    assert len(minibatch) == 2
    assert isinstance(minibatch[0], dict)
    assert (
        minibatch[0]['input'].sum()
        == minibatch[0]['input_msk'].size - minibatch[0]['input_msk'].sum()
    )
    assert minibatch[0]['input_msk'].sum() == minibatch[1].sum()
    assert minibatch[1].shape == (batch_size,) + image.shape[1:]
    assert (
        (1 / numpy.prod(mask_size)) * 0.9
        <= actual_mask_ratio
        <= (1 / numpy.prod(mask_size)) * 1.1
    )
//  [python]
def test_scalar_reduction(self):
        # The functions 'sum', 'prod', etc allow specifying axis=0
        # even for scalars
        assert_equal(np.sum(3, axis=0), 3)
        assert_equal(np.prod(3.5, axis=0), 3.5)
        assert_equal(np.any(True, axis=0), True)
        assert_equal(np.all(False, axis=0), False)
        assert_equal(np.max(3, axis=0), 3)
        assert_equal(np.min(2.5, axis=0), 2.5)

        # Check scalar behaviour for ufuncs without an identity
        assert_equal(np.power.reduce(3), 3)

        # Make sure that scalars are coming out from this operation
        assert_(type(np.prod(np.float32(2.5), axis=0)) is np.float32)
        assert_(type(np.sum(np.float32(2.5), axis=0)) is np.float32)
        assert_(type(np.max(np.float32(2.5), axis=0)) is np.float32)
        assert_(type(np.min(np.float32(2.5), axis=0)) is np.float32)

        # check if scalars/0-d arrays get cast
        assert_(type(np.any(0, axis=0)) is np.bool_)

        # assert that 0-d arrays get wrapped
        class MyArray(np.ndarray):
            pass
        a = np.array(1).view(MyArray)
        assert_(type(np.any(a)) is MyArray)
//  [python]
def peakSNR(snr, amp):
    """Find the SNR peak by fitting the SNR curve.

    A smooth curve is fitted to the top end of the SNR vs input amplitude
    data with Legendre's least-squares method.

    The curve fitted to the data is:

    .. math::

        y = ax + \\frac{b}{x - c}

    **Parameters:**

    snr : 1D ndarray or array-like
          Signal to Noise Ratio array, expressed in decibels (dB).

    amp : 1D ndarray or array-like
          Amplitude array, expressed in decibels (dB).

    The two arrays ``snr`` and ``amp`` should have the same size.

    **Returns:**

    (peak_snr, peak_amp) : tuple
                           The peak SNR value and its corresponding amplitude,
                           expressed in dB.
    """

    # sanitize inputs
    snr = np.atleast_1d(np.asarray(snr))
    amp = np.atleast_1d(np.asarray(amp))
    # Delete garbage data
    for check in (np.isinf, np.isnan):
        i = check(snr)
        snr = np.delete(snr, np.where(i))
        amp = np.delete(amp, np.where(i))
    for x in (snr, amp):
        if len(x.shape) > 1 and np.prod(x.shape) != max(x.shape):
            raise ValueError("snr and amp should be vectors (ndim=1)" +
                             "snr.shape: %s, amp.shape: %s" % (snr.shape, amp.shape))
    # All good
    max_snr = np.max(snr)
    i = np.flatnonzero(snr > max_snr - 10)
    min_i = np.min(i)
    max_i = np.max(i)
    j = np.flatnonzero(snr[min_i:max_i + 1] < max_snr - 15)
    if j:
        max_i = min_i + np.min(j) - 2
        i = np.arange(min_i, max_i + 1)
    snr = 10.0**(snr[i]/20)
    amp = 10.0**(amp[i]/20)
    #n = max(i.shape) # unused variable, REP?
    c = np.max(amp)*1.05
    # fit y = ax + b/(x-c) to the data
    A = np.hstack((amp.reshape(-1, 1), 1.0/(amp.reshape(-1, 1) - c)))
    ab = np.linalg.lstsq(A, snr.reshape((-1, 1)))[0]
    peak_amp = c - np.sqrt(ab[1, 0]/ab[0, 0])
    peak_snr = np.dot(np.array([[peak_amp, 1./(peak_amp-c)]]), ab) #None check mcode
    peak_snr = dbv(peak_snr)
    peak_amp = dbv(peak_amp)
    if _debug:
        import pylab as plt
        pred = np.dot(A, ab)
        hold = plt.ishold()
        plt.hold(True)
        plt.plot(dbv(amp), dbv(pred), '-', color='b')
        plt.hold(hold)
    return peak_snr, peak_amp
//  [python]
def set_initial_pure_state(self, gi, gv, mat=True):
        with tf.Session() as sess:
            x = tf.Variable(tf.cast(tf.sparse_tensor_to_dense(tf.SparseTensor(gi, gv, self.d+[self.ntraj])), self.cdtype), name='initial_pure_state_tensor')
            sess.run(x.initializer)
            self.initial_state_vec = tf.Variable(sess.run(x), name='initial_pure_state_vec_variable')
            sess.run(self.initial_state_vec.initializer)
            if mat:
                x_flat1 = tf.reshape(x, [n.prod(self.d), 1, self.ntraj])
                x_flat2 = tf.reshape(x, [1, n.prod(self.d), self.ntraj])
                self.initial_state_mat = tf.Variable(sess.run(tf.reshape(x_flat1*tf.conj(x_flat2), self.d+self.d+[self.ntraj])), name='initial_pure_state_mat_variable')
//  [python]
def set_initial_pure_state(self, gi, gv, mat=True):
        with tf.Session() as sess:
            x = tf.Variable(tf.cast(tf.sparse_tensor_to_dense(tf.SparseTensor(gi, gv, self.d+[self.ntraj])), self.cdtype), name='initial_pure_state_tensor')
            sess.run(x.initializer)
            self.initial_state_vec = tf.Variable(sess.run(x), name='initial_pure_state_vec_variable')
            sess.run(self.initial_state_vec.initializer)
            if mat:
                x_flat1 = tf.reshape(x, [numpy.prod(self.d), 1, self.ntraj])
                x_flat2 = tf.reshape(x, [1, numpy.prod(self.d), self.ntraj])
                self.initial_state_mat = tf.Variable(sess.run(tf.reshape(x_flat1*tf.conj(x_flat2), self.d+self.d+[self.ntraj])), name='initial_pure_state_mat_variable')
//  [python]
def _pool_hook(
        self,
        mod: Union[_MaxPoolNd, _AvgPoolNd],
        inp: Union[Tuple[Tensor, ...], Tensor],
        out: Union[Tuple[Tensor, ...], Tensor],
    ):
        desc, inp, out = self._init_forward_hook(mod, inp, out)

        params = {key: val for key, val in mod.named_parameters()}
        desc.params = sum([val.numel() for val in params.values()])
        desc.prunable_params = 0
        desc.zeroed_params = 0

        batch_size, input_channels, input_height, input_width = inp[0].size()
        batch_size, output_channels, output_height, output_width = out[0].size()

        if isinstance(mod.kernel_size, numbers.Number) or mod.kernel_size.dim() == 1:
            kernel_ops = mod.kernel_size * mod.kernel_size
        else:
            kernel_ops = numpy.prod(mod.kernel_size)
        flops = kernel_ops * output_channels * output_height * output_width * batch_size

        desc.flops = flops
        desc.total_flops += desc.flops
//  [python]
def __init__(self, **params):
        super(UniformLog10, self).__init__(**params)
        self._norm = n.prod([n.log10(bnd[1]) - n.log10(bnd[0])
                                   for bnd in self._bounds.values()])
        self._lognorm = n.log(self._norm)
//  [python]
def __init__(self, **params):
        super(UniformLog10, self).__init__(**params)
        self._norm = numpy.prod([numpy.log10(bnd[1]) - numpy.log10(bnd[0])
                                   for bnd in self._bounds.values()])
        self._lognorm = numpy.log(self._norm)
//  [python]
def _softmax_hook(
        self,
        mod: Union[_MaxPoolNd, _AvgPoolNd],
        inp: Union[Tuple[Tensor, ...], Tensor],
        out: Union[Tuple[Tensor, ...], Tensor],
    ):
        desc, inp, out = self._init_forward_hook(mod, inp, out)

        params = {key: val for key, val in mod.named_parameters()}

        desc.params = sum([val.numel() for val in params.values()])
        desc.prunable_params = 0
        desc.zeroed_params = 0

        flops_per_channel = (
            2 if len(out[0].shape) < 3 else float(numpy.prod(out[0].shape[2:]))
        )
        desc.flops = flops_per_channel * out[0].shape[1]
        desc.total_flops += desc.flops
//  [python]
def _pdf(self, **kwargs):
        """Returns the pdf at the given values. The keyword arguments must
        contain all of parameters in self's params. Unrecognized arguments are
        ignored.
        """
        if kwargs in self:
            vals = numpy.array([numpy.log(10) * self._norm * kwargs[param]
                                for param in kwargs.keys()])
            return 1.0 / numpy.prod(vals)
        else:
            return 0.
//  [python]
def __array__(self, dtype=None):
        # Special case for (0,)*-shape datasets
        if numpy.prod(self.shape) == 0:
            return self[()]
        else:
            return numpy.array(self[...], dtype=self.dtype if dtype is None else dtype)
//  [python]
def _adaptive_pool_hook(
        self,
        mod: Union[_MaxPoolNd, _AvgPoolNd],
        inp: Union[Tuple[Tensor, ...], Tensor],
        out: Union[Tuple[Tensor, ...], Tensor],
    ):
        desc, inp, out = self._init_forward_hook(mod, inp, out)

        params = {key: val for key, val in mod.named_parameters()}

        desc.params = sum([val.numel() for val in params.values()])
        desc.prunable_params = 0
        desc.zeroed_params = 0

        stride = tuple(
            inp[0].shape[i] // out[0].shape[i] for i in range(2, len(inp[0].shape))
        )
        kernel_size = tuple(
            inp[0].shape[i] - (out[0].shape[i] - 1) * stride[i - 2]
            for i in range(2, len(inp[0].shape))
        )
        kernel_ops = n.prod(kernel_size)

        batch_size, output_channels, output_height, output_width = out[0].size()

        flops = kernel_ops * output_channels * output_height * output_width * batch_size

        desc.flops = flops
        desc.total_flops += desc.flops
//  [python]
def _adaptive_pool_hook(
        self,
        mod: Union[_MaxPoolNd, _AvgPoolNd],
        inp: Union[Tuple[Tensor, ...], Tensor],
        out: Union[Tuple[Tensor, ...], Tensor],
    ):
        desc, inp, out = self._init_forward_hook(mod, inp, out)

        params = {key: val for key, val in mod.named_parameters()}

        desc.params = sum([val.numel() for val in params.values()])
        desc.prunable_params = 0
        desc.zeroed_params = 0

        stride = tuple(
            inp[0].shape[i] // out[0].shape[i] for i in range(2, len(inp[0].shape))
        )
        kernel_size = tuple(
            inp[0].shape[i] - (out[0].shape[i] - 1) * stride[i - 2]
            for i in range(2, len(inp[0].shape))
        )
        kernel_ops = numpy.prod(kernel_size)

        batch_size, output_channels, output_height, output_width = out[0].size()

        flops = kernel_ops * output_channels * output_height * output_width * batch_size

        desc.flops = flops
        desc.total_flops += desc.flops
//  [python]
def setUp(self):
        in_size = numpy.prod(self.in_shape)
        self.link = links.Linear(
            in_size, self.out_size,
            initialW=chainer.initializers.Normal(1, self.W_dtype),
            initial_bias=chainer.initializers.Normal(1, self.x_dtype))
        W = self.link.W.data
        b = self.link.b.data
        self.link.cleargrads()

        x_shape = (4,) + self.in_shape
        self.x = numpy.random.uniform(-1, 1, x_shape).astype(self.x_dtype)
        self.gy = numpy.random.uniform(
            -1, 1, (4, self.out_size)).astype(self.x_dtype)
        self.y = self.x.reshape(4, -1).dot(W.T) + b
        self.check_forward_options = {}
        self.check_backward_options = {}
        if self.x_dtype == numpy.float16:
            self.check_forward_options = {'atol': 1e-3, 'rtol': 1e-2}
            self.check_backward_options = {'atol': 1e-2, 'rtol': 5e-2}
        elif self.W_dtype == numpy.float16:
            self.check_backward_options = {'atol': 1e-3, 'rtol': 1e-2}
//  [python]
def volume(self, index=None):
        """Returns the volume of the voxel at a given index"""
        if index is None:
            if numpy.isscalar(self.alpha):
                vol = self._nx * self._ny * self._nz * n.prod(self._dx) * self.alpha
            else:
                vol = numpy.sum(self.alpha) * n.prod(self._dx)
            return vol
        if numpy.isscalar(self.alpha):
            return n.prod(self._dx) * self.alpha
        return n.prod(self._dx) * self.alpha[index]
//  [python]
def volume(self, index=None):
        """Returns the volume of the voxel at a given index"""
        if index is None:
            if numpy.isscalar(self.alpha):
                vol = self._nx * self._ny * self._nz * numpy.prod(self._dx) * self.alpha
            else:
                vol = numpy.sum(self.alpha) * numpy.prod(self._dx)
            return vol
        if numpy.isscalar(self.alpha):
            return numpy.prod(self._dx) * self.alpha
        return numpy.prod(self._dx) * self.alpha[index]
//  [python]
def add_tensor_info(self, name: str, tensor: LazyTensor) -> None:
        n_elements = int(np.prod(tensor.shape))
        raw_dtype = getattr(tensor.data_type, 'ggml_type', None)
        data_type = getattr(tensor.data_type, 'quantized_type', None) or tensor.data_type.dtype
        data_nbytes = tensor.data_type.elements_to_bytes(n_elements)
        self.gguf.add_tensor_info(name, tensor.shape, data_type, data_nbytes, raw_dtype=raw_dtype)
//  [python]
log = logging.getLogger(__name__)

numpy.random.RandomState(23455)
# set a fixed number initializing RandomSate for 2 purpose:
#  1. repeatable experiments; 2. for multiple-GPU, the same initial weights

# these are the possible formulas for the interval when building weights from a uniform distribution
_uniform_interval = {
    # shape[0] = rows
    # shape[1] = cols
    # numpy.prod(shape[2:]) = receptive field from Glorot et al.
    # use this only when the activation function is sigmoid
    'sigmoid': lambda shape: 4 * numpy.sqrt(6. / ((shape[0] + shape[1]) * n.prod(shape[2:]))),
    # this is the default provided in other codebases
    'default': lambda shape: 1 / numpy.sqrt(shape[0]),
    # this is the default for the GSN code from Li Yao
    'glorot': lambda shape: numpy.sqrt(6. / ((shape[0] + shape[1]) * n.prod(shape[2:]))),
    'montreal': lambda shape: numpy.sqrt(6. / ((shape[0] + shape[1]) * n.prod(shape[2:])))  # compatibility syntax
}
//  [python]
log = logging.getLogger(__name__)

numpy.random.RandomState(23455)
# set a fixed number initializing RandomSate for 2 purpose:
#  1. repeatable experiments; 2. for multiple-GPU, the same initial weights

# these are the possible formulas for the interval when building weights from a uniform distribution
_uniform_interval = {
    # shape[0] = rows
    # shape[1] = cols
    # numpy.prod(shape[2:]) = receptive field from Glorot et al.
    # use this only when the activation function is sigmoid
    'sigmoid': lambda shape: 4 * numpy.sqrt(6. / ((shape[0] + shape[1]) * numpy.prod(shape[2:]))),
    # this is the default provided in other codebases
    'default': lambda shape: 1 / numpy.sqrt(shape[0]),
    # this is the default for the GSN code from Li Yao
    'glorot': lambda shape: numpy.sqrt(6. / ((shape[0] + shape[1]) * numpy.prod(shape[2:]))),
    'montreal': lambda shape: numpy.sqrt(6. / ((shape[0] + shape[1]) * numpy.prod(shape[2:])))  # compatibility syntax
}
//  [python]
def get_weights_identity(shape, name="W", add_noise=None, gain=1.):
    """
    This will return a weights matrix as close to the identity as possible. If a non-square shape, it will make
    a matrix of the form (I 0)

    Identity matrix for weights is useful for RNNs with ReLU! http://arxiv.org/abs/1504.00941

    Parameters
    ----------
    shape : tuple
        Tuple giving the shape information for the weight matrix.
    name : str
        Name to give the shared variable.
    add_noise : functools.partial
        A partially applied noise function (just missing the input parameter) to add noise to the identity
        initialization. Noise functions can be found in opendeep.utils.noise.
    gain : float
        A multiplicative factor to affect the whole weights matrix.

    Returns
    -------
    shared variable
        The theano shared variable identity matrix with given shape.
    """
    log.debug("Creating Identity matrix weights %s with shape %s", name, str(shape))
    weights = numpy.eye(N=shape[0], M=int(n.prod(shape[1:])), k=0, dtype=config.floatX)

    if add_noise:
        if isinstance(add_noise, partial):
            weights = add_noise(input=weights)
        else:
            log.error("Add noise to identity weights was not a functools.partial object. Ignoring...")

    val = weights * gain
    return sharedX(value=val, name=name)
//  [python]
def get_weights_identity(shape, name="W", add_noise=None, gain=1.):
    """
    This will return a weights matrix as close to the identity as possible. If a non-square shape, it will make
    a matrix of the form (I 0)

    Identity matrix for weights is useful for RNNs with ReLU! http://arxiv.org/abs/1504.00941

    Parameters
    ----------
    shape : tuple
        Tuple giving the shape information for the weight matrix.
    name : str
        Name to give the shared variable.
    add_noise : functools.partial
        A partially applied noise function (just missing the input parameter) to add noise to the identity
        initialization. Noise functions can be found in opendeep.utils.noise.
    gain : float
        A multiplicative factor to affect the whole weights matrix.

    Returns
    -------
    shared variable
        The theano shared variable identity matrix with given shape.
    """
    log.debug("Creating Identity matrix weights %s with shape %s", name, str(shape))
    weights = numpy.eye(N=shape[0], M=int(numpy.prod(shape[1:])), k=0, dtype=config.floatX)

    if add_noise:
        if isinstance(add_noise, partial):
            weights = add_noise(input=weights)
        else:
            log.error("Add noise to identity weights was not a functools.partial object. Ignoring...")

    val = weights * gain
    return sharedX(value=val, name=name)
//  [python]
def create(shape, dtype='d', alignment=32):
    """Create an uninitialised shared array. Avoid object arrays, as these
    will almost certainly break as the objects themselves won't be stored in shared
    memory, only the pointers"""
    shape = numpy.atleast_1d(shape).astype('i')
    dtype = numpy.dtype(dtype)

    # we're going to use a flat ctypes array
    N = int(numpy.prod(shape) + alignment)
    # The upper bound of size we want to allocate to be certain
    #  that we can take an aligned array of the right size from it.
    N_bytes_big = int(N * dtype.itemsize)
    # The final (= right) size of the array
    N_bytes_right = int(numpy.prod(shape) * dtype.itemsize)

    dt = 'b'

    # We create the big array first
    a = sharedctypes.RawArray(dt, N_bytes_big)

    sa = shmarray(a, (N_bytes_big,), dt)

    # We pick the first index of the new array that is aligned
    # If the address of the first element is 1 and we want 8-alignment, the
    #  first aligned index of the array is going to be 7 == -1 % 8
    start_index = -sa.ctypes.data % alignment
    # Finally, we take the (aligned) subarray and reshape it.
    sa = sa[start_index:start_index + N_bytes_right].view(dtype).reshape(shape)

    return sa
//  [python]
def _GetEigTest(dtype_, shape_, compute_v_):

  def CompareEigenVectors(self, x, y, tol):
    x = EquilibrateEigenVectorPhases(x, y)
    self.assertAllClose(x, y, atol=tol)

  def CompareEigenDecompositions(self, x_e, x_v, y_e, y_v, tol):
    num_batches = int(np.prod(x_e.shape[:-1]))
    n = x_e.shape[-1]
    x_e = np.reshape(x_e, [num_batches] + [n])
    x_v = np.reshape(x_v, [num_batches] + [n, n])
    y_e = np.reshape(y_e, [num_batches] + [n])
    y_v = np.reshape(y_v, [num_batches] + [n, n])
    for i in range(num_batches):
      x_ei, x_vi = SortEigenDecomposition(x_e[i, :], x_v[i, :, :])
      y_ei, y_vi = SortEigenDecomposition(y_e[i, :], y_v[i, :, :])
      self.assertAllClose(x_ei, y_ei, atol=tol, rtol=tol)
      CompareEigenVectors(self, x_vi, y_vi, tol)

  def Test(self):
    np.random.seed(1)
    n = shape_[-1]
    batch_shape = shape_[:-2]
    np_dtype = dtype_.as_numpy_dtype

    def RandomInput():
      # Most matrices are diagonalizable
      a = np.random.uniform(
          low=-1.0, high=1.0, size=n * n).reshape([n, n]).astype(np_dtype)
      if dtype_.is_complex:
        a += 1j * np.random.uniform(
            low=-1.0, high=1.0, size=n * n).reshape([n, n]).astype(np_dtype)
      a = np.tile(a, batch_shape + (1, 1))
      return a

    if dtype_ in (dtypes_lib.float32, dtypes_lib.complex64):
      atol = 1e-4
    else:
      atol = 1e-12

    a = RandomInput()
    np_e, np_v = np.linalg.eig(a)
    with self.session():
      if compute_v_:
        tf_e, tf_v = linalg_ops.eig(constant_op.constant(a))

        # Check that V*diag(E)*V^(-1) is close to A.
        a_ev = math_ops.matmul(
            math_ops.matmul(tf_v, array_ops.matrix_diag(tf_e)),
            linalg_ops.matrix_inverse(tf_v))
        self.assertAllClose(self.evaluate(a_ev), a, atol=atol)

        # Compare to numpy.linalg.eig.
        CompareEigenDecompositions(self, np_e, np_v, self.evaluate(tf_e),
                                   self.evaluate(tf_v), atol)
      else:
        tf_e = linalg_ops.eigvals(constant_op.constant(a))
        self.assertAllClose(
            SortEigenValues(np_e),
            SortEigenValues(self.evaluate(tf_e)),
            atol=atol)

  return Test
//  [python]
def dims(self, new_dims: ShapeLike) -> None:
        new_dims = tuple(new_dims)
        shape = getattr(self, "_shape", None)
        if shape is None:  # shape not set yet
            self._dims = new_dims
        else:
            if n.prod(new_dims) == self.shape[1]:
                self._dims = new_dims
            else:
                raise ValueError("dims incompatible with shape[1]")
//  [python]
def dims(self, new_dims: ShapeLike) -> None:
        new_dims = tuple(new_dims)
        shape = getattr(self, "_shape", None)
        if shape is None:  # shape not set yet
            self._dims = new_dims
        else:
            if np.prod(new_dims) == self.shape[1]:
                self._dims = new_dims
            else:
                raise ValueError("dims incompatible with shape[1]")