0

I have an internal project (separated folder) in my MVC App. It contains classes of one of my projects. Then I have one controller for this project and method that initializes it looks like this :

[HttpPost]
    public ActionResult initiate() {
               // System.Diagnostics.Debug.WriteLine("Initiated");
                Network net = new Network(normalizeInput);
                net.train();
               // System.Diagnostics.Debug.WriteLine("average error :" + net.averageError);
                double err = net.averageError;
                ViewBag.err = err.ToString();
                return View("initiate");
        }

and its view :

@{
    ViewBag.Title = "initiate";
}

<h3>Everything is prepared</h3>
<!--<button type="button" onclick="location.href='@Url.Action("initiate", "BackpropController")'"/> -->
@using (Html.BeginForm("initiate", "Backprop", FormMethod.Post)) {
    @Html.AntiForgeryToken()
    <input type="submit" value="Initiate" />
}
<br>
<br>
    @{ string averageError = ViewBag.err;
        if (averageError != null) {
        <h4>@averageError</h4>  } 
        else { <h4>no error</h4>}
        }

The question is : How can I show in the view updated value by each iteration? Because that variable is final one, but method train() contains loop where average error is like this :
averageError += Math.Abs(testingSet[j].desiredOutput - neuron.getOutput());

It interests me because I would like to make graph of error as it would be function of iteration.. simply said :

f(error,iteration)

The graph should be placed in the view I posted and should be updated every iteration situated in method train()

Thanks for the answers.

wesker
  • 1
  • 7

2 Answers2

0

I am not sure if i understand your question correctly.

If you want to pass iteration on your view, you have to either:

  1. pass the train() method return value (if any) to view or
  2. create View model you want to iterate from train() and pass it to view (preferable)

https://stackoverflow.com/a/18608533/5929494

Community
  • 1
  • 1
RizkiDPrast
  • 1,695
  • 1
  • 14
  • 21
  • Thanks for the answer. Its like I want to have a label in view and there should be value (for example number of iteration or value of error) changing every each iteration in method train() I cant pass final value, however the value is adjusting every iteration and I want to make it viewable.. and I need it to make a plot later as I reminded. – wesker Nov 06 '16 at 11:54
0

You need to return the averageError values from your Network class train method to your controller. For example, you could do this with a property in the Network class such as...

class Network {
    //your other code

    public IEnumerable<double> averageErrors {get; set;}

    public void train() {
        averageErrors = new List<double>();

        for (//your loop) {
            averageError += Math.Abs(testingSet[j].desiredOutput - neuron.getOutput());
            averageErrors.Add(averageError);
        }
    }
}

Then in your controller...

[HttpPost]
public ActionResult initiate() {
           // System.Diagnostics.Debug.WriteLine("Initiated");
            Network net = new Network(normalizeInput);
            net.train();
           // System.Diagnostics.Debug.WriteLine("average error :" + net.averageError);
            double err = net.averageError;

            // you now have a list of averageErrors in your controller to use as you please
            List<double> allErrors = net.averageErrors;

            ViewBag.err = err.ToString();
            return View("initiate");
    }

This gives you access to all the average errors after the train method has completed, so you can report this in the view however you like.

If train is a long-running method and you want to show a real-time update of average errors as the method is running, then things will get a bit more complicated. The general approach I would use would be to make the train method asynchronous, but not await it's execution before returning a view. The view will have placeholders, and use some form of client-side code to listen for updates e.g. SignalR. The train method would then periodically send updates to the client, and then finally send the completed data.

Alex Young
  • 4,009
  • 1
  • 16
  • 34