保存经过训练以减小尺寸的自动编码器

时间:2018-10-11 09:12:11

标签: python-3.x variables keras autoencoder dimensionality-reduction

我制作了用于减少维度的自动编码器,我想保存它以用于测试数据集的缩减。这是我的代码

dom_state = seed(123)
print('Rescaling Data')
y = minmax_scale(X, axis=0)
ncol = y.shape[1] #here ncol = 19
print('Encoding Dimensions')
encoding_dim = 3
input_dim = Input(shape = (ncol,))

with tf.Session(config=tf.ConfigProto(intra_op_parallelism_threads=24)) as sess:
    K.set_session(sess)
    print('Initiating Encoder Layer')
    encoded1 = Dense(20, activation = 'relu')(input_dim)
    encoded2 = Dense(10, activation = 'relu')(encoded1)
    encoded3 = Dense(5, activation = 'relu')(encoded2)
    encoded4 = Dense(encoding_dim, activation = 'relu')(encoded3)
    print('Initiating Decoder Layer')
    decoded1 = Dense(5, activation = 'relu')(encoded4)
    decoded2 = Dense(10, activation = 'relu')(decoded1)
    decoded3 = Dense(20, activation = 'relu')(decoded2)
    decoded4 = Dense(ncol, activation = 'sigmoid')(decoded3)

    print('Combine Encoder and Decoder layers')
    autoencoder = Model(input = input_dim, output = decoded4)
    print('Compiling Mode')
    autoencoder.compile(optimizer = 'Nadam', loss ='mse')
    autoencoder.fit(y, y, nb_epoch = 300, batch_size = 20, shuffle = True)
    encoder = Model(input = input_dim, output = decoded4)
    encoder.save('reduction_param.h5')

    print('Initiating Dimension Reduction')
    model = load_model('reduction_param.h5')
    encoded_input = Input(shape = (encoding_dim, ))
    encoded_out = model.predict(y)

但是,即使我限制了尺寸,在model.predict(y)部分上,我仍然得到完整的19列而不是3列。此外,我还收到错误消息:

UserWarning: No training configuration found in save file: the model was *not* compiled. Compile it manually.
  warnings.warn('No training configuration found in save file:

据我了解,因为encoder.save('reduction_param.h5')实际上未使用优化程序进行编译。我想念什么吗?

编辑

我不知道这是否是解决问题的正确方法,基本上我将MinMAXScaler()训练到训练数据集,将特征另存为泡菜,然后在维护自动编码器的同时重新使用它,按照代码:

dom_state = seed(123)
print('Rescaling Data')
feature_space= MinMaxScaler()
feature_pkl = feature_space.fit(X)
filename = 'lc_feature_space.sav'
pickle.dump(feature_pkl, open(filename, 'wb'))
loaded_model = pickle.load(open(filename, 'rb'))
y = loaded_model.transform(X)
ncol = y.shape[1]
print(ncol)
print('Encoding Dimensions')
encoding_dim = 3
input_dim = Input(shape = (ncol,))

with tf.Session(config=tf.ConfigProto(intra_op_parallelism_threads=24)) as sess:
    K.set_session(sess)
    print('Initiating Encoder Layer')
    encoded1 = Dense(20, activation = 'relu')(input_dim)
    encoded2 = Dense(10, activation = 'relu')(encoded1)
    encoded3 = Dense(5, activation = 'relu')(encoded2)
    encoded4 = Dense(encoding_dim, activation = 'relu')(encoded3)
    print('Initiating Decoder Layer')
    decoded1 = Dense(5, activation = 'relu')(encoded4)
    decoded2 = Dense(10, activation = 'relu')(decoded1)
    decoded3 = Dense(20, activation = 'relu')(decoded2)
    decoded4 = Dense(ncol, activation = 'sigmoid')(decoded3)

    print('Combine Encoder and Deocoder layers')
    autoencoder = Model(input = input_dim, output = decoded4)
    print('Compiling Mode')
    autoencoder.compile(optimizer = 'Nadam', loss ='mse')
    autoencoder.fit(y, y, nb_epoch = 300, batch_size = 20, shuffle = True)

    print('Initiating Dimension Reduction')
    encoder = Model(input = input_dim, output = decoded4)
    encoded_input = Input(shape = (encoding_dim, ))
    encoded_out = encoder.predict(y)
    result = encoded_out[0:2]
我在这里的论点是将训练数据集的特征保存在MINMAXScaler()级别,基于这些特征变换测试数据集,然后仅使用自动编码器进行简化。仍然我不知道这是否正确。

1 个答案:

答案 0 :(得分:0)

我认为您之所以看不到encoder正常工作(即减小输入张量的维数)是因为您定义并保存了错误的模型。您应该使用

encoder = Model(input = input_dim, output = encoded4 )

其输出节点为encoded4而不是decoded4