【问题标题】:Tensorflow reshape on convolution output gives TypeError卷积输出上的 Tensorflow reshape 给出了 TypeError
【发布时间】:2016-10-09 15:03:53
【问题描述】:

当我尝试使用 tf.reshape() 重塑卷积输出时,我得到一个 TypeError

TypeError: Expected binary or unicode string, got -1

我写的模型是:

with tf.name_scope('conv1'):
    filter = tf.Variable(tf.truncated_normal([5, 5, 1, self.num_hidden / 2], mean=0.0,
                                             stddev=0.02, dtype=tf.float32),
                         name='filter')
    b = tf.Variable(tf.zeros([self.num_hidden / 2], dtype=tf.float32),
                    name='b')
    h1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(inp, filter,
                                   [1, 2, 2, 1], padding='SAME'), b))
with tf.name_scope('conv2'):
    filter = tf.Variable(tf.truncated_normal([5, 5, self.num_hidden / 2, self.num_hidden], mean=0.0,
                                             stddev=0.02, dtype=tf.float32),
                         name='filter')
    b = tf.Variable(tf.zeros([self.num_hidden], dtype=tf.float32),
                    name='b')
    h2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(h1, filter,
                                   [1, 2, 2, 1], padding='SAME'), b))
    # h2 -> [-1, 7, 7, 32]
    # num_units -> [-1, 1568]
    shape = h2.get_shape()
    num_units = shape[1]*shape[2]*shape[3]
with tf.name_scope('reshape'):
    h2_flattened = tf.reshape(h2, [-1, num_units])
    h2_flattened = tf.nn.dropout(h2_flattened, keep_prob=0.9)
with tf.name_scope('prediction'):
    W = tf.Variable(tf.truncated_normal([num_units, 1], mean=0.0, stddev=0.01,
                                        dtype=tf.float32), name='W')
    b = tf.Variable(tf.zeros([1], dtype=tf.float32), name='b')
    self.pred = tf.matmul(h2_flattened, W) + b

我得到的确切错误是:

Traceback (most recent call last):
  File "single_model_conv.py", line 108, in <module>
    gan = GAN(num_latent, 28, 'single')
  File "single_model_conv.py", line 23, in __init__
    self.adversary(self.gen_image)
  File "single_model_conv.py", line 93, in adversary
    h2_flattened = tf.reshape(h2, [-1, num_units])
  File "/nfs/nemo/u3/idurugkar/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1977, in reshape
    name=name)
  File "/nfs/nemo/u3/idurugkar/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 490, in apply_op
    preferred_dtype=default_dtype)
  File "/nfs/nemo/u3/idurugkar/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 657, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/nfs/nemo/u3/idurugkar/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 180, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/nfs/nemo/u3/idurugkar/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 163, in constant
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
  File "/nfs/nemo/u3/idurugkar/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 422, in make_tensor_proto
    tensor_proto.string_val.extend([compat.as_bytes(x) for x in proto_values])
  File "/nfs/nemo/u3/idurugkar/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/util/compat.py", line 64, in as_bytes
    (bytes_or_text,))
TypeError: Expected binary or unicode string, got -1

我不明白为什么会这样。将形状数组转换为张量似乎存在一些错误,但是当我尝试将任意数组转换为张量时它可以工作。 我还尝试将所有维度转换为实际值(batch_size 而不是 -1),但这也不起作用。

我的 tensorflow 版本是 0.11,我在支持 GPU 的 Linux 机器上运行它。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    我以前必须这样做。改变这个

    shape = h2.get_shape()
    

    到这里:

    shape = h2.get_shape().as_list()
    

    【讨论】:

    • 这也解决了我的(类似)问题,谢谢!知道为什么会这样吗?
    • 我相信 .get_shape() 返回张量类型,而您真正想要的是一个 numpy 数组或一个可以使用的普通 python 列表。文档和功能不是很好,希望它会变得更好,或者它的一个分支会让正在发生的事情更加明显。
    猜你喜欢
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 2017-10-06
    • 2023-03-17
    相关资源
    最近更新 更多