我正在学习如何使用Keras,并使用Chollet的Python深度学习中的示例在标记数据集上取得了合理的成功。数据集是〜1000个时间序列,长度为3125,具有3个潜在类别。
我想超越基本的Dense层,该层为我提供了约70%的预测率,并且本书继续讨论LSTM和RNN层。
所有示例似乎都为每个时间序列使用了具有多个功能的数据集,因此我正在努力研究如何实现数据。
例如,如果我有1000x3125时间序列,如何将其输入到SimpleRNN或LSTM层中?我是否缺少有关这些层功能的一些基本知识?
当前代码:
import pandas as pd
import numpy as np
import os
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM, Dropout, SimpleRNN, Embedding, Reshape
from keras.utils import to_categorical
from keras import regularizers
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
def readData():
# Get labels from the labels.txt file
labels = pd.read_csv('labels.txt', header = None)
labels = labels.values
labels = labels-1
print('One Hot Encoding Data...')
labels = to_categorical(labels)
data = pd.read_csv('ts.txt', header = None)
return data, labels
print('Reading data...')
data, labels = readData()
print('Splitting Data')
data_train, data_test, labels_train, labels_test = train_test_split(data, labels)
print('Building Model...')
#Create model
model = Sequential()
## LSTM / RNN goes here ##
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print('Training NN...')
history = model.fit(data_train, labels_train, epochs=1000, batch_size=50,
validation_split=0.25,verbose=2)
results = model.evaluate(data_test, labels_test)
predictions = model.predict(data_test)
print(predictions[0].shape)
print(np.sum(predictions[0]))
print(np.argmax(predictions[0]))
print(results)
acc = history.history['acc']
val_acc = history.history['val_acc']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()