pdfcoffee

soumyasankar99
from soumyasankar99 More from this publisher
09.05.2023 Views

We train the autoencoder for 20 epochs using the following code. 20 epochs werechosen because the MSE loss converges within this time:num_train_steps = len(Xtrain) // BATCH_SIZEnum_test_steps = len(Xtest) // BATCH_SIZEsteps_per_epoch=num_train_steps,epochs=NUM_EPOCHS,validation_data=test_gen,validation_steps=num_test_steps,history = autoencoder.fit_generator(train_gen,steps_per_epoch=num_train_steps,epochs=NUM_EPOCHS,validation_data=test_gen,validation_steps=num_test_steps)Chapter 9The results of the training are shown as follows. As you can see, the training MSEreduces from 0.1161 to 0.0824 and the validation MSE reduces from 0.1097 to 0.0820:Since we are feeding in a matrix of embeddings, the output will also be a matrix ofword embeddings. Since the embedding space is continuous and our vocabulary isdiscrete, not every output embedding will correspond to a word. The best we cando is to find a word that is closest to the output embedding in order to reconstructthe original text. This is a bit cumbersome, so we will evaluate our autoencoder ina different way.Since the objective of the autoencoder is to produce a good latent representation,we compare the latent vectors produced from the encoder using the original inputversus the output of the autoencoder.[ 371 ]

AutoencodersFirst, we extract the encoder component into its own network:encoder = Model(autoencoder.input, autoencoder.get_layer("encoder_lstm").output)Then we run the autoencoder on the test set to return the predicted embeddings.We then send both the input embedding and the predicted embedding through theencoder to produce sentence vectors from each and compare the two vectors usingcosine similarity. Cosine similarities close to "one" indicate high similarity and thoseclose to "zero" indicate low similarity. The following code runs against a randomsubset of 500 test sentences and produces some sample values of cosine similaritiesbetween the sentence vectors generated from the source embedding and thecorresponding target embedding produced by the autoencoder:def compute_cosine_similarity(x, y):return np.dot(x, y) / (np.linalg.norm(x, 2) * np.linalg.norm(y,2))k = 500cosims = np.zeros((k))i= 0for bid in range(num_test_steps):xtest, ytest = test_gen.next()ytest_ = autoencoder.predict(xtest)Xvec = encoder.predict(xtest)Yvec = encoder.predict(ytest_)for rid in range(Xvec.shape[0]):if i >= k:breakcosims[i] = compute_cosine_similarity(Xvec[rid], Yvec[rid])if i <= 10:print(cosims[i])i += 1if i >= k:breakThe first 10 values of cosine similarities are shown as follows. As we can see, thevectors seem to be quite similar:0.9846865534782410.98157465457916260.97936713695526120.98051124811172490.9630994200706482[ 372 ]

We train the autoencoder for 20 epochs using the following code. 20 epochs were

chosen because the MSE loss converges within this time:

num_train_steps = len(Xtrain) // BATCH_SIZE

num_test_steps = len(Xtest) // BATCH_SIZE

steps_per_epoch=num_train_steps,

epochs=NUM_EPOCHS,

validation_data=test_gen,

validation_steps=num_test_steps,

history = autoencoder.fit_generator(train_gen,

steps_per_epoch=num_train_steps,

epochs=NUM_EPOCHS,

validation_data=test_gen,

validation_steps=num_test_steps)

Chapter 9

The results of the training are shown as follows. As you can see, the training MSE

reduces from 0.1161 to 0.0824 and the validation MSE reduces from 0.1097 to 0.0820:

Since we are feeding in a matrix of embeddings, the output will also be a matrix of

word embeddings. Since the embedding space is continuous and our vocabulary is

discrete, not every output embedding will correspond to a word. The best we can

do is to find a word that is closest to the output embedding in order to reconstruct

the original text. This is a bit cumbersome, so we will evaluate our autoencoder in

a different way.

Since the objective of the autoencoder is to produce a good latent representation,

we compare the latent vectors produced from the encoder using the original input

versus the output of the autoencoder.

[ 371 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!