将数组重新排列成适合神经网络训练的形式

Rearrange array into a form suitable for NN training

我正在处理一个大型数据集,我需要将其转换为特定格式以便进一步处理。我正在寻求这方面的建议。

示例输入:

A = [0.99  -0.99
     1     -1
     0.55  -0.55]

示例输出:

val(:,:,1,1)=0.99
val(:,:,2,1)=-0.99
val(:,:,1,2)=1
val(:,:,2,2)=-1
val(:,:,1,3)=0.55
val(:,:,2,3)=-0.55

在处理这个的过程中,我在 MATLAB R2018b 的 CNN 工具箱中找到了一段代码

function dummifiedOut = dummify(categoricalIn)
    % iDummify   Convert a categorical input into a dummified output.
    %
    % dummifiedOut(1,1,i,j)=1 if observation j is in class i, and zero
    % otherwise. Therefore, dummifiedOut will be of size [1, 1, K, N],
    % where K is the number of categories and N is the number of
    % observation in categoricalIn.

    %   Copyright 2015-2016 The MathWorks, Inc.

    numObservations = numel(categoricalIn);
    numCategories = numel(categories(categoricalIn));
    dummifiedSize = [1, 1, numCategories, numObservations];
    dummifiedOut = zeros(dummifiedSize);
    categoricalIn = iMakeHorizontal( categoricalIn );
    idx = sub2ind(dummifiedSize(3:4), int32(categoricalIn), 1:numObservations);
    dummifiedOut(idx) = 1;
end
function vec = iMakeHorizontal( vec )
    vec = reshape( vec, 1, numel( vec ) );
end

我们可以修改此代码块以生成示例输出吗?

直接执行 , or just use permute 的操作:

>> val = permute(A, [4,3,2,1])
val(:,:,1,1) =
    0.9900
val(:,:,2,1) =
   -0.9900
val(:,:,1,2) =
     1
val(:,:,2,2) =
    -1
val(:,:,1,3) =
    0.5500
val(:,:,2,3) =
   -0.5500 

请注意,您发布的函数需要 categorical 数据,而您有一个简单的 double 数组。如果你坚持 "adapting" 现有的 dummify,你可以这样做:

function dummifiedOut = dummify(categoricalIn)
    dummifiedOut = zeros([1,1,size(categoricalIn)]);
    dummifiedOut(:) = categoricalIn;
end

(...虽然,恕我直言,这没什么意义。)