Tensor 转 LoD-Tensor 示例,为什么输入的是list
Created by: dingsiyu
- 版本、环境信息: 1)PaddlePaddle版本:1.5 2)CPU/GPU:cuda 10, cudnn 7.4 3)系统环境:centos 6 4)Python版本号 3
这是官方代码:
LoD-Tensor 转 Tensor
import paddle.fluid as fluid import numpy as np
创建一个 LoD-Tensor
a = fluid.create_lod_tensor(np.array([[1.1], [2.2],[3.3],[4.4]]).astype('float32'), [[1,3]], fluid.CPUPlace()) def LodTensor_to_Tensor(lod_tensor):
获取 LoD-Tensor 的 lod 信息
lod = lod_tensor.lod()
转换成 array
array = np.array(lod_tensor) new_array = []
依照原LoD-Tensor的层级信息,转换成Tensor
for i in range(len(lod[0]) - 1): new_array.append(array[lod[0][i]:lod[0][i + 1]]) return new_array new_array = LodTensor_to_Tensor(a)
输出结果
print(new_array)
Tensor 转 LoD-Tensor import paddle.fluid as fluid import numpy as np
def to_lodtensor(data, place):
存储Tensor的长度作为LoD信息
seq_lens = [len(seq) for seq in data] cur_len = 0 lod = [cur_len] for l in seq_lens: cur_len += l lod.append(cur_len)
对待转换的 Tensor 降维
flattened_data = np.concatenate(data, axis=0).astype("int64") flattened_data = flattened_data.reshape([len(flattened_data), 1])
为 Tensor 数据添加lod信息
res = fluid.LoDTensor() res.set(flattened_data, place) res.set_lod([lod]) return res
new_array 为上段代码中转换的Tensor
lod_tensor = to_lodtensor(new_array,fluid.CPUPlace())
输出 LoD 信息
print("The LoD of the result: {}.".format(lod_tensor.lod()))
检验与原Tensor数据是否一致
print("The array : {}.".format(np.array(lod_tensor)))
问题:为什么new_array是个list,按照标题,不是应该是tensor吗? 而且,我直接向to_lodtensor输入一个tensor后,报错。具体如下:
代码: a = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype="float32") b = fluid.layers.create_tensor(dtype="float32") fluid.layers.assign(a,b) lodtensor = to_lodtensor(b, fluid.CPUPlace)
报错信息: Traceback (most recent call last): File "b.py", line 56, in lodtensor = to_lodtensor(b, fluid.CPUPlace) File "b.py", line 35, in to_lodtensor seq_lens = [len(seq) for seq in data] File "b.py", line 35, in seq_lens = [len(seq) for seq in data] TypeError: object of type 'Variable' has no len()