【发布时间】:2019-01-31 03:11:12
【问题描述】:
我正在使用 while_loop 迭代更新矩阵。循环在密集张量下运行良好,但是当我使用稀疏张量时出现以下错误:
InvalidArgumentError: a_indices 的行数不匹配 a_values [[节点: 而/SparseTensorDenseMatMul/SparseTensorDenseMatMul = SparseTensorDenseMatMul[T=DT_FLOAT, Tindices=DT_INT64, adjoint_a=假,adjoint_b=假, _device="/job:localhost/replica:0/task:0/device:GPU:0"](while/SparseTensorDenseMatMul/SparseTensorDenseMatMul/Enter, 而/SparseTensorDenseMatMul/SparseTensorDenseMatMul/Enter_1, ConstantFolding/dense_to_sparse/Shape_enter/_1, while/Switch_1:1)]]
[[节点:while/Exit_1/_5 = _Recvclient_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_62_while/Exit_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]]
我在两个版本之间唯一改变的是将 HH 转换为 HH=tf.contrib.layers.dense_to_sparse(HH) 并使用 tf.sparse_tensor_dense_matmul(HH,f) 而不是 tf.matmul(HH,f) - 显示在下面的注释代码中。
with tf.device('/gpu:0'):
g=tf.constant(g,shape=[np.size(g),1],dtype=tf.float32)
H=tf.constant(H,dtype=tf.float32);
Ht=tf.transpose(H)
HH=tf.matmul(Ht,H)
#HH=tf.contrib.layers.dense_to_sparse(HH)
a=tf.matmul(Ht,g)
i=tf.constant(0,dtype=tf.int32)
f=tf.constant(f,dtype=tf.float32)
body = lambda i,f:(tf.add(i,1),tf.divide(tf.multiply(f,a),tf.matmul(HH,f)+10e-9))
#body = lambda i,f:(tf.add(i,1),tf.divide(tf.multiply(f,a),tf.sparse_tensor_dense_matmul(HH,f)+10e-9))
cond= lambda i,f:tf.less(i,iterations)
i,f=tf.while_loop(cond,body,(i,f))
sess=tf.Session()
i,f=sess.run([i,f])
请注意,只要 H、g 和 f 足够小,此代码就可以工作。例如,此错误发生在 H.shape=(8000,3840) ,g.shape=(8000,1), f.shape=(3840,1) 和更大的情况下,但适用于 H.shape=(8000, 3584) ,g.shape=(8000,1), f.shape=(3584,1) 和更小。我是否需要为 while 循环中的稀疏张量做一些特殊的事情以确保它们保持形状?
【问题讨论】:
标签: python tensorflow