简体   繁体   English

如何在 Wavenet 的 Keras 实现中准备输入以进行时间序列预测

[英]How to prepare the inputs in Keras implementation of Wavenet for time-series prediction

In Keras implementation of Wavenet, the input shape is (None, 1).在 Wavenet 的 Keras 实现中,输入形状为 (None, 1)。 I have a time series (val(t)) in which the target is to predict the next data point given a window of past values (the window size depends on maximum dilation).我有一个时间序列 (val(t)),其中目标是在给定过去值的 window 的情况下预测下一个数据点(window 的大小取决于最大膨胀)。 The input-shape in wavenet is confusing. Wavenet 中的输入形状令人困惑。 I have few questions about it:我有几个问题:

  1. How Keras figure out the input dimension (None) when a full sequence is given?给出完整序列时,Keras 如何计算输入维度(无)? According to dilations, we want the input to have a length of 2^8.根据膨胀,我们希望输入的长度为 2^8。
  2. If a input series of shape (1M, 1) is given as training X, do we need to generate vectors of 2^8 time-steps as input?如果给定一个形状为 (1M, 1) 的输入序列作为训练 X,我们是否需要生成 2^8 个时间步长的向量作为输入? It seems, we can just use the input series as input of wave-net (Not sure why raw time series input does not give error).看来,我们可以只使用输入序列作为波网的输入(不确定为什么原始时间序列输入不会出错)。
  3. In general, how we can debug such Keras networks.一般来说,我们如何调试这样的 Keras 网络。 I tried to apply the function on numerical data like Conv1D(16, 1, padding='same', activation='relu')(inputs), however, it gives error.我尝试将 function 应用于 Conv1D(16, 1, padding='same', activation='relu')(inputs) 等数值数据,但是,它给出了错误。

# #

n_filters = 32
filter_width = 2
dilation_rates = [2**i for i in range(7)] * 2 

from keras.models import Model
from keras.layers import Input, Conv1D, Dense, Activation, Dropout, Lambda, Multiply, Add, Concatenate
from keras.optimizers import Adam

history_seq = Input(shape=(None, 1))
x = history_seq

skips = []
for dilation_rate in dilation_rates:

    # preprocessing - equivalent to time-distributed dense
    x = Conv1D(16, 1, padding='same', activation='relu')(x) 

    # filter
    x_f = Conv1D(filters=n_filters,
                 kernel_size=filter_width, 
                 padding='causal',
                 dilation_rate=dilation_rate)(x)

    # gate
    x_g = Conv1D(filters=n_filters,
                 kernel_size=filter_width, 
                 padding='causal',
                 dilation_rate=dilation_rate)(x)

    # combine filter and gating branches
    z = Multiply()([Activation('tanh')(x_f),
                    Activation('sigmoid')(x_g)])

    # postprocessing - equivalent to time-distributed dense
    z = Conv1D(16, 1, padding='same', activation='relu')(z)

    # residual connection
    x = Add()([x, z])    

    # collect skip connections
    skips.append(z)

# add all skip connection outputs 
out = Activation('relu')(Add()(skips))

# final time-distributed dense layers 
out = Conv1D(128, 1, padding='same')(out)
out = Activation('relu')(out)
out = Dropout(.2)(out)
out = Conv1D(1, 1, padding='same')(out)

# extract training target at end
def slice(x, seq_length):
    return x[:,-seq_length:,:]

pred_seq_train = Lambda(slice, arguments={'seq_length':1})(out)

model = Model(history_seq, pred_seq_train)
model.compile(Adam(), loss='mean_absolute_error')

you are using extreme values for dilatation rate, they don't make sense.您正在使用膨胀率的极端值,它们没有意义。 try to reduce them using, for example, a sequence made of [1, 2, 4, 8, 16, 32].尝试使用例如由 [1, 2, 4, 8, 16, 32] 组成的序列来减少它们。 the dilatation rates aren't a constraint on the dimension of the input passed膨胀率不是对传递的输入维度的约束

your network work simply passing this input您的网络工作只需传递此输入

n_filters = 32
filter_width = 2
dilation_rates = [1, 2, 4, 8, 16, 32]

....

model = Model(history_seq, pred_seq_train)
model.compile(Adam(), loss='mean_absolute_error')

n_sample = 5
time_step = 100

X = np.random.uniform(0,1, (n_sample,time_step,1))

model.predict(X)

specify a None dimension in Keras means to leave the model free to receive every dimension.在 Keras 中指定 None 维度意味着让 model 自由接收每个维度。 this not means you can pass samples of various dimension, they always must have the same format... you can build the model every time with a different dimension size这并不意味着您可以传递各种尺寸的样本,它们必须始终具有相同的格式......您可以每次使用不同的尺寸大小构建 model

for time_step in np.random.randint(100,200, 4):

  print('temporal dim:', time_step)
  n_sample = 5

  model = Model(history_seq, pred_seq_train)
  model.compile(Adam(), loss='mean_absolute_error')

  X = np.random.uniform(0,1, (n_sample,time_step,1))

  print(model.predict(X).shape)

I suggest also you a premade library in Keras which provide WAVENET implementation: https://github.com/philipperemy/keras-tcn you can use it as a baseline and investigate also the code to create a WAVENET我还建议您在 Keras 中提供 WAVENET 实现的预制库: https://github.com/philipperemy/keras-tcn您可以将其用作基线并研究创建 WAVENET 的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM