【问题标题】:InvalidArgumentError in while_loop with sparse tensors具有稀疏张量的 while_loop 中的 InvalidArgumentError
【发布时间】: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


    【解决方案1】:

    我尝试从 tensorflow 1.8 更新到 1.12 并且 tensorflow 完全停止工作(ts.Session 将无限期挂起)。因此,我对我的 anaconda 环境进行了核对,并从头开始使用 tensorflow 1.12。在这次更新/重新安装之后,稀疏张量的问题就消失了,但目前还不清楚问题是出在我的 anaconda 环境中的 tensorflow 版本还是其他问题上。

    【讨论】:

      猜你喜欢
      • 2012-01-03
      • 1970-01-01
      • 2020-08-05
      • 2016-04-24
      • 2018-03-20
      • 1970-01-01
      • 2018-06-20
      • 2017-02-11
      • 1970-01-01
      相关资源
      最近更新 更多