Skip to content

Instantly share code, notes, and snippets.

@janisz
Created May 16, 2015 19:41
Show Gist options
  • Select an option

  • Save janisz/67bd6e9aa53e5da63497 to your computer and use it in GitHub Desktop.

Select an option

Save janisz/67bd6e9aa53e5da63497 to your computer and use it in GitHub Desktop.
install.packages("nnet")
# Funkcja trenująca, ktra bierze jako argument Y (wektor cyfr)
# oraz X (macierz obrazu) i zwraca obiekt sieć neuronową.
train_ANN = function(X,Y) {
library(nnet) # ładujemy bibliotekę
y <- class.ind(Y) # normalizujemy oczekiwane wyjśćie do postacie macieży zer z jedną jedynką
ANN <- nnet(X,y,size=10,linout=T,maxit=1,MaxNWts=100000) # tworzymy i uczymy sieć
# aby zwiększyć skuteczność nauki należy zwiększyć liczbę iteracji (maxit),
# korzystne może też być zwiększenie liczby neuronów (size)
return(ANN)
}
# Funkcjia testujca, ktra bierze jako argument x (wektor obrazka)
# oraz sieć neuronową zwrconą w poprzednim punkcie i zwraca przewidywaną cyfre.
test_ANN = function(ANN, x ) {
return(which.max(predict(ANN, x))[1]-1) # zwracamy maksymalny index
}
train = read.csv('train.csv', header=TRUE)
X <- train[,2:dim(train)[2]]
Y <- train[,1]
ann <- train_ANN(X, Y)
test_ANN(ann, X[1,])
Y[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment