首页 > 解决方案 > TensorFlow:使用 conv2d_transpose 列出超出范围的索引

问题描述

我想使用卷积转置来获得具有以下输入的 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

欢迎提供一些帮助

标签: pythontensorflowneural-network

解决方案


tf.nn.conv2d_transpose的文档中,您可以看到您需要为定义占位符filter并且output_shape类似于您为input.

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

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

filter = tf.placeholder(tf.float32, shape=(batch_size, 1 , 2700 ,1))

out = tf.placeholder(tf.int32, shape=(4,))

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

推荐阅读