使用自动编码器在每个时期中传递到隐藏层后检索输入值

Retrieve input values after passing to hidden layer in every epoch by using autoencoder

我想在每个时期都传递到隐藏层后检索输入值。我使用自动编码器从输入 (iris.csv) 训练数据。在下面的代码中,我不知道如何干预 fit() 函数并获取数据值。谢谢!

data = pd.read_csv("data/iris.csv")
x_train, x_test, y_train, y_test = train_test_split(data[['SepalLengthCm', 'SepalWidthCm',
                                                          'PetalLengthCm', 'PetalWidthCm']],
                                                    data['Species'], test_size=0.1, random_state=1)
input_img = Input(shape=(input_dim,))

# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim)(input_img)

# "decoded" is the lossy reconstruction of the input
decoded = Dense(input_dim)(encoded)

# this model maps an input to its reconstruction
autoencoder = Model(input_img, decoded)

# this model maps an input to its encoded representation
encoder = Model(input_img, encoded)

# create a placeholder for an encoded (2-dimensional) input
encoded_input = Input(shape=(encoding_dim,))

# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]

# create the decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))

hidden_layer = autoencoder.layers[0] # get hidden layer values
hidden_layer_decoder = Model(encoded_input, hidden_layer(encoded_input))

autoencoder.compile(loss='mean_squared_error', optimizer='sgd')
autoencoder.fit(x_train, x_train,
                    epochs=500,
                    batch_size=135,
                    shuffle=True,
                    validation_data=(x_test, x_test),
                    verbose=0,
                    callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])

# I want to get these values in every epoch -----
encoded_datapoints = encoder.predict(data[['SepalLengthCm', 'SepalWidthCm','PetalLengthCm', 
'PetalWidthCm']])
decoded_datapoints = decoder.predict(encoded_datapoints)

hidden_layer_dataset = hidden_layer_decoder.predict(encoded_datapoints)
print('Hidden Datapoints :')
print(hidden_layer_dataset)

你可以把 fit 放在一个循环中

for _ in range(500):
    autoencoder.fit(x_train, x_train,epochs=1,batch_size=135,
                    shuffle=True,validation_data=(x_test, x_test),
                    verbose=0,
                    callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])

    encoded_datapoints = encoder.predict(data[['SepalLengthCm','SepalWidthCm','PetalLengthCm', 
'PetalWidthCm']])
    decoded_datapoints = decoder.predict(encoded_datapoints)

    hidden_layer_dataset = hidden_layer_decoder.predict(encoded_datapoints)
    print('Hidden Datapoints :')
    print(hidden_layer_dataset)