当前位置: 首页>>代码示例>>Python>>正文


Python numpy.array_shapes方法代码示例

本文整理汇总了Python中hypothesis.extra.numpy.array_shapes方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.array_shapes方法的具体用法?Python numpy.array_shapes怎么用?Python numpy.array_shapes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在hypothesis.extra.numpy的用法示例。


在下文中一共展示了numpy.array_shapes方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _strategy_2d_array

# 需要导入模块: from hypothesis.extra import numpy [as 别名]
# 或者: from hypothesis.extra.numpy import array_shapes [as 别名]
def _strategy_2d_array(dtype, minval=0, maxval=None, **kwargs):
    if 'min_side' in kwargs:
        min_side = kwargs.pop('min_side')
    else:
        min_side = 1

    if 'max_side' in kwargs:
        max_side = kwargs.pop('max_side')
    else:
        max_side = None

    if dtype is np.int:
        elems = st.integers(minval, maxval, **kwargs)
    elif dtype is np.float:
        elems = st.floats(minval, maxval, **kwargs)
    elif dtype is np.str:
        elems = st.text(min_size=minval, max_size=maxval, **kwargs)
    else:
        raise ValueError('no elements strategy for dtype', dtype)

    return arrays(dtype, array_shapes(2, 2, min_side, max_side), elements=elems) 
开发者ID:WZBSocialScienceCenter,项目名称:tmtoolkit,代码行数:23,代码来源:_testtools.py

示例2: anyarray

# 需要导入模块: from hypothesis.extra import numpy [as 别名]
# 或者: from hypothesis.extra.numpy import array_shapes [as 别名]
def anyarray(
    draw,
    min_dims: int = 0,
    max_dims: int = 2,
    include_complex_numbers: bool = True,
    dtype: Optional[np.dtype] = None,
):
    if dtype is None:
        if include_complex_numbers:
            dtype = one_of(
                integer_dtypes(), floating_dtypes(), complex_number_dtypes()
            )
        else:
            dtype = one_of(integer_dtypes(), floating_dtypes())

    arr = draw(
        arrays(
            dtype=dtype,
            shape=array_shapes(min_dims=min_dims, max_dims=max_dims),
        )
    )
    assume(not np.any(np.isnan(arr)))
    assume(np.all(np.isfinite(arr)))

    return arr 
开发者ID:GoLP-IST,项目名称:nata,代码行数:27,代码来源:strategies.py

示例3: random_array

# 需要导入模块: from hypothesis.extra import numpy [as 别名]
# 或者: from hypothesis.extra.numpy import array_shapes [as 别名]
def random_array(
    draw,
    *,
    min_dims: int = 0,
    max_dims: int = 3,
    return_with_indexing: bool = False,
):
    arr = draw(
        arrays(
            dtype=one_of(integer_dtypes(), floating_dtypes()),
            shape=array_shapes(min_dims=min_dims, max_dims=max_dims),
        )
    )

    assume(not np.any(np.isnan(arr)))
    assume(np.all(np.isfinite(arr)))

    if return_with_indexing:
        ind = draw(basic_indices(arr.shape))
        return arr, ind
    else:
        return arr 
开发者ID:GoLP-IST,项目名称:nata,代码行数:24,代码来源:test_containers.py

示例4: test_negative_log_likelihood

# 需要导入模块: from hypothesis.extra import numpy [as 别名]
# 或者: from hypothesis.extra.numpy import array_shapes [as 别名]
def test_negative_log_likelihood(data: st.DataObject, labels_as_tensor: bool):
    s = data.draw(
        hnp.arrays(
            shape=hnp.array_shapes(max_side=10, min_dims=2, max_dims=2),
            dtype=float,
            elements=st.floats(-100, 100),
        )
    )
    y_true = data.draw(
        hnp.arrays(
            shape=(s.shape[0],),
            dtype=hnp.integer_dtypes(),
            elements=st.integers(min_value=0, max_value=s.shape[1] - 1),
        ).map(Tensor if labels_as_tensor else lambda x: x)
    )
    scores = Tensor(s)
    nll = negative_log_likelihood(mg.log(mg.nnet.softmax(scores)), y_true)
    nll.backward()

    cross_entropy_scores = Tensor(s)
    ce = softmax_crossentropy(cross_entropy_scores, y_true)
    ce.backward()

    assert_allclose(nll.data, ce.data, atol=1e-5, rtol=1e-5)
    assert_allclose(scores.grad, cross_entropy_scores.grad, atol=1e-5, rtol=1e-5) 
