Created
April 10, 2020 18:56
-
-
Save juanmav/22d9524c0573301637a8369e25b4aa53 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(testthat) | |
| calculateConnections <- function(layers) { | |
| l <- length(layers) | |
| total <- 0 | |
| for(i in 1:(l-1)) { | |
| total <- total + layers[i] * layers[i+1] | |
| } | |
| return(total) | |
| } | |
| calculateBiases <- function(layers) { | |
| l <- length(layers) | |
| total <- 0 | |
| for(i in 2:l) { | |
| total <- total + layers[i] | |
| } | |
| return(total) | |
| } | |
| calculateSkipConnections <- function(layers){ | |
| l <- length(layers) | |
| return(layers[1] * layers[l]) | |
| } | |
| calculateFromParameters <- function(layers, skip=FALSE) | |
| { | |
| params = calculateConnections(layers) + calculateBiases(layers) | |
| if(skip){ | |
| return(params + calculateSkipConnections(layers)) | |
| } else { | |
| return(params) | |
| } | |
| } | |
| test_that("Simple Network", { | |
| layers <- c(3,4,2) | |
| # Test basic functions | |
| expect_equal(calculateConnections(layers), 20) | |
| expect_equal(calculateBiases(layers), 6) | |
| expect_equal(calculateSkipConnections(layers), 6) | |
| # Test all integrated | |
| expect_equal(calculateFromParameters(layers), 26) | |
| expect_equal(calculateFromParameters(layers, skip=T), 32) | |
| # Another networks | |
| layers <- c(3,5,2) | |
| expect_equal(calculateFromParameters(layers), 32) | |
| expect_equal(calculateFromParameters(layers, skip=T), 38) | |
| # Another networks | |
| layers <- c(50,100,1,100,50) | |
| expect_equal(calculateFromParameters(layers), 10451) | |
| expect_equal(calculateFromParameters(layers, skip=T), 12951) | |
| }) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment