Un­der­stand­ing How a Neu­ral Net­work Works Us­ing R

In the sim­plest of terms, a neu­ral net­work is a com­puter sys­tem modelled on the hu­man ner­vous sys­tem. It is widely used in ma­chine learn­ing tech­nol­ogy. R is an open source pro­gram­ming lan­guage, which is mostly used by statis­ti­cians and data min­ers. It gre

OpenSource For You - - Open Gurus - By: Di­pankar Ray The au­thor is a mem­ber of IEEE, IET, with more than 20 years of ex­pe­ri­ence in open source ver­sions of UNIX op­er­at­ing sys­tems and Sun So­laris. He is presently work­ing on data anal­y­sis and ma­chine learn­ing us­ing a neu­ral net­work and dif­fere

The neu­ral net­work is the most widely used ma­chine learn­ing tech­nol­ogy avail­able to­day. Its al­go­rithm mim­ics the func­tion­ing of the hu­man brain to train a com­pu­ta­tional net­work to iden­tify the in­her­ent pat­terns of the data un­der in­ves­ti­ga­tion. There are sev­eral vari­a­tions of this com­pu­ta­tional net­work to process data, but the most com­mon is the feed­for­ward-back­prop­a­ga­tion con­fig­u­ra­tion. Many tools are avail­able for its im­ple­men­ta­tion, but most of them are ex­pen­sive and pro­pri­etary. There are at least 30 dif­fer­ent pack­ages of open source neu­ral net­work soft­ware avail­able, and of them, R, with its rich neu­ral net­work pack­ages, is much ahead.

R pro­vides this ma­chine learn­ing en­vi­ron­ment un­der a strong pro­gram­ming plat­form, which not only pro­vides the sup­port­ing com­pu­ta­tion par­a­digm but also of­fers enor­mous flex­i­bil­ity on re­lated data pro­cess­ing. The open source ver­sion of R and the sup­port­ing neu­ral net­work pack­ages are very easy to in­stall and also com­par­a­tively sim­ple to learn. In this ar­ti­cle, I will demon­strate ma­chine learn­ing us­ing a neu­ral net­work to solve qua­dratic equa­tion prob­lems. I have cho­sen a sim­ple prob­lem as an ex­am­ple, to help you learn ma­chine learn­ing con­cepts and un­der­stand the train­ing pro­ce­dure of a neu­ral net­work. Ma­chine learn­ing is widely used in many ar­eas, rang­ing from the di­ag­no­sis of dis­eases to weather fore­cast­ing. You can also ex­per­i­ment with any novel ex­am­ple, which you feel can be in­ter­est­ing to solve us­ing a neu­ral net­work.

Qua­dratic equa­tions

The ex­am­ple I have cho­sen shows how to train a neu­ral net­work model to solve a set of qua­dratic equa­tions. The general form of a qua­dratic equa­tion is: ax2 + bx +c = 0. At the out­set, let us con­sider three sets of co­ef­fi­cients a, b and c and cal­cu­late the cor­re­spond­ing roots r1 and r2.

The co­ef­fi­cients are pro­cessed to elim­i­nate the linear equa­tions with neg­a­tive val­ues of the dis­crim­i­nant, i.e., when b2 – 4ac < 0. A neu­ral net­work is then trained with these data sets. Co­ef­fi­cients and roots are nu­mer­i­cal vec­tors, and they have been con­verted to the data frame for fur­ther op­er­a­tions.

The ex­am­ple of train­ing data sets con­sists of three co­ef­fi­cients with 10 val­ues each:

aa<-c(1, 1, -3, 1, -5, 2,2,2,1,1) bb<-c(5, -3, -1, 10, 7, -7,1,1,-4,-25) cc<-c(6, -10, -1, 24, 9, 3,-4,4,-21,156)

Data pre­pro­cess­ing

To dis­card equa­tions with zero as co­ef­fi­cients of x2, use the fol­low­ing code:

k <- which(aa != 0) aa <-aa[k] bb <-bb[k] cc <-cc[k]

To ac­cept only those co­ef­fi­cients for which the dis­crim­i­nant is zero or more, use the code given be­low:

disc <-(bb*bb-4*aa*cc) k <- which(disc >= 0) aa <-aa[k] bb <-bb[k] cc <-cc[k] a <- as.data.frame(aa) con­verted to data frame b <- as.data.frame(bb) c <- as.data.frame(cc) # a,b,c vec­tors are

