目录
项目场景
大口径超表面的逆向优化设计,需要处理大型稀疏张量,超过10^10次方个数据。
问题描述
在使用tf.sparse.SparseTensor将大批量三维稀疏张量整形成二维稀疏张量的时候,代码报错,用简单的代码举个例子:
import tensorflow as tf
indices = [(0, 0, 1), (0, 1, 0), (1, 0, 0)]
values = [1, 1, 1]
dense_shape = [1000, 2000, 2000]
tensor = tf.sparse.SparseTensor(indices, values, dense_shape)
reshape_tensor = tf.sparse.reshape(tensor, [1000, -1])
报错信息
Traceback (most recent call last):
File "D:/Desktop/doctor/IOE/stagework/202408_broadband thermal imaging using meta optics/metabox-main/examples/test.py", line 11, in <module>
reshape_tensor = tf.sparse.reshape(tensor, [1000, -1])
File "C:\Users\Hamburg\.conda\envs\metabox_test\lib\site-packages\tensorflow\python\util\traceback_utils.py", line 153, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\Hamburg\.conda\envs\metabox_test\lib\site-packages\tensorflow\python\ops\sparse_ops.py", line 961, in sparse_reshape
raise ValueError(
ValueError: Cannot reshape a tensor with -294967296 elements to shape [1000, None] (-294968000 elements).
原因分析
新的维度是[1000, -1],-1或者None意味着需要tensorflow自己确定第二维度的大小,所以需要计算1000*2000*2000再除以1000,但前面的1000*2000*2000是4000000000,已经大于int32的范围[-2147483648, 2147483647],所以报错信息里面也会提到新的维度是-294967296,这个数值就是4000000000-2147483647-2147483648-1 = -294967296
解决方案
import tensorflow as tf
indices = [(0, 0, 1), (0, 1, 0), (1, 0, 0)]
values = [1, 1, 1]
dense_shape = [1000, 2000, 2000]
tensor = tf.sparse.SparseTensor(indices, values, dense_shape)
reshape_tensor = tf.sparse.reshape(tensor, [1000, dense_shape[1]*dense_shape[2]])
提前帮tensorflow计算好第二维的大小就可以了
后续问题
现在这个方法处理数据还是够用的。但如果计算好的这个第二维大小也超过了int32的范围,代码还是会报错,就不知道怎么办了,可能需要切割处理后再合并,还求助一下各位大佬。
标签:None,elements,tensor,reshape,shape,sparse,tf,1000 From: https://blog.csdn.net/Han_burger/article/details/144269845