我正在嘗試在 R 中實作一個 for 回圈,以填充df機器學習中使用的學習率和衰減的一些組合。想法是嘗試幾種學習率和衰減,計算這些組合的錯誤度量并保存在資料集中。所以我可以指出哪種組合更好。
下面是代碼和我的結果。我不明白為什么我會得到這個結果。
learning_rate = c(0.01, 0.02)
decay = c(0, 1e-1)
combinations = length(learning_rate) * length(decay)
df <- data.frame(Combination=character(combinations),
lr=double(combinations),
decay=double(combinations),
result=character(combinations),
stringsAsFactors=FALSE)
for (i in 1:combinations) {
for (lr in learning_rate) {
for (dc in decay) {
df[i, 1] = i
df[i, 2] = lr
df[i, 3] = dc
df[i, 4] = 10*lr dc*4 # Here I'd do some machine learning. Just put this is easy equation as example
}
}
}
我得到的結果。似乎只有組合回圈運作良好。我做錯了什么?
Combination lr decay result
1 0.02 0.1 0.6
2 0.02 0.1 0.6
3 0.02 0.1 0.6
4 0.02 0.1 0.6
我期待這個結果
Combination lr decay result
1 0.01 0 0.1
2 0.01 1e-1 0.5
3 0.02 0 0.2
4 0.02 1e-1 0.6
uj5u.com熱心網友回復:
調優for-loop:
df <- data.frame()
for (lr in learning_rate) {
for (dc in decay) {
df <- rbind(df, data.frame(
lr = lr,
decay = dc,
result = 10*lr dc*4
))
}
}
df
# lr decay result
# 1 0.01 0.0 0.1
# 2 0.01 0.1 0.5
# 3 0.02 0.0 0.2
# 4 0.02 0.1 0.6
調優mapply():
df <- expand.grid(lr = learning_rate, decay = decay)
ML.fun <- function(lr, dc) 10*lr dc*4
df$result <- mapply(ML.fun, lr = df$lr, dc = df$decay)
df
# lr decay result
# 1 0.01 0.0 0.1
# 2 0.02 0.0 0.2
# 3 0.01 0.1 0.5
# 4 0.02 0.1 0.6
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/505259.html
上一篇:動態命名串列中的專案
下一篇:如何在正確位置列印元素總數?
