我使用t-SNE和本網站的matlab代碼(https://lvdmaaten.github.io/tsne/)。但是,每當我運行此程序時數據的維度大於數據的數量都會出現錯誤。下面的代碼是我目前使用的代碼,並總是在這裏發生錯誤尺寸大於數據數量時可以使用t-SNE嗎?
M = M(:,ind(1:initial_dims));
誤差
Index exceeds matrix dimensions.
Error in tsne (line 62)
M = M(:,ind(1:initial_dims));
我的命令調用此tsne功能的MATLAB
output = tsne(input, [], 2, 640, 30);
輸入尺寸爲(162x640),尺寸爲640,數據數量爲162.下面的程序是上述網站的代碼。
function ydata = tsne(X, labels, no_dims, initial_dims, perplexity)
%TSNE Performs symmetric t-SNE on dataset X
%
% mappedX = tsne(X, labels, no_dims, initial_dims, perplexity)
% mappedX = tsne(X, labels, initial_solution, perplexity)
%
% The function performs symmetric t-SNE on the NxD dataset X to reduce its
% dimensionality to no_dims dimensions (default = 2). The data is
% preprocessed using PCA, reducing the dimensionality to initial_dims
% dimensions (default = 30). Alternatively, an initial solution obtained
% from an other dimensionality reduction technique may be specified in
% initial_solution. The perplexity of the Gaussian kernel that is employed
% can be specified through perplexity (default = 30). The labels of the
% data are not used by t-SNE itself, however, they are used to color
% intermediate plots. Please provide an empty labels matrix [] if you
% don't want to plot results during the optimization.
% The low-dimensional data representation is returned in mappedX.
%
%
% (C) Laurens van der Maaten, 2010
% University of California, San Diego
if ~exist('labels', 'var')
labels = [];
end
if ~exist('no_dims', 'var') || isempty(no_dims)
no_dims = 2;
end
if ~exist('initial_dims', 'var') || isempty(initial_dims)
initial_dims = min(50, size(X, 2));
end
if ~exist('perplexity', 'var') || isempty(perplexity)
perplexity = 30;
end
% First check whether we already have an initial solution
if numel(no_dims) > 1
initial_solution = true;
ydata = no_dims;
no_dims = size(ydata, 2);
perplexity = initial_dims;
else
initial_solution = false;
end
% Normalize input data
X = X - min(X(:));
X = X/max(X(:));
X = bsxfun(@minus, X, mean(X, 1));
% Perform preprocessing using PCA
if ~initial_solution
disp('Preprocessing data using PCA...');
if size(X, 2) < size(X, 1)
C = X' * X;
else
C = (1/size(X, 1)) * (X * X');
end
[M, lambda] = eig(C);
[lambda, ind] = sort(diag(lambda), 'descend');
M = M(:,ind(1:initial_dims));
lambda = lambda(1:initial_dims);
if ~(size(X, 2) < size(X, 1))
M = bsxfun(@times, X' * M, (1 ./ sqrt(size(X, 1) .* lambda))');
end
X = bsxfun(@minus, X, mean(X, 1)) * M;
clear M lambda ind
end
% Compute pairwise distance matrix
sum_X = sum(X .^ 2, 2);
D = bsxfun(@plus, sum_X, bsxfun(@plus, sum_X', -2 * (X * X')));
% Compute joint probabilities
P = d2p(D, perplexity, 1e-5); % compute affinities using fixed perplexity
clear D
% Run t-SNE
if initial_solution
ydata = tsne_p(P, labels, ydata);
else
ydata = tsne_p(P, labels, no_dims);
end
我想了解這段代碼,但我無法理解錯誤發生的部分。
if size(X, 2) < size(X, 1)
C = X' * X;
else
C = (1/size(X, 1)) * (X * X');
end
爲什麼需要此條件?由於'X'的大小是(162x640),所以else語句將被執行。我想這是問題所在。在else語句中,'C'的大小將是(162x162)。但是,在下一行
M = M(:,ind(1:initial_dims));
使用等於640的'initial_dims'。我以錯誤的方式使用了這些代碼嗎?或者它只是不適用於我使用的數據集?