在本文中,keras
我想将模型与结构的初始层一起用于给定的经过训练的神经网络,并在训练过程中获得权重。
案例:假设我们有一个数据集df
,拆分为train
,dev
然后test
训练一个神经网络,在这个示例中为autoencoder
。
一段真实的代码说明了这个概念,但没有提供数据(我认为没有必要):
from keras.models import Model
from keras.layers import Activation, Dense, Dropout, Input
# Define input layer
input_data = Input(shape=(train.shape[1],), name='Input')
# Define encoding layer
encoded = Dense(encoding_dim, activation='relu')(input_data)
# Define decoding layer
decoded = Dense(train.shape[1], activation='sigmoid')(encoded)
# Create the autoencoder model
autoencoder = Model(input_data, decoded, name='Simple AutoEncoder')
#Compile the autoencoder model
autoencoder.compile(optimizer='rmsprop',
loss='binary_crossentropy')
autoencoder.fit(train, train,
epochs=50,
batch_size=256,
shuffle=True,
validation_data=(dev_x, dev_x), verbose=0)
之后compile
和fit
模型,我们有了一个神经网络,其权重是我们从拟合过程中获得的。
如何encoder part
通过保持重量来只使用该网的?
通过训练一个Autoencoder
,encoder
将使用包含自动编码器训练后的权重的编码对象创建神经网络部分。
# Getting the trained weights of the first layer(dense layer of encoder)
weights_ae = autoencoder.layers[1].get_weights()[0]
# The previous code of the example...
# Creating the encoder model
encoder = Model(input_data, encoded, name='Encoder')
# Getting the weights of the encoder model
weights_e = encoder.layers[1].get_weights()[0]
因此,最终将确定,通过创建模型,encoder
将具有来自自动编码器的权重(“受过训练的经验”)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句