Hes*_*sam 4 python machine-learning deep-learning keras tensorflow
在 Wavenet 的 Keras 实现中,输入形状为 (None, 1)。我有一个时间序列 (val(t)),其中目标是在给定过去值窗口的情况下预测下一个数据点(窗口大小取决于最大扩张)。Wavenet 中的输入形状令人困惑。我对此有几个疑问:
#
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')
Run Code Online (Sandbox Code Playgroud)
您使用膨胀率的极值,它们没有意义。尝试使用例如由 [1, 2, 4, 8, 16, 32] 组成的序列来减少它们。膨胀率并不限制所传递的输入的维度
您的网络只需传递此输入即可工作
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)
Run Code Online (Sandbox Code Playgroud)
在 Keras 中指定 None 维度意味着让模型自由地接收每个维度。这并不意味着您可以传递各种维度的样本,它们始终必须具有相同的格式...您可以每次使用不同的维度大小构建模型
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)
Run Code Online (Sandbox Code Playgroud)
我还建议您在 Keras 中使用一个预制库,它提供 WAVENET 实现:https://github.com/philipperemy/keras-tcn您可以使用它作为基线并研究创建 WAVENET 的代码
| 归档时间: |
|
| 查看次数: |
1928 次 |
| 最近记录: |