Tensorflow稀疏张量发行

时间:2018-12-11 18:25:22

标签: python tensorflow sparse-matrix

我正在尝试运行以下TensorFlow代码。它包括稀疏矩阵,但似乎不起作用。我已经修改了tensorflow文档(Link)中给出的示例。我正在使用Tensorflow版本1.12.0。

代码:

failing_jobs = []

Delayed::Job.find_each do |job|
  begin
    # this code is from dj source https://github.com/collectiveidea/delayed_job/blob/master/lib/delayed/psych_ext.rb#L15
    yaml = job.handler
    result = Psych.parse(yaml)
    result ? Delayed::PsychExt::ToRuby.create.accept(result) : result
  rescue Psych::BadAlias => e
    failing_jobs << job    
  end
end

错误:

import tensorflow as tf
import numpy as np

x = tf.sparse.placeholder(shape=[-1,8,8], dtype=np.float32)
x_reshaped = tf.sparse.reshape(x, shape=[-1,64],name='flow_sizes_reshaped')
layer = tf.Variable(initial_value=tf.random_normal(shape=[64, 32],stddev=.05), name='hidden_layer_0', dtype=np.float32)
x_final = tf.sparse.matmul(x_reshaped, layer)

with tf.Session() as sess:
  indices = np.array([[0, 2, 0], [0, 5, 1]], dtype=np.int64)
  values = np.array([1.0, 2.0], dtype=np.float32)
  shape = np.array([1, 8, 8], dtype=np.int64)

  print(sess.run(x_final, feed_dict={
    x: (indices, values, shape)}))

我不认为这是尺寸不匹配的问题,因为如果我在代码中进行此更改:

2018-12-11 13:24:39.039224: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Traceback (most recent call last):
  File "Test_Sparse.py", line 15, in <module>
    x: (indices, values, shape)}))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 929, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1132, in _run
    raise ValueError('Tensor %s may not be fed.' % subfeed_t)
ValueError: Tensor Tensor("Const:0", shape=(3,), dtype=int64) may not be fed.

我收到以下错误:

indices = np.array([[2, 0], [5, 1]], dtype=np.int64)

我试图做很多事情,但最终每次都遇到这个问题。我现在正在考虑转到TF源代码,并检查为什么会出现此错误。

1 个答案:

答案 0 :(得分:1)

这很奇怪,但是由于tf.sparse.reshape而发生。 更准确地说,当张量流构建计算图时,它将所有恒定张量添加为不可馈送,这是有道理的。因此,当您重塑形状并传递稀疏张量x时,它会添加其density_shape作为恒定张量(我认为在此位置必须复制此张量)。最后,当您使用sess.run运行图并通过由三个x组成的张量的x.dense_shape时,张量流检查所有包含的张量都可以馈送,并在x.dense_shape上失败。

另一种选择是为x的形状创建一个占位符:

x_shape = tf.placeholder(shape=[3], dtype=np.int64)
x = tf.sparse.placeholder(shape=x_shape, dtype=np.float32)
...
print(sess.run(x_final, feed_dict={
    x_shape: [-1, 8, 8],
    x: tf.SparseTensorValue(indices, values, shape)
}))
相关问题