public void ModelGradients3() { Matrix <double> in_hid = Matrix <double> .Build.DenseOfArray( new double[, ] { { 1 }, { 2 } } ); Matrix <double> hid_class = Matrix <double> .Build.DenseOfArray( new double[, ] { { 1, 0 }, { 0, 1 } } ); Matrix <double> input = Matrix <double> .Build.DenseOfArray( new double[, ] { { 1 } } ); Matrix <double> target = Matrix <double> .Build.DenseOfArray( new double[, ] { { 1 }, { 0 } } ); MutableTypes.Model networkModel = new MutableTypes.Model(in_hid, hid_class); MutableTypes.NetworkState networkState = NetRun.RunNetwork(networkModel, input); MutableTypes.Model gradients = Training._getModelGradients(networkModel, networkState, target); double l1 = NetworkFunctions.Logistic(1); double l2 = NetworkFunctions.Logistic(2); double el1 = Math.Exp(l1); double el2 = Math.Exp(l2); double N = el1 + el2; double el1N = el1 / N; double el2N = el2 / N; double ld1 = NetworkFunctions.LogisticDerivative_f_output(l1); double ld2 = NetworkFunctions.LogisticDerivative_f_output(l2); Matrix <double> hid_class_gradient_check = Matrix <double> .Build.DenseOfArray( new double[, ] { { l1 *(el1N - 1), l2 *(el1N - 1) }, { l1 *el2N, l2 *el2N } } ); Matrix <double> in_hid_gradient_check = Matrix <double> .Build.DenseOfArray( new double[, ] { { ld1 *(el1N - 1) }, { ld2 *el2N } } ); Assert.AreEqual(hid_class_gradient_check, gradients.HiddenToClassifier); Assert.AreEqual(in_hid_gradient_check, gradients.InputToHidden); }
public void ModelGradients2() { Matrix <double> in_hid = Matrix <double> .Build.DenseOfArray( new double[, ] { { 1 } } ); Matrix <double> hid_class = Matrix <double> .Build.DenseOfArray( new double[, ] { { 1 }, { 1 } } ); Matrix <double> input = Matrix <double> .Build.DenseOfArray( new double[, ] { { 1 } } ); Matrix <double> target = Matrix <double> .Build.DenseOfArray( new double[, ] { { 1 }, { 0 } } ); MutableTypes.Model networkModel = new MutableTypes.Model(in_hid, hid_class); MutableTypes.NetworkState networkState = NetRun.RunNetwork(networkModel, input); MutableTypes.Model gradients = Training._getModelGradients(networkModel, networkState, target); Matrix <double> hid_class_gradient_check = Matrix <double> .Build.DenseOfArray( new double[, ] { { -NetworkFunctions.Logistic(1) / 2 }, { NetworkFunctions.Logistic(1) / 2 } } ); Matrix <double> in_hid_gradient_check = Matrix <double> .Build.DenseOfArray( new double[, ] { { 0 } } ); Assert.AreEqual(hid_class_gradient_check, gradients.HiddenToClassifier); Assert.AreEqual(in_hid_gradient_check, gradients.InputToHidden); }