1
$\begingroup$

Is there any technique to debug a large cell which crashes kernel? IE, perhaps some way to print each line in the cell before it gets evaluated?

The following is such an example cell, crashes both desktop 13.1.0 and the wolframcloud version:

initialize[h0_] := (
   d = Length[h0];
   sampler = gaussianSampler[h];
   sigma = DiagonalMatrix[h];
   );


gaussianSampler[diag_] := With[{d = Length[diag]},
   Compile[{{n, _Integer}},
    Module[{vals},
     vals = 
      Sqrt[diag]*# & /@ RandomVariate[NormalDistribution[], {n, d}]]]];

(* estimates average value of func on sample size s *)
expectation[func_, s_] := 
  With[{numBatches = Max[1, Floor[Sqrt[s]*numSamples/(s*d)]]},
   Nest[func[sampler@s] + # &, 0., numBatches]/numBatches];

cosineSimilaritySquared = Compile[{{X, _Real, 2}},
   Module[{b = Length[X], meanLengthSquared, X2, gram},
    meanLengthSquared = Total[Total[# . #] & /@ X]/b;
    X2 = #/Norm[#] & /@ X;
    gram = X2 . X2\[Transpose];
     Total@Flatten@(gram*gram)/b/b
    ]
   ];


getVals[d_, b_] := (
   With[{h = Table[i^-1., {i, 1, d}]},
    
    initialize[h/Total[h]];
    ];
   
   {1/expectation[cosineSimilaritySquared, b], 
    expectation[1/cosineSimilaritySquared[#] &, b]}
   );
numSamples = 2;
getVals[2, 2]
$\endgroup$
2
  • $\begingroup$ They are defined. And it's probably a user-error discoverable by rerunning parts of the code manually, just curious how I can automate the process. $\endgroup$ Sep 6, 2022 at 4:51
  • $\begingroup$ Simplest method: Put some write statements in your code to see how far the code executes. Then investigate the statement that causes the crash. $\endgroup$ Sep 6, 2022 at 7:50

1 Answer 1

0
$\begingroup$

Kernel crashes are rare and seem to be due to Compile in this case. Simplest approach is replace all instances of Compile with Function, which lets you see the Stack Trace

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.