如何在 Keras 中高效处理重复的共享层
How to handle efficiently Repeated Shared Layers in Keras
我是 Keras 深度学习的菜鸟。我正在 Keras 中建立一个神经网络模型,其输入 xi(每个输入都有一对变量)由较小的密集网络层进行预处理。之后,较小的密集层吐出两个新变量 xf(如编码器),将它们连接起来,然后作为更大的密集网络的输入。
在预处理层,由于每个输入 xi 都是同一层 运行,我想知道是否可以通过循环对其进行有效编码。
我试过 for-loop 但它似乎不起作用,因为这样的张量对象不支持项目分配。所以,我用以下简单的方式编写了它,并且代码运行良好。然而,这是为 5 个输入而写的;在实践中,我的输入数量是一个很大的变量,可以达到500。因此我正在寻找一种有效的方法是必要的。
shared_dense_lvl1 = Dense(4, activation='sigmoid')
shared_dense_lvl2 = Dense(4, activation='sigmoid')
shared_dense_lvl3 = Dense(2, activation='sigmoid')
x1i = Input(shape=(2,))
x11 = shared_dense_lvl1(x1i)
x12 = shared_dense_lvl2(x11)
x1f = shared_dense_lvl3(x12)
x2i = Input(shape=(2,))
x21 = shared_dense_lvl1(x2i)
x22 = shared_dense_lvl2(x21)
x2f = shared_dense_lvl3(x22)
x3i = Input(shape=(2,))
x31 = shared_dense_lvl1(x3i)
x32 = shared_dense_lvl2(x31)
x3f = shared_dense_lvl3(x32)
x4i = Input(shape=(2,))
x41 = shared_dense_lvl1(x4i)
x42 = shared_dense_lvl2(x41)
x4f = shared_dense_lvl3(x42)
x5i = Input(shape=(2,))
x51 = shared_dense_lvl1(x5i)
x52 = shared_dense_lvl2(x51)
x5f = shared_dense_lvl3(x52)
xy0 = concatenate([x1f, x2f, x3f, x4f, x5f])
xy = Dense(2, activation='relu',name='xy')(xy0)
y1 = Dense(50, activation='sigmoid',name='y1')(xy)
y2 = Dense(50, activation='sigmoid',name='y2')(y1)
y3 = Dense(250, activation='sigmoid',name='y3')(y2)
merged = Model(inputs=[x1i,x2i,x3i,x4i,x5i],outputs=[y3])
你写的模型相当于:
inp = Input(shape=(5, 2,))
l1 = Dense(4, ...)(inp)
l2 = Dense(4, ...)(l1)
l3 = Dense(2, ...)(l2)
xy0 = Flatten()(l3)
即如果您有一个超过 2 维的输入形状,例如形状(batch_size、time_steps、n_features),密集层将共享权重应用于所有 time_steps.
您的数据类似于 time-series 吗?在这种情况下,您可以考虑使用循环或卷积网络。
您似乎还使用了很多 sigmoid 激活函数。当心渐变消失。如果您发现网络没有训练,请通过打印特定训练批次的梯度来检查梯度是否消失。例如,这个包可能会有所帮助:https://github.com/philipperemy/keract
我是 Keras 深度学习的菜鸟。我正在 Keras 中建立一个神经网络模型,其输入 xi(每个输入都有一对变量)由较小的密集网络层进行预处理。之后,较小的密集层吐出两个新变量 xf(如编码器),将它们连接起来,然后作为更大的密集网络的输入。
在预处理层,由于每个输入 xi 都是同一层 运行,我想知道是否可以通过循环对其进行有效编码。
我试过 for-loop 但它似乎不起作用,因为这样的张量对象不支持项目分配。所以,我用以下简单的方式编写了它,并且代码运行良好。然而,这是为 5 个输入而写的;在实践中,我的输入数量是一个很大的变量,可以达到500。因此我正在寻找一种有效的方法是必要的。
shared_dense_lvl1 = Dense(4, activation='sigmoid')
shared_dense_lvl2 = Dense(4, activation='sigmoid')
shared_dense_lvl3 = Dense(2, activation='sigmoid')
x1i = Input(shape=(2,))
x11 = shared_dense_lvl1(x1i)
x12 = shared_dense_lvl2(x11)
x1f = shared_dense_lvl3(x12)
x2i = Input(shape=(2,))
x21 = shared_dense_lvl1(x2i)
x22 = shared_dense_lvl2(x21)
x2f = shared_dense_lvl3(x22)
x3i = Input(shape=(2,))
x31 = shared_dense_lvl1(x3i)
x32 = shared_dense_lvl2(x31)
x3f = shared_dense_lvl3(x32)
x4i = Input(shape=(2,))
x41 = shared_dense_lvl1(x4i)
x42 = shared_dense_lvl2(x41)
x4f = shared_dense_lvl3(x42)
x5i = Input(shape=(2,))
x51 = shared_dense_lvl1(x5i)
x52 = shared_dense_lvl2(x51)
x5f = shared_dense_lvl3(x52)
xy0 = concatenate([x1f, x2f, x3f, x4f, x5f])
xy = Dense(2, activation='relu',name='xy')(xy0)
y1 = Dense(50, activation='sigmoid',name='y1')(xy)
y2 = Dense(50, activation='sigmoid',name='y2')(y1)
y3 = Dense(250, activation='sigmoid',name='y3')(y2)
merged = Model(inputs=[x1i,x2i,x3i,x4i,x5i],outputs=[y3])
你写的模型相当于:
inp = Input(shape=(5, 2,))
l1 = Dense(4, ...)(inp)
l2 = Dense(4, ...)(l1)
l3 = Dense(2, ...)(l2)
xy0 = Flatten()(l3)
即如果您有一个超过 2 维的输入形状,例如形状(batch_size、time_steps、n_features),密集层将共享权重应用于所有 time_steps.
您的数据类似于 time-series 吗?在这种情况下,您可以考虑使用循环或卷积网络。
您似乎还使用了很多 sigmoid 激活函数。当心渐变消失。如果您发现网络没有训练,请通过打印特定训练批次的梯度来检查梯度是否消失。例如,这个包可能会有所帮助:https://github.com/philipperemy/keract