Comments (5)
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.
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.
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=2
and 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.
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.
You're welcome! Closing this issue.
Cheers, Aurélien
from handson-ml.
Related Issues (20)
- mnist dataset HOT 2
- Chapter#02 FileNotFoundError HOT 1
- Chapter 2 error during prediction HOT 2
- Ml
- Dropout at test time HOT 3
- How can I use my own dataset and fit it to your code
- Need help understanding crc hash used to explain test train split in Chapter 2 HOT 1
- ImportError: cannot import name 'fetch_mldata' from 'sklearn.datasets' (F:\Anaconda3\lib\site-packages\sklearn\datasets\__init__.py) HOT 1
- Chapter 3 : Exercise 1 - MNIST Classifier with 97% accuracy - Could not pickle the task to send it to the workers. HOT 3
- Broken image in readme HOT 1
- Chapter 5 SVM why should center before LinearSVC
- Chapter 3 (Page 82): Getting error during Fitting the SGD Classifier with Training data
- Chapter 2: Value differences in prediction
- Chapter 2: Looking for Correlations - ValueError: could not convert string to float: 'INLAND' HOT 1
- Use github.com/apssouza22/chatflow as a conversational layer. It would enable actual API requests to be carried out from natural language inputs.
- chapter 4: SGDRegressor(tol=-np.infty) is not accepted by the module HOT 1
- Hi
- Ch.2 Error using corr() HOT 1
- Problem downloading data HOT 1
- Why does saving the test set not work?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from handson-ml.