Sometime you need to find something similar to a diagonal in a non-square matrix. For instance, in neural networks, when you have an
x layer of m units to be topologically connected to an
y layer of n units, you could simply fill the diagonal elements of the matrix
W of weights. so that the activations given by the weighted sum
W*
x would be topologically spread on the
y layer.
The following matlab function returns a mask with the diagonal elements (plus a given neiborourhood) for any matrix dimentionality.
function m = gendiag(N,M,slope)
x=1:N; y=1:M;
[Y X]=meshgrid(y/M,x/N);
M = 1-abs(X-Y);
m = exp(-(M-1).^2/(2*(slope^2)));
end
example:
octave:12> imagesc(gendiag(70,30, 0.2))
gives
while
octave:12> imagesc(gendiag(40,75, 0.05))
gives
Hi !
ReplyDeletedo you have the same code in Python or C++ ?
Or maybe the pseudo-code ?