开发者ID:rsokl,项目名称:MyGrad,代码行数:27,代码来源:test_negative_log_likelihood.py

示例5: test_upcast_roundtrip

# 需要导入模块: from hypothesis.extra import numpy [as 别名]
# 或者: from hypothesis.extra.numpy import array_shapes [as 别名]
def test_upcast_roundtrip(type_strategy, data: st.DataObject):
    thin, wide = data.draw(
        st.tuples(type_strategy, type_strategy).map(
            lambda x: sorted(x, key=lambda y: np.dtype(y).itemsize)
        )
    )
    orig_tensor = data.draw(
        hnp.arrays(
            dtype=thin,
            shape=hnp.array_shapes(),
            elements=hnp.from_dtype(thin).filter(np.isfinite),
        ).map(Tensor)
    )

    roundtripped_tensor = orig_tensor.astype(wide).astype(thin)
    assert_array_equal(orig_tensor, roundtripped_tensor) 
开发者ID:rsokl,项目名称:MyGrad,代码行数:18,代码来源:test_astype.py

示例6: _glu_shape

# 需要导入模块: from hypothesis.extra import numpy [as 别名]
# 或者: from hypothesis.extra.numpy import array_shapes [as 别名]
def _glu_shape(draw):
    shape = draw(hnp.array_shapes())
    if not any(x % 2 == 0 for x in shape):
        index = draw(st.integers(0, len(shape) - 1))
        shape = list(shape)
        shape[index] = draw(st.integers(0, 3).map(lambda x: 2 * x))
    return tuple(shape) 
开发者ID:rsokl,项目名称:MyGrad,代码行数:9,代码来源:test_glu.py

示例7: test_multiclass_hinge

# 需要导入模块: from hypothesis.extra import numpy [as 别名]
# 或者: from hypothesis.extra.numpy import array_shapes [as 别名]
def test_multiclass_hinge(data):
    """Test the built-in implementation of multiclass hinge
    against the pure mygrad version"""
    s = data.draw(
        hnp.arrays(
            shape=hnp.array_shapes(max_side=10, min_dims=2, max_dims=2),
            dtype=float,
            elements=st.floats(-100, 100),
        )
    )
    loss = data.draw(
        hnp.arrays(
            shape=(s.shape[0],),
            dtype=hnp.integer_dtypes(),
            elements=st.integers(min_value=0, max_value=s.shape[1] - 1),
        )
    )
    hinge_scores = Tensor(s)
    hinge_loss = multiclass_hinge(hinge_scores, loss, constant=False)
    hinge_loss.backward()

    mygrad_scores = Tensor(s)
    correct_labels = (range(len(loss)), loss)
    correct_class_scores = mygrad_scores[correct_labels]  # Nx1

    Lij = mygrad_scores - correct_class_scores[:, np.newaxis] + 1.0  # NxC margins
    Lij[Lij <= 0] = 0
    Lij[correct_labels] = 0

    mygrad_loss = Lij.sum() / mygrad_scores.shape[0]
    mygrad_loss.backward()
    assert_allclose(hinge_loss.data, mygrad_loss.data)
    assert_allclose(mygrad_scores.grad, hinge_scores.grad) 
开发者ID:rsokl,项目名称:MyGrad,代码行数:35,代码来源:test_hinge.py

示例8: test_softmax_crossentropy

# 需要导入模块: from hypothesis.extra import numpy [as 别名]
# 或者: from hypothesis.extra.numpy import array_shapes [as 别名]
def test_softmax_crossentropy(data: st.DataObject, labels_as_tensor: bool):
    s = data.draw(
        hnp.arrays(
            shape=hnp.array_shapes(max_side=10, min_dims=2, max_dims=2),
            dtype=float,
            elements=st.floats(-100, 100),
        )
    )
    y_true = data.draw(
        hnp.arrays(
            shape=(s.shape[0],),
            dtype=hnp.integer_dtypes(),
            elements=st.integers(min_value=0, max_value=s.shape[1] - 1),
        ).map(Tensor if labels_as_tensor else lambda x: x)
    )
    scores = Tensor(s)
    softmax_cross = softmax_crossentropy(scores, y_true, constant=False)
    softmax_cross.backward()

    mygrad_scores = Tensor(s)
    probs = softmax(mygrad_scores)

    correct_labels = (range(len(y_true)), y_true.data if labels_as_tensor else y_true)
    truth = np.zeros(mygrad_scores.shape)
    truth[correct_labels] = 1

    mygrad_cross = (-1 / s.shape[0]) * (log(probs) * truth).sum()
    mygrad_cross.backward()
    assert_allclose(softmax_cross.data, mygrad_cross.data, atol=1e-5, rtol=1e-5)
    assert_allclose(scores.grad, mygrad_scores.grad, atol=1e-5, rtol=1e-5) 
