【问题标题】:Reshaping tensor after max pooling ValueError: Shapes are not compatible在最大池化 ValueError 后重塑张量:形状不兼容
【发布时间】:2016-09-26 19:59:25
【问题描述】:

我正在基于this example构建适合我自己数据的CNN

基本上,我的数据有 3640 个特征;我有一个卷积层,然后是一个池化层,它汇集了所有其他特征,所以我最终得到维度 (?, 1, 1819, 1),因为在卷积层 / 2 == 1819 之后有 3638 个特征。

当我尝试在池化后重塑我的数据以将其以 [n_samples, n_fetures] 形式获取时

    print("pool_shape", pool_shape) #pool  (?, 1, 1819, 10)
    print("y_shape", y_shape) #y  (?,)

    pool.set_shape([pool_shape[0], pool_shape[2]*pool_shape[3]])
    y.set_shape([y_shape[0], 1])

我收到一个错误:

ValueError: Shapes (?, 1, 1819, 10) and (?, 18190) are not compatible

我的代码:

N_FEATURES = 140*26
N_FILTERS = 1
WINDOW_SIZE = 3

def my_conv_model(x, y):

    x = tf.cast(x, tf.float32)
    y = tf.cast(y, tf.float32)

    print("x ", x.get_shape())
    print("y ", y.get_shape())

    # to form a 4d tensor of shape batch_size x 1 x N_FEATURES x 1
    x = tf.reshape(x, [-1, 1, N_FEATURES, 1])
    # this will give you sliding window of 1 x WINDOW_SIZE convolution.
    features = tf.contrib.layers.convolution2d(inputs=x,
                                               num_outputs=N_FILTERS,
                                               kernel_size=[1, WINDOW_SIZE],
                                               padding='VALID')

    print("features ", features.get_shape()) #features  (?, 1, 3638, 10)

    # Max pooling across output of Convolution+Relu.
    pool = tf.nn.max_pool(features, ksize=[1, 1, 2, 1],
                             strides=[1, 1, 2, 1], padding='SAME')

    pool_shape = pool.get_shape()
    y_shape = y.get_shape()
    print("pool_shape", pool_shape) #pool  (?, 1, 1819, 10)
    print("y_shape", y_shape) #y  (?,)

### here comes the error ###
    pool.set_shape([pool_shape[0], pool_shape[2]*pool_shape[3]])
    y.set_shape([y_shape[0], 1])

    pool_shape = pool.get_shape()
    y_shape = y.get_shape()
    print("pool_shape", pool_shape) #pool  (?, 1, 1819, 10)
    print("y_shape", y_shape) #y  (?,)

    prediction, loss = learn.models.logistic_regression(pool, y)
    return prediction, loss

如何重塑数据以获得任何有意义的表示,然后将其传递给逻辑回归层?

【问题讨论】:

    标签: machine-learning tensorflow reshape convolution


    【解决方案1】:

    这看起来像是 Tensor.set_shape() 方法和 tf.reshape() 运算符之间的混淆。在这种情况下,您应该使用tf.reshape(),因为您正在更改pooly 张量的形状:

    • tf.reshape(tensor, shape) 运算符采用任意形状的 tensor,并返回具有给定 shape 的张量,只要它们具有相同数量的元素。此运算符应用于更改输入张量的形状。

    • tensor.set_shape(shape) 方法采用可能具有部分已知或未知形状的 tensor,并向 TensorFlow 断言它实际上具有给定的 shape。此方法应该用于提供有关特定张量形状的更多信息。

      它可以用于,例如,当您获取具有与数据相关的输出形状(例如tf.image.decode_jpeg())的运算符的输出并断言它具有静态形状(例如,基于关于数据集中的图像)。

    在您的程序中,您应该将对set_shape() 的调用替换为以下内容:

    pool_shape = tf.shape(pool)
    pool = tf.reshape(pool, [pool_shape[0], pool_shape[2] * pool_shape[3]])
    
    y_shape = tf.shape(y)
    y = tf.reshape(y, [y_shape[0], 1])
    
    # Or, more straightforwardly:
    y = tf.expand_dims(y, 1)    
    

    【讨论】:

    • 当我执行pool = tf.reshape(pool, [pool_shape[0], pool_shape[2]*pool_shape[3]]) 时,我得到:TypeError: Expected int32, got Dimension(None) of type 'Dimension' instead.。我怀疑这是因为池形状是pool_shape == (?, 1, 1819, 10),其中第一个参数需要被推断出来,而不是显式整数。我该如何解决这个问题?我猜推断的参数是可能会变化的批量大小。
    • 另外,在开头:def my_conv_model(x, y): x = tf.cast(x, tf.float32) y = tf.cast(y, tf.float32) print("x ", x.get_shape()) print("y ", y.get_shape()) 打印的形状是x (?, 3640)y (?,)。有没有办法实际获取维度并将它们存储在变量或其他东西中?
    • 啊,我更新了答案以使用tf.shape()(它使用实际的运行时形状)而不是Tensor.get_shape()。看起来批处理大小在您的程序中是可变的。如果您想查看大小可以变化的张量的实际形状,您可以将张量传递给tf.shape() 操作并将结果传递给sess.run()
    • 我后来收到一个错误ValueError: Shape of a new variable (logistic_regression/weights) must be fully defined, but instead was (?, 1). 有什么想法可能会出错吗?这是代码:github.com/Oleksandra28/Research/blob/master/step_4_cnn_4.py
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 2021-05-29
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 2021-02-23
    相关资源
    最近更新 更多