简体   繁体   English

Tensorflow稀疏张量发行

[英]Tensorflow Sparse Tensors Issue

I was trying to run the following TensorFlow Code. 我正在尝试运行以下TensorFlow代码。 It included sparse matrices but it don't seem to be working. 它包括稀疏矩阵,但似乎不起作用。 I've modified the example given in tensorflow's documentation ( Link ). 我已经修改了tensorflow文档( Link )中给出的示例。 I'm using tensorflow version 1.12.0. 我正在使用Tensorflow版本1.12.0。

Code: 码:

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)}))

Error: 错误:

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.

I don't think its a dimension mismatch issue because if I make this change in the code: 我不认为这是尺寸不匹配的问题,因为如果我在代码中进行以下更改:

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

I get the following error: 我收到以下错误:

2018-12-11 13:30:01.538664: 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 1128, in _run
    str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (2, 2) for Tensor u'Placeholder_1:0', which has shape '(?, 3)'

I've tried to do multiple things, but I end up getting this issue everytime. 我试图做很多事情,但最终每次都遇到这个问题。 I am now thinking of going to the TF source code and checking out why this error is being raised. 我现在正在考虑转到TF源代码,并检查为什么会引发此错误。

It's weird but it happens because of tf.sparse.reshape . 这很奇怪,但是由于tf.sparse.reshape发生了。 More precisely, when tensorflow builds the computation graph, it adds all constant tensors as non-feedable, which makes sense. 更准确地说,当张量流构建计算图时,它将所有恒定张量添加为不可馈送,这是有道理的。 Thus, when you do reshape and pass a sparse tensor x , it adds its dense_shape as a constant tensor (I think at this place there must be made a copy of this tensor). 因此,当您进行重整形并传递稀疏张量x ,它会添加其density_shape作为恒定张量(我认为在此位置必须复制此张量)。 Finally, when you run the graph with sess.run and passes x which consists of 3 tensors including x.dense_shape , tensorflow checks that all included tensors can be fed and fails on x.dense_shape . 最后,当您使用sess.run运行图并传递xx由3个张量组成,包括x.dense_shape ,tensorflow检查是否可以馈送所有包含的张量并且在x.dense_shapex.dense_shape

Another option is to make a placeholder for the shape of x : 另一种选择是使占位符为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)
}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM