如何确定输入层的形状?
How to determine the shape of my input layer?
我是机器学习的新手,目前正在尝试使用 Tensorflow 和 Keras。
我有一个时间序列 windowed 数据集,window 大小为 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、window 为 128 和 4 个特征的时间序列,您的输入形状将是:
(None, 批次数 (Nb), 批次大小 (Bs), window 大小 (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)
我是机器学习的新手,目前正在尝试使用 Tensorflow 和 Keras。
我有一个时间序列 windowed 数据集,window 大小为 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、window 为 128 和 4 个特征的时间序列,您的输入形状将是: (None, 批次数 (Nb), 批次大小 (Bs), window 大小 (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)