The following question came up in reading group discussion of paper -- imagine taking two vectors, multiplying each by a random matrix, then taking tanh pointwise. If you repeat this process forever, does the angle between vectors eventually converge to 0 or Pi?
Mathematica seems like a good way to test this theory computationally, but my approach wasn't fast enough. IE, below is histogram of cosine similarities for 2500 pairs of vectors after applying 200 steps....but I'd like to get this for 20000 steps in reasonable time. Are there some tricks to use to speed this up significantly? (can Mathematica use GPUs?)
(* create properly scaled random matrix *)
randXavier[{rows_, cols_}] :=
RandomVariate[
NormalDistribution[0, Sqrt[2/(rows + cols)]], {rows, cols}];
(* normalize each row of matrix to have norm 1 *)
rowNormalize[mat_] := Module[{rowNorms},
rowNorms = Norm /@ mat;
DiagonalMatrix[1/rowNorms] . mat
];
(* take uppper triangular part of matrix as a vector *)
upperTriangular[mat_] :=
Statistics`Library`UpperTriangularMatrixToVector[mat];
d = 200; (* depth *)
w = 1024; (* layer dimension *)
b = 50; (* batch \
size *)
mat := randXavier[{w, w}]
x = RandomVariate[NormalDistribution[], {b, w}];
a = Nest[Tanh[# . mat] &, x, d];
a = rowNormalize[a];
gram = a . Transpose[a];
plot1 = Histogram[upperTriangular[gram]]
d=20000
$\endgroup$