MCPcopy Create free account
hub / github.com/explosion/thinc / asarray

Method asarray

thinc/backends/ops.py:741–761  ·  view source on GitHub ↗

Ensure a given array is of the correct type.

(
        self,
        data: Union[ArrayXd, Sequence[ArrayXd], Sequence[Any]],
        *,
        dtype: Optional[DTypes] = None,
    )

Source from the content-addressed store, hash-verified

739 return cast(IntsXd, self.asarray(data, dtype=dtype))
740
741 def asarray(
742 self,
743 data: Union[ArrayXd, Sequence[ArrayXd], Sequence[Any]],
744 *,
745 dtype: Optional[DTypes] = None,
746 ) -> ArrayXd:
747 """Ensure a given array is of the correct type."""
748 if isinstance(data, self.xp.ndarray):
749 if dtype is None:
750 return data
751 elif data.dtype == dtype:
752 return data
753 else:
754 return self.xp.asarray(data, dtype=dtype)
755 elif hasattr(data, "numpy"):
756 # Handles PyTorch Tensor
757 return data.numpy() # type: ignore[union-attr]
758 elif dtype is not None:
759 return self.xp.array(data, dtype=dtype)
760 else:
761 return self.xp.array(data)
762
763 def as_contig(self, data: ArrayT, dtype: Optional[DTypes] = None) -> ArrayT:
764 """Allow the backend to make a contiguous copy of an array.

Callers 15

get_dropout_maskMethod · 0.95
asarray4fMethod · 0.95
asarray3fMethod · 0.95
asarray2fMethod · 0.95
asarray1fMethod · 0.95
asarray_fMethod · 0.95
asarray1iMethod · 0.95
asarray2iMethod · 0.95
asarray3iMethod · 0.95
asarray4iMethod · 0.95
asarray_iMethod · 0.95
test_seq2col_window_oneFunction · 0.95

Calls

no outgoing calls

Tested by 15

test_seq2col_window_oneFunction · 0.76
test_L2Function · 0.36
test_cosine_orthogonalFunction · 0.36
test_cosine_equalFunction · 0.36
test_cosine_unmatchedFunction · 0.36
test_to_categoricalFunction · 0.36
test_optimizer_initFunction · 0.36
test_model_gpuFunction · 0.36
test_chainFunction · 0.36
test_noopFunction · 0.36
test_addFunction · 0.36