开发者ID:rsokl,项目名称:MyGrad,代码行数:32,代码来源:test_softmaxcrossentropy.py

示例9: test_weighted_negative_log_likelihood

# 需要导入模块: from hypothesis.extra import numpy [as 别名]
# 或者: from hypothesis.extra.numpy import array_shapes [as 别名]
def test_weighted_negative_log_likelihood(data: st.DataObject, labels_as_tensor: bool):
    s = data.draw(
        hnp.arrays(
            shape=hnp.array_shapes(min_side=1, max_side=10, min_dims=2, max_dims=2),
            dtype=float,
            elements=st.floats(-100, 100),
        )
    )
    y_true = data.draw(
        hnp.arrays(
            shape=(s.shape[0],),
            dtype=hnp.integer_dtypes(),
            elements=st.integers(min_value=0, max_value=s.shape[1] - 1),
        ).map(Tensor if labels_as_tensor else lambda x: x)
    )
    weights = data.draw(
        hnp.arrays(
            shape=(s.shape[1],),
            dtype=float,
            elements=st.floats(1e-8, 100),
        )
    )
    scores = Tensor(s)
    weights = Tensor(weights)

    for score, y in zip(scores, y_true):
        score = mg.log(mg.nnet.softmax(score.reshape(1, -1)))
        y = y.reshape(-1)
        nll = negative_log_likelihood(score, y)
        weighted_nll = negative_log_likelihood(score, y, weights=weights)
        assert np.isclose(weighted_nll.data, weights[y.data].data * nll.data) 
开发者ID:rsokl,项目名称:MyGrad,代码行数:33,代码来源:test_negative_log_likelihood.py

示例10: coords_2d_array

# 需要导入模块: from hypothesis.extra import numpy [as 别名]
# 或者: from hypothesis.extra.numpy import array_shapes [as 别名]
def coords_2d_array():
    return st_numpy.arrays(np.float_, st_numpy.array_shapes(min_dims=2, max_dims=2),
                           elements=st.floats(allow_nan=False, allow_infinity=False))\
                    .filter(lambda arr: arr.shape[1] == 2) 
开发者ID:WZBSocialScienceCenter,项目名称:geovoronoi,代码行数:6,代码来源:_testtools.py

示例11: adv_integer_index

# 需要导入模块: from hypothesis.extra import numpy [as 别名]
# 或者: from hypothesis.extra.numpy import array_shapes [as 别名]
def adv_integer_index(
    shape: Shape,
    min_dims: int = 1,
    max_dims: int = 3,
    min_side: int = 1,
    max_side: int = 3,
) -> st.SearchStrategy[Tuple[ndarray, ...]]:
    """ Hypothesis search strategy: given an array shape, generate a
    a valid index for specifying an element/subarray of that array,
    using advanced indexing with integer-valued arrays.

    Examples from this strategy shrink towards the index
    `len(shape) * (np.array([0]), )`.

    Parameters
    ----------
    shape : Tuple[int, ...]
        The shape of the array whose indices are being generated

    min_dims : int, optional (default=1)
        The minimum dimensionality permitted for the index-arrays.

    max_dims : int, optional (default=3)
        The maximum dimensionality permitted for the index-arrays.

    min_side : int, optional (default=1)
        The minimum side permitted for the index-arrays.

    max_side : int, optional (default=3)
        The maximum side permitted for the index-arrays.

    Returns
    -------
    hypothesis.searchstrategy.SearchStrategy[Tuple[numpy.ndarray, ...]]
    """

    return hnp.integer_array_indices(
        shape=shape,
        result_shape=hnp.array_shapes(
            min_dims=min_dims, max_dims=max_dims, min_side=min_side, max_side=max_side
        ),
    ) 
开发者ID:rsokl,项目名称:MyGrad,代码行数:44,代码来源:__init__.py


注:本文中的hypothesis.extra.numpy.array_shapes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。