在使用LSTM时,日期时间索引应该是升序还是降序?我的意思是,数据集的头部应该是2014年而尾部是2020年,还是反过来?我之所以问这个问题是因为LSTM的回顾期,我担心如果排序不正确,它会查看错误的时间范围。
我当前的时间框架索引在执行print(df)时看起来像这样:
2014-21-3 XYZ2014-22-3 XYZ2014-23-3 XYZ
为了让LSTM“工作”,是否应该更改为这种顺序?
2014-23-3 XYZ2014-22-3 XYZ2014-21-3 XYZ
另外,当我加载一个数据集时,该数据集有一个在“Up”和“Down”两个字符串之间波动的二元响应变量。在使用Keras模型时,它会输出1值的概率,但是我如何知道哪个字符串是1,哪个是0?
如果答案是使用predict_classification,那么很抱歉,这不是我在这里寻找的(只是提醒一下,因为我见过类似的问题有这样的答案)。
回答:
你必须使用升序…
2014-21-3 XYZ2014-22-3 XYZ2014-23-3 XYZ
编辑(如何解释概率):
你可以将Up编码为类1,将Down编码为类0
label = ['up','down','up','up','down']map_label = {'up':1,'down':0}[map_label[l] for l in label]# ===> [1, 0, 1, 1, 0]
你有两种选择来解释分类问题的输出。这取决于你选择的激活输出(sigmoid或softmax)
sigmoid激活函数用于生成二元分类问题的概率。在这种情况下,模型输出一个概率数组,其形状等于要预测的样本长度。我们可以通过检查概率分数来简单地检索预测类…如果它高于0.5(这是一种常见的做法,但你也可以根据需要进行更改),则该样本属于类1,否则属于类0。在sigmoid的情况下,你的最后一个输出层必须是Dense(1, activation='sigmoid')
n_sample = 10timestemp = 5n_features = 3X = np.random.uniform(0,1, (n_sample, timestemp, n_features))y = np.random.randint(0,2, n_sample)inp = Input((timestemp,n_features))x = LSTM(8, activation='relu')(inp)out = Dense(1, activation='sigmoid')(x)model = Model(inp, out)model.compile('adam', 'binary_crossentropy')model.fit(X,y, epochs=3)preds = model.predict(X) # (n_samples, 1)y_classes = ((preds > 0.5)+0).ravel() # Up == 1 and Down == 0
在softmax的情况下,使用argmax
检索预测类,你的最后一个输出层必须是Dense(n_classes, activation='softmax')
n_sample = 10timestemp = 5n_features = 3X = np.random.uniform(0,1, (n_sample, timestemp, n_features))y = np.random.randint(0,2, n_sample)inp = Input((timestemp,n_features))x = LSTM(8, activation='relu')(inp)out = Dense(2, activation='softmax')(x)model = Model(inp, out)model.compile('adam', 'sparse_categorical_crossentropy')model.fit(X,y, epochs=3)preds = model.predict(X) # (n_samples, n_class)y_classes = np.argmax(preds , axis=1) # Up == 1 and Down == 0
我随时为您服务