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



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

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(( 级别保存训练数据集的特征,根据这些特征转换测试数据集,然后使用自动编码器减少。我仍然不知道这是否正确。

我认为您没有看到encoder正常工作(即降低输入张量的维数(的原因是因为您定义并保存了一个错误的模型。你应该使用

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

其输出节点为encoded4而不是decoded4

相关内容

  • 没有找到相关文章

最新更新