Our program

With our main.cs file filled out, here's how it will look. As this is a microservice that may run multiple instances for performance reasons when training the network, I have added leadership capabilities via Topshelf.Leader. I have highlighted the relevant sections of code that pertain to this:

static void Main(string[] args)
{
var builder = new ContainerBuilder();
// Service itself
builder.RegisterType<MLMicroService>()
.AsImplementedInterfaces()
.AsSelf()
?.InstancePerLifetimeScope();
builder.RegisterType<Logger>().SingleInstance();
var container = builder.Build();
XmlConfigurator.ConfigureAndWatch(new FileInfo(@".log4net.config"));
HostFactory.Run(c =>
{
c?.UseAutofacContainer(container);
c?.UseLog4Net();
c?.ApplyCommandLineWithDebuggerSupport();
c?.EnablePauseAndContinue();
c?.EnableShutdown();
c?.OnException(ex => { Console.WriteLine(ex.Message); });
c?.UseWindowsHostEnvironmentWithDebugSupport();
c?.Service<MLMicroService>(s =>
{
s.ConstructUsingAutofacContainer<MLMicroService>();
s?.ConstructUsing(settings =>
{
var service = AutofacHostBuilderConfigurator.LifetimeScope.Resolve<MLMicroService>();
return service;
});
s?.ConstructUsing(name => new MLMicroService());
s?.WhenStartedAsLeader(b =>
{
b.WhenStarted(async (service, token) =>
{
await service.Start(token);
});
b.Lease(lcb => lcb.RenewLeaseEvery(TimeSpan.FromSeconds(2))
.AquireLeaseEvery(TimeSpan.FromSeconds(5))
.LeaseLength(TimeSpan.FromDays(1))
.WithLeaseManager(new MLMicroService()));
b.WithHeartBeat(TimeSpan.FromSeconds(30), (isLeader, token) => Task.CompletedTask);
b.Build();
});
s?.WhenStarted((MLMicroService server, HostControl host) => server.OnStart(host));
s?.WhenPaused(server => server.OnPause());
s?.WhenContinued(server => server.OnResume());
s?.WhenStopped(server => server.OnStop());
s?.WhenShutdown(server => server.OnShutdown());
});
c?.RunAsNetworkService();
c?.StartAutomaticallyDelayed();
c?.SetDescription(string.Intern("Machine Learning Microservice Sample"));
c?.SetDisplayName(string.Intern("MachineLearningMicroservice"));
c?.SetServiceName(string.Intern("MachineLearningMicroService"));
c?.EnableServiceRecovery(r =>
{
r?.OnCrashOnly();
r?.RestartService(1); //first
r?.RestartService(1); //second
r?.RestartService(1); //subsequents
r?.SetResetPeriod(0);
});
});
}

For this microservice, we have our message titled MLMessage. This message takes a different turn from previous messages in that it allows for four parameters, two reply values and two reply messages. You see, we really don't know what is going to be required each step of the way. Each step in training or configuring our machine learning microservice takes different parameters and produces different results. By leaving our message definition flexible, we put the onus on the caller for populating parameters correctly, and then the implementor of our microservice to interpret them together. Consider it a contract between developers, not classes. Our MLMessage looks as follows:

[Serializable]
[Queue("MachineLearning", ExchangeName = "EvolvedAI")]
public class MLMessage
{
public int MessageType { get; set; }
public int LayerType { get; set; }
public double param1 { get; set; }
public double param2 { get; set; }
public double param3 { get; set; }
public double param4 { get; set; }
public double replyVal1 { get; set; }
public double replyVal2 { get; set; }
public string replyMsg1 { get; set; }
public string replyMsg2 { get; set; }
}

Moving on from the message, let's take a look at what makes our OnStart method unique. The highlighted lines, of which there are many, are publishing configuration messages to the machine learning microservice. Once all the parameters are created properly, the network is trained and our results are published. Normally, this code would not go here. I only put it here so that as soon as the microservice starts (the same applies with other microservices in the book), it immediately sends out a message relative to what it does. I will leave it as an exercise to you, the reader, to put this in its proper place and call it when and how required.

When we are finished, our CNN will look as follows:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset