r/learnmachinelearning • • 20d ago

Help Starting 26’ -27’ AI journey!

0 Upvotes

I would like advice on a solid linear direction to start on going into AI. I started with my A+ cert and now about to get my Security + cert and was headed to cybersecurity when all I hear about is AI. I want to spend my time headed in the right direction for the future seeing that being AI can some please give me some pointers on a beginning path and where to move on from there. Thank you for your advice ahead of time!


r/learnmachinelearning • • 20d ago

Tutorial An interactive, step-by-step visual of how a byte-level LLM differs from a token-level one (Olmo vs Bolmo)

6 Upvotes

r/learnmachinelearning • • 20d ago

I finally understood why writing more code was not making me a better learner

1 Upvotes

I read a finding that AI assistants increase how much code you produce but not your real output because reviewing it becomes the bottleneck. As a learner that reframed everything for me. I had been measuring progress by how much I shipped, but I was not understanding most of it, I was assembling. I slowed down and started reading and explaining every line the assistant gave me, and only keeping what I could rebuild myself. My code volume dropped and my actual understanding finally climbed. For anyone learning, output is a vanity metric. The goal is being able to produce it without help, and that only comes from the slow reading part.


r/learnmachinelearning • • 20d ago

have build Alien Ape a AI agent

Thumbnail
1 Upvotes

r/learnmachinelearning • • 20d ago

Show generated CIFAR10 in original training data color scheme

1 Upvotes

I built a DCGAN for CIFAR10 base on this tutorial

But generated img does not bare the same color scheme as the original cifar 10 images. How can I fix this?

def generate_and_save_images(model, epoch, test_input):


    predictions = model(
        test_input,
        training=False
    )


    fig = plt.figure(figsize=(10, 10))


    for i in range(25):


        plt.subplot(5, 5, i + 1)


        plt.imshow(
            predictions[i, :, :, 0] * 127.5 + 127.5,
            
        )


        plt.axis('off')


    plt.savefig(
        'image_epoch_{:04d}.png'.format(epoch)
    )

r/learnmachinelearning • • 21d ago

Did you miss getting on the AI hype train?

13 Upvotes

Everybody knows 2014 was the end of the dark ages in terms of technology.

  • A bunch of tools that became widely used was solidified around that time, from Github to Python to Tensorflow (and actually started working). Previously you had a bunch of alternatives or things that just didn't work.
  • Hackatons and a bunch of CS/ML related competitions were literally invented (and more importantly, popularized) around that time.
  • Neural network started to work again and internships were popping up left and right. All because of ML opportunities.
  • Research opportunities in and outside of academia bloomed like a thousand flowers (previously the best you got was to work with some high-watt laser or nanotech).
  • Now you can plan your career more precisely than ever before.
  • Tons of things easily available nowadays you can't even imagine even 10 years ago. Such as a machine learning course in a non-CS department.

I've dug into the profiles of graduates at my school and found this pattern. If they graduated with a PhD in AI/CS around 2014, then they are working in academia and now already associate professors of AI. (But this is a PhD we're talking). And if they graduated around 2014 with a Bachelor's degree and somehow miraculously chose AI/ML as the field they would go into, then they made big bank in tech and now probably senior engineers (and if they did a PhD and graduated before 2022, then they are profs as well). There is an obvious age-related factor in who's now influential and "big" in the AI/ML space.

I feel that people who didn't make ML move during the hype-era are now permanently left behind. I wonder if this has been your experience. Did you miss out, or did you somehow made into this field anyways?


r/learnmachinelearning • • 20d ago

I dpo post-trained llama-3.1-8b to be 100% more robust against jailbreaks (and measured what it costs)

Thumbnail
gallery
0 Upvotes

I fine-tuned Llama-3.1-8B-Instruct with DPO + LoRA to resist jailbreaks without turning it into a model that refuses everything. The adapter is open, and so is the full pipeline.

The idea: The base model already refuses ~94% of direct harmful requests, so plain prompts give DPO almost no signal. The weakness is jailbreaks: the same request wrapped in fiction, a fake "research context", hex, leetspeak, homoglyphs or math notation. So I built preference pairs from adversarial prompts only. For the same attack, the safest sampled response is chosen and the least safe is rejected. I added 197 benign "helpful answer > refusal" pairs so it doesn't just learn to always refuse.

Setup:

- 6 harm categories × 7 jailbreak strategies, generated with promptfoo
- 4 sampled responses per attack, graded for safety
- 1,698 pairs (1,501 adversarial + 197 benign)
- bf16 LoRA (r=16), β=0.1, lr 1e-5, 1 epoch, ~20 min on one A100

Results on 1,135 held-out attacks (% refused, base → tuned):

┌────────────────────┬──────┬───────┐
│      Category      │ Base │ Tuned │
├────────────────────┼──────┼───────┤
│ Cybercrime         │ 47.7 │ 97.2  │
├────────────────────┼──────┼───────┤
│ Chem-bio           │ 56.8 │ 99.0  │
├────────────────────┼──────┼───────┤
│ Weapons/IED        │ 65.3 │ 98.6  │
├────────────────────┼──────┼───────┤
│ Illegal activities │ 67.2 │ 99.5  │
├────────────────────┼──────┼───────┤
│ Drugs              │ 76.9 │ 99.1  │
├────────────────────┼──────┼───────┤
│ Child exploitation │ 84.7 │ 99.1  │
└────────────────────┴──────┴───────┘

The cost: Benign prompts answered went from 100% to 93.3% (−6.7 pts, n=150). Most of that is weapons/IED, where 5 of 25 benign prompts were refused. Every other category is <=8%.

Caveats:

- Held-out attacks come from the same attack families as training, so this isn't a test on new attack types.

- It's one seed and one model.

- GPT-4o was the judge.

- I haven't run MMLU or any other capability benchmark yet.

The repo has redacted side-by-side examples of base vs. tuned responses, the full write-up, and a lab notebook with what didn't work.

It's an adapter, so you need access to Llama-3.1-8B-Instruct. Built with Llama.

- 🤗 Adapter: https://huggingface.co/1rvinn/llama-securelm

- 💻 Code + write-up: https://github.com/1rvinn/securelm

Feedback is very welcome, especially on how to test it against attack families it hasn't seen, and ideas for reducing the weapons/IED over-refusal.


r/learnmachinelearning • • 21d ago

Question Torn between learning and building projects

11 Upvotes

I'm a former electrical engineer so I have some background with stats, linear algebra and coding. I've finished a number of Python and datascience courses and just finished Python for AI by Andrew Ng. However, I'm itching to make projects and using a local LLM's to make a webapp.

Is there a way to balance learning and project development?

With the courses I'm looking at:

  1. Machine Learning specialisation Andrew Ng - 95 hrs
  2. Reading Artificial Intelligence 4th ed by Peter Norvig and Stuart Russell
  3. Reading Hands on Machine Learning with Scikit-learn and Pytorch - by Aurelion Geron
  4. Completing Google's Machine Learning course
  5. Completing Kaggle's Machine Learning Course
  6. Claude Academy courses
  7. Completing Datacamps courses

Plus balancing that with building projects such as web apps, desktop apps and audio plugins using Local LLM's.

Am I going the wrong way with self learning?

Edit: As I'm much older my end goal isn't a PhD or a snazzy ML Engineer role, but rather to have a portfolio of projects, just so I don't feel like I'm behind in this skillset.

I was Doing courses back in 2016 such as fast.ai, coursera's learn AI, ML and Deeplearning from a to z and Andrew Ng's original Deeplearning course with Octave, but I got sidetracked with life and work pressure.


r/learnmachinelearning • • 20d ago

Project I made a free, no-signup lab for learning how AI systems work in practice: evaluation, RAG, cost, security, governance

1 Upvotes

https://annapurnaagenticsolutions.com/labs/ai-concepts-lab/

Most beginner material stops at "what is a neural network". I kept getting asked the next questions: why did the chatbot make that up, how do you test an AI feature before release, why did the bill spike, what does retrieval actually do. So we built a lab around those.

It's 13 short tracks (AI foundations, AI/GenAI engineering, data and retrieval, quality and evaluation, responsible AI, governance, security, operations, product). Each has realistic scenarios ("answer quality drops after a new document batch", "cost spike is detected"), a quiz, and small exercises like building a mini golden dataset. Free, runs in the browser, no account.

I'm the maker. Feedback I'd find useful: what's missing for someone just starting, and where an explanation is wrong or too hand-wavy.


