Python Keras-如何实际使用自动编码器

Python Keras-如何实际使用自动编码器,python,scikit-learn,keras,Python,Scikit Learn,Keras,为了减少尺寸,我在Keras中开发了以下自动编码器 from keras.layers import Input, Dense, BatchNormalization from keras.models import Model from keras import regularizers from keras.callbacks import TensorBoard, EarlyStopping import keras.backend as K from sklearn.metrics im

为了减少尺寸,我在Keras中开发了以下自动编码器

from keras.layers import Input, Dense, BatchNormalization
from keras.models import Model
from keras import regularizers
from keras.callbacks import TensorBoard, EarlyStopping
import keras.backend as K
from sklearn.metrics import r2_score

input_size = len(spot_dat.columns)
coder_size = 32
inner_size = 64
betwe_size = 96
outer_size = 128
batch_size = 25


def r2(y_true, y_pred):
    SS_res = K.sum(K.square(y_true - y_pred))
    SS_tot = K.sum(K.square(y_true - K.mean(y_true)))
    return (1 - SS_res / (SS_tot + K.epsilon()))

def rmse(y_true, y_pred):
    return K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1))

input_ = Input(shape=(input_size,))
encoded = Dense(outer_size, activation='selu')(input_) #hidden 1
#encoded = Dense(betwe_size, activation='elu')(input_) #hidden 2
encoded = BatchNormalization()(encoded)
encoded = Dense(inner_size, activation='selu')(encoded) #hidden 3

code = Dense(coder_size, activation='selu')(encoded) #code 


decoded = Dense(inner_size, activation='selu')(code) #hidden 2
decoded = BatchNormalization()(decoded)
#decoded = Dense(betwe_size, activation='elu')(decoded) #hidden 2
decoded = Dense(outer_size, activation='selu')(decoded) #hidden 1
output = Dense(input_size, activation='sigmoid')(decoded) #output

autoencoder = Model(input_, output)
autoencoder.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics = [r2, rmse])
val = autoencoder.fit(x_train, x_train, 
                epochs=1000,
                batch_size = 75,
                shuffle=True,
                validation_data=(x_test, x_test),
                callbacks=[TensorBoard(log_dir='/tmp/test'), EarlyStopping(monitor = 'val_loss', min_delta = 0, patience = 30, verbose = True, mode = 'auto')])

plt.plot(val.history['loss'])
plt.plot(val.history['val_loss'])
plt.title('Model loss (MSR)')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc = 'best')
plt.show()

plt.plot(val.history['r2'])
plt.plot(val.history['val_r2'])
plt.title('Model R2')
plt.ylabel('R2')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc = 'best')
plt.show()

plt.plot(val.history['rmse'])
plt.plot(val.history['val_rmse'])
plt.title('Model RMSE')
plt.ylabel('RMSE')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc = 'best')
plt.show()

该模型运行良好,问题是我无法找到任何方式提取部分模型(从输入到代码)后,它已经在完整的自动编码器上进行了培训。在我看来,如果你不能拉一半的模型,自动编码器就不能用于任何实际的尺寸缩减。因此,应该有一种方法将经过训练的模型冻结并拉入前几层。最终目标是将32维图层应用于t-SNE,并将结果与其他降维技术进行比较。

培训后,只需执行以下操作:

encoder = Model(input_, code)

然后使用编码器。预测从输入样本中获取代码。

培训后,只需执行以下操作:

encoder = Model(input_, code)

然后使用
encoder.predict
从输入样本中获取代码。

有趣的是,我没想到这会奏效。非常感谢你的帮助!有趣的是,我没想到这会奏效。非常感谢你的帮助!