【发布时间】: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 的了解还不够,无法真正理解为什么会这样。
标签: matlab matrix neural-network octave