Skip to content

Commit 63013e0

Browse files
authored
Merge pull request #27 from imohitmayank/feb_2024
Practical part added in Model Compression, and others.
2 parents 272d028 + 72cfb6b commit 63013e0

File tree

7 files changed

+467
-12
lines changed

7 files changed

+467
-12
lines changed

docs/data_science_tools/python_snippets.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,20 @@ def send_message_to_slack(message):
499499
send_message_to_slack("test")
500500
```
501501

502+
## Colab Snippets
503+
504+
- [Google Colab](https://colab.research.google.com/) is the go-to place for many data scientists and machine learning engineers who are looking to perform quick analysis or training for free. Below are some snippets that can be useful in Colab.
505+
506+
- If you are getting `NotImplementedError: A UTF-8 locale is required. Got ANSI_X3.4-1968` or similar error when trying to run `!pip install` or similar CLI commands in Google Colab, you can fix it by running the following command before running `!pip install`. But note, this might break some imports. So make sure to import all the packages before running this command.
507+
508+
```python linenums="1"
509+
import locale
510+
locale.getpreferredencoding = lambda: "UTF-8"
511+
512+
# now import
513+
# !import ...
514+
```
515+
502516
<!-- ## Python Classmethod vs Staticmethod
503517
504518
https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner -->
168 KB
Loading
244 KB
Loading
285 KB
Loading

docs/machine_learning/ML_snippets.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ torch.cuda.get_device_name(0)
276276
## Output: 'GeForce MX110'
277277
```
278278

279+
## Monitor GPU usage
280+
281+
- If you want to continuously monitor the GPU usage, you can use `watch -n 2 nvidia-smi --id=0` command. This will refresh the `nvidia-smi` output every 2 second.
282+
279283
## HuggingFace Tokenizer
280284

281285
- Tokenizer is a pre-processing step that converts the text into a sequence of tokens. [HuggingFace tokenizer](https://huggingface.co/docs/transformers/main_classes/tokenizer) is a wrapper around the [tokenizers library](https://github.com/huggingface/tokenizers), that contains multiple base algorithms for fast tokenization.
@@ -309,6 +313,74 @@ vocabulary = tokenizer.get_vocab()
309313
# vocabulary['hello'] returns 7592
310314
```
311315

316+
## Explore Model
317+
318+
- You can use the `summary` method to check the model's architecture. This will show the layers, their output shape and the number of parameters in each layer.
319+
320+
=== "Keras"
321+
``` python linenums="1"
322+
# import
323+
from keras.models import Sequential
324+
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten
325+
326+
# create a model
327+
model = Sequential()
328+
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
329+
model.add(MaxPooling2D((2, 2)))
330+
model.add(Conv2D(64, (3, 3), activation='relu'))
331+
model.add(MaxPooling2D((2, 2)))
332+
model.add(Conv2D(64, (3, 3), activation='relu'))
333+
model.add(Flatten())
334+
model.add(Dense(64, activation='relu'))
335+
model.add(Dense(10, activation='softmax))
336+
337+
# print the model summary
338+
model.summary()
339+
```
340+
341+
=== "PyTorch"
342+
``` python linenums="1"
343+
# import
344+
import torch
345+
import torch.nn as nn
346+
347+
# create a model
348+
349+
class Net(nn.Module):
350+
def __init__(self):
351+
super(Net, self).__init__()
352+
self.conv1 = nn.Conv2d(1, 32, 3, 1)
353+
self.conv2 = nn.Conv2d(32, 64, 3, 1)
354+
self.conv3 = nn.Conv2d(64, 64, 3, 1)
355+
self.fc1 = nn.Linear(1024, 64)
356+
self.fc2 = nn.Linear(64, 10)
357+
358+
def forward(self, x):
359+
x = F.relu(self.conv1(x))
360+
x = F.max_pool2d(x, 2, 2)
361+
x = F.relu(self.conv2(x))
362+
x = F.max_pool2d(x, 2, 2)
363+
x = F.relu(self.conv3(x))
364+
x = x.view(-1, 1024)
365+
x = F.relu(self.fc1(x))
366+
x = self.fc2(x)
367+
return F.log_softmax(x, dim=1)
368+
369+
# create an instance of the model
370+
model = Net()
371+
# print the model summary
372+
print(model)
373+
```
374+
375+
- To check the named parameters of the model and their dtypes, you can use the following code,
376+
377+
=== "PyTorch"
378+
``` python linenums="1"
379+
print(f"Total number of names params: {len(list(model.named_parameters()))}")
380+
print("They are - ")
381+
for name, param in model.named_parameters():
382+
print(name, param.dtype)
383+
```
312384
<!-- ## Tensor operations
313385
314386
- Tensors are the building blocks of any Deep Learning project. Here, let's go through some common tensor operations,

docs/machine_learning/interview_questions.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,4 +466,12 @@
466466

467467
=== "Answer"
468468
469-
XGBoost (Extreme Gradient Boosting) is a specific implementation of the Gradient Boosting method that uses a more efficient tree-based model and a number of techniques to speed up the training process and reduce overfitting. XGBoost is commonly used in machine learning competitions and it's one of the most popular libraries used for gradient boosting. It's used for classification and regression problems.
469+
XGBoost (Extreme Gradient Boosting) is a specific implementation of the Gradient Boosting method that uses a more efficient tree-based model and a number of techniques to speed up the training process and reduce overfitting. XGBoost is commonly used in machine learning competitions and it's one of the most popular libraries used for gradient boosting. It's used for classification and regression problems.
470+
471+
!!! Question ""
472+
=== "Question"
473+
#### What is `group_size` in context of Quantization?
474+
475+
=== "Answer"
476+
477+
Group size is a parameter used in the quantization process that determines the number of weights or activations *(imagine weights in a row of matrix)* that are quantized together. A smaller group size can lead to better quantization accuracy, but it can also increase the memory and computational requirements of the model. Group size is an important hyperparameter that needs to be tuned to achieve the best trade-off between accuracy and efficiency. Note, the default groupsize for a GPTQ is 1024. [Refer this interesting Reddit discussion](https://www.reddit.com/r/LocalLLaMA/comments/12rtg82/what_is_group_size_128_and_why_do_30b_models_give/?rdt=46348)

0 commit comments

Comments
 (0)