Skip to content

BuildingABrain.ipynb - Improved notebook portability and fixed Keras model issues#13

Open
johnwhitegm wants to merge 11 commits into
NVDLI:masterfrom
johnwhitegm:master
Open

BuildingABrain.ipynb - Improved notebook portability and fixed Keras model issues#13
johnwhitegm wants to merge 11 commits into
NVDLI:masterfrom
johnwhitegm:master

Conversation

@johnwhitegm
Copy link
Copy Markdown

Summary

This PR improves the notebook’s portability, reliability, and compatibility across environments (Google Colab, SageMaker/Kaggle, and GitHub preview) while also fixing functional issues in the model definition.

Changes Made

Fix YouTube embed (Colab Error 153)

  • Replaced YouTubeVideo() with a custom iframe embed
  • Added: html referrerpolicy="strict-origin-when-cross-origin"
  • Ensures video renders correctly in Colab

Fix GitHub notebook preview issues

  • GitHub preview was stripping or corrupting HTML inside Python strings (e.g., <iframe>)
  • Resolved by dynamically constructing HTML tags (e.g., "<" + "iframe")
  • Ensures code displays correctly in GitHub and HTML still renders correctly in execution environments

Fix image rendering issue

  • Replaced Wikimedia thumbnail URL (/thumb/...) with direct image URL

Fix model initialization error (Keras ValueError)

  • Issue: ValueError: expected a positive integer for units
  • Root cause: number_of_classes was a NumPy scalar (numpy.uint8)
  • Fix: number_of_classes = int(train_labels.max() + 1)
  • Ensures compatibility with Keras validation logic

Resolve Keras UserWarning (best practice)

  • Updated model definition to use explicit input layer: tf.keras.Input(shape=(28, 28))
  • Removed input_shape argument from Flatten
  • Aligns with modern Keras recommendations for Sequential models

Why This Matters

These changes ensure the notebook:

  • Works consistently across Colab, SageMaker, and Kaggle
  • Displays correctly in GitHub preview
  • Avoids runtime errors and warnings in Keras
  • Follows current best practices for model definition

Testing

  • Verified in Google Colab (video + images render correctly)
  • Verified model builds successfully without errors
  • Verified GitHub preview displays code properly without corruption

Additional Notes

  • Some behaviors (e.g., iframe rendering) are environment-specific due to:
    • Colab iframe security policies
    • GitHub HTML sanitization
  • Workarounds were chosen to maintain cross-platform compatibility

@johnwhitegm johnwhitegm changed the title Improve notebook portability and fix Keras model issues BuildingABrain.ipynb - Improve notebook portability and fix Keras model issues May 5, 2026
@johnwhitegm johnwhitegm changed the title BuildingABrain.ipynb - Improve notebook portability and fix Keras model issues BuildingABrain.ipynb - Improved notebook portability and fixed Keras model issues May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant