python - 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
欢迎提供一些帮助
解决方案
从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')
推荐阅读
- javascript - 允许模式以可选的特定字符开头但没有其他字符的正则表达式
- android - 我想让我的联系人和拨号器应用程序默认设置为颤振
- python - 如何使用 Python 在二进制状态下编辑视频元数据
- python - 如何让 fastavro 支持逻辑类型?
- python - 使用 Python 在上位词级别通过类比进行文本语义相似性
- python - 颤振/飞镖:在数据点之间使用梯度插值绘制二维图
- reactjs - 在 React 中显示 openlayers 信息窗口
- python - 没有TensorFlow的张量上的Python操作?
- c++ - 如何使这个多线程 CAS 程序正常工作?
- racket - Racket (ASL) 中的调度算法