pdfcoffee
Chapter 9self.conv4 = Conv2D(1, 3, 1, activation='sigmoid',padding='same')self.upsample = UpSampling2D((2, 2))def call(self, encoded):x = self.conv1(encoded)#print("dx1", x.shape)x = self.upsample(x)#print("dx2", x.shape)x = self.conv2(x)x = self.upsample(x)x = self.conv3(x)x = self.upsample(x)return self.conv4(x)6. We combine the encoder and decoder to make an autoencoder model.This remains exactly the same as before:class Autoencoder(K.Model):def __init__(self, filters):super(Autoencoder, self).__init__()self.encoder = Encoder(filters)self.decoder = Decoder(filters)def call(self, input_features):#print(input_features.shape)encoded = self.encoder(input_features)#print(encoded.shape)reconstructed = self.decoder(encoded)#print(reconstructed.shape)return reconstructed7. Now we instantiate our model, then specify the binary cross entropy as theloss function and Adam as the optimizer in the compile() method. Then, fitthe model to the training dataset:model = Autoencoder(filters)model.compile(loss='binary_crossentropy', optimizer='adam')loss = model.fit(x_train_noisy,x_train,validation_data=(x_test_noisy, x_test),epochs=max_epochs,batch_size=batch_size)[ 363 ]
Autoencoders8. You can see the loss curve as the model is trained; in 50 epochs the loss wasreduced to 0.0988:plt.plot(range(max_epochs), loss.history['loss'])plt.xlabel('Epochs')plt.ylabel('Loss')plt.show()9. And finally, you can see the wonderful reconstructed images from the noisyinput images:number = 10 # how many digits we will displayplt.figure(figsize=(20, 4))for index in range(number):# display originalax = plt.subplot(2, number, index + 1)plt.imshow(x_test_noisy[index].reshape(28, 28), cmap='gray')ax.get_xaxis().set_visible(False)ax.get_yaxis().set_visible(False)# display reconstructionax = plt.subplot(2, number, index + 1 + number)plt.imshow(tf.reshape(model(x_test_noisy)[index], (28, 28)),cmap='gray')ax.get_xaxis().set_visible(False)ax.get_yaxis().set_visible(False)plt.show()[ 364 ]
- Page 347 and 348: Recurrent Neural NetworksFinally, w
- Page 349 and 350: Recurrent Neural Networks38 - val_l
- Page 351 and 352: Recurrent Neural NetworksIf you wou
- Page 353 and 354: Recurrent Neural NetworksExample
- Page 355 and 356: Recurrent Neural NetworksNext we ha
- Page 357 and 358: Recurrent Neural Networksself.embed
- Page 359 and 360: Recurrent Neural NetworksThis is a
- Page 361 and 362: Recurrent Neural Networksreturn np.
- Page 363 and 364: Recurrent Neural NetworksAttention
- Page 365 and 366: Recurrent Neural NetworksFinally, V
- Page 367 and 368: Recurrent Neural Networks# query.sh
- Page 369 and 370: Recurrent Neural Networksself.atten
- Page 371 and 372: Recurrent Neural Networks30 try to
- Page 373 and 374: Recurrent Neural Networks3. Because
- Page 375 and 376: Recurrent Neural NetworksSummaryIn
- Page 377 and 378: Recurrent Neural Networks18. Shi, X
- Page 380 and 381: AutoencodersAutoencoders are feed-f
- Page 382 and 383: Depending upon the actual dimension
- Page 384 and 385: • __init__(): Here, you define al
- Page 386 and 387: Chapter 9And then we reshape the te
- Page 388 and 389: Chapter 9plt.imshow(x_test[index].r
- Page 390 and 391: Chapter 9Keeping the rest of the co
- Page 392 and 393: noise = np.random.normal(loc=0.5, s
- Page 394 and 395: Chapter 9x_train,validation_data=(x
- Page 396 and 397: Chapter 9import matplotlib.pyplot a
- Page 400 and 401: Chapter 9You can see that the image
- Page 402 and 403: [ 367 ]Chapter 9Let us use the prec
- Page 404 and 405: Chapter 9Our autoencoder model take
- Page 406 and 407: We train the autoencoder for 20 epo
- Page 408 and 409: Chapter 90.97905576229095460.989323
- Page 410 and 411: Unsupervised LearningThis chapter d
- Page 412 and 413: Chapter 10Next we load the MNIST da
- Page 414 and 415: Chapter 10TensorFlow Embedding APIT
- Page 416 and 417: 3. Recompute the centroids using cu
- Page 418 and 419: Chapter 10Figure 4: Plot of the fin
- Page 420 and 421: Chapter 10In SOMs, neurons are usua
- Page 422 and 423: [ 387 ]Chapter 10Colour mapping usi
- Page 424 and 425: Chapter 10# Calculating Neighbourho
- Page 426 and 427: We will also need to normalize the
- Page 428 and 429: Chapter 10ρρ(vv oo |h oo ) = σσ
- Page 430 and 431: # Generate the sample probabilityde
- Page 432 and 433: Chapter 10And the reconstructed ima
- Page 434 and 435: Chapter 10inpX = rbm.rbm_output(inp
- Page 436 and 437: Chapter 10(60000, 28, 28) (60000,)(
- Page 438 and 439: Chapter 10Figure 11: Summary of the
- Page 440 and 441: Chapter 10This chapter, along with
- Page 442 and 443: Reinforcement LearningThis chapter
- Page 444 and 445: Chapter 11And unlike unsupervised l
- Page 446 and 447: Chapter 11Normally, the value is de
Chapter 9
self.conv4 = Conv2D(1, 3, 1, activation='sigmoid',
padding='same')
self.upsample = UpSampling2D((2, 2))
def call(self, encoded):
x = self.conv1(encoded)
#print("dx1", x.shape)
x = self.upsample(x)
#print("dx2", x.shape)
x = self.conv2(x)
x = self.upsample(x)
x = self.conv3(x)
x = self.upsample(x)
return self.conv4(x)
6. We combine the encoder and decoder to make an autoencoder model.
This remains exactly the same as before:
class Autoencoder(K.Model):
def __init__(self, filters):
super(Autoencoder, self).__init__()
self.encoder = Encoder(filters)
self.decoder = Decoder(filters)
def call(self, input_features):
#print(input_features.shape)
encoded = self.encoder(input_features)
#print(encoded.shape)
reconstructed = self.decoder(encoded)
#print(reconstructed.shape)
return reconstructed
7. Now we instantiate our model, then specify the binary cross entropy as the
loss function and Adam as the optimizer in the compile() method. Then, fit
the model to the training dataset:
model = Autoencoder(filters)
model.compile(loss='binary_crossentropy', optimizer='adam')
loss = model.fit(x_train_noisy,
x_train,
validation_data=(x_test_noisy, x_test),
epochs=max_epochs,
batch_size=batch_size)
[ 363 ]