THE EDGE OF ML: Introducing Adversaric a GAN(Generative Adversarial Network) in Mojo

Note: This is an ongoing project-more like a prototype not a full build.

Traditional NN’s (e.g RNN’s and CNN’s)have long dominated the world of ML and AI running as a single Neural Network in layers. GAN’s give a significantly different edge by having 2 Networks competing against each other consisting of a Generator and a Discriminator. Introducing this in Mojo shows that Mojo is capable of diving in to infrastructures like high resiliance cyber systems. And I’m really interested and looking forward to implementing this in Mojo.

This simpleNet using PyTorch via Interop shows how 2 Neural Networks can compete against each other.

from std.python import Python,PythonObject

def create_generator(torch: PythonObject, nn: PythonObject, latent_dim: Int = 64)        

    return nn.Sequential(
        nn.Linear(latent_dim, 128),
        nn.LeakyReLU(0.2),
        nn.Linear(128, 256),
        nn.BatchNorm1d(256),
        nn.LeakyReLU(0.2),
        nn.Linear(256, 512),
        nn.BatchNorm1d(512),
        nn.LeakyReLU(0.2),
        nn.Linear(512, 1 * 28 * 28),
        nn.Tanh()  # Normalizes output pixel values to [-1, 1]
    )

    var torch = Python.import_module("torch")
    var nn = Python.import_module("torch.nn")
    var optim = Python.import_module("torch.optim")


    var ns = build_models(torch, nn)
    var generator = ns["generator"]
    var discriminator = ns["discriminator"]
    var criterion = nn.BCELoss()
    var lr: PythonObject = 0.0002
    var betas = Python.evaluate("(0.5, 0.999)")

    var opt_g = optim.Adam(generator.parameters(), lr=lr, betas=betas)
    var opt_d = optim.Adam(discriminator.parameters(), lr=lr, betas=betas)
```

Note: How this is defined doesn’t clarify a complete Neural Network this is just a simple snippet displaying the architecture of a GAN. (May contain wrong code).

This snippet describes a sequential process in which a generator takes a script or any input - an image for our case then performs scans as the first network then uses a generative cycle to send the response to the discriminator where the discriminator evaluates the respond,analyzes for flaws and if the generator made an error in the first output if flags the output as wrong then goes through the cycle again.

var opt_g = optim.Adam(generator.parameters(), lr=lr, betas=betas)
    var opt_d = optim.Adam(discriminator.parameters(), lr=lr, betas=betas)```

As for this opt_gdescribes the generator response to the image. And opt_ddescribes the discriminator input cycle.

The Purpose:

This Project Adversaric is designed is built to display Mojo’s inherent capabilities deep into the field of Adversarial Machine Learning,Minimal-Error Prone and Fault Tolerant systems designed to embed it’s AI capabilities. I’m also very interested in utilizing MAX to build this Generative Adversarial Networks rather than just interop. I shall inform everyone once I’ve completed the project but the real question is whether anyone is interested in this?