Created
February 15, 2018 09:44
-
-
Save rikvdkleij/1bb0a3ec1365fa2868952716f4df3b70 to your computer and use it in GitHub Desktop.
Autoencoder with TF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.file.Paths | |
import org.platanios.tensorflow.api._ | |
import org.platanios.tensorflow.api.learn.Mode | |
import org.platanios.tensorflow.api.learn.layers.Layer | |
import org.platanios.tensorflow.api.ops.variables.RandomNormalInitializer | |
object Autoencoder2 { | |
case class IrisFlowerRecord(sepalLength: Float, sepalWidth: Float, petalLength: Float, petalWidth: Float, irisClass: String) | |
def main(args: Array[String]): Unit = { | |
val fileName = "/irisflowerdata.csv" | |
val data = parseIrisFlowerData(fileName).map(r => Tensor(r.petalLength, r.petalWidth, r.sepalLength, r.sepalWidth)) | |
val dataset = tf.data.fromGenerator(() => data.toIterable, FLOAT32, Shape(-1, 4)) | |
// Because input === output in Supervised model | |
val trainDataset = dataset.zip(dataset) | |
val learningRate = 0.1 | |
val epoch = 1000 | |
val inputDim = 4 | |
val hiddenDim = 3 | |
val input = tf.learn.Input(FLOAT32, Shape(-1, -1)) | |
val trainingInput = tf.learn.Input(FLOAT32, Shape(-1, -1)) | |
val encoderLayer = new Layer[Output, Output]("Encoder") { | |
override protected def _forward(input: Output, node: Mode): Output = { | |
val encoderWeights = tf.variable(name = "weights1", initializer = RandomNormalInitializer(), shape = Shape(inputDim, hiddenDim), dataType = FLOAT32) | |
val encoderBiases = tf.variable(name = "biases1", dataType = tf.zeros(dataType = FLOAT32, shape = Shape(hiddenDim)).dataType, shape = Shape(hiddenDim)) | |
tf.tanh(tf.matmul(input, encoderWeights) + encoderBiases) | |
} | |
override val layerType: String = "EncoderType" | |
} | |
val decoderLayer = new Layer[Output, Output]("Decoder") { | |
override protected def _forward(input: Output, node: Mode): Output = { | |
val decoderWeights = tf.variable(name = "weights2", initializer = RandomNormalInitializer(), shape = Shape(hiddenDim, inputDim), dataType = FLOAT32) | |
val decoderBiases = tf.variable(name = "biases2", dataType = tf.zeros(dataType = FLOAT32, shape = Shape(inputDim)).dataType, shape = Shape(inputDim)) | |
tf.matmul(input, decoderWeights) + decoderBiases | |
} | |
override val layerType: String = "DecoderType" | |
} | |
val layer = encoderLayer >> decoderLayer | |
val loss = tf.learn.SequenceLoss("Loss/Sequence") >> | |
tf.learn.Sum("Loss/Sum") >> | |
tf.learn.ScalarSummary("Loss/Summary", "Loss") | |
val optimizer = tf.train.AdaGrad(learningRate) | |
// What is difference between input and trainingInput? | |
val model = tf.learn.Model(input, layer, trainingInput, loss, optimizer) | |
val summariesDir = Paths.get("autoencoderSummaries") | |
val estimator = tf.learn.InMemoryEstimator( | |
model, | |
tf.learn.Configuration(Some(summariesDir)), | |
tf.learn.StopCriteria(maxSteps = Some(epoch)), | |
Set( | |
// tf.learn.LossLogger(trigger = tf.learn.StepHookTrigger(10)), | |
// tf.learn.StepRateLogger(log = false, summaryDir = summariesDir, trigger = tf.learn.StepHookTrigger(100)), | |
tf.learn.SummarySaver(summariesDir, tf.learn.StepHookTrigger(10)), | |
tf.learn.CheckpointSaver(summariesDir, tf.learn.StepHookTrigger(1000))), | |
tensorBoardConfig = tf.learn.TensorBoardConfig(summariesDir, reloadInterval = 1)) | |
estimator.train(() => trainDataset, tf.learn.StopCriteria(maxSteps = Some(10000))) | |
} | |
private def parseIrisFlowerData(fileName: String) = { | |
val inputStream = getClass.getResourceAsStream(fileName) | |
val lines = scala.io.Source.fromInputStream(inputStream).getLines | |
lines.map(l => { | |
val strings = l.split(",") | |
IrisFlowerRecord(strings(0).toFloat, strings(1).toFloat, strings(2).toFloat, strings(3).toFloat, strings(4)) | |
} | |
).toArray | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment