用于时间序列预测的最佳激活函数是什么

What is the best activation function to use for time series prediction

我正在使用 Keras 的顺序模型,具有 DENSE 层类型。我写了一个递归计算预测的函数,但预测偏离了。我想知道用于我的数据的最佳激活函数是什么。目前我正在使用 hard_sigmoid 函数。输出数据值的范围为 5 到 25。输入数据的形状为 (6,1),输出数据为单个值。当我绘制预测时,它们永远不会减少。谢谢你的帮助!!

# create and fit Multilayer Perceptron model
model = Sequential();
model.add(Dense(20, input_dim=look_back, activation='hard_sigmoid'))
model.add(Dense(16, activation='hard_sigmoid'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=200, batch_size=2, verbose=0)

#function to predict using predicted values
numOfPredictions = 96;

for i in range(numOfPredictions):
    temp = [[origAndPredictions[i,0],origAndPredictions[i,1],origAndPredictions[i,2],origAndPredictions[i,3],origAndPredictions[i,4],origAndPredictions[i,5]]]
    temp = numpy.array(temp)
    temp1 = model.predict(temp)   
    predictions = numpy.append(predictions, temp1, axis=0)
    temp2 = []
    temp2 = [[origAndPredictions[i,1],origAndPredictions[i,2],origAndPredictions[i,3],origAndPredictions[i,4],origAndPredictions[i,5],predictions[i,0]]]
    temp2 = numpy.array(temp2)
    origAndPredictions = numpy.vstack((origAndPredictions, temp2))

更新: 我用这段代码实现了swish。

from keras.backend import sigmoid
def swish1(x, beta = 1):
    return (x * sigmoid(beta * x))
def swish2(x, beta = 1):
    return (x * sigmoid(beta * x))
from keras.utils.generic_utils import get_custom_objects
from keras.layers import Activation
get_custom_objects().update({'swish': Activation(swish)})

model.add(Activation(custom_activation,name = "swish1"))

更新: 使用此代码:

from keras.backend import sigmoid
from keras import backend as K
def swish1(x):
        return (K.sigmoid(x) * x)
def swish2(x):
        return (K.sigmoid(x) * x)

感谢大家的帮助!!

虽然没有最好的激活函数,但我发现 Swish 对于时间序列问题特别有效。 AFAIK keras 不提供 Swish 内置,您可以使用:

from keras.utils.generic_utils import get_custom_objects
from keras import backend as K
from keras.layers import Activation

def custom_activation(x, beta = 1):
        return (K.sigmoid(beta * x) * x)

get_custom_objects().update({'custom_activation': Activation(custom_activation)})

然后在模型中使用它:

model.add(Activation(custom_activation,name = "Swish"))

您的输出数据范围从 525,您的输出 ReLU 激活将为您提供从 0inf 的值。所以你尝试的是 "parameterize" 你的输出或标准化你的标签。这意味着,使用 sigmoid 作为激活((0,1) 中的输出)并通过减去 5 除以 20 来转换标签,因此它们将(几乎)与您的输出具有相同的间隔,[0,1]。或者您可以使用 sigmoid 并将输出乘以 20 并在计算损失之前加 5。

看到结果会很有趣。