【Tensorflow、Keras】关于Reshape层小结(部分问题未解决)

every blog every motto: We would rather reuse an active dwarf than a sleeping giant.
https://blog.csdn.net/weixin_39190382?type=blog

0. 前言

reshape说起来不复杂,就是改变特征图的尺寸,但在实际过程中,却发现了有意思的问题,遂记之。暂未解决。
本节主要讲解有关tensorflow的Rehape层以及Keras中的Reshape层及其比较,最后附上有关tf.reshape方法。

1. 在模型中

1.1 Keras的Reshape

1.1.1 Keras正常情况

  1. 代码部分
from keras.layers import *

input = Input((16, 16, 3))
print('改变之前的shape:', input.shape)
a = Reshape((16 * 4, -1))(input)
print('改变之后的shape:', a.shape)
  1. 结果:
    在这里插入图片描述
  2. 个人看法:
  • 不管是改变之前的还是改变之后的shape,第一维的None都是批(batch)的大小
  • 改变之后的shape的第三维,应该是由计算所得,即:16 * 16 * 3)/ (16 * 4) = 12 (关于这里面为什么没有显示具体的数字,12,没有找到一个合理的解释)

1.1.2 Keras不正常情况

  1. 代码部分
    reshape总元素个数不等于传入的元素个数
    代码中多乘了200,
from keras.layers import *

input = Input((16, 16, 3))
print('改变之前的shape:', input.shape)
a = Reshape((16 * 4*200, -1))(input) # 这里多乘了200
print('改变之后的shape:', a.shape)
  1. 结果:
ValueError: total size of new array must be unchanged
  1. 个人看法:
  • 这里reshape 的元素个数超过了总元素个数,按照预期是报错显示,没有问题。

1.2 Tensorflow的Reshape

1.2.1 Tensorflow正常情况

  1. 代码部分
from tensorflow.keras.layers import *

input = Input((16, 16, 3))
print('改变之前的shape:', input.shape)
a = Reshape((16 * 4, -1))(input)
print('改变之后的shape:', a.shape)
  1. 结果
    在这里插入图片描述
  2. 个人看法:
    这里和Keras的情况相同,不赘述。

1.2.2 Tensorflow不正常情况

  1. 代码部分
    reshape总元素个数不等于传入的元素个数
    代码中多乘了200,
import tensorflow as tf
from tensorflow.keras.layers import *
print('tensorflow版本为:' + tf.__version__)

input = Input((16, 16, 3))
print('改变之前的shape:', input.shape)
a = Reshape((16 * 4*200, -1))(input)  # 这里多乘了200
print('改变之后的shape:', a.shape)
  1. 结果
    在这里插入图片描述
  2. 个人看法:
  • 这里并没有检测reshape以后的总元素个数是否和输入的元素个数相同,此处应该是一个bug。
  • tensorflow版本为2.0.0

2. 在具体数据中

2.1 keras 的Reshape层

  1. 代码
from keras.layers import *
import numpy as np
array = np.arange(2 * 3 * 4).reshape((2, 3, 4))
a_tensor = tf.convert_to_tensor(array)

print('改变之前的shape:', a_tensor.shape)
x = Reshape((1, -1, 2))(a_tensor)
print('改变之后的shape:', x.shape)
  1. 结果
    在这里插入图片描述
  2. 个人看法:
  • 这里值得注意的是,Reshape,将a_tensor第一维认为是批的大小,批的大小是不计入计算的,剩下的元素个数进行改变shape的计算。
  • Reshape层返回的是一个batch_size + target_shape,具体参考文后链接

2.2 tensorflow 的Reshape层

  1. 代码
import tensorflow as tf
from tensorflow.keras.layers import *
import numpy as np
array = np.arange(2 * 3 * 4).reshape((2, 3, 4))
a_tensor = tf.convert_to_tensor(array)

print('改变之前的shape:', a_tensor.shape)
x = Reshape((1, -1, 2))(a_tensor)
print('改变之后的shape:', x.shape)
  1. 结果

在这里插入图片描述
4. 个人看法
这里Reshape和tensorflow里面的Reshape是一样的。

2.3 tf.reshape(附)

  1. 代码
import tensorflow as tf
import numpy as np
array = np.arange(2 * 3 * 4).reshape((2, 3, 4))
a_tensor = tf.convert_to_tensor(array)

print('改变之前的shape:', a_tensor.shape)
x = tf.reshape(a_tensor, (3, -1, 2))
print('改变之后的shape:', x.shape)
  1. 结果
    在这里插入图片描述
  2. 个人看法
    这里用的是tf.reshape,可以看到,其中的-1,是总元素个数除于已确定的元素个数。

3. 总结

  1. 关于Reshape层中,
  • 当在模型中时,Reshape,第一维度为批的大小,其显示为None
  • 当在具体数字计算中,第一维度为批的大小,其显示为传入数据shape的第一个维度,即批的大小。
  1. 关于tf.reshape
  • tf.reshape这里即正常的思维,改变形状,它不像Reshape层,会有批(batch)的概念。

参考文献

[1] https://tensorflow.google.cn/api_docs/python/tf/keras/layers/Reshape?hl=en
[2] https://keras-cn.readthedocs.io/en/latest/layers/core_layer/#reshape

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡侃有料

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值