我是机器学习的新手,目前正在尝试使用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))
我无法确定我的第一层输入的形状应该是什么。能有人提供建议吗?谢谢
供参考,我使用的方法是:
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个特征的时间序列数据,你的输入形状应该是:(None, 批次数(Nb), 批次大小(Bs), 窗口大小(Ws), 4),但是你应该指定的形状是:
shape=(None, None, Ws, 4)
其中
第一个None:用于Nb(因为Nb可能会变化)第二个None:用于Bs(因为Bs可能会变化)
但我不明白为什么你得到的是:
shape=(None, None, 4, 1)