This is the R script used to create and train the neuronal net for the Python project. Please consider that the lines for the export of the neuron weights are disabled by the comment marker #.
################################## # # COMMON SAMPLE DATA HANDLING # ################################## load_samples = function(filename, attrib) { data = read.csv(filename, sep="\t") # Remove result column data=data[,-5] # Calculate all combination with count of measure points n = aggregate(x ~ run + subset, data = data, FUN=length) # Keep only measurement with 80 points n = n[n[,3] == 80,] # Create the training data matrix for non-matches dataset = matrix(ncol=80, nrow=0) # Fill up the training data matrix with the valid cases (n) of data and transpose it for (i in 1:dim(n)[1]) dataset = rbind(dataset, t(data$x[(data$run == n[i, 1]) & (data$subset == n[i, 2])])) # add class column dataset = cbind(c(attrib), dataset) # tidy up remove(data) remove(n) return(dataset); } # Sigmoid function like in Python3 example sigmoid = function(x) { 1 / (1 + exp(-x)) } #test = load_samples("C://...//test.csv", -1); neg = load_samples("C://...//neg.csv", -1); pos = load_samples("C://...//pos.csv", 1); alldata = as.data.frame(rbind(pos, neg)) colnames(alldata) = c("event", paste("n", rep(1:80), sep="")) library(caTools) set.seed(101) # Create Split (any column is fine) split = sample.split(alldata$event, SplitRatio = 0.90) # Split based off of split Boolean Vector train = subset(alldata, split == TRUE) test = subset(alldata, split == FALSE) feats <- names(alldata[,-1]) # Concatenate strings f = paste(feats, collapse=' + ') f = paste('event ~',f) # Convert to formula f <- as.formula(f) library(neuralnet) # Works good # nn <- neuralnet(f, train, hidden = c(20, 10, 4), linear.output = TRUE) #nn <- neuralnet(f, train, hidden = c(20, 10, 4), act.fct = 'logistic') nn <- neuralnet(f, train, hidden = c(40, 20, 4), act.fct = 'logistic') # Compute Predictions off Test Set pred <- compute(nn, test[2:81]) #pred <- compute(nn, test[1, 2:81]) pred$net.result = (pred$net.result > apply(pred$net.result, 2, mean))*1 pred$net.result[pred$net.result == 0] = -1 #pred$net.result <- sapply(pred$net.result, round, digits = 0) # Check of accuracy by the confusion matrix. table(test$event, pred$net.result) # Try to plot the neuronal net ... try. ;) # plot(nn, show.weights=FALSE, radius=0.05, x.entry=0.15, intercept=FALSE) # neural net weights export #write.csv(nn$weights[[1]][[1]][1:81,], file = "C://...//nnlayer1.csv", row.names = FALSE) #write.csv(nn$weights[[1]][[2]][1:41,], file = "C://...//nnlayer2.csv", row.names = FALSE) #write.csv(nn$weights[[1]][[3]][1:21,], file = "C://...//nnlayer3.csv", row.names = FALSE) #write.csv(nn$weights[[1]][[4]][1:5,], file = "C://...//nnlayer4.csv", row.names = FALSE) #nn$weights[[1]][[1]][1,]
Kommentare
Kommentar veröffentlichen