from_struct_array¶
- class tensordict.from_struct_array(struct_array: ndarray, device: Optional[device] = None)¶
將結構化的 numpy 陣列轉換為 TensorDict。
產生的 TensorDict 的內容將與 numpy 陣列共享相同的記憶體內容(這是一個零複製操作)。就地更改結構化的 numpy 陣列的值將會影響 TensorDict 的內容。
範例
>>> x = np.array( ... [("Rex", 9, 81.0), ("Fido", 3, 27.0)], ... dtype=[("name", "U10"), ("age", "i4"), ("weight", "f4")], ... ) >>> td = from_struct_array(x) >>> x_recon = td.to_struct_array() >>> assert (x_recon == x).all() >>> assert x_recon.shape == x.shape >>> # Try modifying x age field and check effect on td >>> x["age"] += 1 >>> assert (td["age"] == np.array([10, 4])).all()