mardi 29 octobre 2019

Sampling from normal distribution not working in tflite model

In variational autoencoders, the model samples from a normal distribution in the latent space. That means that even when providing the same inputs for the model, the output will be slightly different during each run. I want to use such a VAE as tflite model.

Even though the model is convertible and can be loaded, the result is always exactly the same. Due to the random functions not being included in tflite as of now, I allow the model to use actual tensorflow ops. Below is the code for training, converting and testing the model.

import numpy as np
import tensorflow as tf
from tensorflow import keras

print(tf.__version__)

### TRAINING DATA
training_data = np.random.rand(1000, 10)
train_dataset = tf.data.Dataset.from_tensor_slices((training_data, training_data))
train_dataset = train_dataset.shuffle(1000).batch(100)

### MODEL WITH RANDOM NORMAL
x = keras.layers.Input(shape=(10,))
z_mu = keras.layers.Dense(5)(x)
z_sigma = keras.layers.Dense(5, activation=tf.nn.sigmoid)(x)
eps = tf.random.normal(shape=(5,))  # THIS should ensure different outputs on each run
z = z_mu + eps * z_sigma
x_decoded = keras.layers.Dense(10)(z)
model = keras.models.Model(x, x_decoded)

### LOSS
recon_err = tf.reduce_sum(tf.abs(x - x_decoded), axis=1)
kl_div = -.5 * tf.reduce_sum(1 + 2 * tf.math.log(z_sigma) - tf.square(z_mu) - tf.square(z_sigma), axis=1)
total_loss = tf.reduce_mean(recon_err + kl_div)
model.add_loss(total_loss)

### TRAINING
model.compile(optimizer='adam')
print(model.summary())
model.fit(train_dataset, epochs=5)

### SAVE
keras_file = 'vae_test.h5'
keras.models.save_model(model, keras_file)

### CONVERSION
converter = tf.compat.v1.lite.TFLiteConverter.from_keras_model_file(keras_file)
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
                                       tf.lite.OpsSet.SELECT_TF_OPS]
tflite_file_name = 'vae.tflite'
tflite_model = converter.convert()
open(tflite_file_name, 'wb').write(tflite_model)

### TEST DATA
dataset = np.zeros((1, 10)).astype(np.float32)

### TEST THE MODEL
interpreter = tf.lite.Interpreter(model_path=tflite_file_name)
interpreter.allocate_tensors()
input_detail = interpreter.get_input_details()[0]
output_detail = interpreter.get_output_details()[0]
print('Input detail: ', input_detail)
print('Output detail: ', output_detail)

interpreter.set_tensor(input_detail['index'], dataset)
interpreter.invoke()
pred_litemodel = interpreter.get_tensor(output_detail['index'])
print(f'\nPrediction')
print(pred_litemodel)
print()

interpreter.set_tensor(input_detail['index'], dataset)
interpreter.invoke()
pred_litemodel = interpreter.get_tensor(output_detail['index'])
print(f'\nPrediction')
print(pred_litemodel)
print()

Can somebody explain why the model behaves like a deterministic function?




Aucun commentaire:

Enregistrer un commentaire