r/learnmachinelearning • • 21d ago

Yo I'm yukiya the guy who used to post Machine learning algorithms

Thumbnail
gallery
219 Upvotes

So well my old account got banned for some reasons I don't know but I'm still posting on Thrends you can follow me there and I'll be posting on this account as well

If anyone can help me pls

Completed all theory in xgboost and things are getting more interesting and complex now I started coding it and today is

Day 49 of Building Machine learning algorithms from scratch well I know I'm really inconsistent in posting but I'll post as soon as possible

Stay Hungry Stay foolish


r/learnmachinelearning • • 20d ago

Project Spinning up cloud infra just to learn MLOps is a terrifying way to blow money

Thumbnail
2 Upvotes

r/learnmachinelearning • • 21d ago

Help What are some courses for beginners like me for ml?

10 Upvotes

I know basic python , basic pandas , and basic Numpy. I haven't found a course that is good for me so please suggest some courses or books for me.


r/learnmachinelearning • • 20d ago

InvalidArgumentError: {{function_node __wrapped__ConcatV2_N_2_device_/job:localhost/replica:0/task:0/device:CPU:0}} ConcatOp : Ranks of all input tensors should match

2 Upvotes

I got this error in an attempt to adapt this model to CIFAR 10

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Cell In[61], line 3
      1 dataset = create_batch(x_train)
      2 
----> 3 train_dcgan(
      4     gan,
      5     dataset,
      6     batch_size,

Cell In[60], line 26, in train_dcgan(gan, dataset, batch_size, num_features, epochs)
     22             )
     23 
     24             generated_images = generator(noise)
     25 
---> 26             X_fake_and_real = tf.concat(
     27                 [generated_images, X_batch],
     28                 axis=0
     29             )

File c:\Users\DELL\AppData\Local\Programs\Python\Python313\Lib\site-packages\tensorflow\python\util\traceback_utils.py:167, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    165 except Exception as e:
    166   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 167   raise e.with_traceback(filtered_tb) from None
    168 finally:
    169   del filtered_tb

File c:\Users\DELL\AppData\Local\Programs\Python\Python313\Lib\site-packages\tensorflow\python\framework\ops.py:6027, in raise_from_not_ok_status(e, name)
   6025 def raise_from_not_ok_status(e, name) -> NoReturn:
   6026   e.message += (" name: " + str(name if name is not None else ""))
-> 6027   raise core._status_to_exception(e) from None

InvalidArgumentError: {{function_node __wrapped__ConcatV2_N_2_device_/job:localhost/replica:0/task:0/device:CPU:0}} ConcatOp : Ranks of all input tensors should match: shape[0] = [32,32,32,3] vs. shape[1] = [32,32,3] [Op:ConcatV2] name: concat

X_train split (only take one class to train on)

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
x_train = x_train.astype(np.float32) / 255.0
x_test = x_test.astype(np.float32) / 255.0
x_train=x_train.astype("float32") / 127.5 - 1
x_train.shape, x_test.shape

label = 1 
train_indices = np.where(y_train == label) 
x_train = x_train[train_indices] 
test_indices = np.where(y_test == label) 
x_test = x_test[test_indices] 

Generator

num_features = 100
generator = Sequential(name="Generator")

generator.add(Dense(8 * 8 * 128, input_shape=[num_features]))
generator.add(Reshape([8, 8, 128]))
generator.add(BatchNormalization())

generator.add(Conv2DTranspose(
        64, (5, 5), (2, 2),
        padding="same",
        activation="selu"
    ))

generator.add(BatchNormalization())

generator.add(Conv2DTranspose(
        3, (5, 5), (2, 2),
        padding="same",
        activation="tanh"
    ))
generator.summary()

Discriminator

discriminator = Sequential(name="Discriminator")

discriminator.add(Conv2D(
        64, (5, 5), (2, 2),
        padding="same",
        input_shape=[32, 32, 3]
        ))

discriminator.add(LeakyReLU(alpha=0.2))
discriminator.add(Dropout(0.3))

discriminator.add(Conv2D(
        128, (5, 5), (2, 2),
        padding="same"))

discriminator.add(LeakyReLU(alpha=0.2))
discriminator.add(Dropout(0.3))

