Data modeling, restructuring, and massaging: Difference between revisions

From Helpful
Jump to navigation Jump to search
Line 27: Line 27:


=NLP data massage=
=NLP data massage=




Line 182: Line 183:


-->
-->
==Putting meanings or numbers to words==
===Knowledge base style===
===Statistic style===
====Collocations====
Collocations are statistically idiosyncratic sequences - the math that is often used asks
"do these adjacent words occur together more often than the occurrence of each individually would suggest?".
This doesn't ascribe any meaning,
it just tends to signal anything from
empty habitual etiquette,
jargon,
various [[substituted phrases]],
and many other things that go beyond purely [[compositional]] construction,
because why other than common sentence structures would they co-occur so often?
...actually, there are varied takes on how useful [[collocations]] are, and why.
====Word embeddings====
<!--
{{zzz|For context|
Where people are good at words and bad at numbers, computers are good at numbers and bad at words.
So it makes sense to express words ''as'' numbers?
...that goes a moderate way, but if you want to approximate the contained meaning ''somehow'',
that's demonstrably still too clunky.
To illustrate one limitation, you could say that 'saw' is represented by a specific number.
Such an enumeration means that "we saw the saw" cannot be expressed in numbers without those two words ''having'' to be the same thing.
You can imagine that's not a problem for, say, antidisestablishmentarianism.
There's not a lot of varied subtle variation in its use, so you can pretend has one meaning, one function.
Can't you just have multiple saws? Sure, but how do you decide?
Saw as a verb, saw as a noun? Aside from cases where variants inflect a little regularly, or not visible,
the larger problem is that that's that's somewhat circular, depending on sort-of-already-knowing.
In reality, having a good parse of sentence structure is just ''not'' independent from its meaning,
you end up having to do both concurrently.
It turns out that a lot language, ''does'' need to be resolved by context of what they do to nearby words. 
And it turns out the most commonly used words often the weirder ones.
This entanglement seems to help thing stay compact, with minimal ambiguity, and without requiring very strict rules,
which are things that natural languages seem to like to balance (even [[conlang]]s like lobjan engineer this balance),
and has some other uses - like [[double meanings]], and intentionally generalizing meanings.
So we're stuck with compact complexity.
We can try to model absolutely everything we do in each language, and that might even give us something more useful in the end.
Yet imagine for a moment a system that would just pick up 'saw after a pronoun' and 'saw after a determiner',
and not even because it knows what pronouns or determiners are, but because given a ''load'' of examples,
those are two of the things the word 'saw' happens to be next to.
Such a system ''also'' doesn't have to know that it is modeling a certain verbiness and nouniness as a result.
It might, from different types of context, perhaps learn that one of these contexts relates it to a certain tooliness as well.
If fact, such a system won't and ''can't'' explain such aspects.
So why do it?
Well, it learns these things without us telling it anything, and the similarity it ends up helping things like:
: "if I search for walk, I also get things that mention running",
: "this sentence probably says something about people moving"
: "walk is like run, and to a lesser degree swim",
: "I am making an ontology to encode these and would like some assistance to not forget things"
Also, this is an [[unsupervised]] technique. Yes, you will probably get more precise answers with the same amount of a supervised technique, i.e. with well annotated data,
but it is usually harder to have a good amount of well-annotated data (that you can have endless discussions about),
and much esier to have ''tons'' of un-annotated data.
It may pick up relations only softly, and in ways you can't easily fetch out,
but it's pretty good at not missing them,
meaning you don't have to annotate half the world's text with precise meaning
for it to work.
You just feed it lots of text.
There are limitations. It might pick up on more subtleties,
but like any unsupervised technique,
and tends to be better at finding things that at describing ''what'' it found.
You would get further by encoding 'saw as a verb' and 'saw as an implement' as different things,
sure, but that would only solves how to ''store'' knowing that. Not finding out.
This is not a great introduction, because there are multiple underlying issues here,
and we aren't even going to solve them all, we will just choose to go just half a step fuzzier,
to a point where we can maybe encode multiple uses of words (and if they have a single one, great!).
Word embedding are usually explained as "vector representation for a word"
Such vectors are useful for things like subject similarity, sentiment analysis, syntactic parsing,
The fact that it's a vector isn't that important.
It happens to be mathematically handy to work with,
and we happen to want to end up with vector values where similar vectors ''hopefully'' carry similar meanings.
'Embeddings' is a bit of a strange term for that concept,
and seems to point out (with most methods we use today) how training these considered their context
And when using the same to figure out what unseen text means, it may well assign the tool-ish sense, or the verb-ish sense, on similar context.
In fact, some patterns are strong enough that even unseen words will get a decent estimation.
Say, give we spoiaued to an embedding-style parser, and it's going to guess it's a verb ''while also'' pointing out it's out of its vocabulary.
Note that
* you won't really know what these vectors mean.
: You can fish this out, to a degree, because you can compare vectors.
: say, if any given vector compares much better to 'hammer' or to 'see' (e.g. from examples sentences), you can start to figure out what it meant.
* the thing you train does not necessarily
In ''use'', the assigned vector is typically not dependent on the context of the current,
but the vector that was learned earlier was dependent on the context in the training data. {{verify}}
(you can call this [[distributional similarity]], that a word is characterized by the company it keeps.
These vectors come from machine learning (of varying type and complexity).
* LSA
* [[word2vec]] -
: technically patented?
: T Mikolov et al. (2013) "Efficient Estimation of Word Representations in Vector Space"
* tok2vec
* FastText
: https://fasttext.cc/
* lda2vec
: https://multithreaded.stitchfix.com/blog/2016/05/27/lda2vec/#topic=38&lambda=1&term=
All of this may still apply a single vector to the same word always (sometimes called static word embeddings).
This is great for unambiguous content words, but less so for polysemy and
-->
=====Contextual word embeddings=====
<!--
The first attempts at word embeddings were typically static vectors,
meaning that the lookup (even if trained from something complex)
always gives the same vector for the same word.
This was mostly to keep the data manageable, and to keep it a simple and fast lookup.
They help in broader tasks like estimating the overall topic of a text,
and more details where ambiguity is low,
but will do poorly in the specific cases where words's meaning depends on context.
''' ''Contextual'' word embeddings''', on the other hand,
learns about words ''in a sequence''.
This is still just statistics, but the model you run will give
Depending on how much context you pay attention to,
this is even a modestly decent approach to machine translation.
Consider "we saw a bat".
In theory, it may assign
* a vector to the verb saw that is more related to seeing than to cutting
* a vector to the noun bat that is more related to other animals than it is to sports equipment
That said, that is not a given.
: There's at least four options and it might land on any of them, particularly for a sentence in isolation
: the meanings we propose to be isolated here may get weirdly blended in training
Consider "passing out" can mean anything from giving people things to losing consciousness.
Generally, the more commonly used the verb, the tricksier it is.
We like the idea of one word, one meaning, but most languages ''really'' messed that one up.
-->
=====Subword embeddings=====
<!--
A model where a word/token can be characterized by something ''smaller'' than that exact whole word.
Often just because words happen to share large fragments of characters,
(not because of stronger analysis like good given lemmatization, strongly compositional agglutination (e.g. turkish).
Chances are it will pick up on such strong patterns, but that's more a side effect of them indeed being regular)
Examples:
fastText
[https://d2l.ai/chapter_natural-language-processing-pretraining/subword-embedding.html]
-->
=====Bloom embeddings=====
<!--
A [[bloom filter]] applied to word embeddings to get better-than-nothing embeddings from something very compact.
https://explosion.ai/blog/bloom-embeddings
https://spacy.io/usage/v3-2#vectors
-->
<!--
===(something inbetween)===
====semantic folding====
https://en.wikipedia.org/wiki/Semantic_folding
-->
<!--
====Hyperspace Analogue to Language====
-->
===Could be either style===
====Semantic similarity====
<!--
Semantic similarity is the bread area of [[metric]]s between words, phrases, and/or documents.
Some people use this term specifically when it is based on strongly coded meaning/semantics (ontology style),
because this lets you make stronger statements,
contrasted with similarity based only on [[lexicographical]] details word embeddings.
...the latter is fuzzier, but also tends to give a reasonable answer to a lot of things that the more exact approach
https://en.wikipedia.org/wiki/Semantic_similarity
This can include
* words that appear in similar context (see also [[word embeddings]], even [[topic modelling]])
* words that have similar meaning
* Topological / ontological similarity - based on more strongly asserted properties
:: '''Semantic similarity''' may well refer more specifically to ''only'' "is a" and other [[ontology|ontological]] relationships,
and ''not'' just "seems to co-occur"
:: '''Semantic relatedness''' then might also include [[antonyms]] (opposites), [[meronyms]] (part of whole), [[hyponyms]]/[[hypernyms]]
The last tries to not only know e.g. 'car' and 'road' and 'driving' are related
but also roughly ''how'' they are related, in a semantic sense.
between documents, or between
Semantic similarity is a metric defined over a set of documents or terms, where the idea of distance between items is based on the likeness
-->
===The distributional hypothesis===
The distributional hypothesis is the idea that
words that are used and occur in the same contexts tend to convey similar meanings - "a word is characterized by the company it keeps".
This idea is known under a few names,
but note that few of them really describe a technique,
or even the specific assumptions they make.
<!--
Distributional Similarity
Distributional semantics
https://en.wikipedia.org/wiki/Distributional_semantics
'''Distributional similarity''' can refer to analysis to bring those out, often for the goal of figuring the relevant semantics.
for example on noun-verb combinations, this can be referred to as .
This also means being able to, in that example, being able to e.g. predict noun similarity based on their likeliness of combination with the same verbs.
-->
===Moderately specific ideas and calculations===
====latent semantic analysis====
Latent Semantic Analysis (LSA) is the application of [[Singular Value Decomposition]] on text analysis and search.
====random indexing====
https://en.wikipedia.org/wiki/Random_indexing
====Topic modeling====
Roughly the idea given documents that are about a particular topic,
one would expect particular words to appear in the each more or less frequently.
Assuming such documents sharing topics,
you can probably find groups of words that belong to those topics.
Assuming each document is primarily about one topic,
you can expect a larger set of documents to yield multiple topics,
and an assignment of one or more of these topics, so act like a soft/[[fuzzy clustering]].
This is a ''relatively'' weak proposal in that it relies on a number of assumptions,
but given that it requires zero training,
it works better than you might expect when those assumptions are met.
(the largest probably being your documents having singular topics).
https://en.wikipedia.org/wiki/Topic_model


==Words as features  - one-hot coding and such==
==Words as features  - one-hot coding and such==

Revision as of 23:07, 22 March 2024

This is more for overview of my own than for teaching or exercise.

Overview of the math's areas

Arithmetic · 'elementary mathematics' and similar concepts
Set theory, Category theory
Geometry and its relatives · Topology
Elementary algebra - Linear algebra - Abstract algebra
Calculus and analysis
Logic
Semi-sorted
: Information theory · Number theory · Decision theory, game theory · Recreational mathematics · Dynamical systems · Unsorted or hard to sort


Math on data:

  • Statistics as a field
some introduction · areas of statistics
types of data · on random variables, distributions
Virtues and shortcomings of...
on sampling · probability
glossary · references, unsorted
Footnotes on various analyses


  • Other data analysis, data summarization, learning
Data modeling, restructuring, and massaging
Statistical modeling · Classification, clustering, decisions, and fuzzy coding ·
dimensionality reduction ·
Optimization theory, control theory · State observers, state estimation
Connectionism, neural nets · Evolutionary computing
  • More applied:
Formal grammars - regular expressions, CFGs, formal language
Signal analysis, modeling, processing
Image processing notes



Intro

NLP data massage

Bag of words / bag of features

The bag-of-words model (more broadly bag-of-features model) use the collection of words in a context, unordered, in a multiset, a.k.a. bag.

In other words, we summarize a document (or part of it) it by appearance or count of words, and ignore things like adjacency and order - so any grammar.



In text processing

In introductions to Naive Bayes as used for spam filtering, its naivety essentially is this assumption that feature order does not matter.


Though real-world naive bayes spam filtering would take more complex features than single words (and may re-introduce adjacenct via n-grams or such), examples often use 1-grams for simplicity - which basically is bag of words, exc.

Other types of classifiers also make this assumption, or make it easy to do so.


Bag of features

While the idea is best known from text, hence bag-of-words, you can argue for bag of features, applying it to anything you can count, and may be useful even when considered independently.

For example, you may follow up object detection in an image with logic like "if this photo contains a person, and a dog, and grass" because each task may be easy enough individually, and the combination tends to narrow down what kind of photo it is.


In practice, the bag-of-features often refers to models that recognize parts of a whole object (e.g. "we detected a bunch of edges of road signs" might be easier and more robust than detecting it fully), and used in a number image tasks, such as feature extraction, object/image recognition, image search, (more flexible) near-duplicate detection, and such.

The idea that you can describe an image by the collection of small things we recognize in it, and that combined presence is typically already a strong indicator (particularly when you add some hypothesis testing). Exact placement can be useful, but often easily secondary.


See also:

N-gram notes

N-grams are contiguous sequence of length n.


They are most often seen in computational linguistics.


Applied to sequences of characters it can be useful e.g. in language identification, but the more common application is to words.

As n-grams models only include dependency information when those relations are expressed through direct proximity, they are poor language models, but useful to things working off probabilities of combinations of words, for example for statistical parsing, collocation analysis, text classification, sliding window methods (e.g. sliding window POS tagger), (statistical) machine translation, and more


For example, for the already-tokenized input This is a sentence . the 2-grams would be:

This   is
is   a
a   sentence
sentence   .


...though depending on how special you do or do not want to treat the edges, people might fake some empty tokens at the edge, or some special start/end tokens.


Skip-grams

This article/section is a stub — some half-sorted notes, not necessarily checked, not necessarily correct. Feel free to ignore, or tell me about it.

Note: Skip-grams seem to refer to now two different things.


An extension of n-grams where components need not be consecutive (though typically stay ordered).


A k-skip-n-gram is a length-n sequence where the components occur at distance at most k from each other.


They may be used to ignore stopwords, but perhaps more often they are intended to help reduce data sparsity, under a few assumptions.

They can help discover patterns on a larger scale, simply because skipping makes you look further for the same n. (also useful for things like fuzzy hashing).


Skip-grams apparently come from speech analysis, processing phonemes.


In word-level analysis their purpose is a little different. You could say that we acknowledge the sparsity problem, and decide to get more out of the data we have (focusing on context) rather than trying to smooth.

Actually, if you go looking, skip-grams are now often equated with a fairly specific analysis.



Syntactic n-grams

Flexgrams

Putting meanings or numbers to words

Knowledge base style

Statistic style

Collocations

Collocations are statistically idiosyncratic sequences - the math that is often used asks "do these adjacent words occur together more often than the occurrence of each individually would suggest?".

This doesn't ascribe any meaning, it just tends to signal anything from empty habitual etiquette, jargon, various substituted phrases, and many other things that go beyond purely compositional construction, because why other than common sentence structures would they co-occur so often?

...actually, there are varied takes on how useful collocations are, and why.

Word embeddings

Contextual word embeddings
Subword embeddings
Bloom embeddings

Could be either style

Semantic similarity

The distributional hypothesis

The distributional hypothesis is the idea that words that are used and occur in the same contexts tend to convey similar meanings - "a word is characterized by the company it keeps".


This idea is known under a few names, but note that few of them really describe a technique, or even the specific assumptions they make.


Moderately specific ideas and calculations

latent semantic analysis

Latent Semantic Analysis (LSA) is the application of Singular Value Decomposition on text analysis and search.


random indexing

https://en.wikipedia.org/wiki/Random_indexing


Topic modeling

Roughly the idea given documents that are about a particular topic, one would expect particular words to appear in the each more or less frequently.

Assuming such documents sharing topics, you can probably find groups of words that belong to those topics.

Assuming each document is primarily about one topic, you can expect a larger set of documents to yield multiple topics, and an assignment of one or more of these topics, so act like a soft/fuzzy clustering.

This is a relatively weak proposal in that it relies on a number of assumptions, but given that it requires zero training, it works better than you might expect when those assumptions are met. (the largest probably being your documents having singular topics).


https://en.wikipedia.org/wiki/Topic_model

Words as features - one-hot coding and such

Word embeddings

Word2vec

Continuous bag of words (cbow)

Continuous skip-grams

GloVe