如何确定我的输入图层的形状?



我是机器学习的新手,我目前正在尝试使用Tensorflow和Keras。

我有一个时间序列窗口数据集,窗口大小为128,批处理为32,如果重要的话,有4个特征。

这是在PrefetchDataset格式,当我试图检查形状使用。element_spec我得到:(TensorSpec(shape=(None, None, 4, 1), dtype=tf.float64, name=None), TensorSpec(shape=(None, 4, 1), dtype=tf.float64, name=None))

我不知道我的第一层的input_shape必须是什么。有人能给点建议吗?由于

作为参考,我使用的方法是:

def windowed_dataset(series, window_size, batch_size, shuffle_buffer=None):
series = tf.expand_dims(series, axis=-1)
dataset = tf.data.Dataset.from_tensor_slices(series)
dataset = dataset.window(window_size + 1, shift=1, drop_remainder=True)
dataset = dataset.flat_map(lambda window: window.batch(window_size + 1))
if shuffle_buffer != None: 
dataset = dataset.shuffle(shuffle_buffer)
dataset = dataset.map(
lambda window: (window[:-1], window[-1]))
dataset = dataset.batch(batch_size).prefetch(1)
return dataset 

数据集(Dataframe.to_numpy ()):

array([[0.86749387, 0.87223695, 0.02077445, 0.87542179],
[0.86755952, 0.87322277, 0.02047971, 0.87551724],
[0.86749387, 0.8733104 , 0.01424521, 0.8756016 ],
...,
[0.18539916, 0.19000153, 0.00700078, 0.18666753],
[0.18325455, 0.19000153, 0.        , 0.18610588],
[0.18636204, 0.19144741, 0.00573779, 0.18572627]])

第一个图层:

Conv1D(filters=128, kernel_size=3, strides=1, padding='causal', input_shape=[None, None, window_size, 4] , activation='relu'),

错误:

ValueError: Input 0 of layer sequential_53 is incompatible with the layer: expected axis -1 of input shape to have value 4 but received input with shape (None, None, 4, 1)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_21174/3802335098.py in <module>
----> 1 history = model.fit(train_dataset, epochs=epochs, batch_size=batch_size, shuffle=False, verbose=1)

对于批次为32,窗口为128和4个特征的时间序列,您的输入形状将为:(无,批数(Nb),批大小(Bs),窗口大小(Ws), 4)但是你应该指定的是:

shape=(None, None, Ws, 4)

First None: for Nb (because Nb can vary)
Scd None : for Bs (Because Bs can vary)

但是我不明白为什么你得到:

shape=(None, None, 4, 1)

最新更新