【问题标题】:One hot encode column vectors in matrix without iterating一个热编码矩阵中的列向量而不迭代
【发布时间】:2020-03-31 21:57:04
【问题描述】:

我正在实现一个神经网络,并尝试根据每列中的最大值对列向量矩阵进行热编码。以前,我一直在逐个向量地遍历矩阵向量,但有人告诉我这是不必要的,实际上我可以同时对矩阵中的每个列向量进行热编码。不幸的是,在仔细阅读了 SO、GitHub 和 MathWorks 之后,似乎没有任何工作可以完成。我在下面列出了我以前的代码。请帮忙!谢谢:)

更新: 这就是我想要完成的...除了这只是将整个矩阵中的最大值更改为 1。我想将每个 COLUMN 中的最大值更改为 1。

one_hots = bsxfun(@eq, mini_batch_activations, max(mini_batch_activations(:))) 

更新 2: 这是我正在寻找的,但它只适用于行。我需要专栏。

V = max(mini_batch_activations,[],2);
      idx = mini_batch_activations == V;

迭代代码:

     % This is the matrix I want to one hot encode
     mini_batch_activations = activations{length(layers)};

     %For each vector in the mini_batch:
     for m = 1:size(mini_batch_activations, 2)

        % Isolate column vector for mini_batch
        vector = mini_batch_activations(:,m);

        % One hot encode vector to compare to target vector
        one_hot = zeros(size(mini_batch_activations, 1),1);
        [max_val,ind] = max(vector);
        one_hot(ind) = 1;

        % Isolate corresponding column vector in targets
        mini_batch = mini_batch_y{k};
        target_vector = mini_batch(:,m);

        % Compare one_hot to target vector , and increment result if they match
        if isequal(one_hot, target_vector)
          num_correct = num_correct + 1;
        endif

        ...

     endfor

【问题讨论】:

标签: matlab matrix neural-network octave


【解决方案1】:

你已经得到了每一列的最大值:

V = max(mini_batch_activations,[],1); % note 1, not 2!

现在您需要做的就是相等比较,输出是一个逻辑数组,可以轻松转换为 0 和 1。请注意,MATLAB 和 Octave 执行隐式单例扩展:

one_hot = mini_batch_activations==V;

【讨论】:

    猜你喜欢
    • 2020-06-01
    • 2016-12-03
    • 2020-03-14
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    相关资源
    最近更新 更多