我嘗試創建 GRU 模型,但遇到有關設定時間戳的問題
這是我的輸入示例:
Date = ['2021-08-06', '2021-08-07', '2021-08-08', '2021-08-09', '2021-08-10']
Date = pd.to_datetime(Date)
Close_SP = [4436.52, 4436.52, 4436.52, 4432.35, 4436.75]
Close_DJ = [333.96, 333.96, 333.96, 332.12, 328.85]
Close_Nasdaq = [14835.8, 14835.8, 14835.8, 14860.2, 14788.1]
X = pd.DataFrame({'Close_SP': Close_SP, 'Close_DJ': Close_DJ, 'Close_Nasdaq': Close_Nasdaq}, index = Date)
X.head()
Close_SP Close_DJ Close_Nasdaq
2021-08-06 4436.52 333.96 14835.8
2021-08-07 4436.52 333.96 14835.8
2021-08-08 4436.52 333.96 14835.8
2021-08-09 4432.35 332.12 14860.2
2021-08-10 4436.75 328.85 14788.1
GRU模型的輸入大小是(batch size、timestamp、features),所以我打算先獲取日期資料和feature,然后壓縮。
x1 = tf.convert_to_tensor(X.index)
x2 = tf.convert_to_tensor(X)
input = tf.data.Dataset.zip((x1, x2))
但是,我遇到了 ValueError: Failed to convert a NumPy array to a Tensor (Unsupported numpy type: NPY_DATETIME)
那么,我該如何解決這個問題?有沒有另一種有效的方法來達到我的目標?
uj5u.com熱心網友回復:
我認為您只需要將datetime物件轉換為時間戳即可。
x1 = tf.convert_to_tensor(X.index.values.astype(np.int64))
我還在這一行遇到了另一個錯誤:
input = tf.data.Dataset.zip((x1, x1))
型別錯誤:引數
Dataset.zip()必須是Dataset物件的(嵌套)結構。
為了解決這個問題,我將兩個張量都轉換為資料集。
d1 = tf.data.Dataset.from_tensors(x1)
d2 = tf.data.Dataset.from_tensors(x2)
input = tf.data.Dataset.zip((d1, d2))
這導致 的物件<ZipDataset shapes: ((5,), (5, 3)), types: (tf.int64, tf.float64)>。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/396764.html
上一篇:為影像資料分配標簽時遇到問題
