Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
|
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
我正在实现一个神经网络,并试图根据每一列的最大值对列向量的矩阵进行热编码。以前,我一直在逐个向量地迭代矩阵向量,但是有人告诉我这是不必要的,实际上我可以同时对矩阵中的每个列向量进行一次热编码。不幸的是,在仔细阅读了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 更多&回答... |
![]() |
![]() |