[英]row weights with neuralnet() in R
將 R 中的不同模型與每行具有可變觀察量的數據混為一談。 訓練數據如下所示:
df <- data.frame('name' = c('Jimmy','Greg','Alice','Alice'),
'year' = c(2020, 2020, 2020, 2021),
'high_jump_average' = c(24, 22, 18, 19),
'high_jump_tests' = c(15, 8, 1, 9),
'max_squat' = c(405, 365, 245, 265),
'weight' = c(218, 212, 165, 168))
嘗試根據年初的體重和 max_squat 預測每個人的 high_jump_average(更多功能,只是保持問題簡單)
每個人每年都會進行不同數量的 high_jump_tests。 我正在嘗試根據他們當年將要進行的這些(目前未知的)high_jump_tests 數量來預測他們的 high_jump_average
在模型中,我想根據他們當年進行的 jump_tests 對行進行加權。 例如,在此示例中,我希望模型能夠理解 Alice 的 2020 年結果(僅 1 次測試/觀察)不如 Jimmy 的 2020 年結果(15 次測試/觀察)提供的信息量大
使用線性回歸,我可以使用以下參數:
lm(high_jump_average ~ max_squat + weight,
data = df,
weights = high_jump_tests)
為了這。 但是對於 neuralnet() 我不確定如何表達這一點——目前,我只是根據觀察到的 jump_tests 的數量來復制行:
df <- df %>%
uncount(high_jump_tests)
然后對所有數據進行歸一化,並使用這個新數據幀運行神經網絡()
但這會大大增加行數,並使神經網絡需要一個多小時才能完成。
有沒有辦法在不復制行的情況下做到這一點? 神經網絡()中的一個論點?
提前致謝!
您可以在startweights
參數中分配權重:
包含權重起始值的向量。 設置為 NULL 以進行隨機初始化。
您可以使用此可重現的代碼:
df <- data.frame('name' = c('Jimmy','Greg','Alice','Alice'),
'year' = c(2020, 2020, 2020, 2021),
'high_jump_average' = c(24, 22, 18, 19),
'high_jump_tests' = c(15, 8, 1, 9),
'max_squat' = c(405, 365, 245, 265),
'weight' = c(218, 212, 165, 168))
library(neuralnet)
neuralnet(high_jump_average ~ max_squat + weight, data = df, startweights = c(df$high_jump_tests))
#> $call
#> neuralnet(formula = high_jump_average ~ max_squat + weight, data = df,
#> startweights = c(df$high_jump_tests))
#>
#> $response
#> high_jump_average
#> 1 24
#> 2 22
#> 3 18
#> 4 19
#>
#> $covariate
#> max_squat weight
#> [1,] 405 218
#> [2,] 365 212
#> [3,] 245 165
#> [4,] 265 168
#>
#> $model.list
#> $model.list$response
#> [1] "high_jump_average"
#>
#> $model.list$variables
#> [1] "max_squat" "weight"
#>
#>
#> $err.fct
#> function (x, y)
#> {
#> 1/2 * (y - x)^2
#> }
#> <bytecode: 0x7f7e6876c2e8>
#> <environment: 0x7f7e6876eb70>
#> attr(,"type")
#> [1] "sse"
#>
#> $act.fct
#> function (x)
#> {
#> 1/(1 + exp(-x))
#> }
#> <bytecode: 0x7f7e68760fc0>
#> <environment: 0x7f7e687645d8>
#> attr(,"type")
#> [1] "logistic"
#>
#> $linear.output
#> [1] TRUE
#>
#> $data
#> name year high_jump_average high_jump_tests max_squat weight
#> 1 Jimmy 2020 24 15 405 218
#> 2 Greg 2020 22 8 365 212
#> 3 Alice 2020 18 1 245 165
#> 4 Alice 2021 19 9 265 168
#>
#> $exclude
#> NULL
#>
#> $net.result
#> $net.result[[1]]
#> [,1]
#> [1,] 20.75207
#> [2,] 20.75207
#> [3,] 20.75207
#> [4,] 20.75207
#>
#>
#> $weights
#> $weights[[1]]
#> $weights[[1]][[1]]
#> [,1]
#> [1,] 0.9346973
#> [2,] 0.5025252
#> [3,] 0.7208604
#>
#> $weights[[1]][[2]]
#> [,1]
#> [1,] 9.094366
#> [2,] 11.657705
#>
#>
#>
#> $generalized.weights
#> $generalized.weights[[1]]
#> [,1] [,2]
#> [1,] 0 0
#> [2,] 0 0
#> [3,] 0 0
#> [4,] 0 0
#>
#>
#> $startweights
#> $startweights[[1]]
#> $startweights[[1]][[1]]
#> [,1]
#> [1,] 0.9346973
#> [2,] 0.5025252
#> [3,] 0.7208604
#>
#> $startweights[[1]][[2]]
#> [,1]
#> [1,] -0.6453341
#> [2,] 1.9180050
#>
#>
#>
#> $result.matrix
#> [,1]
#> error 1.137501e+01
#> reached.threshold 8.283793e-03
#> steps 1.140000e+02
#> Intercept.to.1layhid1 9.346973e-01
#> max_squat.to.1layhid1 5.025252e-01
#> weight.to.1layhid1 7.208604e-01
#> Intercept.to.high_jump_average 9.094366e+00
#> 1layhid1.to.high_jump_average 1.165770e+01
#>
#> attr(,"class")
#> [1] "nn"
由reprex 包(v2.0.1)於 2022-07-23 創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.