tf.contrib.layers.flatten(x) 张量流的替代函数

Posted

技术标签:

【中文标题】tf.contrib.layers.flatten(x) 张量流的替代函数【英文标题】:Alternative function for tf.contrib.layers.flatten(x) Tensor Flow 【发布时间】:2021-01-25 07:18:15 【问题描述】:

我在 Jetson TK1 上使用 Tensor flow 0.8.0 版本,在 32 位臂架构上使用 Cuda 6.5。为此,我无法升级 Tensor Flow 版本,并且在 Flatten 功能中遇到了麻烦

x = tf.placeholder(dtype = tf.float32, shape = [None, 28, 28])
y = tf.placeholder(dtype = tf.int32, shape = [None])
images_flat = tf.contrib.layers.flatten(x)

此时我遇到的错误是

AttributeError: 'module' object has no attribute 'flatten'

Tensor Flow V0.8 是否支持此功能的替代方法

到目前为止,我尝试过的是

images_flat = tf.reshape(x, (tf.shape(x)[0], -1))

但为此我得到以下错误

  File "demo_code.py", line 113, in <module>
    images_flat = tf.reshape(x, (tf.shape(x)[0], -1))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1092, in reshape
    name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 411, in apply_op
    as_ref=input_arg.is_ref)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 566, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/constant_op.py", line 179, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/constant_op.py", line 162, in constant
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 332, in make_tensor_proto
    _AssertCompatible(values, dtype)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 272, in _AssertCompatible
    (dtype.name, repr(mismatch), type(mismatch).__name__))
TypeError: Expected int32, got list containing Tensors of type '_Message' instead.

有关它的更多详细信息,我正在关注本教程 https://www.datacamp.com/community/tutorials/tensorflow-tutorial

谢谢

【问题讨论】:

这能回答你的问题吗? tf.reshape vs tf.contrib.layers.flatten 很遗憾没有。我已经尝试了该帖子中建议的所有 3 个选项。我在我的帖子中发布了这 3 个解决方案之一的错误。如果你愿意,我也可以为其他人做。 请立即尝试解决方案,我已对其进行了编辑(使用x.get_shape().as_list() 【参考方案1】:

您可以改用tf.reshape

images_flat = tf.reshape(x, [x.get_shape(x).as_list()[0], -1])

【讨论】:

是的,我已经尝试过了,但仍然出现错误:文件“/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py”,第 272 行,在AssertCompatible (dtype.name, repr(mismatch), type(mismatch).__name_)) TypeError: Expected int32, get list contains Tensors of type '_Message' 相反。 我已在原帖中为该命令添加了完整的错误

以上是关于tf.contrib.layers.flatten(x) 张量流的替代函数的主要内容,如果未能解决你的问题,请参考以下文章