Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions QR-DQN/QR-DQN.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def __init__(self, state_dim, action_dim):
self.action_dim = action_dim
self.atoms = args.atoms
self.tau = [(2*(i-1)+1)/(2*self.atoms) for i in range(1, self.atoms+1)]
self.huber_loss = tf.keras.losses.Huber(
reduction=tf.keras.losses.Reduction.NONE)
# self.huber_loss = tf.keras.losses.Huber(
# reduction=tf.keras.losses.Reduction.NONE)
self.opt = tf.keras.optimizers.Adam(args.lr)
self.model = self.create_model()

Expand All @@ -61,6 +61,17 @@ def create_model(self):
Reshape([self.action_dim, self.atoms])
])


def huber_loss(self, y_true, y_pred, clip_delta=1.0):
error = y_true - y_pred
cond = tf.keras.backend.abs(error) < clip_delta

squared_loss = 0.5 * tf.keras.backend.square(error)
linear_loss = clip_delta * (tf.keras.backend.abs(error) - 0.5 * clip_delta)

return tf.where(cond, squared_loss, linear_loss)


def quantile_huber_loss(self, target, pred, actions):
pred = tf.reduce_sum(pred * tf.expand_dims(actions, -1), axis=1)
pred_tile = tf.tile(tf.expand_dims(pred, axis=2), [1, 1, self.atoms])
Expand Down