From 69306e0367ea4c0548ed3757943cc3fb90cd1326 Mon Sep 17 00:00:00 2001 From: Matthias Neeracher Date: Sat, 18 Feb 2017 23:35:45 +0100 Subject: [PATCH] Assignment 3 works --- NNML3/a3.m | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/NNML3/a3.m b/NNML3/a3.m index 354fae0..1f65d9e 100644 --- a/NNML3/a3.m +++ b/NNML3/a3.m @@ -151,12 +151,13 @@ function ret = d_loss_by_d_model(model, data, wd_coefficient) % The returned object is supposed to be exactly like parameter , i.e. it has fields ret.input_to_hid and ret.hid_to_class. However, the contents of those matrices are gradients (d loss by d model parameter), instead of model parameters. - class_input_gradient = (log_class_prob - data.targets) / size(log_class_prob, 2); + error_deriv = class_prob - data.targets; % x + data_size = size(data.targets, 2); % + hidden_grad = model.hid_to_class' * error_deriv .* hid_output .* (1 - hid_output); % hidden x data_size % This is the only function that you're expected to change. Right now, it just returns a lot of zeros, which is obviously not the correct output. Your job is to replace that by a correct computation. - backprop = (model.hid_to_class' * class_input_gradient) .* hid_output .* (1 - hid_output); - ret.input_to_hid = backprop*data.inputs' + wd_coefficient*model.input_to_hid; - ret.hid_to_class = class_input_gradient * hid_output' + wd_coefficient*model.hid_to_class; + ret.input_to_hid = hidden_grad*data.inputs' ./ data_size + wd_coefficient*model.input_to_hid; + ret.hid_to_class = error_deriv*hid_output' ./ data_size + wd_coefficient*model.hid_to_class; end function ret = model_to_theta(model)