将数组重新排列为适合于NN训练的形式

时间:2019-04-28 10:34:50

标签: matlab matrix neural-network dimensions training-data

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

样本输入:

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

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

1 个答案:

答案 0 :(得分:1)

要么做rinkert suggested,要么直接使用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数组。如果您坚持“适应”现有的dummify,则可以执行以下操作:

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

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