MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / wrap_as_scalar

Function wrap_as_scalar

python/paddle/base/framework.py:1607–1626  ·  view source on GitHub ↗

Wrap a number(either python scalar or numpy scalar) as core.Scalar if it is not a scalar. Args: number (Number): number Returns: Scalar: A Scalar that contains the value.

(number)

Source from the content-addressed store, hash-verified

1605
1606
1607def wrap_as_scalar(number):
1608 """Wrap a number(either python scalar or numpy scalar) as core.Scalar if
1609 it is not a scalar.
1610
1611
1612 Args:
1613 number (Number): number
1614
1615 Returns:
1616 Scalar: A Scalar that contains the value.
1617 """
1618 if isinstance(number, core.Scalar):
1619 return number
1620 if isinstance(number, (bool, int, float, complex)):
1621 return core.Scalar(number)
1622 if isinstance(number, np.number):
1623 # it is a numpy scalar
1624 return core.Scalar(number.item())
1625 else:
1626 raise TypeError(f"Cannot wrap {number} as core.Scalar")
1627
1628
1629def wrap_as_scalars(array):

Callers 2

wrap_as_scalarsFunction · 0.85

Calls 3

TypeErrorClass · 0.85
ScalarMethod · 0.80
itemMethod · 0.80

Tested by

no test coverage detected