Evaluating pairwise distances between the output of a tf.keras.model
I am trying to create a custom loss function in tensorflow. I am using tensorflow v2.0.rc0 for running the code. Following is the code and the function min_dist_loss computes the pairwise loss between the output of the neural network. Here's the code
python def min_dist_loss(_, y_pred): distances = [] for i in range(0, 16): for j in range(i + 1, 16): distances.append(tf.linalg.norm(y_pred[i] - y_pred[j])) return -tf.reduce_min(distances) and the module is being initialized and compiled as follows
```python import tensorflow as tf from tensorboard.plugins.hparams import api as hp
HP_NUM_UNITS = hp.HParam('num_units', hp.Discrete([6, 7])) HP_OPTIMIZER = hp.HParam('optimizer', hp.Discrete(['adam', 'sgd']))
METRIC_ACCURACY = 'accuracy'
with tf.summary.create_file_writer('logs\hparam_tuning').as_default(): hp.hparams_config( hparams=[HP_NUM_UNITS, HP_OPTIMIZER], metrics=[hp.Metric(METRIC_ACCURACY, display_name='Accuracy')] )
def train_test_model(logdir, hparams): weight1 = np.random.normal(loc=0.0, scale=0.01, size=[4, hparams[HP_NUM_UNITS]]) init1 = tf.constant_initializer(weight1) weight2 = np.random.normal(loc=0.0, scale=0.01, size=[hparams[HP_NUM_UNITS], 7]) init2 = tf.constant_initializer(weight2) model = tf.keras.models.Sequential([ # tf.keras.layers.Flatten(), tf.keras.layers.Dense(hparams[HP_NUM_UNITS], activation=tf.nn.sigmoid, kernel_initializer=init1), tf.keras.layers.Dense(7, activation=tf.nn.sigmoid, kernel_initializer=init2) if hparams[HP_NUM_UNITS] == 6 else None, ]) model.compile( optimizer=hparams[HP_OPTIMIZER], loss=min_dist_loss, # metrics=['accuracy'], ) x_train = [list(k) for k in itertools.product([0, 1], repeat=4)] shuffle(x_train) x_train = 2 * np.array(x_train) - 1 model.fit( x_train, epochs=1, batch_size=16, callbacks=[ tf.keras.callbacks.TensorBoard(logdir), hp.KerasCallback(logdir, hparams) ], ) ```
Now since the tensor object y_pred in the min_dist_loss is an object of shape [?, 7], indexing with i is throwing the following error:
Traceback (most recent call last): File "/home/pc/Documents/user/code/keras_tensorflow/src/try1.py", line 95, in <module> run('logs\hparam_tuning' + run_name, hparams) File "/home/pc/Documents/user/code/keras_tensorflow/src/try1.py", line 78, in run accuracy = train_test_model(run_dir, hparams) File "/home/pc/Documents/user/code/keras_tensorflow/src/try1.py", line 66, in train_test_model hp.KerasCallback(logdir, hparams) File "/home/pc/Documents/user/code/keras_tensorflow/venv/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 734, in fit use_multiprocessing=use_multiprocessing) File "/home/pc/Documents/user/code/keras_tensorflow/venv/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2. 695