DEV Community

Aman Shekhar
Aman Shekhar

Posted on

Show HN: I trained a 125M model to autocomplete piano on-device

You know that feeling when you stumble upon a tech project that just ignites a spark in you? Well, I recently had that moment when I decided to train a 125M model to autocomplete piano music right on my device. And let me tell you—it’s been a wild ride!

Diving into the Deep End

I’ve always had a soft spot for music and technology. I mean, what could be cooler than blending the two? Ever wondered why AI-generated music hasn’t taken over the world yet? I certainly did! So, I rolled up my sleeves and started experimenting with a model that could predict the next notes in a melody as you play. The thought of creating a digital piano buddy was just too tempting to resist.

When I first booted up my model, I had high hopes. I fed it a bunch of MIDI files—classics from Beethoven to contemporary pop. To be honest, I expected magic to happen overnight. Spoiler alert: it didn’t. My first attempts were, let’s say, less than harmonious. The notes it generated sounded more like a cat walking across a keyboard than a symphony.

The Learning Curve

Training an AI model isn’t as simple as throwing data at it and hoping for the best. I quickly learned that my initial approach was way too naive. The model was overfitting like a champ—essentially memorizing the training data but failing to generalize. After a few frustrating hours (okay, more like days) of tinkering with hyperparameters, I finally adjusted the learning rate and batch size. A little patience goes a long way, right?

I also started using techniques like data augmentation. I created variations of the MIDI files by shifting notes and adjusting tempos. It’s like baking a cake—you can follow the same recipe but try out different flavors to keep things interesting. The result? A much more versatile model that began to produce melodies that didn’t just make me cringe.

Code Chronicles: Getting Hands-On

Now, I want to share a practical bit of code to illustrate how I structured the model. Using TensorFlow and Keras, I built a simple sequential model that took note sequences as input:

import tensorflow as tf
from tensorflow.keras import layers, models

model = models.Sequential()
model.add(layers.LSTM(128, input_shape=(timesteps, n_features), return_sequences=True))
model.add(layers.LSTM(128))
model.add(layers.Dense(n_features, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
Enter fullscreen mode Exit fullscreen mode

This setup trained the model to predict the next note in a sequence, with the hope that it could learn the intricacies of music. I spent quite a bit of time tweaking the architecture, and I encourage you to experiment with it—different layers can yield wildly different results.

Aha Moments and Breakthroughs

There were moments when it clicked—like when I finally got a melody that didn’t sound like a jumbled mess! One of my favorite breakthroughs was when I realized the importance of using attention mechanisms. This was a game-changer. By allowing the model to focus on different parts of the input sequence, I started to see more coherent outputs. The melodies became not just predictive, but also creative in their own right.

But it wasn’t all smooth sailing. I hit a wall with training time—it was taking hours on my laptop. I finally caved and started using Google Colab for training. If you haven’t tried it yet, it’s a fantastic resource for experimenting with AI models. The free GPU access is a lifesaver!

The Joys and Struggles of On-Device Training

One of the main reasons I wanted an on-device model was the challenge of building something that could run without relying on internet access. There’s a certain satisfaction in knowing your creation can perform independently. However, this came with its own set of challenges. Even with a 125M parameter model, I had to be careful about memory consumption.

I learned to optimize my model using techniques like quantization and pruning. It’s similar to cleaning out your closet—getting rid of the unnecessary bits makes the whole thing way more manageable. The end result was a model that could run smoothly on my phone!

Real-World Applications and Future Thoughts

So, what does all this mean in real-world applications? For me, this project was a fantastic way to explore creative coding and AI in a fun manner. I’ve imagined collaborations with musicians where they could use this to iterate on their ideas quickly. Picture a musician jamming, and the AI suggests the next notes—they can tweak it in real-time!

As I continue this journey, I'm excited to explore generative AI beyond music. I've been diving into text-based models as well, and it’s fascinating to consider how these technologies can change the creative landscape.

Final Thoughts: Embracing the Chaos

In closing, I want to emphasize that not every project will result in a polished product right away. The journey is often filled with trial and error, but that’s part of the fun! I've learned that patience, curiosity, and a willingness to explore can lead to unexpected discoveries.

So, what’s next for me? I’m looking to refine the model further, perhaps incorporating user feedback into the training process. And who knows? Maybe I’ll even get it to collaborate with an AI-driven band one day.

I genuinely hope this little journey of mine inspires you to dive into your projects, make mistakes, and learn along the way. After all, tech is as much about creativity as it is about logic, and the more we explore, the more we can create!


Connect with Me

If you enjoyed this article, let's connect! I'd love to hear your thoughts and continue the conversation.

Practice LeetCode with Me

I also solve daily LeetCode problems and share solutions on my GitHub repository. My repository includes solutions for:

  • Blind 75 problems
  • NeetCode 150 problems
  • Striver's 450 questions

Do you solve daily LeetCode problems? If you do, please contribute! If you're stuck on a problem, feel free to check out my solutions. Let's learn and grow together! 💪

Love Reading?

If you're a fan of reading books, I've written a fantasy fiction series that you might enjoy:

📚 The Manas Saga: Mysteries of the Ancients - An epic trilogy blending Indian mythology with modern adventure, featuring immortal warriors, ancient secrets, and a quest that spans millennia.

The series follows Manas, a young man who discovers his extraordinary destiny tied to the Mahabharata, as he embarks on a journey to restore the sacred Saraswati River and confront dark forces threatening the world.

You can find it on Amazon Kindle, and it's also available with Kindle Unlimited!


Thanks for reading! Feel free to reach out if you have any questions or want to discuss tech, books, or anything in between.

Top comments (0)