简体   繁体   English

tensorflow tf.reshape不起作用

[英]tensorflow tf.reshape not working

I'm new to tensorflow. 我是tensorflow的新手。 I intended to flatten a tensor of shape (28,28,1) to a 1-D tensor by using tf.reshape, but didn't get the expected result. 我打算通过使用tf.reshape将形状为(28,28,1)的张量展平为一维张量,但没有得到预期的结果。 Here is the piece of the code I used. 这是我使用的部分代码。

def input_pipeline(self, batch_size, num_epochs=None):
    images_tensor = tf.convert_to_tensor(self.image_names, dtype=tf.string)
    labels_tensor = tf.convert_to_tensor(self.labels, dtype=tf.int64)
    input_queue = tf.train.slice_input_producer([images_tensor, labels_tensor], num_epochs=num_epochs)

    labels = input_queue[1]
    images_content = tf.read_file(input_queue[0])
    images = tf.image.convert_image_dtype(tf.image.decode_png(images_content, channels=N_CHANNELS), tf.float32)
    new_size = tf.constant([IMAGE_SIZE_CHN, IMAGE_SIZE_CHN], dtype=tf.int32)
    images = tf.image.resize_images(images, new_size)
    reshape_vec = tf.constant([-1], dtype=tf.int32)
    print(images.get_shape())
    tf.reshape(images, reshape_vec)
    print(images.get_shape())
    image_batch, label_batch = tf.train.shuffle_batch([images, labels], batch_size=batch_size, capacity=50000,
                                                      min_after_dequeue=10000)
    return image_batch, label_batch

The result of the two print functions are both (28,28,1). 这两个打印功能的结果均为(28,28,1)。 Can anyone help me on this? 谁可以帮我这个事? Thanks! 谢谢!

reshape returns a new tensor, which is the result of reshaping the old tensor. reshape返回新的张量,这是对旧张量进行重塑的结果。 It does not change the original tensor. 它不会更改原始张量。 To fix, simply change the reshape line to 要修复,只需将重塑线更改为

images = tf.reshape(images, reshape_vec)

Note it's probably nicer/cleaner to just use 请注意,仅使用它可能会更好/更清洁

images = tf.reshape(images, (-1,))

rather than define the new shape in a separate constant tensor, though that's more a personal preference thing. 而不是在单独的恒定张量中定义新形状,尽管那更多是个人喜好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM