Showing posts with label torch. Show all posts
Showing posts with label torch. Show all posts

Friday, December 19, 2025

Reinforcement Learning

An important ML training paradigm is Reinforcement Learning (RL). RL models rely on a reward value generated at the end of each training run/ epoch to update the parameters (weights) of the model. This is different from the other ML methods such as Supervised Learning where labelled data/ examples are given from which the models learns. It's is also different from the Unsupervised Learning approach where inherent features of the unlabeled data are explored by the model through the learning phase to identify clusters, etc.    

The keras-io examples has some RL implementations such as actor_critic, ppo, etc. All of them work solely with the TensorFlow (tf) backend. In keras_io_examples_rl these have been ported to the Torch/ PyTorch backend. The typical changes include:   

  • Torch Imports 
  • Use torch specific Optimizer - torch.optim.Adam
    • deep_q_network_breakout_pytorch () requires grad_clipping, in torch done before optimizer.step() 
  • Gradient computations in torch 
    • Replace tf GradientTape with torch autograd 
    • Disable gradient globally torch.set_grad_enabled(False)
    • Enable autograd within specific flows/ methods where needed
    • Call loss.backward(), optimizer.step() for backpropagation
  • Few torch specific tensor & function changes/ wrappers  

The ported pytorch compatible files are:


References

  • http://www.derongliu.org/adp/adp-cdrom/Barto1983.pdf
  • https://hal.inria.fr/hal-00840470/document
  • https://link.springer.com/content/pdf/10.1007/BF00992698.pdf
  • https://www.semanticscholar.org/paper/Human-level-control-through-deep-reinforcement-Mnih-Kavukcuoglu/340f48901f72278f6bf78a04ee5b01df208cc508
  • Continuous control with deep reinforcement learning: https://arxiv.org/abs/1509.02971)
  • Deep Deterministic Policy Gradient (DDPG) 
  • https://gymnasium.farama.org/
  • Reinforcement Learning: An Introduction, Richard S. Sutton and Andrew G. Barto

Saturday, November 22, 2025

Text Classification from Scratch using PyTorch

The AI/ ML development framework Keras 3x in recent times has introduced support for Torch & Jax backends in addition to Tensorflow. However, given Keras's Tensorflow legacy large sections of the code are deeply integrated with Tensorflow. 

One such piece of code is the text_classification_from_scratch.py from keras-io/examples project. Without tensorflow this piece of code simply doesn't run!

Here's text_classification_torch.py a Torch/ PyTorch port of the same code. The bits that needed modification were:

  • Removing all tensorflow related imports
  • Loading the Imdb text files in "grain" format in place of "tf" format, by passing the appropriate param: 

    keras.utils->text_dataset_from_directory(format="grain") 

Which obviously needed grain to be installed:

    pip3 install grain 

  • Using torchtext for Vocab/ Tokenizer/ Vectorizing :

    pip3 install torchtext

  • Few other changes such as ensure max_features constraint's honoured, text is standardized, padded, and so on.