# tensorplay.frombuffer Source: https://www.tensorplay.cn/docs/generated/tensorplay.frombuffer.html ```python tensorplay.frombuffer(buffer, *, dtype, count=-1, offset=0, requires_grad=False) → Tensor ``` Creates a 1-dimensional [Tensor](/docs/generated/tensorplay.Tensor.html#tensorplay.Tensor) from an object that implements the Python buffer protocol. Skips the first offset bytes in the buffer, and interprets the rest of the raw bytes as a 1-dimensional tensor of type [dtype](/docs/generated/tensorplay.DType.html#tensorplay.DType) with count elements. Note that either of the following must be true: 1. count is a positive non-zero number, and the total number of bytes in the buffer is more than offset plus count times the size (in bytes) of [dtype](/docs/generated/tensorplay.DType.html#tensorplay.DType). 2. count is negative, and the length (number of bytes) of the buffer subtracted by the offset is a multiple of the size (in bytes) of [dtype](/docs/generated/tensorplay.DType.html#tensorplay.DType). The returned tensor and buffer share the same memory. Modifications to the tensor will be reflected in the buffer and vice versa. The returned tensor is not resizable. > **Note** > > This function increments the reference count for the object that owns the shared memory. Therefore, such memory will not be deallocated before the returned tensor goes out of scope. > **Warning** > > This function’s behavior is undefined when passed an object implementing the buffer protocol whose data is not on the CPU. Doing so is likely to cause a segmentation fault. > **Warning** > > This function does not try to infer the dtype (hence, it is not optional). Passing a different dtype than its source may result in unexpected behavior. Parameters: buffer ([object](https://docs.python.org/3/builtins/functions.html#object)) – a Python object that exposes the buffer interface. Keyword Arguments: - dtype ([tensorplay.dtype](/docs/generated/tensorplay.DType.html#tensorplay.DType)) – the desired data type of returned tensor. - count ([int](https://docs.python.org/3/builtins/functions.html#int), optional) – the number of desired elements to be read. If negative, all the elements (until the end of the buffer) will be read. Default: -1. - offset ([int](https://docs.python.org/3/builtins/functions.html#int), optional) – the number of bytes to skip at the start of the buffer. Default: 0. - requires_grad ([bool](https://docs.python.org/3/builtins/functions.html#bool), optional) – If autograd should record operations on the returned tensor. Default: False. Example: ``` >>> import array >>> a = array.array('i', [1, 2, 3]) >>> t = tensorplay.frombuffer(a, dtype=tensorplay.int32) >>> t tensor([ 1, 2, 3]) >>> t[0] = -1 >>> a array([-1, 2, 3]) >>> # Interprets the signed char bytes as 32-bit integers. >>> # Each 4 signed char elements will be interpreted as >>> # 1 signed 32-bit integer. >>> import array >>> a = array.array('b', [-1, 0, 0, 0]) >>> tensorplay.frombuffer(a, dtype=tensorplay.int32) tensor([255], dtype=tensorplay.int32) ```