登录论坛

查看完整版本 : 一种无需迭代即可对矩阵中的列向量进行热编码的方法


poster
2019-12-07, 06:27
我正在实现一个神经网络,并试图根据每一列的最大值对列向量的矩阵进行热编码。以前,我一直在逐个向量地迭代矩阵向量,但是有人告诉我这是不必要的,实际上我可以同时对矩阵中的每个列向量进行一次热编码。不幸的是,在仔细阅读了SO,GitHub和MathWorks之后,似乎什么也做不完。我在下面列出了我以前的代码。请帮忙!谢谢 :)

% 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

更多&回答... (https://stackoverflow.com/q/59221062)