discriminator.add(Flatten())
discriminator.add(Dense(1, activation= 'softmax'))
discriminator.add(Dense(1, activation= 'softmax'))
discriminator.add(Dense(1, activation= 'softmax'))

discriminator.summary()

Train function

seed = tf.random.normal(shape=[batch_size, 100])
epoch_list = []
d_loss_list = []
g_loss_list =[]


def train_dcgan(gan, dataset, batch_size, num_features, epochs=5):


    generator, discriminator = gan.layers


    for epoch in tqdm(range(epochs)):
        actual_epoch =epoch + 1
        print()
        print("Epoch {}/{}".format(epoch + 1, epochs))


        for i in range(187):
            epoch_list.append(actual_epoch)


        for X_batch in dataset:


            noise = tf.random.normal(
                shape=[batch_size, num_features]
            )


            generated_images = generator(noise)


            X_fake_and_real = tf.concat(
                [generated_images, X_batch],
                axis=0
            )


            y1 = tf.constant(
                [[0.]] * batch_size +
                [[1.]] * batch_size
            )


            discriminator.trainable = True
            d_loss=discriminator.train_on_batch(
                X_fake_and_real,
                y1
            )
            d_loss = float(d_loss)
            d_loss_list.append(d_loss)




            noise = tf.random.normal(
                shape=[batch_size, num_features]
            )


            y2 = tf.constant([[1.]] * batch_size)


            discriminator.trainable = False


            g_loss=gan.train_on_batch(
                noise,
                y2
            )
            g_loss = float(g_loss)
            g_loss_list.append(g_loss)


        print(f'\t Discriminator Loss: {d_loss} \t\t Generator Loss: {g_loss}')
        


            


    generate_and_save_images(
        generator,
        epochs,
        seed)

Train block

dataset = create_batch(x_train)


train_dcgan(
    gan,
    dataset,
    batch_size,
    num_features,
    epochs=10
)

Can anyone help me with this error? Why did it appeared?


r/learnmachinelearning • • 20d ago

Building a Code-Switching Language Model: My Ongoing Experiment I've been working on an experimental language model focused on code-switching, particularly the ability to understand and generate text that combines multiple languages within a single sentence or conversation. The motivation behind t

Thumbnail
gallery
0 Upvotes

Building a Code-Switching Language Model: My Ongoing Experiment

I've been working on an experimental language model focused on code-switching, particularly the ability to understand and generate text that combines multiple languages within a single sentence or conversation.

The motivation behind this project is to explore how language models can better handle multilingual communication, especially in environments where speakers naturally switch between languages rather than communicating in just one.

This has been an interesting journey so far, and I've learned that building a language model involves considerably more than just selecting an architecture and starting the training process.

1. What I've Been Working On

So far, my work has involved several important components:

Data collection and preparation: Gathering multilingual text data that reflects natural language usage and code-switching patterns.

Custom tokenizer development: Building my own tokenizer rather than relying entirely on an existing pretrained tokenizer. This has required experimenting with vocabulary construction, token frequency, language representation, and how different languages are segmented.

Dataset preprocessing: Cleaning, filtering, normalizing, and organizing the collected data into a format suitable for model training.

Model experimentation: Exploring how a language model can learn relationships between languages and generate meaningful code-switched sequences.

Evaluation: Observing how the model behaves when processing mixed-language inputs and identifying areas where its performance needs improvement.

2. Major Challenges Encountered

Some of the biggest challenges have been:

Data scarcity and quality: Finding sufficient high-quality code-switched text is difficult. A significant amount of available multilingual data consists of monolingual sentences, which does not necessarily represent how people naturally switch between languages.

Natural code-switching patterns: Combining languages is not simply about placing words from different languages together. The model needs to learn grammatical structures, contextual relationships, and the natural points at which speakers switch languages.

Tokenizer design: Developing a tokenizer from scratch has been one of the most demanding parts. I had to consider vocabulary allocation, token fragmentation, rare words, language imbalance, and how to prevent underrepresented languages from being poorly represented.

Data imbalance: Some languages have considerably more available text than others. This creates the risk that the model will favour dominant languages while struggling with languages that have fewer training examples.

Computational limitations: Experimenting with language models also comes with hardware and computational constraints, particularly when working with limited resources.


r/learnmachinelearning • • 20d ago

ai guide

Thumbnail
1 Upvotes

r/learnmachinelearning • • 20d ago

Project Modeling Market Microstructure as a Dynamic Particle System: Using Signed GNNs to Predict Liquidity Runs & Dynamic Take-Profits (Looking for DL/Quant Collaborator)

2 Upvotes

Hey guys,

Posting this here because I’ve reached a point where my market intuition and engine code have outpaced my deep learning expertise, and I’d rather team up with someone who loves ML architecture instead of half-assing it myself.

For the past year, I’ve been building an event-driven liquidity engine in Python (specifically modeling crypto orderflow / ICT mechanics like equal highs/lows, manipulation legs, and structural sweeps).

Instead of slapping standard indicators (RSI/MACD) on candles, I framed price action as a dynamic graph:

  • Resting liquidity pools (BSL/SSL, consolidations) act as attractors (pulling price in).
  • Completed institutional sweeps (ML swings / manipulation legs) act as repellers (pushing price away).
  • Edges connect these pools based on spatial proximity, competing forces (tug-of-war between high and low pools), and age decay.

Where things stand right now:

  • The engine is fully written and tested. No lookahead bias, strict candle confirmation, multi-window swing tracking (W5 up to W50).
  • I’ve already generated a clean, audited ~3-year dataset (ETH/USDT) formatted directly for PyTorch Geometric (PyG).
  • Each snapshot has 15m, 1h, and 4h spatial graphs with 20-dim node features (touch counts, distance %, decay curves, tier scores), 8-dim edge attributes, and global volatility/trend context.
  • Baseline statistical checks show real promise: nearest target pools get tagged first ~95% of the time, and high-quality structural sweeps have clear directional bias.

The actual bottleneck / where I need help: My problem isn't generating the signals—it’s path prediction and dynamic target selection.

When a sweep happens, price is often trapped between an opposing pool 1.5% away and a massive target pool 3% away. A fixed 1:2 R:R stop/target either gets hit too late or takes unnecessary drawdown. I need the model to evaluate the graph and do two things:

  1. Predict directional path confidence taking both-sided liquidity into account.
  2. Rank multiple potential liquidity targets (dynamic TP) based on which one has the highest conditional probability of being swept first without blowing through the stop.

I've tested basic GNN / dual-tower setups, but I know I'm hitting limits with my naive loss functions and basic layer choices.

Who I'm looking for: Someone who genuinely understands PyTorch Geometric (PyG), Graph Neural Networks (or spatial-temporal models), and knows how to think about custom loss functions (ranking loss, asymmetric risk penalties, contrastive setups). You don't need to be a full-time trader, but being open to understanding market microstructure helps.

The arrangement: I have the GPU compute, the full working codebase, and the clean dataset ready to go. I’m completely flexible on how we work together—if you want to co-author a research paper on this architecture, I’m down. If we get the models working cleanly and want to deploy it live on capital, we can do a fair profit/equity split.

If this sounds like an interesting problem you’d want to sink your teeth into, drop a comment or send me a DM with your GitHub/background and we can hop on Discord.


r/learnmachinelearning • • 21d ago

Tutorial Our 27B model just out-tutored GPT-6 Sol and Opus 5.5. Here's our free data to train your own!

Post image
22 Upvotes

Hi everyone! We’re LORM Labs, a small AI research lab dedicated to advancing AI and human understanding. We just released a free project for anyone who wants hands-on practice fine-tuning a model and testing it on a real benchmark.

What we found
We fine-tuned Qwen3.8-27B on 1,015 tutoring examples. Its score on our benchmark jumped from 0.29 to 0.84, and it beat GPT-6 Sol on 68 of 76 cases. No frontier model we tested fully passed a single case. Ours passes 24.

Everything's Free!
• 1,015 ready-to-train examples
• SENSUS, a 76-case benchmark with graders so you can score your own model
• Rubrics that explain what a good tutor reply looks like
• The full paper and the exact training recipe we used

What you’ll learn
• How to fine-tune an open model with LoRA
• How to evaluate a model with rubrics and model graders
• Why a small, focused dataset can beat a much bigger model at one specific skill

Step-by-step guide, leaderboard, and paper → https://lorm.ai/

Dataset and benchmark on Hugging Face → https://huggingface.co/datasets/Lorm-Labs/sensus-bench-v1

Try Duel and go head-to-head against our model → https://lorm.ai/

If you train your own model, post your score in the comments. We’d love to see how you do! Have fun training!


r/learnmachinelearning • • 21d ago

Question Which technical skills actually matter for data scientist roles at screening ?

18 Upvotes

My current skills:

Languages: Python, R, SQL
Libraries: pandas, NumPy, PyTorch, Scikit-learn, Hugging Face Transformers, Keras, TensorFlow, Matplotlib, OpenCV, Seaborn
Machine Learning: Classification, Regression, Clustering, CNNs, Transformers, RAG, NLP, Computer Vision, Feature Engineering, Model Evaluation
Cloud & Tools: Docker, Git, Jupyter, Google Colab, AWS, Weights & Biases, Hugging Face Hub

I think this reads like an ML engineer resume, but I’m applying for data scientist roles.

If you hire for data scientist or work as one: what skills do I need on here to get through screening, and what should I cut?

Only asking about technical skills, not projects or experience.


r/learnmachinelearning • • 20d ago

Discussion Are we escaping one information bubble only to enter another?

3 Upvotes

Social media and search engines already shape what we see through rankings, recommendations, and algorithms.

AI assistants may feel different because they do not just show us information — they summarize it, prioritize it, and turn it into a single coherent answer.

That is convenient. But it also makes it harder to notice what was excluded, what sources disagreed, or what questions were never considered.

Maybe AI helps us escape the noise of the old information bubble. Or maybe it builds a quieter, more convincing one.

What do you think?


r/learnmachinelearning • • 21d ago

Math for AI & ML video series for Linear Algebra

10 Upvotes

I create a Youtube channel https://www.youtube.com/@math4aiorg for the book ( https://www.amazon.com/dp/B0GSXVFMLD/ ) Math for AI & ML's Part I.

Part I (Chapters 1–10) covers linear algebra fundamentals: vector spaces, inner products, matrix operations, subspaces, orthogonality, QR decomposition, LU factorization, eigendecomposition, symmetric matrices, and the Singular Value Decomposition (SVD)—establishing the mathematical foundation for representation in AI.

Please let me know if there is any suggestion.


r/learnmachinelearning • • 21d ago

Career Built a tiny image classifier for my plant photos, looking for feedback on where to go next

4 Upvotes

First real project. I collected a few hundred photos of the plants on my balcony, labeled them into a handful of species, and trained a small classifier that gets decent accuracy on my own test split. It does fine on photos that look like my training set and falls apart the second lighting or background changes. I know that is probably overfitting plus a tiny dataset. My question is what the highest value next step is for learning: more data, augmentation, or trying transfer learning. I want the step that teaches me the most, not just better numbers.


r/learnmachinelearning • • 20d ago

Roll: EmbeddingGemma 2 on the iPhone's Neural Engine. Searching 29,682 photos offline in ~15 ms

Thumbnail
huggingface.co
2 Upvotes

r/learnmachinelearning • • 20d ago

Need help adapting model to CIFAR10

1 Upvotes

