Giter Site home page Giter Site logo

Questions about handson-ml HOT 5 CLOSED

jingw222 avatar jingw222 commented on May 2, 2024
Questions

from handson-ml.

Comments (5)

ageron avatar ageron commented on May 2, 2024 1

You're welcome. You're doing it right, this is just one possible technique (option 2), which indeed means that a particular batch may have the same instance multiple times (but it will be very rare for large datasets and small batches). If you want to implement option 1 instead, here's one approach:

import numpy as np

# Create dummy training set
m = 50
n = 2
X_train = np.arange(m * n).reshape(m, n)
y_train = np.arange(m)

# Start training
n_epochs = 3
n_batches = 10
for epoch in range(n_epochs):
    indices = np.random.permutation(50)
    for batch_indices in np.array_split(indices, n_batches):
        X_batch = X_train[batch_indices]
        y_batch = y_train[batch_indices]
        print(X_batch, y_batch) # train on batch

from handson-ml.

ageron avatar ageron commented on May 2, 2024

Hi @jingw222,

Thanks a lot, I'm really glad you find my book useful. :)

Regarding your first question, there's an error in the text, sorry about that, it should read "on the last mini-batch and on the full test set" (it's listed in the errata page). You could evaluate the training error on the full training set rather than just on the last mini-batch, the only reason I did not do that is because it was very slow, but it would of course be more accurate.

Regarding your second question, you have two options when implementing fetch_batch():

  • option 1: you shuffle the training set at the beginning of each epoch, you chop it into disjoint mini-batches of batch_size instances each, and then you return these mini-batches one by one for each training iteration of this epoch. With this option, mini-batches do not overlap during the epoch, and all training instances are fed to the training algorithm at each epoch.
  • option 2: you simply pick batch_size instances randomly from the training set, at each training iteration, meaning you don't really care about the epoch number, it's just a rough measure of how many times the model was fed the training set. This solution implies that mini-batches may have overlapping instances, and some instances will almost certainly be left out during a particular epoch (in fact, it's possible to estimate that roughly 36% of the instances will be left out at each epoch).

Option 1 seems better, but in practice option 2 is actually just as good and simpler to implement.

Hope this helps!

from handson-ml.

jingw222 avatar jingw222 commented on May 2, 2024

Thanks a lot for your detailed explanation. I cannot be more grateful for that. Here's my dummy example:

Let's just assume we have a training set with index range(0, 10) (so m=10 in this case), epoch=0, n_batches=2and therefore batch_size=5.

So, then rnd.seed(0*2+0); rnd.randint(10, size=5) gives me array([5, 0, 3, 3, 7]), and rnd.seed(0*2+1); rnd.randint(10, size=5) outputs array([5, 8, 9, 5, 0]). Basically, not only does these two batches not contain the whole range, but they can have replacements themselves. Am I doing it wrong?

from handson-ml.

jingw222 avatar jingw222 commented on May 2, 2024

Great help! I really learned a lot. Thank you very much indeed. And I'm going to continue to dive into the book. :D

from handson-ml.

ageron avatar ageron commented on May 2, 2024

You're welcome! Closing this issue.
Cheers, Aurélien

from handson-ml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.