R

时间:2018-01-20 21:52:23

标签: r neural-network normalization training-data

随着我的输入不断涌现,我想实现对神经网络的持续训练。但是,当我获得新数据时,标准化值将随时间而变化。让我们及时说一下:

df <- "Factor1 Factor2 Factor3 Response
        10      10000   0.4     99
        15      10200   0       88
        11      9200    1       99
        13      10300   0.3     120"
df <- read.table(text=df, header=TRUE)

normalize <- function(x) {
    return ((x - min(x)) / (max(x) - min(x)))
}

dfNorm <- as.data.frame(lapply(df, normalize))

### Keep old normalized values
dfNormOld <- dfNorm 

library(neuralnet)
nn <- neuralnet(Response~Factor1+Factor2+Factor3, data=dfNorm, hidden=c(3,4), 
    linear.output=FALSE, threshold=0.10,  lifesign="full", stepmax=20000)

然后,随着时间的推移,

df2 <- "Factor1 Factor2 Factor3 Response
        12      10100   0.2     101
        14      10900   -0.7    108
        11      9800    0.8     120
        11      10300   0.3     113"

df2 <- read.table(text=df2, header=TRUE)

### Bind all-time data
df <- rbind(df2, df)

### Normalize all-time data in one shot
dfNorm <- as.data.frame(lapply(df, normalize))

### Continue training the network with most recent data
library(neuralnet)
Wei <- nn$weights
nn <- neuralnet(Response~Factor1+Factor2+Factor3, data=df[1:nrow(df2),], hidden=c(3,4), 
    linear.output=FALSE, threshold=0.10,  lifesign="full", stepmax=20000, startweights = Wei)

这将是我将如何训练它的方式。然而,我想知道是否有任何优雅的方法来减少这种不断训练的偏差,因为标准化值将不可避免地随时间变化。在这里,我假设非标准化值可能有偏差。

1 个答案:

答案 0 :(得分:1)

您可以使用此代码:

normalize <- function(x,min1,max1,row1) {
     if(row1>0)
        x[1:row1,] = (x[1:row1,]*(max1-min1))+min1
     return ((x - min(x)) / (max(x) - min(x)))
 }

past_min = rep(0,dim(df)[2])
past_max = rep(0,dim(df)[2])
rowCount = 0

while(1){
df = mapply(normalize, x=df, min1 = past_min, max1 = past_max,row1 = rep(rowCount,dim(df)[2]))
nn <- neuralnet(Response~Factor1+Factor2+Factor3, data=dfNorm, hidden=c(3,4), 
                    linear.output=FALSE, threshold=0.10,  lifesign="full", stepmax=20000)

past_min = as.data.frame(lapply(df, min))
past_max = as.data.frame(lapply(df, max))
rowCount = dim(df)[1]

df2 <- read.table(text=df2, header=TRUE)
df <- rbind(df2, df)
}