ReshapeLayer[dims]
表示一个网络层,将输入重新诠释为维数为 dims 的数组.
ReshapeLayer
ReshapeLayer[dims]
表示一个网络层,将输入重新诠释为维数为 dims 的数组.
更多信息和选项
- ReshapeLayer 开放下列端口以便用在 NetGraph 等中:
-
"Input" 任意秩的数组 "ReferenceShape" 从数组中复制形状(可选) "Output" 重塑后的数组 - 在 ReshapeLayer[spec] 中,允许使用以下 spec:
-
{dim1,…} 明确指定尺寸 "ReferenceShape" 为 "Reference" 端口提供的数组维度 - 在 ReshapeLayer[dims] 中,列表 dims 中允许有以下元素:
-
n 显式指定大小 Inherited 复制输入的大小 Automatic 根据其他维度的大小推断大小 All 从输入复制余下维度的大小 - 维度列表中只能出现一个 Automatic 或一个 All.
- 输入数组中元素的总数等于输出数组中元素的总数.
- ReshapeLayer 通常会根据其在 NetChain 等中的上下文推断其输入的维度. 要将维度明确指定为 {n1,n2,…},使用 ReshapeLayer["Input"->{n1,n2,…}].
- ReshapeLayer[…][input] 明确计算将该网络层应用于 input 的输出.
- ReshapeLayer[…][{input1,input2,…}] 明确计算每个 inputi 的输出.
- 使用 "ReferenceShape" 重塑规格,输入可以是 ReshapeLayer[…][<|"Input"input,"ReferenceShape"ref|>] 或 ReshapeLayer[…][<|"Input"{input1,…},"ReferenceShape"{ref1,…}|>].
- 当输入 NumericArray 时,输出将是 NumericArray.
- Options[ReshapeLayer] 给出了构建网络层的默认选项列表. Options[ReshapeLayer[…]] 给出了默认选项列表,以便根据某些数据对网络层进行运算.
- Information[ReshapeLayer[…]] 给出该网络层的报告.
- Information[ReshapeLayer[…],prop] 给出 ReshapeLayer[…] 属性 prop 的值. 可能的属性和 NetGraph 的相同.
范例
打开所有单元 关闭所有单元基本范例 (3)
创建 ReshapeLayer 将任意输入的形状改变为 2x3 矩阵:
ReshapeLayer[{2, 3}]创建 ReshapeLayer 将任意输入的形状变成 2x3 矩阵:
reshape = ReshapeLayer[{2, 3}]reshape[{1, 2, 3, 4, 5, 6}]reshape[{{{{1, 2}, {3, 4}, {5, 6}}}}]创建一个 ReshapeLayer 重塑长度为 1 的向量为单个数字:
reshape = ReshapeLayer[{}, "Input" -> 1]reshape[{9.}]范围 (5)
创建一个 ReshapeLayer,调整对特定大小的输入数组进行调整:
reshape = ReshapeLayer[{4}, "Input" -> {1, 1, 4}]reshape[{{{1, 2, 3, 4}}}]创建一个 ReshapeLayer,把一个数字调整为 1×1 的矩阵:
reshape = ReshapeLayer[{Inherited, 2, 2}, "Input" -> {6, 1, 4}]reshape@RandomReal[1, {6, 1, 4}]Dimensions@%创建一个 ReshapeLayer 复制输入的第一个维度大小并根据其他推断第四维度的大小:
reshape = ReshapeLayer[{Inherited, 1, 8, Automatic}, "Input" -> {6, 4, 4}]reshape@RandomReal[1, {6, 4, 4}];Dimensions@%创建一个 ReshapeLayer,在数组的最深层添加大小为 1 的维数:
reshape = ReshapeLayer[{All, 1}]reshape[{{1, 2, 3}, {4, 5, 6}}]创建一个 ReshapeLayer,重塑输入,使其与参考数组的形状相匹配:
reshape = ReshapeLayer["ReferenceShape"]reshape[<|"Input" -> {-1., 0., 1.}, "ReferenceShape" -> {{.4}, {-.2}, {7.2}}|>]应用 (1)
创建一个 ReshapeLayer,获取 32×32 RGB 图像并返回 96×32 灰度图像:
reshape = ReshapeLayer[{1, Automatic, Inherited}, "Input" -> NetEncoder[{"Image", 32}], "Output" -> NetDecoder["Image"]]reshape[[image]]属性和关系 (3)
可以用 ReshapeLayer 来实现 FlattenLayer. 通过将2×3×3 数组调整成正确的输出维度,把它的前两个维度压平:
input = RandomInteger[1, {2, 3, 3}];
input//MatrixFormReshapeLayer[{6, Automatic}][input]//MatrixFormFlattenLayer[1][input]//MatrixForm当输入大小由 NetReplacePart 改变,规范 Automatic 和 Inherited 允许 ReshapeLayer 重新计算输出大小. 创建一个 ReshapeLayer:
reshape = ReshapeLayer[{2, Inherited, 3, Automatic}, "Input" -> {24, 5}]NetReplacePart[reshape, "Input" -> {12, 5}]如果用固定的规范创建同样的 ReshapeLayer,调整大小会导致失败:
reshape = ReshapeLayer[{2, 5, 3, 4}, "Input" -> {24, 5}]NetReplacePart[reshape, "Input" -> {12, 5}]对于整数维度规范,ReshapeLayer 计算:
reshape[array_, dims_] := ArrayReshape[array, dims];data = {1, 2, 3, 4};
dims = {2, 2};
ReshapeLayer[dims]@data//Normal
reshape[data, dims]可能存在的问题 (2)
reshape = ReshapeLayer[{2, 2}][{1, 2, 5}]在维度规范中只允许一个 Automatic,多个会导致输出大小的模糊性:
ReshapeLayer[{2, Automatic, Automatic}, "Input" -> {2, 4, 4}]互动范例 (1)
巧妙范例 (2)
n = 8;
list = Subdivide[0., 1., n - 1]shapes = Flatten[
Table[{i, j, n / (i j)}, {i, Divisors[n]}, {j, Divisors[n / i]}], 1]将列表重塑为深度为 3 的数组,并使用 Image3D 将结果可视化:
Labeled[Image3D[ReshapeLayer[#][list], ImageSize -> Tiny, ColorFunction -> "LightTemperatureMap"], Row[#, "×"]]& /@ shapesn = 12;
list = Range[n];
shapes = Map[{n / #, #}&, Divisors[n]];
Labeled[Grid[ReshapeLayer[#][list], Dividers -> LightGray], Row[#, "×"]]& /@ shapes技术笔记
相关指南
-
▪
- 神经网络层
文本
Wolfram Research (2016),ReshapeLayer,Wolfram 语言函数,https://reference.wolfram.com/language/ref/ReshapeLayer.html (更新于 2025 年).
CMS
Wolfram 语言. 2016. "ReshapeLayer." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2025. https://reference.wolfram.com/language/ref/ReshapeLayer.html.
APA
Wolfram 语言. (2016). ReshapeLayer. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/ReshapeLayer.html 年
BibTeX
@misc{reference.wolfram_2026_reshapelayer, author="Wolfram Research", title="{ReshapeLayer}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/ReshapeLayer.html}", note=[Accessed: 08-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_reshapelayer, organization={Wolfram Research}, title={ReshapeLayer}, year={2025}, url={https://reference.wolfram.com/language/ref/ReshapeLayer.html}, note=[Accessed: 08-November-2026]}