首页 Paddle框架 帖子详情
请问 paddle 里矩阵合到底该怎么写? 已解决
收藏
快速回复
Paddle框架 问答学习资料 1263 2
请问 paddle 里矩阵合到底该怎么写? 已解决
收藏
快速回复
Paddle框架 问答学习资料 1263 2

现有两个 np 数组,shape分别问 100*180 和 100*1 ,即代码中的data和label 我想合并成 100*2的 数组,代码如下

ls = []
for i in range(len(data)):
    ls.append([data[i],label[i]])
src = np.asarray(ls)
print(src.shape)
print(src[0])

输出为

(100, 2)
[array([1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
       0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1,
       0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0,
       0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0,
       1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1,
       1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0,
       0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0,
       0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
       1, 0, 1, 0])
 array([0])]

然后在调用 paddle.to_tensor(src) 时报错:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_16455/4191702599.py in 
     19 print(src.shape)
     20 print(src[0])
---> 21 train_dataset = paddle.to_tensor(src)

 in to_tensor(data, dtype, place, stop_gradient)

/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/wrapped_decorator.py in __impl__(func, *args, **kwargs)
     23     def __impl__(func, *args, **kwargs):
     24         wrapped_func = decorator_func(func)
---> 25         return wrapped_func(*args, **kwargs)
     26 
     27     return __impl__

/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/framework.py in __impl__(*args, **kwargs)
    227         assert in_dygraph_mode(
    228         ), "We only support '%s()' in dynamic graph mode, please call 'paddle.disable_static()' to enter dynamic graph mode." % func.__name__
--> 229         return func(*args, **kwargs)
    230 
    231     return __impl__

/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py in to_tensor(data, dtype, place, stop_gradient)
    170         persistable=False,
    171         zero_copy=False,
--> 172         stop_gradient=stop_gradient)
    173 
    174 

ValueError: (InvalidArgument) Input object type error or incompatible array data type. tensor.set() supports array with bool, float16, float32, float64, int8, int16, int32, int64, uint8 or uint16, please check your input or input array data type. (at /paddle/paddle/fluid/pybind/tensor_py.h:411)

请问改如何修改代码,谢谢

3# 回复于2022-01
先用 np.zeros((100,2)) 新建一个全0矩阵,然后再使用for进行循环赋值。
0
收藏
回复
全部评论(2)
时间顺序
JavaRoom
#2 回复于2022-01

嗯,你这个是不行,合并的时候合成一个数组,不是再增加一个纬度

0
回复
十进制到二进制
#3 回复于2022-01

先用 np.zeros((100,2)) 新建一个全0矩阵,然后再使用for进行循环赋值。

0
回复
需求/bug反馈?一键提issue告诉我们
发现bug?如果您知道修复办法,欢迎提pr直接参与建设飞桨~
在@后输入用户全名并按空格结束,可艾特全站任一用户