Skip to content

Instantly share code, notes, and snippets.

@bquast
Created May 14, 2023 21:52
Show Gist options
  • Select an option

  • Save bquast/afecceb34aa7939e9abb4395b50fb4d4 to your computer and use it in GitHub Desktop.

Select an option

Save bquast/afecceb34aa7939e9abb4395b50fb4d4 to your computer and use it in GitHub Desktop.
# DNN2.R
# Bastiaan Quast
# [email protected]
# derivative
der <- function(x){
x * (1-x)
}
# input data
inputs = matrix(c(0,0,1,
0,1,1,
1,0,1,
1,1,1), nrow=4, byrow=TRUE)
# output data
true_values = matrix(c(0,
1,
1,
0), nrow=4)
weights_0 = matrix(runif(n = 12, min=-1, max=1), nrow=3)
weights_1 = matrix(runif(n = 4, min=-1, max=1), nrow=4)
for (j in 1:60000) {
layer_1 = plogis( inputs %*% weights_0 )
layer_2 = plogis( layer_1 %*% weights_1 )
layer_2_update = (true_values - layer_2 ) * der(layer_2)
layer_1_update = layer_2_update %*% t(weights_1) * der(layer_1)
weights_1 = weights_1 + t(layer_1) %*% layer_2_update
weights_0 = weights_0 + t(inputs) %*% layer_1_update
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment