WOLFRAM NOTEBOOK

Machine Learning, Neural Networks, and LLMs in Wolfram Mathematica

Retrieving and Shaping Data

This data will be used a couple times throughout:
In[]:=
rawdata=EntityValue[FilteredEntityClass["Galaxy",EntityFunction[g,MemberQ[{"elliptical","spiral","barred spiral"},g["Shape"]]]],EntityFunction[e,e["Image"]->e["Shape"]]];
In[]:=
readydata=Select[rawdata,Information[First[#],"ImageDimensions"]==={150,150}&&Information[First[#],"ColorSpace"]==="RGB"&];
In[]:=
rawimages=Map[First,readydata];
In[]:=
cropped=Map[ImageCrop[#,{64,64}]&,rawimages];
In[]:=
Take[cropped,10]
Out[]=
,
,
,
,
,
,
,
,
,
Functions for Statistics and Built-In Machine Learning

Regression and Modeling

Many methods here fall under “traditional” machine learning that pre-date neural networks
Create fitted models from data:
data=
Data
;model=LinearModelFit[data,{1,x,
2
x
,
3
x
,
4
x
},x]
Out[]=
FittedModel
0.173963+5.21307x-1.71265
2
x
+0.269347
3
x
-0.0135618
4
x
Use computed properties of a fitted model:
mae=Mean[Abs[model["FitResiduals"]]]
Out[]=
0.696983
Visualize the model and original data:
ShowPlot{model[x],model[x]+2mae,model[x]-2mae},{x,0,10},
Options
,ListPlot[data,PlotStyle->Red]
Out[]=
Create predictor functions using a variety of methods:
pf=Predict[data[[All,1]]->data[[All,2]]]
Out[]=
PredictorFunction
Input type: Numerical
Method: NearestNeighbors
Use computed properties of the predictor function:
σ[x_]:=StandardDeviation[pf[x,"Distribution"]];ShowPlot{pf[x],pf[x]+2σ[x],pf[x]-2σ[x]},{x,0,10},
Options
,ListPlot[data,PlotStyle->Red]
Out[]=

Cluster Analysis

Find clusters in data:
ListPlotFindClusters
GaussianDisks
Out[]=
Specify method and other options for greater control:
ListPlotFindClusters
Rings
,Method->"DBSCAN"
Out[]=
Analyze clusters in the context of graphs:
CommunityGraphPlot[ExampleData[{"NetworkGraph","JazzMusicians"}]]
Out[]=
Analyze components in images:
ClusteringComponents
,7//Colorize
Out[]=

Dimensionality Reduction

A color function shows that the structure of this data can be embedded in a lower dimension space:
Create a dimension reducing function for this dataset:
Apply the dimension reducing function to the data:

Detect Anomalous Data

Retrieve information from a classic dataset:
Create an anomaly detection function:
Use the detector function to find anomalies in the dataset:
Visualize the data and decision boundary:

Impute Values for Missing Data

Simulate missing data by removing information from existing data:
Learn a distribution to predict missing values:
Synthesize missing values and plot the results:

Computer Vision

Highlight objects in an image:
Import pre-trained semantic segmentation networks form the Wolfram Neural Net Repository:
Use the imported network to perform semantic segmentation and colorize the results:

Signal Processing

Common transforms and signal processing techniques are also worth mentioning for this audience:
Apply common filters and transforms to clean a signal:
For those of you who are interested in audio signals specifically, these are also supported:
Import an audio file:
Detect activity specified by a particular parameter, in this case detecting a speaking voice:
Plot the active intervals with red rectangles:
Working with Neural Networks
This example builds up to the task of synthetic data generation. To read more about Generative Adversarial Networks, check out this blog post:
Generative Adversarial Networks (GANs) in the Wolfram Language
For many other applications of neural networks to astronomy, Smith and Geach (2023) have a great review article:
https://royalsocietypublishing.org/doi/10.1098/rsos.221454

Building Blocks of Neural Networks

Neural networks are made of layers. Many different kinds of specialized layers exist for particular architectures.
With some restrictions on the functions supported, you can define (almost) any function that you like applied to the inputs:

Composing a Network

In Wolfram Language, layers are composed into either a NetChain or NetGraph

Using a Generative Adversarial Network (GAN) for Synthetic Data

Define a Generator network (creates synthetic data from noise)
Define a Discriminator network (decides if data is real or synthetic)
Combine the two into a GAN architecture

Training a Network

Using an autoencoder to get a reasonable mapping of data to a latent space:

Define an autoencoder
Train it
Get the trained network:
This imports the autoencoder trained before the presentation. Remember to export your networks. ONNX or WLNET file formats are possibly the most convenient for this.

Note: For many purposes, you may want a variational autoencoder (VAE) instead

You can read more about implementing those in this community post:
https://community.wolfram.com/groups/-/m/t/1379189
Use the pre-trained autoencoder to give the GAN training a head start:
Define a series of functions for generating the training data. The training data must include the real images and latent data from a distribution:
In other words, each real image is paired with some noise from the latent space:
Finally, train the GAN. This mini-example took ~1 hour to train on a GPU.
Create a collage of synthetic galaxy images. Starting with a VAE, using larger networks, or more high quality training examples are next steps to improve a quick prototype to a part of a pipeline.
Remember you will want to export your trained network to save the weights and biases after all the training time has been spent:

Neural Net Repository and Net Surgery

Rather than training networks from scratch or redesigning your own architecture, it is often helpful to leverage a pre-trained network.
You can find many pre-trained networks for various tasks in the Wolfram Neural Net Repository:
https://resources.wolframcloud.com/NeuralNetRepository

See if the learned representations of another network can be used for a new task

Replace only the last layers to change the number of classes represented by the network from 1000 down to 3:
Split the data into test and training sets for this supervised learning task:
Train only the new layers of the network to keep all the previously learned representations
Remember to Export and then Import the previously trained network between sessions:
See how the classifier performed:
Notice how with this very small training run, transfer learning failed to capture the difference between spiral and barred spiral galaxies; however it did capture the difference between elliptical and all types of spiral structure reasonably well:
Tools for Use with Large Language Models
Last but not least, LLMs can be a helpful part of workflows:

LLMs for Code Assistance

Have the LLM act as a code assistant to work through an existing example.
If you want to read more about solid mechanics, you can start with this tutorial (link).
For an overview of PDE modeling in a variety of areas, check out this overview (link).
Please explain the code above to a student new to Wolfram Language. Be concise.
Using this example, what would the student do fix the right end of the beam as well as the left end? Make this adjustment to the code and use only gamma to represent the boundary condition. Reproduce all the code with these adjustments.

Data Retrieval

Write code to create a dataset that contains the "Image" entity property and "HubbleType" entity property for 20 randomly selected galaxies using RandomEntity. Use DocumentationLookup tool if needed.
Wolfram Cloud

You are using a browser not supported by the Wolfram Cloud

Supported browsers include recent versions of Chrome, Edge, Firefox and Safari.


I understand and wish to continue anyway »

You are using a browser not supported by the Wolfram Cloud. Supported browsers include recent versions of Chrome, Edge, Firefox and Safari.