Hello , I try to understand how to add trainable variable in my model.
I wrote some code :
k1 = tf.Variable(0.5, dtype=‘float32’, trainable=True)
k2 = tf.Variable(0.5, dtype=‘float32’, trainable=True)
k3 = tf.Variable(0.5, dtype=‘float32’, trainable=True)
def custom__loss(y_true, y_pred):
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=y_true, logits=y_pred))
cosine0 = cosine_dist(y_true[:MAX_BITS],y_pred[:MAX_BITS])
cosine1 = cosine_dist(y_true[MAX_BITS:2MAX_BITS],y_pred[MAX_BITS:2MAX_BITS])
cosine2 = cosine_dist(y_true[2MAX_BITS:3MAX_BITS],y_pred[2MAX_BITS:3MAX_BITS])
error = custom___error(y_true,y_pred)
loss = loss * k1 + cosine0 * k2 + cosine1 * k2 + cosine2 * k2 + k3 * error
return loss
But the variables k1, k2, k3 never change : how can I add them to the trainable variables list ?
Regards
new test with
def custom__loss( y_true, y_pred, k1, k2, k3):
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=y_true, logits=y_pred))
cosine0 = cosine_dist(y_true[:MAX_BITS],y_pred[:MAX_BITS])
cosine1 = cosine_dist(y_true[MAX_BITS:2MAX_BITS],y_pred[MAX_BITS:2MAX_BITS])
cosine2 = cosine_dist(y_true[2MAX_BITS:3MAX_BITS],y_pred[2MAX_BITS:3MAX_BITS])
error = custom___error(y_true,y_pred)
loss = loss * k1 + cosine0 * k2 + cosine1 * k2 + cosine2 * k2 + k3 * error
return loss
model.compile(loss=lambda y_true, y_pred: custom__loss(y_true, y_pred, k1, k2, k3), optimizer=optimizer, metrics=[‘accuracy’,custom_error,custom___error])
no change :
k1: <tf.Variable ‘Variable:0’ shape=() dtype=float32, numpy=0.5>
k2: <tf.Variable ‘Variable:0’ shape=() dtype=float32, numpy=0.5>
k3: <tf.Variable ‘Variable:0’ shape=() dtype=float32, numpy=0.5>