A bunch of things that I encounter during my journey as NLP/Audio developer
FINE-TUNE HEAVY MODELS
- If we fine-tune this pretrained bert, we will boost our results! But we are poor and don't have enough gpu... i always run into cuda out of memory 🤯
- Being poor is not an argument to give up, let's figure it out.
checklist how to fine-tune a heavy model:
Presteps and caution:
0️⃣.1️⃣check that it is really cuda problem, try batch size of 1 on CPU, maybe you have some operations that trigger the error. Bugs are much better explained when running on CPU. p.s. it's also the best practice to turn off GPU, take only one small batch and check the expected result etc and only then run on GPU. It saves time and GPU usage.
0️⃣.2️⃣ if distilled version of the model is available check it (distilled model is a student model that gained knowledge from its teacher) Distilled model is usually times lighter and will take less GPU memory.
0️⃣.3️⃣ check smaller batch size - be aware that training on a small batch size can lead to worse overall results. The coverage can be slower and you can even achieve the not the precise desiriable result.
If you don't get the idea about GPU, CPU and TPU, don't worry you were not born with this knowledge, check out this and this video.
Most of the this problem happens when you try to allocate too much memory during training, so your operations are heavy like matrix multiplacation, your model is heavy, your optimizer is heavy, your network is deep and you save nodes' gradient.
1️⃣ try gradient checkpoints. tenserflow and explanation, pytorch and explanation
It is a technique when you save in memory only some nodes from the network that are used for backpropogration, then you recount them, so it's a trade off between runtime and memory efficiency.
important note: blocks with dropout/BatchNorm should not be recomputed due to randomness in two runs.
If you use hugging face, check out gradient_checkpoints_steps.
2️⃣ try gradient accumulation (I find it fascinating!) - you just save loss for a few mini-batches and update only when you have gained a needed number (e.g. during main training authors were using batch size of 128, you cannot afford it and use 8 batch size, update optimizer only after 128 / 8 = 16 passes) You can do it in your loop, if you use hugging face, check out gradient_accumulation_steps. code snippet to use in a regular training loop.
3️⃣ use Adam8bit It allows to run operations in optimizer, which saves a lot of memory in 8bit mode. Additional post about adam differences will be here one day.
4️⃣ use different precisions for training. Precision is much more how accurate numbers are during training, it's pretty neet idea that you can catch in the tutorial. Hugging face has a very user-friendly interface for all fp16, bfp16, tp32. By the way, tp32 can be accessed just with one line of code for torch: torch.backends.cuda.matmul.allow_tf32 = True
important note: using different precision remember about coverage time and precision. It's most of the time a trade off between the size of the model, its speed and precision.
By the way 2, if you need to use your model later, you can quantize it. I personally have used only torch resources and intel openvino.
5. Be aware of parameters in DataLoaders and how you save your data. It's a recommendation to not save all your data on GPU even if it can fit in, it's better and faster to set it on the right device during training and make use of higher batch size. If you're worried about time, use pin_memory parameter = True, so your data is pinned directly from CPU to GPU. Illustration and details.
6. Nearly forgot: be aware what operations you can do on CPU, what on GPU. Like the calculation loss function can be done on CPU and observe if you save loss.item() in case of GPU usage. It can overload the memory. The thread when to count loss function on gpu and the comment about using it in regard to cpu and gpu synchronization.
it's not all, but seems like the most obvious. what techniques do you use?
#grokaem_dl
#deep_learning #cpu #gpu #pytorch #hugging_face
TOPIC MODELING
Topic modelling is the task of finding related topics to a bunch of documents. Each document is meant to be constructed of a few topics with their relative importance. You can use topic modelling even when it's not the main task to check data distribution and correlated topics in it and make use of this information by adding new data or using topic-based pre-trained models.
⚡️IMPORTANT:
I've published all links with some more explanations and questions in my notion, please check the link. I also published the same material on medium, my first try, check out milana.shxanukova15/topic-modeling-35cd2acb515f">here. There you can find the link to code samples.
0️⃣ basic info:
Data is a few samples of text, the number of texts should be more or equal to the number of topics.
output is a representation of existing topics in all documents.
1️⃣ approaches:
LDA - a generative model that creates topics based on words that generate texts and appear together.
LSA - model that makes use of SVD on document-term matrix to produce topics.
NMF - the same idea as LSA but with sparse matrixes.
BERTopic - bert embeddings + dimensionality reduction + clustering algorithm.
BERT + LDA - usage of both LDA feature representation and bert embeddings as concatenated input to autoencoder and clustering algorithm as the main topic model.
2️⃣ Metric:
Topic Coherence - how well a topic is supported by a text set - reference corpus. We measure topic coherence as some score of words that occur together in the reference corpus.
#nlp #deep_learning #topic_modeling #bert #milana_medium
#grokaem_nlp
TF-IDF FEATURE SELECTION
#grokaem_nlp
#nlp #data_science #tfidf #feature_selection
Tf-idf надо знать по дефолту и вроде это лакмусовая бумажка для ваших первых гипотез, но в чем могут быть загвоздки?
- Выход у tf-idf матрицы размерности (n_samples, n_features), где n_features количество слов словаря, который вы либо передали, либо он был создан внутри, n_samples - количество текстов.
Понятное дело, что слов у нас может быть дофига, тогда и матрица будет оверогромная. Что делать?
0️⃣ Мы берем за данность, что все стоп слова убрали. Но можно еще и убрать всякие предлоги, которые не всегда стоп слова или выбрать себе набор частей речи и взять их. Если не очень хорошо справляется лемматизатор - взять стемминг поверх, так можно получить псевдооснову и фичей будет меньше. Также всякие имена, названия можно вынести отдельно или удалить вовсе и тд и тп...
1️⃣ Выбрать те фичи (слова или n-граммы), что оставить с помощью SelectKBest, mutual information, variance threshold.
SelectKbest - обертка для score functions, чтобы выбрать на их основанные k лучших скоров и им принадлежащих фич. Для выбора используются функции:
- chi2 (объяснение функции , объяснение как работает в tfidf ) анализ отношений expected / observed frequencies
- mutual information - работает также как information gain в деревьях решений, то есть статистическая зависимость наличия или отсутствия фичи, объяснение раз и два
- anova - еще одна статистика (объяснение через variance)
2️⃣ Выкинуть какие-то фичи после анализа feature importance. Как делается?
Какие методы вы используете?
WIDENING FACTOR
Это понятие довольно старое, но я не cv-шник, так что именно на практике встретила его только в этом году.
widening factor - это фактор, который отвечает за количество feature maps. Он был предложен в статье WideResnet. Как работает обычный resnet.
0️⃣ Главный плюс resnet - это residual block, который должен решать проблему затухающих градиентов при глубоких сетях.
Но у resnet есть минус - это diminishing feature reuse - ситуация, когда либо только несколько блоков действительно 'выучивают' какую-то информацию, либо много слишком мало. (2 страница, wideresnet)
1️⃣ Одно из предложений - это как раз widening factor, который по факту показывает, во сколько раз больше будет аутпут в одном блоке.
Это лучше сто стороны вычислений и глубины сети. (5 страница, wideresnet) Работает просто как умножение количества output feature maps на определенное значение. Усе))
2️⃣ Из плюшек, которые еще показали:
- ввод dropout между слоями конволюций, а не между блоками, что было использовано как некая регуляризация.
3️⃣ Что важно понимать?
- линейно увеличиваются параметры при увеличении глубины сети и слоев внутри блока, но квадратично относительно ширины блока, поэтому все параметры необходимо 'тонко настраивать'.
Имплементация на pytorch
не вау видео, но хорошее
статья
#grokaem_dl
#deep_learning #dl #resnet #wideresnet #cnn
#grokaem_seby_thoughts
It's the second anniversary of this channel, 17th of July. 🎉Thanks to everyone who said that they liked posts here, I appreciate your attention and time a lot.
-
But to be honest, I was really exhausted in the last few months and wanted to abandon everything. It could be caused by many reasons from work-study arrangements draining the life energy up to my private life that I was underestimating for the last 4 years. One way or another, I was in such a dejected condition for too long.
————
If you're in the same situation may be one idea which helped me will be applicable to you too.
I've been always of the opinion that people should make value in any form. Helping others in need was my way of thinking about being beneficial. But if you're subscribed to ai channels, you most of the time (this June was the perfect example) see posts about art generation which is much more about entertainment than necessity. (I don't want to diminish art generation). But AI is not just about it, it's helpful. I literally cried when understood that a girl, Clara woods, could express her thoughts due to text to speech technologies. It's only one example. There are thousands starting from subtitles for deaf people finishing with cancer diagnoses using cv.
You can be beneficial with your knowledge. You can make a difference. You can create history. Even your small steps every day when you struggle with this silly cuda bug can one day be a part of your work for others. Of course, it's not motivational for everyone but I gained power and belief in my own work reminding myself about the perspective to be important. So I will continue working on dementia hoping to make diagnoses faster, and investigate audio and nlp to make solutions for deaf people.
I've decided to make all posts here in English. There are a few reasons:
- maybe the major reason is that I'm losing my English skills while learning German... sad but true... Even though a huge amount of everything I read and listen to is in English (not to mention research publications and technical literature) I speak or write not as frequently as I did before.
- the sphere is fully in English and building communication can be done in English for both Russian and non-Russian speakers
- these posts can better my academic English which I dislike a little bit...
- I enjoy gluttoning myself for challenges....
By the way, you can practise mistakes' corrections by reading my posts, I'm as always open to criticism. Hope you stay tuned.
p.s. I'm currently in Nizhniy Novgorod, so if you're here too and want to practice English with me, I'm very open to try.
GFCC, MFCC, MELSPEC
Спойлер - это сумбурный пост про gfcc, которые используются в обработке звука. Если вам эта область не близка, то мб вам будет просто интересен видос про то, как мы воспринимаем частоты, там про волоски в ушах и их толщину. Тема школьная, но все еще поражающая мое сознание))
Спойлер 2 - все представления пытаются имитировать слух человека. По приколу можете проверить, как вы различаете частоты вот тут. (лучше один наушник снять и убавить), а тут до какой частоты вы слышите. (я до 14к)
________________
Существуют: spectrogram, mel-spectrogram, mfcc, gfcc.
Краткий обзор отличий:
0️⃣ spectrogram отличается от mel-spectrogram mel-filter banks, которые пытаются имитировать то, как слышит человек, где нижние частоты различаются лучше, чем высокие.
1️⃣ mel-spectrogram отличается от mfcc тем, что mfcc делает так называемую декореляцию бинов mel-spectrogram. В видео ниже очень круто это объясняется через понятие vocal track frequency response, в нем у нас отражаются форманты, которые как раз отражают identity of the sound.
2️⃣ mfcc отличаются от gfcc во многом, подробнее в статье, но главные вещи это - фильтр, здесь у нас это не mel filter banks, а gammatone filter bank + берется не log, а кубический корень, что позволяет 0 коэффициент, который отвечает за energy в звуке переносить в сами коэффициенты и таким образом gfcc не scale invariant.
________________
GFCC - gammotone filter cepstral coefficients.
Помним про melspec, там у нас были mel filters, которые по сути дают определенный вес каждому значению частоты. Они треугольные и справляются с задачей - разделения расстояния между низкими и верхними частотами. Gammotone filters схожи с mel filters, но имеют другую форму, их края более мягкие и представлены от impulse response. Впервые их предложили в 1972 году, как говорит Википедия, когда исследовали cochlear nucleus (это нерв такой) у котиков. Форма gammotone filter.
То есть по сути gfcc это тот же melspec, только фильтры не треугольные)
Важно, что хоть в этом сумбурном посте я различаю именно gfcc и mfcc, нужно помнить, что mfcc наследуется от спектрограммы, я gfcc принимает сырой звук.
И melspec, и mfcc работают в torchaudio, а вот gfcc можно посчитать с spafe.
неплохие посты про mfcc и mel spec
лучшее видео про mfcc, melspec
статья про mfcc и gfcc и их разницу
#grokaem_audio
#deep_learning #audio_processing
#датасайнс_ссылки
Удобное расширение, чтобы копировать код и не код со слайдов и видео в обычный текст. Особенно актуально, когда смотришь видос с кодом, а в описании нет ссылки на гитхаб.
blackbox.io