我想知道如何處理記錄的時間序列資料以將其輸入 RNN。
我想用 16 個時間步長的資料和 15 個標簽來讓 RNN 對第 16 個時間步長進行分類(如果這有意義的話)。通過使用批處理的每三個條目,我可以覆寫大約 3 秒的資料,每秒有合理數量的條目。
這是記錄資料的較小的 .csv。“時間”和“五月天”列僅供參考,以確保所有內容都正確標記,因此可以洗掉。
這是洗掉不相關列后我的資料的樣子
這是我迄今為止在google colab中嘗試過的方法, 不幸的是這種方法不起作用,并且在呼叫model.fit時我得到一個“AttributeError:'tuple'物件沒有屬性'shape'”。
或者我也試過這個:
data = pd.read_csv("slim.csv", sep=",")
data.drop(['Time', 'Mayday'], axis=1)
dataset = tf.data.Dataset.from_tensor_slices(data)
但是從那里開始,我不確定如何處理資料以獲得所需的結果,因為在資料集上呼叫 tf.keras.preprocessing.timeseries_dataset_from_array() 會以錯誤訊息終止
'TensorSliceDataset' object is not subscriptable
uj5u.com熱心網友回復:
你的想法很好。問題是train_target并且test_target正在回傳元組,因為正如檔案所述:
回傳一個 tf.data.Dataset 實體。如果通過了目標,則資料集產生元組(batch_of_sequences,batch_of_targets)。如果不是,則資料集僅產生 batch_of_sequences。
由于您只對這種情況下的目標感興趣,因此您可以運行:
data_set = tf.data.Dataset.zip( (train_input ,train_target.map(lambda x, y: y)))
test_set = tf.data.Dataset.zip( (test_input ,test_target.map(lambda x, y: y)))
但請注意,這仍然行不通,因為您的目標具有形狀(32, 11),而模型的輸出形狀為(32, 3). 所以你應該問問自己你到底想要達到什么目標。
更新 1
嘗試:
import tensorflow as tf
data = pd.read_csv("slim.csv", sep=",")
data.drop(['Time', 'Mayday'], axis=1)
window_size = 16
dataset = tf.data.Dataset.from_tensor_slices((data.values)).window(window_size, shift=3, drop_remainder=True)
dataset = dataset.flat_map(lambda window: window.batch(window_size)).batch(32)
dataset = dataset.map(lambda x: (x[:, :, :10], x[:, 15, -1]))
model = tf.keras.models.Sequential([
tf.keras.layers.GRU(input_shape=(None, 10), units= 128),
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Dense(units=128, activation='tanh'),
tf.keras.layers.Dense(units=3, activation='softmax')
])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(dataset, epochs=5)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/452264.html
