捷徑

torcharrow.DataFrame.to_tensor

DataFrame.to_tensor(conversion=None)

轉換為 PyTorch 容器(Tensor、PackedList、PackedMap 等)

參數:

conversion (TensorConversiondict) – 對於 DataFrame.to_tensor(),conversion 只能是 dict 類型。此 dict 將欄位名稱映射到轉換方法。對於 dict 中未包含的欄位名稱,將使用預設的 PyTorch 轉換。

範例

>>> import torcharrow as ta
>>> import torcharrow.pytorch as tap
>>> df = ta.dataframe({"label_ids": [0, 1], "token_ids": [[1, 2, 3, 4, 5], [101, 102]]})
>>> df
index    label_ids  token_ids
-------  -----------  ---------------
    0            0  [1, 2, 3, 4, 5]
    1            1  [101, 102]
dtype: Struct([Field('label_ids', int64), Field('token_ids', List(int64))]), count: 2, null_count: 0
>>> df.to_tensor({"token_ids": tap.PadSequence(padding_value=-1)})
TorchArrowStruct_0(
    label_ids=tensor([0, 1]),
    token_ids=tensor([
        [  1,   2,   3,   4,   5],
        [101, 102,  -1,  -1,  -1]]
    )
)

文件

存取 PyTorch 的完整開發者文件

檢視文件

教學課程

取得適用於初學者和進階開發者的深入教學課程

檢視教學課程

資源

尋找開發資源並獲得問題解答

檢視資源