python - Tensorflow 稀疏张量问题
问题描述
我试图运行以下 TensorFlow 代码。它包括稀疏矩阵,但似乎不起作用。我已经修改了 tensorflow 文档 ( Link ) 中给出的示例。我正在使用 tensorflow 版本 1.12.0。
代码:
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)
我收到以下错误:
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)'
我试图做很多事情,但我最终每次都会遇到这个问题。我现在正在考虑转到 TF 源代码并检查为什么会引发此错误。
解决方案
这很奇怪,但它的发生是因为tf.sparse.reshape
. 更准确地说,当 tensorflow 构建计算图时,它会将所有常量张量添加为不可馈送,这是有道理的。因此,当您重新整形并传递一个稀疏张量x
时,它会将其 dense_shape 添加为一个常量张量(我认为在这个地方必须制作该张量的副本)。最后,当您使用sess.run
和传递x
包含 3 个张量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)
}))
推荐阅读
- javascript - 对当前窗口进行截图并重复存储为数据,具有画面回放和稳定的速度
- postgresql - 如何根据以前的记录值更新日期
- javascript - 使用 Google Maps JavaScript API Loader 未定义“google”
- assertion - 在哪些情况下我们不能使用符号执行?
- netlogo - if 命令中的多个 or 条件
- java - 我可以在不与@Service 进行数据交互的情况下注释类吗?
- python - 即使传递了正确的格式,str 到 datetime 也会返回 NaT
- c - HDF5:并行读写不同文件
- flutter - 直接在whatsapp状态上分享来自flutter应用程序的视频
- c# - 根据 ID 合并 CSV 文件