Wednesday, 5 December 2018

testDNN analysis 1

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Deep Neural Network:                                     %
%                                                          %
% Copyright (C) 2013 Masayuki Tanaka. All rights reserved. %
%                    mtanaka@ctrl.titech.ac.jp             %
%                                                          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;

num = 1000;  % num of cases
nodes = [32 16 8 4]; %input to output layers' neuron counts input layer -> output layer

IN = rand(num,32);  % 1000 x 32
OUT = rand(num,4);  % 1000 x 4

dnn = randDBN( nodes ); % 1 x 1 3 structures of type rbm
%dnn = randDBN( nodes, 'GBDBN' );
nrbm = numel(dnn.rbm); % 3

opts.MaxIter = 20;
opts.BatchSize = num/4; % 250
opts.Verbose = true;
opts.StepRatio = 0.1;
opts.Layer = nrbm-1;  % 2
opts.DropOutRate = 0.5;
opts.object = 'CrossEntropy';

dnn = pretrainDBN(dnn, IN, opts);

dnn= SetLinearMapping(dnn, IN, OUT);

opts.Layer = 0;
dnn = trainDBN(dnn, IN, OUT, opts);
rmse = CalcRmse(dnn, IN, OUT);
rmse

%{
debug> size(IN)
ans =

   1000     32

debug> size(OUT)
ans =

   1000      4

debug> size(dnn)
ans =

   1   1

debug> displayentries(dnn)
error: `displayentries' undefined near line 3 column 1
debug> fieldnames(dnn)
ans =
{
  [1,1] = type
  [2,1] = rbm
}
debug> fieldnames(dnn)
ans =
{
  [1,1] = type
  [2,1] = rbm
}
debug> size(dnn.type)
ans =

   1   5

debug> dnn.type
ans = BBDBN
debug> size(dnn.rbm)
ans =

   3   1

debug> dnn.rbm
ans =
{
  [1,1] =

    scalar structure containing the fields:  32ROWS

      type = BBRBM
      W =

       Columns 1 through 5:

        -9.5439e-002  2.2939e-002  7.2628e-002  -1.6725e-001  -1.8585e-001
        -6.3338e-002  5.5813e-002  9.9650e-002  -3.2411e-002  1.9054e-001
        -2.8721e-002  -3.0517e-001  4.2261e-002  1.8782e-002  1.7918e-002
        1.6754e-001  -6.9326e-003  8.4993e-002  1.3912e-001  1.7788e-001
        1.2296e-001  2.8321e-003  6.8273e-002  -4.1696e-002  1.8666e-001
        -7.1908e-002  7.6576e-002  1.8943e-002  6.0098e-002  -8.2744e-002
        -6.1932e-002  -1.9985e-001  1.7706e-002  -1.7833e-001  3.2422e-002
        1.4648e-001  1.3261e-001  -8.6583e-002  -5.8321e-002  -7.2068e-002
        8.9776e-002  -3.7070e-002  5.6221e-002  -4.0033e-002  9.4303e-003
        8.9454e-002  -4.9070e-002  -6.3078e-002  3.1675e-002  -3.0792e-002
        1.8290e-001  -1.8830e-001  -8.8328e-002  -1.0021e-002  1.1692e-001
        -1.5825e-002  -8.2422e-002  -4.5168e-002  -1.0671e-001  7.1336e-002
        -1.4302e-001  1.1588e-002  -1.9893e-002  -1.8817e-002  8.0709e-002
        5.8547e-002  1.0674e-001  9.2972e-002  7.3739e-004  -8.8002e-002
        -5.2856e-002  5.6408e-002  -2.7927e-002  -5.2166e-002  -2.9046e-002
        -2.3111e-002  -2.5470e-002  1.4229e-002  6.3788e-002  -1.0690e-001
        1.0172e-001  1.6693e-001  -4.2641e-002  -6.0183e-003  8.3208e-002
        9.4523e-003  -1.0553e-001  3.2596e-002  -7.3335e-002  -6.9497e-002
        5.5084e-002  -4.9742e-002  6.3967e-002  -3.0585e-002  -2.2389e-003
        8.7456e-002  -9.3202e-003  5.7180e-002  -3.3230e-002  -1.3572e-002
        1.4025e-001  1.3006e-001  -9.2925e-002  -7.1881e-002  7.9407e-002
        4.6632e-002  2.5287e-002  1.8483e-001  -1.2771e-001  4.0131e-002
        -4.6402e-002  1.8763e-002  1.2175e-001  3.1542e-002  1.1833e-001
        -6.8776e-002  9.5478e-002  1.0184e-001  -1.3870e-001  -1.8715e-002
        -1.3226e-002  4.9959e-002  1.3911e-001  -1.2609e-001  -1.1164e-001
        5.1048e-003  8.2061e-002  -1.3399e-001  1.4166e-002  4.5473e-002
        4.7427e-002  -1.6272e-002  1.2218e-003  2.0601e-002  1.5357e-001
        1.2823e-002  9.5251e-002  9.6114e-002  -5.1072e-002  -4.2220e-003
        -2.7675e-002  1.3981e-001  -6.3217e-002  1.4558e-002  -1.0971e-001
        3.4808e-002  1.0162e-001  -2.7371e-002  -9.5146e-003  -8.1046e-002
        -8.9656e-004  5.3675e-002  3.5876e-002  1.1082e-001  -2.6857e-001
        -4.4603e-002  5.2974e-002  -1.0659e-001  -8.3114e-002  3.8094e-002

      ...
       Column 16:

        1.0590e-001
        -5.0035e-002
        9.8314e-002
        3.4597e-003
        1.8471e-001
        1.1792e-002
        -8.3624e-003
        8.8980e-003
        8.6437e-002
        1.0525e-001
        -1.0155e-001
        5.0527e-002
        -1.0684e-001
        8.2844e-002
        1.3908e-001
        1.8100e-001
        -2.6154e-002
        1.7376e-002
        -1.1795e-001
        -1.4004e-001
        4.5323e-002
        -1.1169e-002
        4.8986e-002
        7.6310e-002
        2.6240e-001
        2.4331e-002
        7.2168e-003
        -1.9634e-002
        -9.0908e-002
        -1.1385e-002
        -1.5389e-001
        -8.0523e-002

      b =

         0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

      c =

       Columns 1 through 18:

         0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

       Columns 19 through 32:

         0   0   0   0   0   0   0   0   0   0   0   0   0   0


  [2,1] =

    scalar structure containing the fields:  16ROWS

      type = BBRBM
      W =

       Columns 1 through 5:

        2.0102e-001  4.3688e-003  1.2167e-002  3.8655e-002  -1.0856e-001
        7.6643e-003  -2.0929e-002  2.1684e-001  2.7952e-002  -1.0454e-001
        -1.8838e-001  -1.1319e-001  1.3241e-001  2.4613e-001  -4.1756e-003
        -4.1513e-002  -2.4252e-001  4.6049e-002  -7.2507e-002  9.5904e-003
        8.3872e-002  9.5719e-002  5.9736e-002  1.0170e-002  -1.9051e-001
        -1.6029e-003  -8.5765e-002  1.3034e-001  1.3020e-001  -4.4126e-002
        -1.1046e-001  -1.6009e-001  8.4373e-002  1.4629e-001  -4.9012e-002
        1.3466e-003  -3.4392e-002  -2.4080e-001  3.8773e-002  -5.2277e-002
        8.6146e-004  -2.1853e-002  1.1403e-001  -9.8974e-002  -5.0794e-002
        -3.2462e-003  1.0488e-001  3.2717e-002  6.8939e-004  7.9416e-002
        -1.1660e-001  -1.1468e-001  1.3624e-001  4.0556e-002  3.1838e-003
        4.7486e-002  -1.3841e-001  -5.5895e-002  -1.5473e-001  -4.0310e-002
        2.6561e-002  -1.2955e-003  1.6016e-001  6.6688e-002  -1.0138e-002
        -1.2804e-001  5.2227e-003  7.8851e-002  -4.8367e-002  2.6254e-004
        1.5797e-003  6.0113e-002  3.2811e-002  -1.1808e-001  7.7672e-002
        1.4736e-001  -1.7833e-001  -8.0439e-002  -3.6698e-003  -8.5635e-002

       Columns 6 through 8:

        -4.6001e-002  -6.9266e-002  -2.2738e-001
        4.7092e-002  -2.0253e-001  -3.2701e-003
        5.1861e-002  -2.1056e-001  -1.8411e-002
        -7.1343e-002  1.2337e-001  1.9276e-002
        -1.8914e-003  -1.5765e-001  -8.6933e-002
        -2.0187e-001  4.3527e-002  -1.7456e-002
        1.0685e-001  1.7238e-002  -2.8500e-002
        -3.1129e-002  -1.1785e-002  -1.8281e-001
        6.6779e-002  -1.9351e-002  1.3626e-001
        1.0567e-001  5.0517e-002  -3.2258e-002
        -6.2616e-002  -4.1738e-005  -3.2652e-003
        2.0329e-003  1.2461e-001  3.1411e-002
        8.1571e-002  8.1036e-002  6.0923e-002
        5.1120e-002  -4.4483e-002  -1.1280e-001
        5.0619e-002  1.1328e-001  1.4899e-001
        3.8269e-002  -1.4240e-001  -3.6423e-002

      b =

         0   0   0   0   0   0   0   0

      c =

         0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0


  [3,1] =

    scalar structure containing the fields:   8ROWS

      type = BBRBM
      W =

         0.0186368  -0.0967863   0.0086856  -0.0792971
        -0.0265770  -0.0345573   0.0390910  -0.2094785
         0.1285115   0.0039218   0.1239428  -0.0057840
        -0.0358651  -0.2717663  -0.0276068   0.0775830
         0.0222669   0.0783432  -0.0770633   0.0245969
         0.0052533   0.0777309  -0.0423200  -0.0332311
        -0.1173345   0.0099952  -0.0984870  -0.1093623
         0.0689255   0.0407822   0.0991716   0.0154337

      b =

         0   0   0   0

      c =

         0   0   0   0   0   0   0   0


}
debug> nrbm
nrbm =  3
%}
%{
dnn = pretrainDBN(dnn, IN, opts);
***************************************
debug> size(dnn)
ans =

   1   1

debug> fieldnames(dnn)
ans =
{
  [1,1] = type
  [2,1] = rbm
}
debug> size(dnn.type)
ans =

   1   5

debug> dnn.type
ans = BBDBN
debug> size(dnn.rbm)
ans =

   3   1

debug> dnn.rbm
scalar structure containing the fields:

      type = BBRBM
      W =

       Columns 1 through 5:

        -1.4456e-001  1.3457e-001  -1.5983e-001  -2.1078e-001  1.4789e-001
        1.9967e-001  3.1571e-002  1.3676e-001  -1.9001e-001  5.2966e-002
        -7.9528e-002  -7.3805e-002  1.3651e-001  -1.4727e-002  -5.0989e-002
        4.6101e-001  -2.0480e-001  2.2967e-002  -4.4049e-002  -1.4408e-001
        -1.5530e-002  5.8309e-002  2.7578e-002  -2.6401e-001  1.6906e-001
        -2.3830e-002  7.3771e-002  -4.0720e-002  1.0873e-001  3.2833e-002
        -1.1991e-001  -2.7482e-002  -1.7003e-001  -4.4027e-002  -1.6771e-001
        -1.4532e-001  1.4782e-001  1.6026e-001  -3.9349e-002  -2.3665e-001
        9.6552e-002  9.6118e-002  1.0965e-002  4.8927e-002  1.3123e-001
        -1.8393e-001  8.8770e-002  9.7955e-002  1.0479e-001  1.9539e-001
        1.5810e-001  8.2610e-003  7.7519e-002  -1.7944e-001  2.0037e-002
        -1.8539e-002  2.2513e-002  -1.4977e-001  -6.7766e-002  1.3686e-001
        1.3339e-001  -2.9161e-002  7.3629e-002  -7.3675e-002  -1.2211e-001
        1.9417e-002  1.4506e-001  -9.1872e-002  -4.9863e-002  -1.4877e-001
        6.8912e-002  -7.8553e-002  -1.5891e-001  1.6622e-001  -9.9249e-002
        4.6266e-002  1.2187e-001  8.3142e-002  -1.9155e-001  1.5987e-001
        1.6179e-002  6.4626e-002  -2.8579e-001  -1.4409e-002  -1.3780e-001
        -9.7421e-002  -3.9605e-002  6.2394e-002  -9.7487e-002  2.5618e-002
        -2.2313e-002  1.1810e-001  4.9739e-002  -1.9756e-001  -3.7360e-002
        1.1228e-001  -2.2252e-001  3.8728e-002  -1.6449e-001  1.3446e-001
        -1.0710e-001  2.4637e-001  -3.1594e-002  8.3380e-002  1.0684e-001
        -8.0976e-002  -2.2602e-003  1.2758e-001  1.2872e-001  -3.5073e-002
        -5.6544e-002  -2.2636e-004  -1.4674e-001  -6.3959e-002  2.7805e-002
        3.1768e-001  -6.6169e-002  -7.9164e-002  1.0105e-001  -2.5982e-001
        -7.4598e-002  1.4319e-001  2.6001e-001  -1.7961e-001  -1.2858e-001
        2.1387e-003  7.2934e-003  -2.0793e-001  -7.9268e-002  2.5656e-001
        -1.5909e-001  -1.6167e-002  3.3853e-002  -9.1057e-002  1.8289e-001
        3.3792e-002  9.1442e-002  1.0779e-001  1.3711e-001  -3.9855e-002
        5.1293e-002  -2.0965e-002  1.6927e-001  -2.1026e-001  -2.0306e-001
        -1.4660e-001  1.3455e-002  -5.2939e-002  -3.4808e-002  1.5054e-001
        1.0657e-001  -1.4081e-002  -1.0672e-001  1.0797e-001  9.5403e-002
        4.2248e-002  -1.3737e-001  -1.6574e-002  1.0592e-001  -1.7172e-001

       Columns 6 through 10:

        -8.5914e-002  1.0224e-002  2.0198e-001  -1.9529e-001  2.9960e-002
        -1.5373e-001  -1.8566e-001  -8.7281e-002  7.6317e-002  -9.4733e-002
        -1.3825e-001  6.1988e-002  -1.0458e-001  5.9529e-002  1.6094e-001
        2.2550e-002  -1.4377e-001  1.2013e-002  9.3174e-002  -6.1050e-004
        -1.1033e-001  3.0341e-001  3.9760e-002  2.7072e-002  -6.9339e-003
        2.7696e-001  -8.5717e-002  -3.9012e-002  -1.0869e-002  -4.4833e-002
        -9.3750e-003  -1.3472e-001  1.2080e-002  7.9816e-002  -9.2314e-003
        1.2384e-001  2.8670e-001  -1.7781e-002  -1.2156e-001  -9.3283e-002
        7.7685e-002  -1.8240e-001  2.3156e-002  -1.0981e-001  7.0797e-004
        4.6324e-002  1.8556e-002  -2.5338e-001  -1.1749e-001  -1.1827e-001
        1.4409e-001  1.0703e-001  -1.9620e-001  -3.1371e-002  -8.1335e-002
        -8.4576e-002  8.0624e-002  3.8084e-002  1.2493e-001  9.2895e-003
        -1.5890e-001  5.5033e-002  1.2631e-001  1.3307e-001  1.5517e-001
        -9.5496e-002  -1.7814e-002  2.5346e-001  1.6847e-001  -5.3799e-002
        2.6378e-001  1.8021e-002  -5.2915e-002  -1.3608e-001  2.7665e-002
        9.1235e-002  -8.6926e-002  -7.1771e-002  4.5970e-003  9.2774e-002
        -9.0323e-002  2.8599e-002  -3.5934e-002  -1.0172e-001  2.7208e-001
        9.0449e-002  1.1858e-001  2.3264e-001  -1.5732e-001  -4.1013e-002
        -1.1754e-001  1.3658e-001  5.4594e-002  9.1868e-002  -6.6272e-002
        -5.5522e-002  -1.4106e-002  -1.0810e-002  -2.2745e-001  1.7566e-002
        4.8655e-002  1.3820e-001  -1.2193e-001  -2.5636e-001  -4.7235e-002
        9.5951e-002  1.2619e-001  -1.9169e-001  2.1087e-001  -1.9027e-001
        3.0819e-002  1.0616e-001  1.0001e-001  1.1986e-001  -3.8117e-001
        -1.1276e-001  -5.5931e-002  9.0106e-003  4.9986e-002  -3.6583e-002
        1.5102e-001  -2.4015e-001  -5.5786e-002  -2.4883e-002  1.4220e-002
        2.2478e-002  -1.2901e-001  -2.4733e-002  -4.6572e-002  7.9630e-002
        5.8555e-002  2.7067e-002  -4.7607e-002  4.0240e-002  -4.0228e-002
        -1.8334e-001  -1.1726e-001  1.6269e-001  1.0437e-001  -3.3466e-001
        -1.3963e-001  -4.8113e-002  -2.4723e-001  1.5928e-001  7.8055e-002
        1.4487e-001  -4.0303e-002  3.6406e-002  7.6593e-002  1.0397e-001
        -1.0251e-002  -1.1552e-001  3.5737e-002  -1.2316e-001  2.1050e-001
        1.9225e-001  -3.8724e-002  -7.8417e-002  5.2468e-002  -9.5619e-002

       Columns 11 through 15:

        2.4098e-003  -2.6345e-001  -4.6159e-002  1.5794e-001  -3.3164e-003
        -1.7913e-001  -2.3133e-002  4.2701e-002  2.3828e-001  1.9139e-001
        2.2087e-002  -1.4815e-001  6.2519e-002  -2.5090e-002  8.0639e-003
        -2.6046e-001  -1.5779e-001  1.6938e-001  -6.9762e-002  1.4018e-001
        8.6579e-002  3.6462e-002  -8.0427e-002  -2.0413e-001  -1.7592e-001
        -1.2733e-001  1.0460e-001  -1.7352e-001  -1.4951e-001  -4.0147e-002
        2.5901e-002  8.6292e-002  1.3644e-001  2.2090e-001  1.3782e-001
        1.1347e-001  4.5161e-002  -4.4923e-002  -3.5492e-002  1.9974e-001
        1.7800e-002  3.5112e-002  -9.3266e-002  9.9701e-002  -2.5843e-001
        -4.9081e-002  -2.1651e-002  4.9206e-002  4.1672e-002  4.5893e-002
        -1.6028e-001  6.9965e-002  -9.9095e-002  -9.8361e-002  1.5742e-002
        -2.2755e-001  -1.3005e-001  3.2077e-001  -2.1002e-002  -1.8576e-002
        -1.0820e-001  2.2273e-002  1.4682e-001  -1.7895e-001  -2.0020e-001
        -1.0212e-001  -9.6141e-002  7.2759e-002  -5.6987e-002  -8.3177e-002
        -1.2048e-002  1.4154e-001  1.8797e-001  -7.5862e-002  -2.2842e-001
        1.9373e-001  -1.5303e-001  7.4419e-002  -2.0184e-001  -1.2280e-002
        8.8329e-002  -5.7753e-002  1.5731e-001  6.5880e-002  -2.9509e-002
        5.1196e-002  -6.7306e-002  6.8033e-002  -1.5876e-002  -1.2128e-001
        1.3562e-001  1.7572e-001  -9.5858e-002  -5.7359e-002  -4.9138e-002
        -1.6606e-002  7.4453e-002  2.2216e-001  -8.0778e-002  9.9390e-002
        -1.4711e-001  -2.0306e-002  3.7244e-002  2.0792e-001  -5.3220e-002
        -2.0906e-001  -7.5341e-002  3.2026e-002  2.1813e-002  -8.0194e-002
        -8.9684e-002  -1.3909e-001  6.9256e-002  1.8700e-001  -3.0837e-002
        2.0385e-001  1.0729e-001  5.6024e-002  -1.2941e-001  1.2849e-001
        -8.7558e-002  6.1155e-002  2.8669e-002  1.3912e-002  8.1313e-002
        -1.1198e-001  1.1722e-001  3.7843e-002  -1.3390e-001  4.9545e-002
        1.1977e-001  3.7913e-002  1.4384e-001  -1.8977e-001  -1.2410e-002
        -4.7232e-002  -3.8173e-002  5.9145e-002  -3.9736e-002  1.1931e-001
        2.6636e-001  1.2498e-001  -2.1388e-002  -2.1876e-001  -4.1982e-003
        -2.5778e-002  1.9604e-001  -2.0281e-001  -3.0428e-002  -6.2732e-002
        2.0315e-001  -1.7387e-001  1.1643e-002  -9.8135e-002  8.3186e-002
        1.8663e-002  2.0766e-002  4.0007e-002  1.4561e-001  1.3943e-002

       Column 16:

        1.2361e-001
        5.3400e-003
        3.9879e-002
        1.8711e-002
        1.3219e-001
        -5.1279e-002
        1.2698e-001
        -5.9815e-002
        6.0131e-002
        -3.3446e-002
        4.0260e-002
        -1.0416e-001
        -1.4745e-001
        -4.7535e-002
        -6.4048e-002
        -2.2644e-001
        -1.8675e-002
        6.2748e-002
        -2.2745e-001
        -7.9144e-002
        -1.1377e-001
        1.4382e-001
        1.2872e-001
        -5.1093e-002
        -1.5694e-001
        5.4393e-002
        -1.9857e-001
        4.2873e-002
        9.1268e-002
        5.2551e-002
        -1.1336e-001
        -1.1882e-001

      b =

       Columns 1 through 5:

        -1.5586e-002  -1.1496e-002  -5.8476e-003  1.5063e-002  -6.7685e-003

       Columns 6 through 10:

        -5.7987e-003  -6.9171e-003  -7.8332e-003  -1.4338e-004  -4.1969e-003

       Columns 11 through 15:

        -8.1333e-003  -9.0226e-003  -2.2251e-002  -1.5057e-003  -1.6091e-002

       Column 16:

        5.6817e-004

      c =

       Columns 1 through 6:

         0.0395916  -0.0773137   0.0312343  -0.0407844  -0.0025245   0.0896613

       Columns 7 through 12:

        -0.0425101  -0.1091811  -0.0149902   0.0279449   0.0730091  -0.0038612

       Columns 13 through 18:

         0.0314979   0.0748003   0.0157778  -0.0173519  -0.0313998  -0.0555946

       Columns 19 through 24:

        -0.0412943  -0.0241210   0.0050137   0.0062377   0.0211264  -0.0494209

       Columns 25 through 30:

         0.0124015  -0.0334159   0.0270998   0.0090238   0.0583709  -0.0652171

       Columns 31 and 32:

        -0.0365972   0.0718705


  [2,1] =

    scalar structure containing the fields:

      type = BBRBM
      W =

       Columns 1 through 6:

        -0.1580402   0.1646551   0.1544616   0.0127687   0.0475242   0.0354418
         0.1059701  -0.0584153   0.1202782   0.0424994   0.0615194   0.0647433
         0.0307732  -0.0471046  -0.2426331   0.1067414   0.0483431  -0.0798042
        -0.0731989  -0.0210007  -0.1234566  -0.1587460   0.0332187  -0.2252511
         0.0978923  -0.1253813   0.0735168  -0.1567597  -0.0283528  -0.0407749
         0.0145115   0.0742178   0.1333530  -0.0073586   0.0637584   0.0700637
         0.0969561   0.0043284  -0.1200178   0.1004060  -0.0754928   0.0167230
        -0.1466019   0.1610189  -0.1535090   0.0476767  -0.0862423  -0.0467296
        -0.0041632   0.1136615  -0.0324736  -0.0667652   0.0516534  -0.0535831
        -0.0434391  -0.1836850   0.0426363   0.0200211   0.1795177  -0.1665324
        -0.1280892  -0.1473259   0.0141963  -0.0618399  -0.0636552   0.1216574
         0.0529018  -0.0140015   0.0391559  -0.0730919   0.2005384  -0.1336324
         0.1655463   0.1814396   0.2060112   0.0534279   0.1115840  -0.0068995
        -0.2106281  -0.1304211   0.1142619  -0.1817210  -0.1178374   0.1359360
         0.0081734  -0.0306936   0.0177958   0.1006536  -0.0477942  -0.0826642
        -0.0018466  -0.0195038  -0.0971977   0.0341731  -0.1154291   0.0489353

       Columns 7 and 8:

        -0.0284126   0.0200409
        -0.0930402   0.0764000
         0.0341746  -0.0478940
        -0.1998030  -0.1460855
         0.0696642   0.0428283
         0.0259319  -0.0889552
        -0.0194931   0.1268362
         0.0665831  -0.0574145
         0.0629943  -0.0650520
        -0.0046916  -0.1639800
        -0.0519509  -0.0733316
        -0.0539535  -0.1465853
         0.2265682   0.0198238
        -0.0749331  -0.0618888
        -0.0175542   0.0231235
        -0.1475020  -0.0175232

      b =

       Columns 1 through 6:

         0.0057857  -0.0016897   0.0027805  -0.0032890  -0.0065730  -0.0010049

       Columns 7 and 8:

         0.0108362   0.0027294

      c =

       Columns 1 through 6:

         0.0519208   0.1673262   0.1023236  -0.2268309   0.0366060   0.0182441

       Columns 7 through 12:

        -0.0512072  -0.0443540   0.0091081  -0.1174627  -0.0111318  -0.0157029

       Columns 13 through 16:

         0.1704334  -0.0983854  -0.0740015  -0.1733870


  [3,1] =

    scalar structure containing the fields:

      type = BBRBM
      W =

        6.4865e-003  3.1768e-003  -4.8833e-002  -6.2117e-002
        -4.3082e-004  -9.2065e-002  -9.6846e-002  3.7618e-002
        -7.6594e-002  1.0289e-001  1.0863e-001  3.7683e-002
        -6.6581e-002  2.9303e-002  -6.4712e-002  8.4750e-002
        1.8016e-001  1.2360e-001  -1.3360e-001  -2.9996e-002
        -1.9455e-002  3.0877e-002  -9.0625e-002  -4.8582e-002
        -7.6954e-002  -1.1390e-001  4.1767e-002  1.1894e-001
        2.4895e-002  -4.6617e-003  -1.1059e-001  -6.8426e-002

      b =

        -0.0018479  -0.0032890  -0.0012351  -0.0015600

      c =

       Columns 1 through 6:

         0.0308390   0.0916032   0.0644636  -0.0433083   0.0800952  -0.0703054

       Columns 7 and 8:

         0.0054586  -0.1400794


}

%}

Sunday, 25 November 2018

Indexing a matrix with two matrixes

How to index a matrix with matrixes?

A short outline of the matrix indexing will be followed by
a=[1,2,3,4;5,6,7,8;9,10,11,12]
b=[1,2;3,3;2,3]
c=[1;2]
octave:6> a(b,c)
ans =

    1    2
    9   10
    5    6
    5    6
    9   10
    9   10

Indexing Vectors (https://de.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html)
Let's start with the simple case of a vector and a single subscript. The vector is:

  v = [16 5 9 4 2 11 7 14];
The subscript can be a single value:

  v(3)     % Extract the third element
  ans =
        9
Or the subscript can itself be another vector:

  v([1 5 6])      % Extract the first, fifth, and sixth elements
  ans =
        16   2   11

The crucial point here is item 1  selects item 1 of v which is 16 and
item 5 selects item 5 of v which is 2 and
item 6 selects item 6 of v which is 11.
------------------------------------------------

Let's take an a matrix with octave:
C:\Users\ars>octave -qf
octave:1> a=[1,2,3,4;5,6,7,8;9,10,11,12]
a =
    1    2    3    4
    5    6    7    8
    9   10   11   12


Let's take a b matrix:
octave:2> b=[1,2;3,4;5,6]
b =
   1   2
   3   4
   5   6


Let's take a c matrix:
octave:3> c=[1;2]
c =
   1
   2

octave:4> a(b,c)
error: A(I,J): row index out of bounds; value 6 out of bound 3
Octave does not like 6 because a has only 3 rows.

Let's take b with only one item bigger than a's row count.
octave:4> b=[1,2;3,4;2,3]
b =

   1   2
   3   4
   2   3

octave:5> a(b,c)
error: A(I,J): row index out of bounds; value 4 out of bound 3
The error repeats.  Hence, the items of b matrix must not be
bigger than the row count of a matrix.


Let's take a b matrix with all of its items less or equal to
the row count of a matrix.

octave:5> b=[1,2;3,3;2,3]
b =

   1   2
   3   3
   2   3

Let's try a(b,c) again:
 
octave:6> a(b,c)
ans =

    1    2
    9   10
    5    6
    5    6
    9   10
    9   10

It works.  Let's recall a and c matrixes and analyze this result:

a =
    1    2    3    4
    5    6    7    8
    9   10   11   12

c =
   1
   2
 
We have to refer for each of b the related row
and each of c to the realed column of a matrix.

b=1 c=1,2 -> 1 2
b=3 c=1,2 -> 9 10
b=2 c=1,2 -> 5 6
Please notice that b points to each row according to the value of item in sequence
of b 1 5 9 2 6 10 3 7 11 4 8 12
and for each row c proceeds for each column of each row with the value of items in matrix sequence
of c that is 1 2 1 2 1 2 1 2 1 2 ...
namely
b=2 c=1,2 -> 5 6
b=3 c=1,2 -> 9 10
b=3 c=1,2 -> 9 10

Just another example:

octave:7> b=[1,1;2,2;3,3]
b =

   1   1
   2   2
   3   3

Let's recal a and c matrixes:

a =
    1    2    3    4
    5    6    7    8
    9   10   11   12

c =
   1
   2
 
octave:8> a(b,c)
ans =

    1    2
    5    6
    9   10
    1    2
    5    6
    9   10

c selects first and second columns. b selects
1 2 3 1 2 3 rows.

Let's increase the rows count of b.  And observe:

octave:9> b=[1,1;2,2;3,3;1,1]
b =

   1   1
   2   2
   3   3
   1   1

octave:10> a(b,c)
ans =

    1    2
    5    6
    9   10
    1    2
    1    2
    5    6
    9   10
    1    2

Increasing the size of b (or c) does not cause an
abnormal end.  It only increases the selected items
of the output matrix.  Infact
the size of the output matrix = size(b) times size(c).
Below given examples show that, the size and shape of the
b and c matrixes a(b,c) does not cause an abend because
the a(b,c) calculation is done according to the item
sequence of b and c matrixes.


octave:11> b=[1,1,1;2,2,2;3,3,3]
b =

   1   1   1
   2   2   2
   3   3   3

octave:12> a(b,c)
ans =

    1    2
    5    6
    9   10
    1    2
    5    6
    9   10
    1    2
    5    6
    9   10

octave:13> c=[1,2,2]
c =

   1   2   2

octave:14> a(b,c)
ans =

    1    2    2
    5    6    6
    9   10   10
    1    2    2
    5    6    6
    9   10   10
    1    2    2
    5    6    6
    9   10   10

octave:15> c=[1,2;3,4]
c =

   1   2
   3   4

octave:16> a(b,c)
ans =

    1    3    2    4
    5    7    6    8
    9   11   10   12
    1    3    2    4
    5    7    6    8
    9   11   10   12
    1    3    2    4
    5    7    6    8
    9   11   10   12

Cheers.

Ali Riza SARAL
arsaral((at))yahoo.com

Wednesday, 14 November 2018

Otomatik Cevap Motoru - Cevap Analizi

Mehmet okula gitti.
-------------------
Mehmet nereye gitti?
Mehmet gitti mi?
Okula kim gitti?
Mehmet okula nasıl gitti?
Mehmet ne yaptı?

REF ANALİZİ
====================
0 1 Mehmet  İsim  (f_önü koşarak   %intr. *o refverbalPOS=0 ) \Nesne\   \possible Özne\ <************ÖZNE
1 2 okula  İsim->Dolaylı Tümleç -a 
2 3 koşarak  Zarf -arak  -fiilimsi  %intr.
3 4 gitti Fiil %dat.intr. *o
3 4 .crlf postPunctuation

Mehmet nereye gitti?
********************
1-Find sentences which has Mehmet as SUBJECT(ÖZNE)
2-Find sentences which has Mehmet and gitti/gitmek(from Morpholojik analysis) as Fiil.
3-Find a Dolaylı Tümleç with -e/a type (nere-ye)
4-Use this tümleç to form an answer sentence -->Mehmet OKULA gitti.

Mehmet gitti mi?
****************
1-Find sentences which has Mehmet as SUBJECT(ÖZNE)
2-Find sentences which has Mehmet and gitti/gitmek(from Morpholojik analysis) as Fiil.
3-If above items are found form answer as YES else NO.

Okula kim gitti?
****************
1-Find sentences which has gitti/gitmek(from Morpholojik analysis) as Fiil.
2-Find sentences which has okula
3-Find Özne in this sentence and ANSWER with the text of that word(s)

Mehmet okula nasıl gitti?
*************************
1-Find sentences which has Mehmet as SUBJECT(ÖZNE)
2-Find sentences which has Mehmet and gitti/gitmek(from Morpholojik analysis) as Fiil.
2-Find sentences which has okula
4- Find Zarf before or after Fiil answer with the text of that word

Mehmet ne yaptı?
****************
1-Find sentences which has Mehmet as SUBJECT(ÖZNE)
2-Limit/warn that the number may be many.
3-Extract Nesne and fiil of these sentences and form sentences such as
'Mehmet gitti'.

Otomatik Cevap Motoru - Soru Dosyası Analizi

REFERANS METİN
==============
Mehmet okula koşarak gitti.
Ayşe topu bana attı.
O matematik dersini çok çalışır.
Ahmet heyecanlı bir çocuktur.

Mehmet okula gitti.
-------------------
Mehmet nereye gitti?
Mehmet gitti mi?
Okula kim gitti?
Mehmet okula nasıl gitti?
Mehmet ne yaptı?

Ayşe topu bana attı.
--------------------
Kim topu attı?
Kim topu bana attı?
Bana topu kim attı?
Ayşe bana ne attı?
Ayşe ne yaptı?
Top ne oldu?

O matematik dersini çok çalışır.
-------------------------------
Matematik dersini kim çok çalışır?
O neyi çok çalışır?
O hangi dersi çok çalışır?
O matematik dersini ne yapar?
O ne yapar?

Ahmet heyecanlı bir çocuktur.
-----------------------------
Ahmet nasıl bir çocuktur?
Ahmet kimdir?

Otomatik Cevap Motoru - Ref Dosyası Analizi

sentence= 0<<****************************dumpSentenceAsText
Mehmet okula koşarak gitti.

sentence= 0<<----------------------------dumpstructlist p="">0 1 Mehmet  İsim  (f_önü koşarak   %intr. *o refverbalPOS=0 ) \Nesne\   \possible Özne\ <************ÖZNE
1 2 okula  İsim->Dolaylı Tümleç -a 
2 3 koşarak  Zarf -arak  -fiilimsi  %intr.
3 4 gitti Fiil %dat.intr. *o
3 4 .crlf postPunctuation


sentence= 1<<****************************dumpSentenceAsText
Ayşe topu bana attı.

sentence= 1<<----------------------------dumpstructlist p="">0 2 Ayşe topu  İsim Tamlaması -i/ı/u/ü  (f_önü attı  %tr.intr.dat. *o ref ) \Nesne\  <************NESNE
2 3 bana  Zamir->Dolaylı Tümleç -a 
3 4 attı Fiil %tr.intr.dat. *o
3 4 .crlf postPunctuation


sentence= 2<<****************************dumpSentenceAsText
O matematik dersini çok çalışır.

sentence= 2<<----------------------------dumpstructlist p="">0 1 O  Zamir   \possible Özne\ ( çalışır  %tr.intr. *o ref )<************ÖZNE
1 3 matematik dersini  Sıfat Tamlaması->Nesne -ini   (f_önü çalışır  %tr.intr. *o ref ) \Nesne\  <************NESNE
3 4 çok  Zarf
4 5 çalışır Fiil %tr.intr. *o
4 5 .crlf postPunctuation


sentence= 3<<****************************dumpSentenceAsText
Ahmet heyecanlı bir çocuktur.

sentence= 3<<----------------------------dumpstructlist p="">0 1 Ahmet  İsim  (f_önü heyecanlı bir çocuktur   %intr. *o ref ) \Nesne\    \possible Özne\ <************ÖZNE
1 4 heyecanlı bir çocuktur  Fiil İsim/Sıfat Tamlaması Türevi %intr.  -tur 
3 4 .crlfcrlfcrlfcrlfcrlf postPunctuation

BUILD SUCCESSFUL (total time: 1 second)





MORPHOLOJIK(kelime yapısı) VERİLERİ
===================================

sentence= 0<<****************************dumpSentenceAsText
Mehmet okula koşarak gitti.

sentence= 0<<----------------------------morphologic analysis="" p="">0 0   0 Mehmet
R is. ===>Mehmet mehmet
1 0   1 okula
N is. ===>okula   =okul -a ^Noun->Noun 
2 0   2 koşarak
V f. ===>koşarak   =koş -arak ^Verb->Adverb ->Adverb %intr.
3 0   3 gitti.

V f. ===>gitti   =git -ti ^DiliGeçmiş %dat.intr.


sentence= 1<<****************************dumpSentenceAsText
Ayşe topu bana attı.

sentence= 1<<----------------------------morphologic analysis="" p="">4 1   0 Ayşe
Ayşe-->ayşe
R is. ===>Ayşe ayşe
5 1   1 topu
R is. ===>topu   top -u 
6 1   2 bana
R zm. ===>bana   ben -a
7 1   3 attı.
V f. ===>attı   =at -tı ^DiliGeçmiş %tr.intr.dat.


sentence= 2<<****************************dumpSentenceAsText
O matematik dersini çok çalışır.

sentence= 2<<----------------------------morphologic analysis="" p="">8 2   0 O
O-->o
R zm. ===>O   o
9 2   1 matematik
R sf. ===>matematik
10 2   2 dersini
N is. ===>dersini   =ders -ini ^Noun->Noun 
11 2   3 çok
R zf. sf. ===>çok
12 2   4 çalışır.

V f. ===>çalışır   =çalış -ır ^IRliGenişZ %tr.intr.


sentence= 3<<****************************dumpSentenceAsText
Ahmet heyecanlı bir çocuktur.

sentence= 3<<----------------------------morphologic analysis="" p="">13 3   0 Ahmet
Ahmet-->ahmet
R is. ===>Ahmet ahmet
14 3   1 heyecanlı
R sf. ===>heyecanlı
N is. ===>heyecanlı   =heyecan -lı ^Noun->Noun/Adjective 
15 3   2 bir
R sf. ===>bir
16 3   3 çocuktur.
N is. ===>çocuktur   =çocuk -tur ^Noun->Verb %intr.

Otomatik Cevap Motoru - Ref Dosyası

Mehmet okula koşarak gitti.
Ayşe topu bana attı.
O matematik dersini çok çalışır.
Ahmet heyecanlı bir çocuktur.




Wednesday, 7 November 2018

mnistclassify analysis 4

CG_CLASSIFY.m
% Version 1.000
%
% Code provided by Ruslan Salakhutdinov and Geoff Hinton
%
% Permission is granted for anyone to copy, use, modify, or distribute this
% program and accompanying programs and documents for any purpose, provided
% this copyright notice is retained and prominently displayed, along with
% a note saying that the original programs are available from our
% web page.
% The programs and documents are distributed without any warranty, express or
% implied.  As the programs were written for research purposes only, they have
% not been tested to the degree that would be advisable in any important
% application.  All use of these programs is entirely at the user's own risk.


function [f, df] = CG_CLASSIFY(VV,Dim,XX,target);

l1 = Dim(1); % 784
l2 = Dim(2); % 250
l3= Dim(3); % 250
l4= Dim(4); 5 50
l5= Dim(5); % 10
N = size(XX,1); % 1000 785

% Do decomversion.
 w1 = reshape(VV(1:(l1+1)*l2),l1+1,l2); % 785 250 = 272060(1:785*250, 785,250)
 xxx = (l1+1)*l2; % 785 * 250
 w2 = reshape(VV(xxx+1:xxx+(l2+1)*l3),l2+1,l3); % 251 250
 xxx = xxx+(l2+1)*l3;
 w3 = reshape(VV(xxx+1:xxx+(l3+1)*l4),l3+1,l4); % 251 50
 xxx = xxx+(l3+1)*l4;
 w_class = reshape(VV(xxx+1:xxx+(l4+1)*l5),l4+1,l5); % 51 10


  XX = [XX ones(N,1)]; %1000 785  data+1col
  w1probs = 1./(1 + exp(-XX*w1)); w1probs = [w1probs  ones(N,1)]; % 1000 785 * 785 250 -> 1000 251
  w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; % 1000 251 * 251 250 -> 1000 251
  w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs  ones(N,1)]; % 1000 251 * 251 50 -> 1000 51

  targetout = exp(w3probs*w_class); % 1000 51 * 51 10 = 1000 10
  targetout = targetout./repmat(sum(targetout,2),1,10); % repmat((1000 1),1,10) normalize
  f = -sum(sum( target(:,1:end).*log(targetout))) ;  % cross enthropy

IO = (targetout-target(:,1:end));  % 1000 10
Ix_class=IO;
dw_class =  w3probs'*Ix_class; % 1000 51' * 1000 10 = 51 10

Ix3 = (Ix_class*w_class').*w3probs.*(1-w3probs); % 1000 10 * 51 10' = 1000 51 .* 1000 51 = 1000 51
Ix3 = Ix3(:,1:end-1); % 1000 50
dw3 =  w2probs'*Ix3; %1000 251' * 1000 50 = 251 50

Ix2 = (Ix3*w3').*w2probs.*(1-w2probs); % 1000 50 * 251 50' .* 1000 251 = 1000 251
Ix2 = Ix2(:,1:end-1); % 1000 250
dw2 =  w1probs'*Ix2; % 1000 251' * 1000 250 = 251 250

Ix1 = (Ix2*w2').*w1probs.*(1-w1probs); % 1000 250 * 251 250' .* 1000 251 = 1000 251
Ix1 = Ix1(:,1:end-1); % 1000 250
dw1 =  XX'*Ix1; % 1000 785' * 1000 250 = 785 250

df = [dw1(:)' dw2(:)' dw3(:)' dw_class(:)']'; % 272060 1

mnistclassify analysis 3

CG_CLASSIFY_INIT.m
% Version 1.000
%
% Code provided by Ruslan Salakhutdinov and Geoff Hinton
%
% Permission is granted for anyone to copy, use, modify, or distribute this
% program and accompanying programs and documents for any purpose, provided
% this copyright notice is retained and prominently displayed, along with
% a note saying that the original programs are available from our
% web page.
% The programs and documents are distributed without any warranty, express or
% implied.  As the programs were written for research purposes only, they have
% not been tested to the degree that would be advisable in any important
% application.  All use of these programs is entirely at the user's own risk.


function [f, df] = CG_CLASSIFY_INIT(VV,Dim,w3probs,target); % 510 1, 2 1, 1000 51, 1000 10
l1 = Dim(1); % 50
l2 = Dim(2); % 10
N = size(w3probs,1); % 1000
% Do decomversion.
  w_class = reshape(VV,l1+1,l2); % 51 10
  w3probs = [w3probs  ones(N,1)];  % 1000 51

  targetout = exp(w3probs*w_class); % 1000 51 * 51 10 = 1000 10
  targetout = targetout./repmat(sum(targetout,2),1,10); % repmat((1000 1),1,10) normalize

  f = -sum(sum( target(:,1:end).*log(targetout))) ; % cross enthropy
IO = (targetout-target(:,1:end)); % 1000 10
Ix_class=IO;
dw_class =  w3probs'*Ix_class; % 1000 51' * 1000 10 = 51 10

df = [dw_class(:)']'; % 510 1

%{
 debug> a=[1,2,3;4,5,6]
a =
   1   2   3
   4   5   6

debug> sum(a,2)
ans =
6
   15

debug> repmat(sum(a,2),1,10)
ans =
6    6    6    6    6    6    6    6    6    6
   15   15   15   15   15   15   15   15   15   15
%}

mnistclassify analysis 2

backpropclassify.m
% Version 1.000
%
% Code provided by Ruslan Salakhutdinov and Geoff Hinton
%
% Permission is granted for anyone to copy, use, modify, or distribute this
% program and accompanying programs and documents for any purpose, provided
% this copyright notice is retained and prominently displayed, along with
% a note saying that the original programs are available from our
% web page.
% The programs and documents are distributed without any warranty, express or
% implied.  As the programs were written for research purposes only, they have
% not been tested to the degree that would be advisable in any important
% application.  All use of these programs is entirely at the user's own risk.

% This program fine-tunes an autoencoder with backpropagation.
% Weights of the autoencoder are going to be saved in mnist_weights.mat
% and trainig and test reconstruction errors in mnist_error.mat
% You can also set maxepoch, default value is 200 as in our paper. 

maxepoch=2; %maxepoch=200;
fprintf(1,'\nTraining discriminative model on MNIST by minimizing cross entropy error. \n');
fprintf(1,'60 batches of 1000 cases each. \n');

load mnistvhclassify
load mnisthpclassify
load mnisthp2classify

makebatches;
[numcases numdims numbatches]=size(batchdata); % 100  784  600
N=numcases; % 100

%%%% PREINITIALIZE WEIGHTS OF THE DISCRIMINATIVE MODEL%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

w1=[vishid; hidrecbiases]; % 784 250 + 1 250 = 785 250
w2=[hidpen; penrecbiases]; % 250 250 + 1 250 = 251 250
w3=[hidpen2; penrecbiases2]; % 250 500 + 1 500 = 251 500
w_class = 0.1*randn(size(w3,2)+1,10); % randn(501,10)  = 501 10


%%%%%%%%%% END OF PREINITIALIZATION OF WEIGHTS  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

l1=size(w1,1)-1; % 784
l2=size(w2,1)-1; % 250
l3=size(w3,1)-1; % 250
l4=size(w_class,1)-1; % 500
l5=10;
test_err=[];
train_err=[];


for epoch = 1:maxepoch

 %%%%%%%%%%%%%%%%%%%% COMPUTE TRAINING MISCLASSIFICATION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 err=0;
 err_cr=0;
 counter=0;
 [numcases numdims numbatches]=size(batchdata); % 100  784  600
 N=numcases; % 100
 for batch = 1:numbatches  % 1 : 600
  data = [batchdata(:,:,batch)]; % 100  784
  target = [batchtargets(:,:,batch)]; % 100 10 600
  data = [data ones(N,1)]; % 100 785
  w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs  ones(N,1)]; % 100 785 * 785 250 += 100 251
  w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; % 100 251 * 251 250 += 100 251
  w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs  ones(N,1)]; % 100 251 * 251 500 += 100 501
  targetout = exp(w3probs*w_class); % 100 501 * 501 10 = 100 10
  targetout = targetout./repmat(sum(targetout,2),1,10); % 100 10 = 100 10 ./ repmat ( 100 1), 1 10) = 100 10

  [I J]=max(targetout,[],2);  % 100 1 , 100 1 = 100 1 -->I has the value J has the sequence
  [I1 J1]=max(target,[],2);  % max(100 10,[],2) 100 1
  counter=counter+length(find(J==J1));  % =6 for the first batch
  err_cr = err_cr- sum(sum( target(:,1:end).*log(targetout))) ;  %cross entrophy

 end
 train_err(epoch)=(numcases*numbatches-counter); % total number of errors for all the batches in this epoche
 train_crerr(epoch)=err_cr/numbatches; % total cross enthropy error for the complete batchdata in this epoche

 %%%%%%%%%%%%%% END OF COMPUTING TRAINING MISCLASSIFICATION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 %%%%%%%%%%%%%%%%%%%% COMPUTE TEST MISCLASSIFICATION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 err=0;
 err_cr=0;
 counter=0;
 [testnumcases testnumdims testnumbatches]=size(testbatchdata); % 100 784 100
 N=testnumcases; % 100
 for batch = 1:testnumbatches % 1: 100
  data = [testbatchdata(:,:,batch)]; % 100 784
  target = [testbatchtargets(:,:,batch)]; % 100 10 = (100 10 100(:,:,batch)
  data = [data ones(N,1)]; % 100 785
  w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs  ones(N,1)]; % 100 785 *785 250 = 100 250 -> 100 251
  w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; % 100 251 * 251 250 = 100 250 -> 100 251
  w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs  ones(N,1)]; % 100 251 * 251 50 = 100 50 -> 100 51
  targetout = exp(w3probs*w_class); % 100 51 * 51 10 = 100 10
  targetout = targetout./repmat(sum(targetout,2),1,10); % = 100 10 ./ repmat ( 100 1), 1 10) = 100 10

  [I J]=max(targetout,[],2); % 100 1 , 100 1 = 100 1 -->I has the value J has the sequence
  [I1 J1]=max(target,[],2); % max(100 10,[],2) 100 1
  counter=counter+length(find(J==J1));  % =9 for the first batch
  err_cr = err_cr- sum(sum( target(:,1:end).*log(targetout)));  %cross entrophy
 
 end
 test_err(epoch)=(testnumcases*testnumbatches-counter);  % total number of errors for all the batches in this epoche
 test_crerr(epoch)=err_cr/testnumbatches; % total cross enthropy error for the complete batchdata in this epoche
 fprintf(1,'Before epoch %d Train # misclassified: %d (from %d). Test # misclassified: %d (from %d) \t \t \n',...
            epoch,train_err(epoch),numcases*numbatches,test_err(epoch),testnumcases*testnumbatches);

 %%%%%%%%%%%%%% END OF COMPUTING TEST MISCLASSIFICATION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 tt=0;
 for batch = 1:numbatches/10
  fprintf(1,'epoch %d batch %d\n',epoch,batch);

  %%%%%%%%%%% COMBINE 10 MINIBATCHES INTO 1 LARGER MINIBATCH %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  tt=tt+1;
  data=[];
  targets=[];
  for kk=1:10
   data=[data
         batchdata(:,:,(tt-1)*10+kk)]; % 1000 784
   targets=[targets
         batchtargets(:,:,(tt-1)*10+kk)];  % 1000 10
  end

 %%%%%%%%%%%%%%% PERFORM CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  max_iter=3;

  if epoch<2 6="" fixed.="" holding="" irst="" nbsp="" original="" other="" p="" top-level="" update="" weights="">    N = size(data,1); % 1000 /1000 784)
    XX = [data ones(N,1)]; % 1000 785
    w1probs = 1./(1 + exp(-XX*w1)); w1probs = [w1probs  ones(N,1)]; % 1000 785 * 785 250 = 1000 250 -> 1000 251
    w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; % 1000 251 * 251 250 = 1000 250 -> 1000 251
    w3probs = 1./(1 + exp(-w2probs*w3)); %w3probs = [w3probs  ones(N,1)]; % 1000 251 * 251 50 = 1000 50 -> 1000 51

    VV = [w_class(:)']'; % 51 10 = 510 1
    Dim = [l4; l5]; % 2 1
    [X, fX] = minimize(VV,'CG_CLASSIFY_INIT',max_iter,Dim,w3probs,targets); % 510 1, 4 1 = min(510 1,'CG_CLASS...',3, 2 1, 1000 51, 1000 10
    w_class = reshape(X,l4+1,l5); %reshape(X,51,10)= 51 10

  else
    VV = [w1(:)' w2(:)' w3(:)' w_class(:)']'; % 272060 1
    Dim = [l1; l2; l3; l4; l5]; % 5 1
    [X, fX] = minimize(VV,'CG_CLASSIFY',max_iter,Dim,data,targets); %[% 272060 1, 4 1]=mini..(272060 1,'CG_CLA..', 3, 5 1, 1000 784, 1000 10);

    w1 = reshape(X(1:(l1+1)*l2),l1+1,l2); %reshape(272060(1 : 785*250,785,250) = 785 250
    xxx = (l1+1)*l2; % 785 * 250
    w2 = reshape(X(xxx+1:xxx+(l2+1)*l3),l2+1,l3); % reshape(X(251 : 251 * 250), 251, 250); = 251 250
    xxx = xxx+(l2+1)*l3; % 785 * 250 + 251 * 250
    w3 = reshape(X(xxx+1:xxx+(l3+1)*l4),l3+1,l4); % reshape(X(xxx+1: xxx+251*50, 251,50) = 251, 50
    xxx = xxx+(l3+1)*l4; % 271550
    w_class = reshape(X(xxx+1:xxx+(l4+1)*l5),l4+1,l5); % 51 10

  end
  %%%%%%%%%%%%%%% END OF CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 end

 save mnistclassify_weights w1 w2 w3 w_class
 save mnistclassify_error test_err test_crerr train_err train_crerr;

end



mnistclassify analysis 1

% Version 1.000
%
% Code provided by Ruslan Salakhutdinov and Geoff Hinton 
%
% Permission is granted for anyone to copy, use, modify, or distribute this
% program and accompanying programs and documents for any purpose, provided
% this copyright notice is retained and prominently displayed, along with
% a note saying that the original programs are available from our
% web page.
% The programs and documents are distributed without any warranty, express or
% implied.  As the programs were written for research purposes only, they have
% not been tested to the degree that would be advisable in any important
% application.  All use of these programs is entirely at the user's own risk.


% This program pretrains a deep autoencoder for MNIST dataset
% You can set the maximum number of epochs for pretraining each layer
% and you can set the architecture of the multilayer net.

clear all
close all

maxepoch=1; % maxepoch=50;
numhid=250; numpen=250; numpen2=50; % numhid=500; numpen=500; numpen2=2000;

fprintf(1,'Converting Raw files into Matlab format \n');
%converter;
dos('erase *.ascii');

fprintf(1,'Pretraining a deep autoencoder. \n');
fprintf(1,'The Science paper used 50 epochs. This uses %3i \n', maxepoch);

makebatches;
[numcases numdims numbatches]=size(batchdata);  % 100 784 600

fprintf(1,'Pretraining Layer 1 with RBM: %d-%d \n',numdims,numhid); % 784 250
restart=1;
rbm;
hidrecbiases=hidbiases; % 1 250
save mnistvhclassify vishid hidrecbiases visbiases; % 784 250, 1 250, 1 784

fprintf(1,'\nPretraining Layer 2 with RBM: %d-%d \n',numhid,numpen); % 250 250
batchdata=batchposhidprobs; % 100 250 600
numhid=numpen; %250
restart=1;
rbm;
hidpen=vishid; penrecbiases=hidbiases; hidgenbiases=visbiases;
save mnisthpclassify hidpen penrecbiases hidgenbiases; %hidpen 250 250, penrecbiases 1 250, hidgenbiases 1 250

fprintf(1,'\nPretraining Layer 3 with RBM: %d-%d \n',numpen,numpen2); % 250 50
batchdata=batchposhidprobs;
numhid=numpen2;
restart=1;
rbm;
hidpen2=vishid; penrecbiases2=hidbiases; hidgenbiases2=visbiases;
save mnisthp2classify hidpen2 penrecbiases2 hidgenbiases2; %hidpen2 250 50, penrecbiases2 1 50, hidgenbiases2 1 250

backpropclassify;