tf.reshape()和caffe的reshape层

本文详细介绍了Caffe和TensorFlow中重塑层的工作原理及其配置方法。在Caffe中,通过定义`Reshape`层并设置`shape`参数来改变blob数据的维度。在TensorFlow中,则使用`tf.reshape`函数实现同样的目的。文章还提供了多个示例来展示如何使用这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先讲caffe的reshape

caffe这一段摘自https://www.cnblogs.com/denny402/p/5072746.html

layer {
    name: "reshape"
    type: "Reshape"
    bottom: "input"
    top: "output"
    reshape_param {
      shape {
        dim: 0  # copy the dimension from below
        dim: 2
        dim: 3
        dim: -1 # infer it from the other dimensions
      }
    }
  }
复制代码

有一个可选的参数组shape,
用于指定blob数据的各维的值(blob是一个四维的数据:n*c*w*h)。

dim:0  表示维度不变,即输入和输出是相同的维度。

dim:2 或 dim:3 将原来的维度变成2或3

dim:-1 表示由系统自动计算维度。数据的总量不变,系统会根据blob数据的其它三维来自动计算当前维的维度值 。

假设原数据为:64*3*28*28, 表示64张3通道的28*28的彩色图片

经过reshape变换:

复制代码
   reshape_param {
      shape {
        dim: 0 
        dim: 0
        dim: 14
        dim: -1 
      }
    }
复制代码

输出数据为:64*3*14*56



tf.reshape(x,shape,Name)

x:输入的张量

shape:也是一个张量表示你希望返回的张量的形状。shape某一元素为-1的意义和上面caffe的-1一样为剩下的变量,为[-1]时,将会flatten成一维向量,为全链接层做准备。

Name:张量的名字,可以为None

这个函数的功能就是将输入的X,重新塑性成和shape一样的张量

官网的例子有

# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' has shape [9]
reshape
(t, [3, 3]) ==> [[1, 2, 3],
               
[4, 5, 6],
               
[7, 8, 9]]

# tensor 't' is [[[1, 1, 1],
#                 [2, 2, 2]],
#                [[3, 3, 3],
#                 [4, 4, 4]],
#                [[5, 5, 5],
#                 [6, 6, 6]]]
# tensor 't' has shape [3, 2, 3]
# pass '[-1]' to flatten 't'
reshape
(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]
# -1 表示剩下的元素:
reshape
(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
                         
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# tensor 't' is [7]
# shape `[]` reshapes to a scalar
reshape
(t, []) ==> 7



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值