|
| 1 | +// Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Based on the paper: "Auto-Encoding Variational Bayes" |
| 16 | +// by Diederik P Kingma and Max Welling |
| 17 | +// Reference implementation: https://github.com/pytorch/examples/blob/master/vae/main.py |
| 18 | + |
| 19 | +import Datasets |
| 20 | +import Foundation |
| 21 | +import ModelSupport |
| 22 | +import TensorFlow |
| 23 | + |
| 24 | +let epochCount = 10 |
| 25 | +let imageHeight = 28 |
| 26 | +let imageWidth = 28 |
| 27 | + |
| 28 | +let outputFolder = "./output/" |
| 29 | +let dataset = MNIST(batchSize: 128, flattening: true) |
| 30 | + |
| 31 | +let inputDim = 784 // 28*28 for any MNIST |
| 32 | +let hiddenDim = 400 |
| 33 | +let latentDim = 20 |
| 34 | + |
| 35 | +// Variational Autoencoder |
| 36 | +public struct VAE: Layer { |
| 37 | + // Encoder |
| 38 | + public var encoderDense1: Dense<Float> |
| 39 | + public var encoderDense2_1: Dense<Float> |
| 40 | + public var encoderDense2_2: Dense<Float> |
| 41 | + // Decoder |
| 42 | + public var decoderDense1: Dense<Float> |
| 43 | + public var decoderDense2: Dense<Float> |
| 44 | + |
| 45 | + public init() { |
| 46 | + self.encoderDense1 = Dense<Float>( |
| 47 | + inputSize: inputDim, outputSize: hiddenDim, activation: relu) |
| 48 | + self.encoderDense2_1 = Dense<Float>(inputSize: hiddenDim, outputSize: latentDim) |
| 49 | + self.encoderDense2_2 = Dense<Float>(inputSize: hiddenDim, outputSize: latentDim) |
| 50 | + |
| 51 | + self.decoderDense1 = Dense<Float>( |
| 52 | + inputSize: latentDim, outputSize: hiddenDim, activation: relu) |
| 53 | + self.decoderDense2 = Dense<Float>(inputSize: hiddenDim, outputSize: inputDim) |
| 54 | + } |
| 55 | + |
| 56 | + @differentiable |
| 57 | + public func callAsFunction(_ input: Tensor<Float>) -> [Tensor<Float>] { |
| 58 | + // Encode |
| 59 | + let intermediateInput = encoderDense1(input) |
| 60 | + let mu = encoderDense2_1(intermediateInput) |
| 61 | + let logVar = encoderDense2_2(intermediateInput) |
| 62 | + |
| 63 | + // Re-parameterization trick |
| 64 | + let std = exp(0.5 * logVar) |
| 65 | + let epsilon = Tensor<Float>(randomNormal: std.shape) |
| 66 | + let z = mu + epsilon * std |
| 67 | + |
| 68 | + // Decode |
| 69 | + let output = z.sequenced(through: decoderDense1, decoderDense2) |
| 70 | + return [output, mu, logVar] |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +var vae = VAE() |
| 75 | +let optimizer = Adam(for: vae, learningRate: 1e-3) |
| 76 | + |
| 77 | +// Loss function: sum of the KL divergence of the embeddings and the cross entropy loss between the input and it's reconstruction. |
| 78 | +func vaeLossFunction( |
| 79 | + input: Tensor<Float>, output: Tensor<Float>, mu: Tensor<Float>, logVar: Tensor<Float> |
| 80 | +) -> Tensor<Float> { |
| 81 | + let crossEntropy = sigmoidCrossEntropy(logits: output, labels: input, reduction: _sum) |
| 82 | + let klDivergence = -0.5 * (1 + logVar - pow(mu, 2) - exp(logVar)).sum() |
| 83 | + return crossEntropy + klDivergence |
| 84 | +} |
| 85 | + |
| 86 | +// TODO: Find a cleaner way of extracting individual images that doesn't require a second dataset. |
| 87 | +let singleImageDataset = MNIST(batchSize: 1, flattening: true) |
| 88 | +let individualTestImages = singleImageDataset.test |
| 89 | +var testImageIterator = individualTestImages.sequenced() |
| 90 | + |
| 91 | +// Training loop |
| 92 | +for epoch in 1...epochCount { |
| 93 | + // Test for each epoch |
| 94 | + if let nextIndividualImage = testImageIterator.next() { |
| 95 | + let sampleTensor = nextIndividualImage.first |
| 96 | + let sampleImage = Tensor( |
| 97 | + shape: [1, imageHeight * imageWidth], scalars: sampleTensor.scalars) |
| 98 | + |
| 99 | + let testOutputs = vae(sampleImage) |
| 100 | + let testImage = testOutputs[0] |
| 101 | + let testMu = testOutputs[1] |
| 102 | + let testLogVar = testOutputs[2] |
| 103 | + if epoch == 1 || epoch % 10 == 0 { |
| 104 | + do { |
| 105 | + try saveImage( |
| 106 | + sampleImage, shape: (imageWidth, imageHeight), format: .grayscale, |
| 107 | + directory: outputFolder, name: "epoch-\(epoch)-input") |
| 108 | + try saveImage( |
| 109 | + testImage, shape: (imageWidth, imageHeight), format: .grayscale, |
| 110 | + directory: outputFolder, name: "epoch-\(epoch)-output") |
| 111 | + } catch { |
| 112 | + print("Could not save image with error: \(error)") |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + let sampleLoss = vaeLossFunction( |
| 117 | + input: sampleImage, output: testImage, mu: testMu, logVar: testLogVar) |
| 118 | + print("[Epoch: \(epoch)] Loss: \(sampleLoss)") |
| 119 | + } |
| 120 | + |
| 121 | + for batch in dataset.training.sequenced() { |
| 122 | + let x = batch.first |
| 123 | + |
| 124 | + let 𝛁model = TensorFlow.gradient(at: vae) { vae -> Tensor<Float> in |
| 125 | + let outputs = vae(x) |
| 126 | + let output = outputs[0] |
| 127 | + let mu = outputs[1] |
| 128 | + let logVar = outputs[2] |
| 129 | + return vaeLossFunction(input: x, output: output, mu: mu, logVar: logVar) |
| 130 | + } |
| 131 | + |
| 132 | + optimizer.update(&vae, along: 𝛁model) |
| 133 | + } |
| 134 | +} |
0 commit comments