当我输入正确的张量形状时,排名错误

时间:2018-09-05 16:28:21

标签: python tensorflow

我正在尝试一个代码段(如下所示!)。当我运行代码时,它给了我形状相关的错误。 基本上,我是将占位符传递给类的成员函数。占位符的形状已根据功能要求进行设置。但是我得到一个错误。

import tensorflow as tf
import numpy as np


class Arranger(object):
    def __init__(self, win_height, win_width, stride_height, stride_width, rate_height, rate_width, padding,
                 numchannels):
        self._height = win_height
        self._width = win_width
        self._stride_height = stride_height
        self._stride_width = stride_width
        self._rate_height = rate_height
        self._rate_width = rate_width
        self._padding = padding
        self._feature_shape = None
        self._row_out = None
        self._col_out = None
        self._num_channels = numchannels

    def unarrange(self, feature_map):
        feature_map.set_shape([None, None, None, None])
        self._feature_shape = tf.shape(feature_map)
        features = tf.extract_image_patches(feature_map, ksizes=[1, self._height, self._width, 1],
                                            strides=[1, self._stride_height, self._stride_width, 1],
                                            rates=[1, 1, 1, 1],
                                            padding=self._padding)

        self._row_out = self._feature_shape[1]
        self._col_out = self._feature_shape[2]
        return features

    def arrange(self, patches):
        patches.set_shape([None, None, None])
        if self._feature_shape is None or self._row_out is None or self._col_out is None:
            raise ValueError('unarrange must be called before calling arrange.')

        batch_size = self._feature_shape[0]
        patches = tf.reshape(patches, [batch_size, self._row_out, self._col_out, self._num_channels])
        _x = tf.zeros_like(self._feature_shape)
        _y = self.unarrange(_x)
        grad = tf.gradients(_y, _x)[0]
        return tf.gradients(_y, _x, grad_ys=patches)[0] / grad


def test():
    patches = np.array([[3, 5, 3, 1], [8, 3, 1, 2], [9, 7, 4, 3], [2, 1, 0, 4]]).astype(np.float32)
    np_sample = np.random.randint(low=0, high=10, size=(10, 3, 3, 3))
    print(np_sample.shape)
    img = tf.placeholder(dtype=tf.float32, shape=[None, None, None, None])
    p = tf.placeholder(dtype=tf.float32, shape=[None, None, None])
    a = Arranger(2, 2, 1, 1, 1, 1, 'VALID', 1)
    b = a.unarrange(img)
    image_r = a.arrange(p)

    with tf.Session() as sess:
        image_r_v = sess.run(image_r, feed_dict={img: np_sample, p: patches})
        print(image_r_v)


if __name__ == "__main__":
    test()

错误如下:

(tf1.10) -bash-4.2$ python arranger.py 
(10, 3, 3, 3)
Traceback (most recent call last):
  File "/data/stars/user/uujjwal/collection-stars/anaconda3/envs/tf1.10/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 525, in set_shape
    unknown_shape)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes must be equal rank, but are 1 and 4

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "arranger.py", line 60, in <module>
    test()
  File "arranger.py", line 53, in test
    image_r = a.arrange(p)
  File "arranger.py", line 40, in arrange
    _y = self.unarrange(_x)
  File "arranger.py", line 21, in unarrange
    feature_map.set_shape([None, None, None, None])
  File "/data/stars/user/uujjwal/collection-stars/anaconda3/envs/tf1.10/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 528, in set_shape
    raise ValueError(str(e))
ValueError: Shapes must be equal rank, but are 1 and 4

1 个答案:

答案 0 :(得分:0)

_x = tf.zeros_like(self._feature_shape)将创建一个零张量,其大小与self._feature_shape相同,但是self._feature_shape是一维向量。也就是说,假设self._feature_shape[1, 2, 3, 4],那么_x将是[0, 0, 0, 0]。也许您想要的是:

_x = tf.zeros(self._feature_shape, dtype=tf.float32)
相关问题