-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use weights as defined in dataset.yaml #31
Draft
mazabou
wants to merge
1
commit into
vinam/subtask-interval
Choose a base branch
from
mehdi-pro-182
base: vinam/subtask-interval
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+41
−56
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,13 +253,11 @@ def __init__( | |
modality_spec: ModalitySpec, | ||
sequence_length: float = 1.0, | ||
eval: bool = False, | ||
subtask_weights: Optional[Iterable[float]] = None, | ||
): | ||
self.unit_tokenizer = unit_tokenizer | ||
self.session_tokenizer = session_tokenizer | ||
|
||
self.modality_spec = modality_spec | ||
self.subtask_weights = subtask_weights | ||
|
||
self.latent_step = latent_step | ||
self.num_latents_per_step = num_latents_per_step | ||
|
@@ -308,33 +306,18 @@ def __call__(self, data: Data) -> Dict: | |
if output_values.dtype == np.float64: | ||
output_values = output_values.astype(np.float32) | ||
|
||
session_index = self.session_tokenizer(data.session) | ||
session_index = np.repeat(session_index, len(output_timestamps)) | ||
output_session_index = self.session_tokenizer(data.session) | ||
output_session_index = np.repeat(output_session_index, len(output_timestamps)) | ||
|
||
# Assign each output timestamp a movement_phase | ||
movement_phase_assignment = dict() | ||
for k in data.movement_phases.keys(): | ||
movement_phase_assignment[k] = interval_contains( | ||
data.movement_phases.__dict__[k], | ||
output_timestamps, | ||
) | ||
|
||
# Weights for the output predictions (used in the loss function) | ||
if self.subtask_weights is None: | ||
output_weights = np.ones(len(output_values), dtype=np.float32) | ||
else: | ||
output_weights = np.zeros_like(output_timestamps, dtype=np.float32) | ||
for k in data.movement_phases.keys(): | ||
output_weights = output_weights + ( | ||
movement_phase_assignment[k].astype(float) * self.subtask_weights[k] | ||
) | ||
|
||
# Mask for the output predictions | ||
output_mask = np.ones(len(output_values), dtype=bool) | ||
if self.eval: | ||
# During eval, only evaluate on the subtask specified in the config | ||
eval_movement_phase = data.config["eval_movement_phase"] | ||
output_mask = output_mask & (movement_phase_assignment[eval_movement_phase]) | ||
output_weights = np.ones_like(output_timestamps, dtype=np.float32) | ||
if "weights" in data.config: | ||
weights = data.config["weights"] | ||
for weight_key, weight_value in weights.items(): | ||
# extract the interval from the weight key | ||
weight = data.get_nested_attribute(weight_key) | ||
if not isinstance(weight, Interval): | ||
raise ValueError(f"Weight {weight_key} is not an Interval") | ||
output_weights[isin_interval(output_timestamps, weight)] *= weight_value | ||
|
||
batch = { | ||
# input sequence | ||
|
@@ -346,9 +329,9 @@ def __call__(self, data: Data) -> Dict: | |
"latent_index": latent_index, | ||
"latent_timestamps": latent_timestamps, | ||
# output sequence | ||
"output_session_index": pad8(session_index), | ||
"output_session_index": pad8(output_session_index), | ||
"output_timestamps": pad8(output_timestamps), | ||
"output_mask": pad8(output_mask), | ||
"output_mask": track_mask8(output_session_index), | ||
Comment on lines
-351
to
+334
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went with the |
||
# ground truth targets | ||
"target_values": pad8(output_values), | ||
"target_weights": pad8(output_weights), | ||
|
@@ -361,26 +344,22 @@ def __call__(self, data: Data) -> Dict: | |
return batch | ||
|
||
|
||
def interval_contains( | ||
interval: Interval, x: Union[float, np.ndarray] | ||
) -> Union[bool, np.ndarray]: | ||
r"""If x is a single number, returns True if x is in the interval. | ||
If x is a 1d numpy array, return a 1d bool numpy array. | ||
""" | ||
def isin_interval(timestamps: np.ndarray, interval: Interval) -> np.ndarray: | ||
r"""Check if timestamps are in any of the intervals in the `Interval` object. | ||
|
||
if isinstance(x, float): | ||
if len(interval) == 0: | ||
return False | ||
Args: | ||
timestamps: Timestamps to check. | ||
interval: Interval to check against. | ||
|
||
return np.logical_and(x >= interval.start, x < interval.end).any() | ||
elif isinstance(x, np.ndarray): | ||
if len(interval) == 0: | ||
return np.zeros_like(x, dtype=bool) | ||
Returns: | ||
Boolean mask of the same shape as `timestamps`. | ||
""" | ||
if len(interval) == 0: | ||
return np.zeros_like(timestamps, dtype=bool) | ||
|
||
x_expanded = x[:, None] | ||
y = np.logical_and( | ||
x_expanded >= interval.start[None, :], x_expanded < interval.end[None, :] | ||
) | ||
y = np.logical_or.reduce(y, axis=1) | ||
assert y.shape == x.shape | ||
return y | ||
timestamps_expanded = timestamps[:, None] | ||
mask = np.any( | ||
(timestamps_expanded >= interval.start) & (timestamps_expanded < interval.end), | ||
axis=1, | ||
) | ||
return mask |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work? I think movement_periods.reach_period is not present in the random-target sessions.