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)
| 875 | |
| 876 | |
| 877 | def 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 | |
| 910 | def to_host(t): |
nothing calls this directly
no test coverage detected