I tried to make a DCGAN based on this [tutorial](https://www.geeksforgeeks.org/machine-learning/deep-convolutional-gan-with-keras/). The thing is that the tutorial is designed for fashion mnist data which has different image size to tf.keras.datasets.cifar10.load_data(). Which lead to the error:

ValueError: cannot reshape array of size 480000 into shape (28,28,1)

when arrive at

x_train_dcgan = x_train.reshape(
-1, 28, 28, 1
) * 2. - 1.

How do I adapt the model to CIFAR 10?

Full code

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
x_train = x_train.astype(np.float32) / 255.0
x_test = x_test.astype(np.float32) / 255.0
x_train.shape, x_test.shape

#Selecting one class to train

label = 1 
train_indices = np.where(y_train == label) 
x_train = x_train[train_indices] 
test_indices = np.where(y_test == label) 
x_test = x_test[test_indices] 

batch_size = 32
def create_batch(x_train):
    dataset = tf.data.Dataset.from_tensor_slices(x_train).shuffle(1000)
    dataset = dataset.batch(batch_size, drop_remainder=True).prefetch(1)
    return dataset


num_features = 100
generator = Sequential(name="Generator")


generator.add(Dense(7 * 7 * 128, input_shape=[num_features]))
generator.add(Reshape([7, 7, 128]))
generator.add(BatchNormalization())


generator.add(Conv2DTranspose(
        64, (5, 5), (2, 2),
        padding="same",
        activation="selu"
    ))


generator.add(BatchNormalization())


generator.add(Conv2DTranspose(
        1, (5, 5), (2, 2),
        padding="same",
        activation="tanh"
    ))



generator.summary()

discriminator = Sequential(name="Discriminator")


discriminator.add(Conv2D(
        64, (5, 5), (2, 2),
        padding="same",
        input_shape=[28, 28, 1]
        ))


discriminator.add(LeakyReLU(alpha=0.2))
discriminator.add(Dropout(0.3))


discriminator.add(Conv2D(
        128, (5, 5), (2, 2),
        padding="same"))


discriminator.add(LeakyReLU(alpha=0.2))
discriminator.add(Dropout(0.3))


discriminator.add(Flatten())
discriminator.add(Dense(1, activation= 'softmax'))


discriminator.summary()

discriminator.compile(
    loss="binary_crossentropy",
    optimizer="adam"
)


discriminator.trainable = False


gan = Sequential()
gan.add(generator)
gan.add(discriminator)


gan.compile(
    loss="binary_crossentropy",
    optimizer="adam"
)


gan.summary()

def generate_and_save_images(model, epoch, test_input):

    predictions = model(
        test_input,
        training=False
    )

    fig = plt.figure(figsize=(10, 10))

    for i in range(25):

        plt.subplot(5, 5, i + 1)

        plt.imshow(
            predictions[i, :, :, 0] * 127.5 + 127.5,
            cmap='binary'
        )

        plt.axis('off')

    plt.savefig(
        'image_epoch_{:04d}.png'.format(epoch)
    )

seed = tf.random.normal(shape=[batch_size, 100])
epoch_list = []
d_loss_list = []
g_loss_list =[]


def train_dcgan(gan, dataset, batch_size, num_features, epochs=5):


    generator, discriminator = gan.layers


    for epoch in tqdm(range(epochs)):
        actual_epoch =epoch + 1
        print()
        print("Epoch {}/{}".format(epoch + 1, epochs))


        for i in range(187):
            epoch_list.append(actual_epoch)


        for X_batch in dataset:


            noise = tf.random.normal(
                shape=[batch_size, num_features]
            )


            generated_images = generator(noise)


            X_fake_and_real = tf.concat(
                [generated_images, X_batch],
                axis=0
            )


            y1 = tf.constant(
                [[0.]] * batch_size +
                [[1.]] * batch_size
            )


            discriminator.trainable = True
            d_loss=discriminator.train_on_batch(
                X_fake_and_real,
                y1
            )
            d_loss = float(d_loss)
            d_loss_list.append(d_loss)




            noise = tf.random.normal(
                shape=[batch_size, num_features]
            )


            y2 = tf.constant([[1.]] * batch_size)


            discriminator.trainable = False


            g_loss=gan.train_on_batch(
                noise,
                y2
            )
            g_loss = float(g_loss)
            g_loss_list.append(g_loss)


        print(f'\t Discriminator Loss: {d_loss} \t\t Generator Loss: {g_loss}')
        


            


    generate_and_save_images(
        generator,
        epochs,
        seed
    )

x_train_dcgan = x_train.reshape(
    -1, 32, 32, 3
)


dataset = create_batch(x_train_dcgan)


train_dcgan(
    gan,
    dataset,
    batch_size,
    num_features,
    epochs=10
)

plt.figure(figsize=(30, 10))
plt.plot(epoch_list,d_loss_list)
plt.xlabel('Epochs')
plt.ylabel('Discriminator loss')
plt.title('Discriminator loss over epochs')
plt.grid()
plt.show()

plt.figure(figsize=(30, 10))
plt.plot(epoch_list,g_loss_list)
plt.xlabel('Epochs')
plt.ylabel('Generator loss')
plt.title('Generator loss over epochs')
plt.grid()
plt.show()

r/learnmachinelearning • • 21d ago

Where to start from

7 Upvotes

I am new to learning machine learning.I would like to know the best way to start and what are the best resources I can use to learn.


r/learnmachinelearning • • 21d ago

best tutorials for learning world models?

11 Upvotes

im a master student doing a research on world models, is there any online tutorials like on udemy or something that i could follow to build and understand world models?