TensorFlow:使用conv2d_transpose超出列表索引

时间:2018-06-26 08:40:29

标签: python tensorflow neural-network

我想使用卷积转置,通过以下输入获得2700个值的张量:

input = tf.placeholder(tf.float32, shape=(batch_size, 1 , 1 ,1))

为此,我使用了tf.nn.conv2d_transpose函数。

这是我的代码:

import tensorflow as tf

import numpy as np

sess = tf.Session()

batch_size = 20

input = tf.placeholder(tf.float32, shape=(batch_size, 1 , 1 ,1))

logits = tf.nn.conv2d_transpose(input, [batch_size,1,2700,1],[batch_size, 1, 2700, 1],[1,1,3,1],'SAME')

运行该程序时,在最后一行出现以下错误:

IndexError: list index out of range

这是Python返回的完整错误:

IndexError                                Traceback (most recent call last)
<ipython-input-34-724f7880c01d> in <module>()
      9 input = tf.placeholder(tf.float32, shape=(batch_size, 1 , 1 ,1))
     10 
---> 11 logits = tf.nn.conv2d_transpose(input, [batch_size,1,2700,1],[batch_size, 1, 2700, 1],[1,1,3,1],'SAME')

/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/nn_ops.py in conv2d_transpose(value, filter, output_shape, strides, padding, data_format, name)
   1223     filter = ops.convert_to_tensor(filter, name="filter")  # pylint: disable=redefined-builtin
   1224     axis = 3 if data_format == "NHWC" else 1
-> 1225     if not value.get_shape()[axis].is_compatible_with(filter.get_shape()[3]):
   1226       raise ValueError("input channels does not match filter's input channels, "
   1227                        "{} != {}".format(value.get_shape()[axis],

/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_shape.py in __getitem__(self, key)
    610         return TensorShape(self._dims[key])
    611       else:
--> 612         return self._dims[key]
    613     else:
    614       if isinstance(key, slice):

IndexError: list index out of range

欢迎一些帮助

1 个答案:

答案 0 :(得分:1)

tf.nn.conv2d_transpose的文档中可以看到,您需要为filteroutput_shape 定义占位符,类似于对{{1}的定义}。

以下测试代码为我运行,没有返回错误。对所需的输出大小进行必要的更改:

input