0

I'm trying to calculate a simple loss function for a mixture density network. The output's shape (batch_size=128,2) and the mu parameter's shape is (batch_size, 2* Num_mixes). To be able to calculate their difference, I need to tile the output so it has the same shape as the mu but I get the subject error

I don't know why despite the (128,2) shape of the output, its rank is 0. Can you please help?

def calc_pdf(y, mu, var):
    """Calculate component density"""
    print(y.shape)
    print(mu.shape)
    value = tf.subtract(y, mu)**2

    value = (1/tf.math.sqrt(2 * np.pi * var)) * tf.math.exp((-1/(2*var)) * value)
    print(value.shape)
    return value
y shape is (128, 2)
mu shape is (128, 60)
y rank is Tensor("Rank:0", shape=(), dtype=int32)

   ValueError: Shape must be rank 1 but is rank 0 for 'Tile' (op: 'Tile') with input shapes: [128,2], [].
pnaseri
  • 95
  • 2
  • 9
  • It turned out its value is zero for all the elements – pnaseri Oct 15 '19 at 21:23
  • Using @tf.function at the beginning of the training made the values zero. Removed it. `#@tf.function def train_step(model, optimizer, train_x, train_y): print('train_y in train_step {}'.format(train_y)) # GradientTape: Trace operations to compute gradients with tf.GradientTape() as tape: mu_, var_, pi_ = model(train_x) loss = mdn_loss(train_y, pi_, mu_, var_) # compute and apply gradients gradients = tape.gradient(loss, model.trainable_variables) optimizer.apply_gradients(zip(gradients, model.trainable_variables)) return loss` – pnaseri Oct 15 '19 at 21:34
  • How could you do `tf.substract(y,mu)` if they have different dims? – zihaozhihao Oct 15 '19 at 23:36
  • I tiled y: `value = tf.subtract(tf.tile(y, [1, N_MIXES]), mu)**2` – pnaseri Oct 16 '19 at 14:19

0 Answers0