create_lod_tensor

paddle.fluid.lod_tensor. create_lod_tensor ( data, recursive_seq_lens, place ) [source]

Create a LoDTensor from a numpy array, list or existing LoDTensor.

The implementation is as follows:

  1. Check whether the length-based LoD, i.e., recursive_seq_lens is valid.

  2. Convert recursive_seq_lens to a offset-based LoD.

  3. Based on place , copy the data from a numpy array, list or existing LoDTensor to CPU or GPU device.

  4. Set offset-based LoD to the output LoDTensor.

Suppose we want to create a LoDTensor to hold data for word sequences, where each word is represented by an integer. If we want to create a LoDTensor to represent two sentences, one of 2 words, and one of 3 words.

Then data would be a numpy array of integers with shape (5, 1). recursive_seq_lens would be [[2, 3]], indicating the word number in each sentence. This length-based recursive_seq_lens [[2, 3]] would be converted to offset-based LoD [[0, 2, 5]] inside the function call.

Please reference user_guide_lod_tensor for more details regarding LoD.

Parameters
  • data (numpy.ndarray|list|LoDTensor) – a numpy array, a list or ad LoDTensor holding the data to be copied.

  • recursive_seq_lens (list[list[int]]) – a list of lists indicating the length-based LoD info.

  • place (CPUPlace|CUDAPlace) – CPU or GPU place indicating where the data in the created LoDTensor will be stored.

Returns

A LoDTensor with tensor data and recursive_seq_lens info.

Examples

import paddle.fluid as fluid
import numpy as np

t = fluid.create_lod_tensor(np.ndarray([5, 30]), [[2, 3]], fluid.CPUPlace())