[R]Logistic Regression in mxnetR, doesn't learn the parameters
I'm doing a logistc regression for a biomedical data to predict an adverse event. I want to adjust a simple logistic regression model with mxnetR, but the model doesn't learn anything in my data. I supos that my architecture is wrong. I put here an simulated example and I fitted a simple glm model with the base R library and it works. Do you know whats the problem?
dades <- data.frame(x=c(rnorm(1000, mean = 10, sd = 20), rnorm(1000, mean = 40, sd = 30)), y=(c(rep(0, 1000), rep(1, 1000))))
model <- glm(y~x, family=binomial(), data=dades)
summary(model)
pred <- as.numeric(predict(model, type='response')>0.5)
confusion_matrix <- table(pred, dades$y)
sum(diag(confusion_matrix))/sum(confusion_matrix)
library(mxnet)
library(magrittr)
dades <- data.matrix(dades)
LR <- mx.symbol.Variable("data") %>%
mx.symbol.FullyConnected(num_hidden=1) %>%
mx.symbol.LogisticRegressionOutput()
mx.set.seed(0)
model <- mx.model.FeedForward.create(LR,
X=dades[,1],
y=dades[,2],
ctx=mx.cpu(),
num.round=10000,
array.batch.size=100,
learning.rate=0.000001,
momentum=0.4,
eval.metric=mx.metric.accuracy)
I think this is the same as https://github.com/dmlc/mxnet/issues/6546.
Try this
library(mxnet)
library(magrittr)
mx.set.seed(0)
LR <- mx.symbol.Variable("data") %>%
mx.symbol.FullyConnected(num_hidden = 1) %>%
mx.symbol.LogisticRegressionOutput()
train.x <- data.matrix(dades[, 1])
train.y <- dades[, 2]
metric <- mx.metric.custom("acc", function(label, pred) {
mean((pred > 0.5) == label)
})
model <- mx.model.FeedForward.create(LR,
X = train.x,
y = train.y,
ctx = mx.cpu(),
num.round = 10,
array.batch.size = 100,
learning.rate = 0.0001,
momentum = 0.4,
eval.metric = metric,
array.layout = 'rowmajor')
pred <- predict(model, train.x, array.layout = 'rowmajor')
pred <- as.numeric((pred > 0.5) == train.y)
confusion_matrix <- table(pred, train.y)
sum(diag(confusion_matrix)) / sum(confusion_matrix)
Start training with 1 devices
[1] Train-acc=0.608947368421053
[2] Train-acc=0.608
[3] Train-acc=0.608
[4] Train-acc=0.608
[5] Train-acc=0.6075
[6] Train-acc=0.608
[7] Train-acc=0.6085
[8] Train-acc=0.609
[9] Train-acc=0.6095
[10] Train-acc=0.6095
0.796
I think @ryangarner gave the correct answer.
I am closing this now. Feel free to reopen if necessary.
@ryangarner awesome code thank you,
and i want output only probability values, can you please tell me