如何将自动编码器分为编码器和解码器(TensorFlow + TFLearn)
How to separate autoencoder into encoder and decoder (TensorFlow + TFLearn)
我一直在使用 tflearn 编写简单的自动编码器。
net = tflearn.input_data (shape=[None, train.shape [1]])
net = tflearn.fully_connected (net, 500, activation = 'tanh', regularizer = None, name = 'fc_en_1')
#hidden state
net = tflearn.fully_connected (net, 100, activation = 'tanh', regularizer = 'L1', name = 'fc_en_2', weight_decay = 0.0001)
net = tflearn.fully_connected (net, 500, activation = 'tanh', regularizer = None, name = 'fc_de_1')
net = tflearn.fully_connected (net, train.shape [1], activation = 'linear', name = 'fc_de_2')
net = tflearn.regression(net, optimizer='adam', learning_rate=0.01, loss='mean_square', metric='default')
model = tflearn.DNN (net)
模型训练得很好,但训练后我想单独使用编码器和解码器。
我该怎么做?现在我可以恢复输入,我希望能够将输入转换为隐藏表示并从任意隐藏表示恢复输入。
您可以只保存编码器和解码器的名称inputs/outputs。
即(添加输入,HIDDEN_STATE,输出):
net = tflearn.input_data (shape=[None, train.shape [1]])
INPUT = net
net = tflearn.fully_connected (net, 500, activation = 'tanh', regularizer = None, name = 'fc_en_1')
#hidden state
net = tflearn.fully_connected (net, 100, activation = 'tanh', regularizer = 'L1', name = 'fc_en_2', weight_decay = 0.0001)
HIDDEN_STATE = net
net = tflearn.fully_connected (net, 500, activation = 'tanh', regularizer = None, name = 'fc_de_1')
net = tflearn.fully_connected (net, train.shape [1], activation = 'linear', name = 'fc_de_2')
OUTPUT = net
net = tflearn.regression(net, optimizer='adam', learning_rate=0.01, loss='mean_square', metric='default')
model = tflearn.DNN (net)
然后用这样的函数来encode/decode:
def encode (X):
if len (X.shape) < 2:
X = X.reshape (1, -1)
tflearn.is_training (False, model.session)
res = model.session.run (HIDDEN_STATE, feed_dict={INPUT.name:X})
return res
def decode (X):
if len (X.shape) < 2:
X = X.reshape (1, -1)
#just to pass something to place_holder
zeros = np.zeros ((X.shape [0], train.shape [1]))
tflearn.is_training (False, model.session)
res = model.session.run (OUTPUT, feed_dict={INPUT.name:zeros, HIDDEN_STATE.name:X})
return res
感谢@discharged-spider 的回答。我只是 encoded/decoded 2,000 个大小为 1,000 的向量,并使用上面提到的自动编码器减少了它们的维度。但是,每当我尝试找到从解码器输出到实际输入的映射时,仅在 1 个向量上它成功地将解码器输出的结果映射到实际输出。我不确定如何提高这里的准确性。
我使用欧几里德距离找到最接近解码器输出的向量。
我一直在使用 tflearn 编写简单的自动编码器。
net = tflearn.input_data (shape=[None, train.shape [1]])
net = tflearn.fully_connected (net, 500, activation = 'tanh', regularizer = None, name = 'fc_en_1')
#hidden state
net = tflearn.fully_connected (net, 100, activation = 'tanh', regularizer = 'L1', name = 'fc_en_2', weight_decay = 0.0001)
net = tflearn.fully_connected (net, 500, activation = 'tanh', regularizer = None, name = 'fc_de_1')
net = tflearn.fully_connected (net, train.shape [1], activation = 'linear', name = 'fc_de_2')
net = tflearn.regression(net, optimizer='adam', learning_rate=0.01, loss='mean_square', metric='default')
model = tflearn.DNN (net)
模型训练得很好,但训练后我想单独使用编码器和解码器。
我该怎么做?现在我可以恢复输入,我希望能够将输入转换为隐藏表示并从任意隐藏表示恢复输入。
您可以只保存编码器和解码器的名称inputs/outputs。
即(添加输入,HIDDEN_STATE,输出):
net = tflearn.input_data (shape=[None, train.shape [1]])
INPUT = net
net = tflearn.fully_connected (net, 500, activation = 'tanh', regularizer = None, name = 'fc_en_1')
#hidden state
net = tflearn.fully_connected (net, 100, activation = 'tanh', regularizer = 'L1', name = 'fc_en_2', weight_decay = 0.0001)
HIDDEN_STATE = net
net = tflearn.fully_connected (net, 500, activation = 'tanh', regularizer = None, name = 'fc_de_1')
net = tflearn.fully_connected (net, train.shape [1], activation = 'linear', name = 'fc_de_2')
OUTPUT = net
net = tflearn.regression(net, optimizer='adam', learning_rate=0.01, loss='mean_square', metric='default')
model = tflearn.DNN (net)
然后用这样的函数来encode/decode:
def encode (X):
if len (X.shape) < 2:
X = X.reshape (1, -1)
tflearn.is_training (False, model.session)
res = model.session.run (HIDDEN_STATE, feed_dict={INPUT.name:X})
return res
def decode (X):
if len (X.shape) < 2:
X = X.reshape (1, -1)
#just to pass something to place_holder
zeros = np.zeros ((X.shape [0], train.shape [1]))
tflearn.is_training (False, model.session)
res = model.session.run (OUTPUT, feed_dict={INPUT.name:zeros, HIDDEN_STATE.name:X})
return res
感谢@discharged-spider 的回答。我只是 encoded/decoded 2,000 个大小为 1,000 的向量,并使用上面提到的自动编码器减少了它们的维度。但是,每当我尝试找到从解码器输出到实际输入的映射时,仅在 1 个向量上它成功地将解码器输出的结果映射到实际输出。我不确定如何提高这里的准确性。 我使用欧几里德距离找到最接近解码器输出的向量。