-
I am trying to replicate the simplest Autoencoder model found in this Keras example. After looking at this DJL example I don't think I understand the API well enough to do this or I simply don't know how the Autoencoder works. Hopping someone can shed some light on this. First things first. The Autoencoder structure and set-up used in the example referred to above is as follows:
So I used the following Scala code: val relu:JFunction[NDList, NDList] = Activation.relu
val sigmoid:JFunction[NDList, NDList] = Activation.sigmoid
def net1(): SequentialBlock =
val net = SequentialBlock()
net.add(Blocks.batchFlattenBlock(784))
//net.add(Activation::relu)
net.add(relu)
net.add(Linear.builder().setUnits(32).build())
net.add(sigmoid)
net.add(Linear.builder().setUnits(784).build())
net.setInitializer( NormalInitializer(0.01f))
net At this point I have 2 questions. The first is, does the above set-up correctly represent the Keras network? Unlike the DJL example, I seem to have placed 2 lambdas one after the other ( The other question is related to the loss: I have found the following losses in the DJL API: I assume that these loss functions are for classification only. I am using: DefaultTrainingConfig(Loss.softmaxCrossEntropyLoss()) But I assume that when I call: EasyTrain.fit(trainer, trainSetUp.numEpochs, trainingSet, validateSet) where the data sets are of type TIA |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I have just realized a fundamental error of mine. The What is the best way to set-up the MNIST dataset so that I can calculate the loss between the input and itself (i.e: the label is the input) using the TIA |
Beta Was this translation helpful? Give feedback.
-
The relu needs to be between (or after) each usage of Linear. The batchFlatten is not a linear transformation, just a reshape from a 2D image into a 1D feature vector. What the Keras (simplest possible autoencoder) is doing is {Linear, relu, Linear, sigmoid}. We would just add a reshape (batchFlatten) before all of that. For your loss, the softmaxCrossEntropyLoss is definitely not right. It is a classification loss where each value in your prediction is a different class and the label indicates which class is correct. What you are looking for here is a pixel by pixel comparison that says that the original and encoded images should be as similar as possible. The SigmoidBinaryCrossEntropyLoss may work (although double check that the Mnist dataset produces inputs from 0 to 1). You could also try the L2Loss. |
Beta Was this translation helpful? Give feedback.
The relu needs to be between (or after) each usage of Linear. The batchFlatten is not a linear transformation, just a reshape from a 2D image into a 1D feature vector. What the Keras (simplest possible autoencoder) is doing is {Linear, relu, Linear, sigmoid}. We would just add a reshape (batchFlatten) before all of that.
For your loss, the softmaxCrossEntropyLoss is definitely not right. It is a classification loss where each value in your prediction is a different class and the label indicates which class is correct. What you are looking for here is a pixel by pixel comparison that says that the original and encoded images should be as similar as possible. The SigmoidBinaryCrossEntropyLo…