Polynomial graph classifier
Polynomial graph classifier
Here, we will train our neural network to recognize whether an image of a graph represents a 1st degree polynomial, or a 2nd degree polynomial
Let’s start by defining our dataset, and our 2 main functions:
randomequation:=Module[{a,b,p},a=RandomInteger[{1,10}];b=RandomInteger[{1,10}];p=Plot[a*x+b,{x,1,10},PlotRange->All];Image[p,ImageSize->{100,100}]]
In[]:=
random2equation:=Module[{c,d,e,p1},c=RandomInteger[{1,10}];d=RandomInteger[{1,10}];e=RandomInteger[{1,10}];p1=Plot[c*x^2+d*x+e,{x,1,10},PlotRange->All];Image[p1,ImageSize->{100,100}]]
In[]:=
randomExample[n_]:=Module[{f,g},Table[f=RandomInteger[];g=If[f==0,randomequation,random2equation];gIf[f==0,"1 degree","2 degree"],n]]randomExample[2]
Out[]=
2 degree,
2 degree
This is the dataset in itself:
training=randomExample[2000];
The neural network (nn) consists of 1 convolution, to learn the local pixels motifs in the image, with 25 4x4 filters (layer 1), then we will reduce the image to a 4x4 matrix (layer 2), and then convert this into a single vector (layer 3). We connect 64 neurones to this vector (layer 4), then we normalize the neuronal activations (layer 5). We apply a ramp function (layer 6), then we connect this to a layer of 2 neurones (layer 7), and we connect this layer to the exit layer (layer 9). However this net is not suited yet to training
In[]:=
net=NetGraph[{ConvolutionLayer[25,{4,4}],PoolingLayer[{4,4}],FlattenLayer[],LinearLayer[64],BatchNormalizationLayer[],Ramp,LinearLayer[2],SoftmaxLayer[]},{1->2->3->4->5->6->7->8},"Input"NetEncoder[{"Image",{100,100}}],"Output"NetDecoder[{"Class",{"1 degree","2 degree"}}]]
Out[]=
NetGraph
We convert the netgraph into a functioning net
In[]:=
net=NetInitialize[net]
Out[]=
NetGraph
we train our nn with our training data, using our CPU (this is for iOS, for other types, use GPU, into 5 rounds, because our nn is good, and we have a lot of data, we don’t need more)
In[]:=
result=NetTrain[net,training,TargetDevice"CPU",MaxTrainingRounds5]
Out[]=
In[]:=
result
Out[]=
2 degree
In[]:=
validation=randomExample[100];
RandomSample[validation,10]
Out[]=
1 degree,
1 degree,
1 degree,
1 degree,
2 degree,
2 degree,
1 degree,
1 degree,
1 degree,
2 degree
We check whether our nn is good
In[]:=
cm=ClassifierMeasurements[result,validation]
Out[]=
In[]:=
cm["ConfusionMatrixPlot"]
Out[]=
In[]:=
Dataset[Table[<|"image"First[v],"actual"Last[v],"prediction"result[First[v]],"features"Map[Image,ng[First[v]]]|>,{v,validation}]]
sine/triangle sound classifier
sine/triangle sound classifier
We want to train our nn to differentiate a Sine wave sound from a Triangle wave sound
Let’s define our data:
In[]:=
randomtrianglewave:=Module[{freq},freq=RandomReal[{200,20000}];Audio[Table[(2π*ArcSin[Sin[freq*t*2π]])/15,{t,0,1,1/44100}]]]
In[]:=
randomtrianglewave
Out[]=
In[]:=
randomsinewave:=Module[{freq},freq=RandomReal[{200,20000}];Audio[Table[Sin[freq*t*2π],{t,0,1,1/44100}]]]
In[]:=
randomsinewave
Out[]=
In[]:=
randomExample2[n_]:=Module[{f,g},Table[f=RandomInteger[];g=If[f==0,randomsinewave,randomtrianglewave];gIf[f==0,"Sine","Triangle"],n]]
Since we generate sound, the calculations are quite heavy, we’ll only use 200 examples, but that will suffice
We use 2 convolution layers, to make the nn more performing, the first to look at the main features of the sound, and the second to look at the details (the stride is lower)
We normalize twice the neuronal activation to ensure a low error, however, we’ll need more batches to train our nn on
We normalize twice the neuronal activation to ensure a low error, however, we’ll need more batches to train our nn on
Our nn is quite good, because it rarely misclassifies Sine waves, and it never gets a triangle wave wrong.
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
Neural net classifier for polynomial graph recognition and audio sine/triangle wave
by Carlo Réan
Wolfram Community, STAFF PICKS, October 5, 2025
https://community.wolfram.com/groups/-/m/t/3557050
by Carlo Réan
Wolfram Community, STAFF PICKS, October 5, 2025
https://community.wolfram.com/groups/-/m/t/3557050