MCPcopy Create free account
hub / github.com/apache/singa / from_numpy

Function from_numpy

python/singa/tensor.py:877–907  ·  view source on GitHub ↗

Create a Tensor instance with the shape, dtype and values from the numpy array. Args: np_array: the numpy array. Returns: A Tensor instance allocated on the default CppCPU device.

(np_array, dev=None)

Source from the content-addressed store, hash-verified

875
876
877def from_numpy(np_array, dev=None):
878 '''Create a Tensor instance with the shape, dtype and values from the numpy
879 array.
880
881 Args:
882 np_array: the numpy array.
883
884 Returns:
885 A Tensor instance allocated on the default CppCPU device.
886 '''
887 assert type(np_array) is np.ndarray, 'Must input numpy array'
888 # convert to float32 array
889 if np_array.dtype == np.float64 or np_array.dtype == np.float:
890 np_array = np_array.astype(np.float32)
891
892 if np_array.dtype == np.int64 or np_array.dtype == np.int:
893 np_array = np_array.astype(np.int32)
894
895 if np_array.dtype == np.float32:
896 dtype = float32
897 elif np_array.dtype == np.float16:
898 dtype = float16
899 else:
900 assert np_array.dtype == np.int32, \
901 'Only float and int tensors are supported'
902 dtype = int32
903 ret = Tensor(np_array.shape, dtype=dtype)
904 ret.copy_from_numpy(np_array)
905 if dev:
906 ret.to_device(dev)
907 return ret
908
909
910def to_host(t):

Callers

nothing calls this directly

Calls 4

copy_from_numpyMethod · 0.95
to_deviceMethod · 0.95
typeFunction · 0.85
TensorClass · 0.70

Tested by

no test coverage detected