Support Keras multi-backend in tinyAES tutorial#311
Conversation
Summary of ChangesHello @wsxrdv, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the tinyAES tutorial by introducing support for multiple Keras backends. It adapts the data loading and preprocessing mechanisms to seamlessly integrate with both TensorFlow and JAX, ensuring the tutorial remains functional and relevant across different Keras configurations. The changes allow the tutorial to dynamically select the appropriate data pipeline based on the detected backend. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
The pull request introduces support for Keras multi-backend in the tinyAES tutorial, specifically adding a process_batch function and modifying the train function to use a match statement for different Keras backends (TensorFlow and JAX). This change improves the flexibility and compatibility of the tutorial with various Keras configurations. The code is generally well-structured and follows good practices.
| def process_batch(batch: dict[str, Any]) -> tuple[Any, dict[str, Any]]: | ||
| """Processing of a batch of records. The input is a dictionary of string | ||
| and tensor, the output of this function is a tuple the neural network's | ||
| input (trace) and a dictionary of one-hot encoded expected outputs. | ||
| """ | ||
| # The first neural network was using just the first half of the trace: | ||
| inputs = batch["trace1"] | ||
| outputs = { | ||
| "sub_bytes_in_0": | ||
| keras.ops.one_hot( | ||
| batch["sub_bytes_in"][:, 0], | ||
| num_classes=256, | ||
| ), | ||
| } | ||
| return (inputs, outputs) |
| match keras.backend.backend(): | ||
| case "tensorflow": | ||
| train_ds = dataset.as_tfdataset( | ||
| split="train", | ||
| process_record=process_record, | ||
| batch_size=batch_size, | ||
| ) | ||
| validation_ds = dataset.as_tfdataset( | ||
| split="test", | ||
| process_record=process_record, | ||
| batch_size=batch_size, | ||
| ) | ||
| case "jax": | ||
| train_ds = dataset.as_numpy_iterator_rust_batched( | ||
| split="train", | ||
| process_batch=process_batch, | ||
| batch_size=batch_size, | ||
| ) | ||
| validation_ds = dataset.as_numpy_iterator_rust_batched( | ||
| split="test", | ||
| process_batch=process_batch, | ||
| batch_size=batch_size, | ||
| ) | ||
| case _: | ||
| print(f"TODO support {keras.backend.backend() = }") | ||
| return |
There was a problem hiding this comment.
No description provided.