Cal­cu­late the roots of valid equa­tions us­ing con­ven­tional for­mu­lae, for train­ing and ver­i­fi­ca­tion of the ma­chine’s results at a later stage.

r1 <- (-b + sqrt(b*b-4*a*c))/(2*a) # r1 and r2 roots of each equa­tions r2 <- (-b - sqrt(b*b-4*a*c))/(2*a)

After get­ting all the co­ef­fi­cients and roots of the equa­tions, con­cate­nate them colum­n­wise to form the in­putout­put data sets of a neu­ral net­work.

train­ing­data <- cbind(a,b,c,r1,r2)

Since this is a sim­ple prob­lem, the net­work is con­fig­ured with three nodes in the in­put-layer, one hid­den-layer with seven nodes and a two-node out­put-layer.

R func­tion neu­ral­net() re­quires in­put-out­put data in a proper for­mat. The for­mat of for­mu­la­tion pro­ce­dure is some­what tricky and re­quires at­ten­tion. The right hand side of the for­mula con­sists of two roots and the left side in­cludes three co­ef­fi­cients a, b and c. The in­clu­sion is rep­re­sented by + signs.

col­names(train­ing­data) <- c(“a”,”b”,”c”,”r1”,”r2”)

net.quad­root <- neu­ral­net(r1+r2~a+b+c, train­ing­data, hid­den=7, thresh­old=0.0001)

An ar­bi­trary per­for­mance thresh­old value 10-4 is taken and this can be ad­justed as per re­quire­ments.

The con­fig­u­ra­tion of the just-con­structed model with its train­ing data can be vi­su­alised with a plot func­tion.

#Plot the neu­ral net­work plot(net.quad­root)

Now it is time to ver­ify the neu­ral net with a set of un­known data. Ar­bi­trar­ily take a few sets of val­ues of three co­ef­fi­cients for the cor­re­spond­ing qua­dratic equa­tions, and ar­range them as data frames as shown be­low.

x1<-c(1, 1, 1, -2, 1, 2) x2<-c(5, 4, -2, -1, 9, 1) x3<-c(6, 5, -8, -2, 22, -3)

Since there is no zero co­ef­fi­cient cor­re­spond­ing to x2, we can ac­cept only those co­ef­fi­cients for which the dis­crim­i­nant is zero or more than zero.

disc <-(x2*x2-4*x1*x3) k <- which(disc >= 0) x1 <-x1[k] x2 <-x2[k] x3 <-x3[k] y1=as.data.frame(x1) y2=as.data.frame(x2) y3=as.data.frame(x3)

The val­ues are then fed to the just-con­fig­ured neu­ral model net.quad­root to pre­dict their roots. The pre­dicted roots are col­lected into net.re­sult$net.re­sult and can be dis­played with the print() func­tion.

test­data <- cbind(y1,y2,y3) net.results <- com­pute(net.quad­root, test­data) #Lets see the results print(net.results$net.re­sult)

Now, how does one ver­ify the results? To do this, let us com­pute the roots us­ing the con­ven­tional root cal­cu­la­tion for­mula, and ver­ify the results by com­par­ing the pre­dicted val­ues with them.

Cal­cu­late the roots and con­cate­nate them into a data frame.

calr1 <- (-y2 + sqrt(y2*y2-4*y1*y3))/(2*y1) calr2 <- (-y2 - sqrt(y2*y2-4*y1*y3))/(2*y1) r<-cbind(calr1,calr2) #Cal­cu­lated roots us­ing for­mula

Then com­bine the test data, its roots and the pre­dicted roots into a data frame for a de­cent tab­u­lar dis­play for ver­i­fi­ca­tion (Ta­ble 1).

#Com­bine In­puts, Ex­pected roots and pre­dicted roots. com­bout­put <- cbind(test­data,r,net.results$net.re­sult) #Put some ap­pro­pri­ate col­umn head­ing col­names(com­bout­put) <- c(“a”,”b”,”c”,”r1”,”r2”,”prer1”,”pre-r2”) print(com­bout­put)

It is clear from the above out­puts that our neu­ral net­work has learnt ap­pro­pri­ately and pro­duces an al­most cor­rect re­sult. You may need to run the neu­ral net­work sev­eral times with the given pa­ram­e­ters to achieve a cor­rect re­sult. But if you are lucky, you may get the right re­sult in the first at­tempt it­self!

a b c l1 l2 l3 B1 H1 H2 H3 H4 H5 H6 H7 B2 01 r1 02 r2 Fig­ure 1: Neu­ral net­work model of the qua­dratic equa­tion solver

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.