##################################################
### see:
# https://www.r-bloggers.com/2021/02/deep-learning-with-r-and-keras-build-a-handwritten-digit-classifier-in-10-minutes/
## for directions on installing keras in R (or google it!!)

## libraries
library(ggplot2)
library(keras)

##################################################
## boston housing data (from keras)
dataset = dataset_boston_housing()

train_x = dataset$train$x
train_y = dataset$train$y

test_x = dataset$test$x
test_y = dataset$test$y

#scale x
mm = apply(train_x,2,mean)
ss = apply(train_x,2,sd)
trX = scale(train_x,center=mm,scale=ss)
teX = scale(test_x,center=mm,scale=ss)

trY = train_y
teY = test_y

# so our train is (trX,trY) and our test is (teX,teY)

# just use x13 = lstat
trX1 = trX[,13,drop=FALSE] #drop = F keeps it a matrix
teX1 = teX[,13,drop=FALSE]

##################################################
### fit model with keras

# create the model and add the layers
numx=1; decay=.00001; nunits=5 # we have 1 x, we will use a single layer with 5 units, and very little decay
model = keras_model_sequential() %>%
   layer_dense( units=nunits, activation="sigmoid",input_shape=c(numx),kernel_regularizer = regularizer_l2(decay)) %>%
   layer_dense(units=1)

# compile the model, we set  the optimizer and the loss(es) here
model %>% compile(optimizer = "rmsprop", loss = "mse", metrics = c("mae"))

#fit the model, training dat, epochs, batch size, validation data
set.seed(99)
fit_history = model %>% fit(trX1,trY,epochs=500,batch_size=16,verbose=1,validation_data = list(teX1,teY))

#plot the iterative fit by epoch
plot(fit_history,smooth=FALSE)

##################################################
### plot fits

## plot the fit
par(mfrow=c(1,2))

#plot the in-sample fit
yhattr = predict(model,trX1)
plot(trX1,trY)
points(trX1,yhattr,col="red")

#plot the out-of-sample fit
yhatte = predict(model,teX1)
plot(teX1,teY)
points(teX1,yhatte,col="blue")

##################################################
## look at weights
print(get_weights(model))

