text:
Why the company such as openai has not build probabilistic LLM which tells the user "How sure the model is about its answer"?
I am curious why the LLM model is not built to give a probabilistic answer. Whether people are working on it or is it not necessary to be accomplished.

Because it's an ill-defined question.
You might be 100% sure of the wrong answer :)

You might, but there exist models where it's possible to specify how certain the model is, and that's valuable information even if there remain false answers that it's confident in.  Recall IBM Watson's Jeopardy answer of "What Is Toronto?????"

@Ray no, you cannot. take a model that has a posterior distribution $p(x_T|x_{i<T})$ to be deterministic with 100% probability on the word "cat" no matter what. That prediction is 100% confident and 0% correct. For the models that give you some score, that usually is either the predictive entropy, the perplexity, or some score of the posterior distribution. However, changing the temperature of the softmax will induce these scores to change, even if the answer is the same, showing how they are definitely not reliable measures :)

"there exist models". There also exist plenty of models where it's not especially practical to give a meaningful confidence measure; I just wouldn't go so far as to call it an ill-defined question.

@Ray then define what you mean by "how sure an LLM is" ;)

I wasn't really thinking LLMs specifically (hence my example of Watson). Language models aren't great for certainty measures, but more due to what the distribution is over, rather than any issues with temperature (we can always select the temperature to the one that minimizes some error metric over a validation set, after all, and while we can set the temperature arbitrarily high, by the time the resulting distribution starts looking anything like uniform, the model has long since stopped being useful).

For language models, the probabilities in question are (to first approximation) of the form "Probability that word i is 'elephant' given the surrounding context", or more generally, the probability that if we randomly select a sentence/phrase/whatever of this length from the set of all possible sentences that we'll get this one. In this distribution, the sentences "The answer is 'elephant'", "An elephant", and "one of those whatsises, like mammoths but less furry" are all separate statements with separate probabilities. The model does not possess a single number that represents the probability that the idea is correct.
Different types of model are more or less amenable to giving certainties of that form, and you could probably define some sort of model that provides a certainty measure over the set of (answer's semantic vector, question's semantic vector) pairs, but that's a decidedly nontrivial project in of itself, and doesn't naturally fall out of the existing math.


text:
How successful are the state-of-the-art (2023) email filters really?
How successful are the state-of-the-art (2023) email filters really?
Some references claim that spam detection may reach high accuracy in test settings, but I've thought that email filtering should essentially be as large problem as filtering a whole language. Because one cannot guarantee anything about strings in emails. There will simply be too many possible combinations of strings.
Spam filters might work, but I doubt they can ever reach 100% accurate labeling. And if they don't, they cannot useful, because even losing few important message can be intolerable.

Please try to ask an objectively answerable question (particularly in the title). The question 'how difficult is something' is not an objectively answerable question. Try to rephrase it such that an objective answer can be given that satisfies the question. Something along the lines (for example) of 'Which aspects of email filtering contribute most to its perceived difficulty?' or the like, depending on what part of the problem interests you most.

@RobinvanHoorn Why is robust email filtering about objective measures?

"How hard" is certainly answerable in e.g. https://en.wikipedia.org/wiki/NP-hardness sense. So an answer could e.g. reference the amount of data needed to train convolutional networks or something.

I like the question, however, it is the fact that the goal of this forum is about establishing factual knowledge, and not opinion-based information (check https://ai.stackexchange.com/tour). As such, we'd like to promote the formation of factually answerable questions. Regarding your second comment, yes NP-hardness is a perfectly good topic to talk about (whether something is NP or not). However, your question was not asking about whether email filtering is NP-hard or not, it was asking about the difficulty of the problem, which is perceived differently by people.

@RobinvanHoorn It would not be objective to give an algorithmic complexity answer, IF a human sorter can perform some functions better. Therefore the question is supposed to contain some amount of choice like ML algorithms usually do. As given in the answer by Alberto, it can be answered by suggesting what AI can do. The sidebar also had e.g. this question https://ai.stackexchange.com/questions/2645/how-do-we-know-that-now-artificial-intelligence-will-really-take-off?rq=1 which seems far more subjective. A possible answer could also reflect the amount of data in languages.

It seems the big companies like Apple are not investing enough on this issue because I keep receiving spam, scam, etc. It's so annoying. Whatever tool it's using, it's quite useless.

What? Goodness in mathematical measure sense is certainly not opinion-based.

No, AI solves literally the problem of the "too many possible combinations"
Indeed, already nowadays most of the spam is managed by some AI system, combined with some rule based on the origin of emails and certifications of the domain, however, the main problem are adversarial attack
Adversarial attacks are special string that fools the AI to believe that an email is save even if it's clear for any human that it's not. Recently a paper has shown that there is even generalization on these adversarial examples over multiple AIs, so a single string might break multiple systems at once
(Gmail already uses AI to detect spams, and every time you mark an email as spam, Google uses that information to improve the AI system)

How are these AI-based systems different from rule-based systems?

If we play with examples, then consider an email list that is legitimate and has been subscribed to by the user, but that the user does not read anymore? Is it spam or not?

Do you mean by "too many possible combinations" that it's about reducing volume and not precision? What if it does errors (i.e. loses important mail)?

@RockPaperLz-MaskitorCasket one is made out of handcrafted rules and the other one has learnt the rules?..

@mavavilj the definition of span depends on what the owner of the system wants

While "human-made labelling" may not achieve perfect accuracy, it's worth considering alternative approaches. One such option involves employing AI-driven labeling techniques, often referred to as self-labelling. This method involves training the model on data that it labels autonomously.
Furthermore, it's worth exploring the inclusion of supplementary information that models can leverage to enhance their predictions. This may encompass data points like dates, email senders, and other pertinent details. Such additional context can significantly contribute to the model's overall predictive capabilities.

This makes it sound tough still, because now it's also about understanding what a human might want to do. I just erased about 20k emails from my bloated inboxes (they were from a span of about 6 years), and I think that an algorithm would not have sped the work at all. It could've added work by requiring me to monitor what the algorithm does. If we'd use speed as a measure, then I think a human can maybe check 1-3 emails in a second so that the human is assured about the content.

source: Stack Exchange

license: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/4.0/

sort: votes


text:
Why LLMs and RNNs learn so fast during inference but, ironically, are so slow during training?
Why LLMs learn so fast during inference, but, ironically, are so slow during training? That is, if you teach an AI a new concept in a prompt, it will learn and use the concept perfectly and flawless, through the whole prompt, after just one shot. Yet, if you train it in just a single sample, it will not influence its behavior at all - it will essentially forget. Why can't RNNs use whatever is happening during inference, rather than gradient descent, to update its weights and, thus, learn? In other words, can't the attention mechanism itself be used to update weights, rather than some cost function?

One easy answer is that, during training, a model is learning grammar, conventions, spelling, word choice, etc. but not concepts. During inference, a model still doesn't know about concepts, but it can tell that a cluster of tokens is novel, and various attention techniques keep that cluster's probability high.

As pointed out by others, what you call "learning" at inference, is nothing more than providing more context. The model can indeed memorize in its short-term, but it is only working for the current task at hand. You suggest that we could make a model with an infinite contextual memory, but then it would mix up all tasks together. It would literally be like if you had to recite all the numbers you ever calculated or counted or saw before starting a new calculation.
Hence, contextualization is only useful for short-term tasks, and it works only thanks to the slow learning phase you have to do the first time around, which is more formally called the "convergence process".
So, what you are looking for is in fact to make the convergence process faster, and more precisely a one-shot or zero-shot learning. If you don't just look at LLMs (Large Language Models) and RNNs (Recursive Neural Networks), there are a lot of other AI models that can do one-shot or even zero-shot learning, such as memory models like grippon-berrou neural network. One-shot learning can learn the first time they see an example, and generalize over it. Zero-shot learning can even learn without being presented some examples, by generalizing from others, or by transferring knowledge from another field.
For example, Text2Video-Zero is a recently published text to video generator, which did NOT learn from any video, but instead reused the weights from Stable Diffusion trained on still images. What this algorithm does is that it can cleverly generalize learning from still images into a coherent sequence of images with the same style, hence mimicking motion, with no additional cost. Of course, it's not completely zero-shot, because it has to be provided with a Stable Diffusion weights model first, but essentially zero-shot learning means that you can reuse one model that was made for one purpose for another purpose, for free (ie, you can directly infer, no need to re-learn anything).
Technically, One/Zero-shot learning typically requires another kind of architecture, more brain-like (ie, with discrete 0/1 synaptic weights). The long convergence processes are usually required by networks using floating weights (ie, the McCulloch-Pitts neurons). Because floating weights are not at all biologically plausible, they are a mathematical abstraction that synthesizes several biological functions of biological neural networks into fewer, more amenable to programming abstractions.
Likewise, convolution layers in CNNs (convolutional neural networks) are another abstraction of how biological systems integrate big populations of neurons, but here we can use a much smaller population of artificial neurons, and use more optimized instructions sets to do the same work as the brain does. You have to keep in mind that for a lot of purposes in AI, current computers are much less efficient than the human brain, hence why all these highly synthetic reproductions, more optimized for the machine but very remote from how real biological systems work, are necessary. Here, long convergence (ie, long learning) is an unavoidable artifact from how we model our artificial neurons and synapses, with floating numbers instead of discrete (binary), and with mathematical functions for integration instead of analog biological integration (which is both more fine grained and simpler than numerical functions, see for example the videos by Veritasium about analog computers, biological systems have similar properties and advantages).
RNNs are a kind of the opposite approach and problem, because they use a more biologically plausible property, recursivity, but the problem is that we have a hard time defining artificial systems that are efficient at learning recursive networks. So here, it's the opposite of what can be observed with CNNs and LLMs: the long convergence is due to current science providing inefficient learning algorithms when recursivity is involved. The last few years saw tremendous progress on this, with very clever algorithms, but it's still very far from how biological systems can neatly manage recursivity.
All that is to say that, to answer directly your question, why the current LLM and RNN models can't learn in zero/one-shot from the get-go: it's because nobody found a way to mathematically formulate such a model. Maybe someone will be able to in the near future, maybe it will take decades, but for now, it's the slow convergence LLM and RNN models that work, it's the ones that provide you with the hyped tools such as ChatGPT.
Personally, I think we won't get there until we find how analog biological neural system work, and then we need to develop new computer technologies to mimic those. There is already a lot of work towards these, with biological neurons reprogramming by ARN signalling or mixing them with silicon neurons, but it's still far from the "real deal". There are at least hundreds of different types of neurons, and there are many other neural cells types with not completely understood functions. We are far from fully understanding biological neural systems, but progress is continuous and steady.
Disclaimer: I am both an AI researcher and a clinical neuroscientist and I studied some computational neuroscience.

/EDIT: A small update to extend my explanation above for the technically and philosophically inclined ones: learning at its most fundamental level can be defined as the ability of a system to modify its structure to reflect some input signal, and memory being the system itself that can modify its structure according to input signals. In biological systems, there are two types of memory: short-term and long-term. Recent artificial recursive neural network models try to mimic this, with the very famous LSTM model (Long-Short Term Memory), itself a precursor of the GPT models. By convention, in machine learning we call "learning" the tweaking of the weights, ie, the long-term memory. But there is also indeed a short-term memory which has its own weights, but AI researchers don’t call this process learning, although it technically is by all standards, the only difference being the exact method used and the length of time the memory is retained.
And just like there are models that modify/learn short-term memory at inference but not long-term memory, there are models that tweak their long-term memory at inference, notably bayesian models, as often used for weather forecasting.
So why LLMs and RNNs learn fast during inference is because they are designed to only learn short-term memory, so that the big lot of weights of long-term memory were learnt beforehand. But future improvements of the tech may very well allow to design networks that also learn long-term memory "online", in real-time, in a stochastic manner with a guarantee of convergence.

Thanks, this was the answer I was looking or. Just to be sure: do we know that McCulloch-Pitts Neuron is how a human neuron works? I.e., our brains are essentially networks of M-P neurons? So, the only question left is how these neurons update their weights and prune or form new synapses?

@MaiaVictor The opposite: we know brain neurons (not just humans') are NOT working like McCulloch-Pitts. But they still are a good enough model for most purposes. But there are more biologically accurate/plausible neurons models, those that can generate action potentials, one of the oldest being Hodgkin–Huxley. But then they are computationally less efficient, so they are better to study human neurons in-silico, but not to make the next big AI app that everyone will want to use to boost their productivity. See computational neuroscience.

@MaiaVictor Also modern neurobiology ditched the old neuron-centric view: we learn more and more about all the other neural structures that are essential if not more than neurons for the brain to work: synapses, glial cells, neuromodulators, neurotransmitters, etc. Also, all have lots of various subtypes. You can see the brain as being just like the whole human population: there are all sorts of colors and hairstyles and face-body shapes, and they build houses of all shapes and designs, there is an unfathomable diversity distributed around the world, adapted to their respective environments.

There is huge difference between what is happening with the information during training and during inference and one can not be used for the other.
Let me start with an analogy to the human brain (which is not a very good analogy and has several flaws, but it gives an intuition, I will late build on):
If I tell you "A palindrome is a number, a word or a sentence that reads the same forward and backward. One example palindrome is 15651" then you will now know what a palindrome is and you will be able to work with this new concept. If I tell the same to a newborn baby, it will not. It takes years to bring a baby to the point that it is able to understand my previous sentence.
Enough of the analogy. Let's have a look at the RNNs:
RNNs are neural networks with weights. Unlike some other networks, they have something that you can call an internal state. Weights and internal state are different:

The internal state serves as a memory that stores information of the previously processed information, e.g. a new concept that was explained earlier.
The weights define how new information changes the internal state and how input and internal state produce some output.

So untrained neural networks typically have randomly initialized weights. New input will then cause kind of arbitrary output und updates of the internal states. So if you give a new concept to an untrained neural network as an input, it will not the new concept, but update the internal state into meaningless numbers and produce random gibberish as output.
If you train the model, the weights are updates to serve a given purpose. This can be a relatively simple model, e.g. that detects whether a tweet is positive or negative. In this case, the network would only be trained on tweets and the internal state would only represent the positivity of the previous words and maybe if the last word was "not" to distinguish "I am happy" from "I am not happy". Probably much more detailed and not so easy to interpret, but something like this.
But if you build an LLM, you will train on much more heterogeneous data and for tasks that will involve to understand new concepts. In this case the weights of the model will be learned in a way, that the network can process new concepts and store the essence of the concept in the internal state.
In short: teaching the network new concepts (which is an update of the internal state) can only happen, because before a long training of the weights enables the LLM to do so.
Avoiding Backpropagation: There is some recent work that explores new ways of training that avoid the costly backpropagation and instead preforms two forward steps. The forward-forward Algorithm, but to my knowledge it is not used for LLMs, yet. And even if, one would still need to train it on a huge amount of data to learn some weights that allow the network to process new concepts as input.

"training it with a limited corpus until it can accept new corpus from its prompt" But the until never happens if you train it with a limited corpus. If the training set isn't big enough it is much more likely that the training will result in weights that make it output human-like writing only in the training set but not when given new prompts.

But if you teach a human what a palindrome is, only once, they will be able to use that concept for the rest of their life, assuming they paid attention. On LLMs, if they never heard about palindromes before, and if you try to teach them what a palindrome is by feeding a single explanation in a fine-turning step, it will absolutely not learn it, at all. And that's the problem: for humans, inference and training is unified; we can "update weights" to learn a new concept forever with a single example, just like LLM can learn that concept temporarily in a prompt, but only for its duration.

For an LLM, the difference is again the difference between state and weights. While the state is typically resettet after each session, the weight stay the same. It would theoretically be possible to keep one state between the sessions and then the model would learn new concepts during inference and remember them. But then the network would need a much bigger state to store all the new concepts. And risk would be that some concepts overwrite others

@user253751 as I wrote before, there are two ways to store information for the RNN: weights and state. The state covers all the information givin in the current session, but has limited size (so during a longer session, knowledge might get lost). The weight store all the knowledge and "abilities" learned during training. This training need so much input data for two reasons: 1. to store knowledge to answer questions and 2. to learn to handle a high variety of task. If you only train on english wikipedia, it will know a lot, but it can only create wikipedia-like articles.

@user253751: I have to admit that I am not sure which model you mean. Typically, there is more than just the wikipedia involved. And note, that LLMs have typically multiple phases of training. Including pre-training, unsupervised training and refinements. Nevertheless, the rule of thumb that more heterogeneous data leads to more generalized abilities still holds.

@user253751 It is a difference, whether you tell the model how to reply to a german greeting (i.e. provide knowledge during inference) or the model has the ability to speak german. Of course, you could start to provide the model with an extensive German course during inference before speaking in German with it. But better train it on German texts as well. And - I am speculating now - it might likely be that as model trained also on texts from language courses is much better able to process German courses than a model only trained on wikipedia (Training gives both knowledge and abilities)

@user253751 "do you think ChatGPT is only able to learn from input that appears in its training set?" Not in the way you use the word "learn".
Let me rephrase: For a given training set, there are different possible sets of weights that make the model score well on the training set. In general, there are much fewer possible sets of weights that allow the model perform well on new input. When you increase the training set, it is more and more likely that a set of weights that scores well in the training set is also one that performs well in general.

Actually @user253751 your gibberish example about priffins currently leads to chatGPT asking to explain because those are not commonly used words and the meaning is unclear. And if you did take enough time to explain any define everything, That training would disappear once it slides out of ChatGPTs recent context window or you start a new session.

They are not "learning" during inference at all.
Learning is the process of updating the weights of the model (to lower loss). This does not happen during inference. The model weights stay the same.
When you are "teaching an AI a new concept", you are just giving it some context, which improves its ability to answer what happens next. But weights do not get updated. That is the computationally expensive part.
In a meta context, I guess you can call this "learning", and this is effectively what is being done with Microsoft's new venture with GPT. They are letting it search things on the fly. Probably a lot of interesting research will surface using such techniques soon :)

I think both answers here miss my point though! It is absolutely capable of learning concepts very fast at inference, and, yes, it is not updating its weights. Do you see the problem there? (;

It is "learning" in the sense that you are giving it some context. But we associate the term "learning" with how humans learn something. For some intuition, if you would consider a human to be a neural network, its as if we are neural networks that have no ability to control whether or not our weights get updated. So we are always "learning".
But in this context, the definition of "learning" is not the same. Because if you switch to another context (like another chat in chatgpt), it will not remember the context.

If you really want to, you can say the model has "learned" how to use context. But that is as far as I would go.

@MaiaVictor: since you think that our answers miss the point, I will try to update my answer and hopefully meet your expectations a bit better.
In short (and for my taste too short):
training = learning how to learn new concepts + learning a huge knowledge base
inference = learn new concepts fast, but only if the training has been done before

@Broele I wasn't saying that in a condescending tone, both answers were great! I just wanted to point that perhaps there is some way to adjust weights based not on gradient descent, but on the attention mechanism itself, that we're somehow missing. Thanks for the work you put in it.

@MaiaVictor: I was not understanding it in a condescending tone :). But it sounds like your question is not fully answered. If that's the case, we might try to improve the answer.

It's kind of like short-term memory versus long-term memory. Giving a language model a small amount of information at inference time allows it to use that information, and so you might say that the model has "learned" that information, but this "learning" isn't really useful in the long term.
For RNNs, the problem is that the state vector only contains a limited amount of information. You can tell an RNN something once, but as you give it more information, it will forget what you told it previously. So if you have a large amount of information that you want your RNN to be able to access, then providing that information as input during inference won't do the trick; you need to train it.
For transformers, the problem is that the amount of time it takes the model to process a token of input is proportional to the number of tokens it's already processed. If you have just a small amount of information that you want the transformer to learn, that's not a problem, but if you try to give a transformer a very large amount of information as input, that will make inference very slow.
Note that language models are sometimes permanently "taught" things by means of input instead of training. For example, it's been reported that ChatGPT and Bing Chat have a hard-coded prompt that's always present at the beginning of the input, and which contains some information about what the developers want the model to do.

all_licenses: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/4.0/


text:
machine learning for a budgeting application
I am interested in finding references and previous applications where prior year budgets are analyzed to provide guidance for a current year budget.   Specifically, each year some two thousand items are evaluated for funding, with perhaps 500 funded in that year. Information is available in a spreadsheet with multiple parameters that are manually evaluated to determine if an individual item is funded in the budget. I would appreciate any guidance as to how best to make use of such data for say the previous 5 years, where I know what has been funded in those years, to assist in screening items for the current budget year, in particular what approach to ML would be best.  I have attempted a literature search but have not found anything directly relevant.
Edit:
Found this reference in my literature search, looks to be applicable:
https://www.datacamp.com/courses/case-study-school-budgeting-with-machine-learning-in-python

One approach you could take is to use a supervised learning algorithm to train a model on past budget data in order to predict which items are likely to be funded in the current budget year. This would require labelling the data with a binary label indicating whether or not each item was funded in each budget year. You could then use this labelled data to train a classification algorithm such as a logistic regression or a decision tree. Once you have trained your model, you can use it to predict which items are likely to be funded in the current budget year.


text:
What does consistency of heuristic intuitively mean in the A* algorithm and why are consistent heuristics monotonic?
Could someone give the intuition behind consistency of heuristic function in the A* algorithm?
From wikipedia:

Every node i will give an estimate that, after accounting for the cost
to reach i + 1, is always lesser than the estimate at node i + 1.

I understand what this means, but is there any intuitive meaning behind it? And how does this lead to monotonicity of the evaluation function = f(n) = g(n) + h(n)?

Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.

@Community modified, is it better now?

A consistent heuristic must not overestimate the true cost to get to the goal from a particular node - the heuristic estimate is always less than or equal to the cost to get to another node plus the estimated cost from that node to the goal (which itself cannot be an overestimate).
Basically, a consistent heuristic will not spuriously discard truly good options because it overestimates their cost and treats them as bad options. An inconsistent heuristic might overestimate the cost of some path, and instead explore another, costlier path and miss the optimal solution. A consistent heuristic allows paths to turn out longer than expected (since you can explore them and then go elsewhere if needed), but it does not allow paths to turn out shorter than expected (since you may miss them entirely if you get to the goal first).
With a consistent heuristic, the estimated final cost of of a partial solution (f(n) = g(n) + h(n)) is monotonically non-decreasing along the best path. The consistent heuristic can't overestimate costs, so true the cost must always be as large or larger than the estimated cost. If you move along the best path closer to the goal, the estimated final cost must not decrease - if you get closer to the goal and find your estimated final cost has decreased, it must mean that you overestimated the remaining cost somewhere along the way, which means the heuristic is not consistent.

Imagine three states: A, B, and G, where the start state is A and the goal state is G.
Suppose you have the heuristic $h(n)$ that maps state $n$ to an estimated numerical cost of going from state n to the goal state. So you input $n$ into the function and it returns an estimated cost $h(n)$.
Now, for this heuristic function to be deemed "consistent" or "monotonic", it needs to meet one criterion that you may consider logical and geometrical if you will. And that is, in simplified terms:

The heuristic "distance" from A to G "directly" must be shorter than or equal to the sum of: a step to a third state B and the heuristic distance from that state B to the goal state G.

Only then, a heuristic function is consistent in the estimates it's providing. It needs to adhere to the triangle inequality.
Formally, the inequality defines the relationship between $h(n)$, $h(n')$, and $c(n, n')$, or three sides of a triangle if you draw vertices as states or nodes and label the edges as appropriate. So you end up with $h(n) \leq c(n, n') + h(n')$ where $c(n, n')$ is the cost of going from state n to state n'.


text:
Matching sentences (/bullet points) in two sets using NLP
I am working on a problem where I have two texts T1, T2. T1 contains some important points that I have entered. How can I make sure that T2 has those points? I am aware of the algorithms like cosine, jaccard, BERT for semantic similarity but the problem is that they apply to the whole text whereas I want point-wise similarity i.e. T2 must contain the T1 points although the order and words used may differ a bit.
By points I meant bullet points covering discrete concepts and I basically want to check how many discrete concepts in my T1's points are covered in T2 where they could be in a single sentence or spread out across multiple sentences.
Example:
So T1 could have the following two points:

The Queen reigned from 1943 to 2022.
The Queen was the second
longest reigning monarch.

Now T2 could either be:
The Queen was the second longest reigning monarch with her reign spanning 1952 to 2022.
or

The Queen reigned from 1952 to 2022.
She was Britain's second longest monarch.

In both these cases, T2 should be considered to contain both points in T1.

what do you mean by points

@HadarSharvit edited to add an example.

If the structure of both $T_1$ and $T_2$ is bullet points, I presume that the similarity between every pair of bullet points is a nice baseline. I'd go through various encoders and see how they compare.
Otherwise, if $T_2$ is a paragraph, maybe split it to sentences and treat those as bullet points.
Other option (which might be harder) is to fine-tune a language model with the following input and label:
suppose the bullet points are $\{bp_1^1,...,bp_1^n\}$ for $T_1$ and $\{bp_2^1,...,bp_2^m\}$ for $T_2$, we can formulate a prompt as

Input: text1=$bp_1^i$, text2=$bp_2^j$
Output: True iff text2 in text1

Where the targets were tagged by you apriori

How to split into sentences when the same point could be spread across two or more sentences in T2? Also how to order the points in T2 after the split?

And what do you mean by "language model"?

Lastly, this won't be contributing anything new to preexisting research right?


text:
AIMA, Mutation in Genetic Algorithm

With regards to the highlighted line, the authors earlier stated that:

The mutation rate, which determines how often offspring have random mutations to
their representation. Once an offspring has been generated, every bit in its composition
is flipped with probability equal to the mutation rate.

Doesn't this contradict the highlighted line in the code? Because according to this quote we mutate the children with the small probability called mutation rate every time they are generated so the actual code in my opinion should be:
for each bit in child string
    if (small random probability >= mutation rate) mutate bit

instead of:
 if (small random probability) mutate(child)

Please don't upload code as images, use > . Images are not searchable for the text that they contain. Thanks.

GAs are really a framework around a bunch of closely related ideas that can be tweaked in slightly different ways, and there's no good way to know what the authors intended aside from asking them or maybe finding an errata listing somewhere.
That said, I think your assumption is correct. I think the pseudocode is probably not saying exactly what they intended it to say, and the textual description is the correct one.
Generally, we'd implement this as
child <- REPRODUCE(parent1, parent2)
for each bit in child
    if random() < mutation rate
        flip bit

as you suspected.

Agree. Also, a good place to check what is "standard practice" is the microbial GA, which agrees with this too.


text:
Why does my activation function cause NaNs?
I implemented the following activation function:
$$\sigma(x) = x^{1/3},$$
which returns NaNs after some epochs. I think this is due to the derivative exploding close to $0$. To fix this issue, I implemented a modified activation function:
$$
\sigma'(x) =
\begin{cases}
 x, & \text{if } -1 \leq x \leq 1, \\
 x^{1/3}, & \text{otherwise.}
\end{cases}
$$
Unfortunately, $\sigma'$ suffers from the same issue. Why is that?
Here is my code:
def odd_pow(input, exponent):
    return input.sign() * input.abs().pow(exponent)

def custom_activation(x):
    # This will create a mask where values are between -1 and 1
    mask = (x >= -1) & (x <= 1)
    # For values between -1 and 1, apply x
    # For other values, apply x^1/3
    # Use where to combine results based on the mask
    return torch.where(mask, odd_pow(x, 1), odd_pow(x, 1/3))

Note: Both ReLU and Tanh don't return NaNs, but I am looking for homogeneous activation functions $\sigma(ax) = g(a)\sigma(x)$.

Does the derivative cause the NaNs, or the normal activation function?

@RobinvanHoorn I think the derivative is what is causing the NaNs.

You need to use a small constant to prevent exploding gradients. Please, see my answer.

Please look at the gradients being backpropagated to see where the issue comes from.

Update: the issue is that your odd_pow() is numerically unstable, and so to fix the exploding gradients, which cause a NaN loss, you simply need to add a small constant when computing the power:
def odd_pow(input, exponent, eps=1e-4):
    return input.sign() * (input.abs() + eps).pow(exponent)

this ensures that the pow is not computed at zero, avoiding NaNs.

Note: for reference I also keep my old answer, since also the activation $\sigma(x)$ suffers the same problem.
The issue is that the function $\sigma(x) = x^{1/3}$ is zero for $x\le 0$, and so its derivative becomes NaN for negative $x$ and infinity for $x=0$ as shown here:

where the blue curve is the derivative of $\sigma(x)$, which is depicted in green.
To avoid either NaN or exploding gradients, you need to add a small value (usually called an epsilon) to make the computation of the derivative more numerically stable. The following should work:
def custom_activation(x, eps=1e-3):
    # set negative values to zero
    tmp = torch.nn.functional.relu(x)
    # add the small epsilon `eps`
    return torch.pow(tmp + eps, exponent=1 / 3.0)

Notice that as you lower the eps, e.g. at 1e-5, the derivative would be larger. If you really need a small epsilon, then you should also consider to clip the individual gradients (there should be a clip_grad functionality related to the optimizer) to ensure stable training.

The function $\sigma(x) = x^{1/3}$ is not zero for $x \leq 0$—I am taking the real root.

The zero at $x=0$ is not the issue. It's the derivative is $\frac{1}{3}x^{-\frac{2}{3}}$ which is undefined at zero. With their intended $\sigma'$ function, which should simply have derivative of $1$ at $x=0$, the OP looks to have an implementation problem here, not a theoretical one.

@NeilSlater It could be that torch isn't taking the real roots for the negative values of $x^{-2/3}$.

@user572780: I would have thought that the .abs() would have stopped that issue. I cannot see clearly what is wrong, although I suspect one or more sub-components of your custom_activation function is confusing the auto-differentiation.

@NeilSlater I implemented and tested the code I provide here in Tensorflow, on a simple MNIST classification problem and it trains without any issue

In TensorFlow? Do you mean PyTorch? Either way, it is not the OP's activation function, and there's nothing wrong with the OP's function $\sigma'(x)$ in theory AFAICS.

There's also nothing wrong with the activation function presented in this answer. However, the reason given for the OP's problem in the first sentence is incorrect, and the suggestion to use a different function is also not an answer to the original question.

@NeilSlater Sorry but I made a bit of confusion here :) As you said, both $\sigma$ and $\sigma'$ are fine in theory. The issue, in practice, is numerical instability around the zero (I think). I've updated my answer and the issue should be fixed now

I think you need to take a careful look at OP's definition of $\sigma'(x)$ The function is linear near the origin. You seem to be talking about the OP's first function -  at least a lot of your comments are correct when applied to that

@NeilSlater Yes, I was referring to the first function, $\sigma(x)$ because as far as I understood $\sigma'$ was a replacement for that, since not working as intended. So I aimed at just fixing $\sigma$


text:
Which models can be applied recursively?
I come from a math background, so I am not up-to-date with machine learning literature.
For the purpose of learning dynamics, I would like to train a model to minimize the following loss:
$$\mathcal{L} = |\mathcal{M}(\mathcal{M}(x)) - y|,$$
more generally, there can be $k \geq 2$ recursive applications of the model $\mathcal{M}$.
From my experimentation, regular fully-connected neural networks perform poorly—a single application of $\mathcal{M}$ performs better and intermediate results do not correspond to intermediate data.
Are there other architectures that are better suited for this problem? Maybe graph-based models or the U-net from DDPMs?
Edit: Just a note on RNNs (I could be completely wrong): Although RNNs are used for recursive applications, I don't think they are what I am looking for. RNNs accept a sequence of inputs, whereas my input is a single tensor. Also, going from one hidden state to the next corresponds to a matrix multiplication which seems not very expressive(?).

In StyleGAN there is two steps mapping and synthesis. Since the mapping step has same amount of inputs as outputs, I once had an idea to run it twice using its own output as input. And let's just say the results can be interesting sometimes if you do something you do not have any idea about the outcome.

You mentioned dynamics, so I assume you have some kind of vectorised state $S$ and the model should output $S'$ in the same space. A transformer will do this.
A special case of UNet which outputs the same size tensor as its input would also work, if your input is image-like (amenable to convolution).
An RNN would also work, but its recursive architecture wouldn't be the reason.
A neural network is essentially just a function, and they have very flexible domains and codomains.
If you want $M(M(x))$ to be well defined, you just need $M$ to map $X \rightarrow X$ which basically any neural network can do. The best architecture mostly depends on $X$.

In my experiments (Lorenz system), a fully-connected NN completely fails. What stops the model from mapping inputs in the $X$ domain to the $Y$ domain and inputs in the $Y$ domain to the $X$ domain?

@user572780 - depends on how you are using "domain", I think the answer here is focused on dimensionality only. And you cannot write a neural network that converts between different dimensionalities by itself automatically e.g. if it receives $\mathbb{R}^n$ it should output in  $\mathbb{R}^m$ and vice-versa  . . . although you could add supporting code for this and use the same neural network to attempt both tasks with redundancy in unused inputs or outputs depending which step was being processed.

@NeilSlater By "domain" I mean an open connected set—my question was not to do with dimensionality.

@user572780 Then you may have a problem in a single model if you need specific mappings and X/Y domains overlap. But if you don't care how X/Y mapping works, or if the domains do not overlap, then a single NN model should be fine. Or, as a special case, if where X and Y domains overlap, then the mapping is always the exact same vector in X and Y.

@NeilSlater I think there was a misunderstanding. Essentially, I am applying a NN twice so that the intermediate results follow the dynamics of the problem. It seems that a fully-connected NN cannot do this.

@user572780 Fully connected networks are known to be universal approximators. We just use different architectures to make computation feasible. How was your fully connected network implemented?

Also, suppose f has domain X but f's codomain Y is not a subset of X. Then there is some x in X such that f(x) is not in X, which means f(f(x)) is not defined. So even if your network maps X to Y and Y to X, outputs of f(x) still need to be in X. Hence mapping X to X. But that is not really the point. It was rather that any network with the same input and output space can work in theory, and a huge number of networks satisfy this.
Besides, your issue is probably that your loss says nothing about M(x) matching intermediary state. You can't expect NN to do something you didn't train it to do.

@VennaBanana It is just a regular fully-connected neural network—not sure what you mean by how it was implemented. The loss indeed says nothing about M(x), but it does say something about M(M(x)) and it performs poorly in that scenario. I double-checked this by implementing a single network (L = |M(x) - y|) with repeated fully-connected layers: fc1 -> fc2 -> fc1 -> fc2.

@user572780 Yes a fully connected nn can do that. It can be used in RL to predict state progression, is used in image generators that progressively remove noise (e.g. diffusion models) and there are physics based predictions such as turbulence models. All of these however will have same domain input and output, perhaps with additional conditioning input

@NeilSlater Do they use fully connected NNs in RL/image generation?

@user572780 In RL, yes, typically, although it will depend on the state vector type, and it is not a common/standard requirement to predict next state. In image generation, it is typically CNNs, but there's no meaningful difference between CNNs and FCNs in terms of how they behave in your $M(M(x))$ composition - what you care about is having an output structure and values that are also suitable as an input, so typically the same domain $M(x): \mathcal{X} \rightarrow \mathcal{X}$

@user572780 As per this answer, the internal architecture choice once input/output are set is not about the composition problem that you are focusing on in the question. It depends on the nature of $x$ and the problem space you are trying to solve. Your need to be able to feed back the output as a new input is not unusual. But perhaps your problem setup is unusual in other ways?

@NeilSlater Of course in my experiments, the input is the same shape as the output.


text:
$\gamma^t$ in REINFORCE update (Sutton-Barto RL book Exercise 13.2)
I've struggled with solving exercise 13.2 from Reinforcement Learning: An Introduction Second Edition :

Generalize the box on page 199, the policy gradient theorem (13.5),
the proof of the policy gradient theorem (page 325), and the steps
leading to the REINFORCE update equation (13.8), so that (13.8) ends
up with a factor of $\gamma^t$ and thus aligns with the general algorithm given
in the pseudocode.

Below is my attempt. Please let me know if this is correct or where I've made a mistake (exercise 13.2 was was not in the authors' official solutions when I checked).
Generalize box on 199
As explained in the box on 199, include a factor of $\gamma$  in the second term of (9.2):
$$
\eta_\gamma(s) = h(s) + \gamma \sum_{\overline{s}} \eta(\overline{s}) \sum_a \pi(a|\overline{s}) p(s|\overline{s},a)
$$
$$
\mu_\gamma(s) = \frac{\eta_\gamma(s)}{\sum_{s^\prime} \eta(s^\prime)}
$$
I initially thought the definition of $\mu_\gamma(s)$ was
$$
\mu_\gamma(s) = \frac{\eta_\gamma(s)}{\sum_{s^\prime} \eta_\gamma(s^\prime)}
$$
but I could not see how to finish the exercise with this definition. Given that we are assuming a single starting state $s_0$, we can also write $\eta_\gamma(s)$ as:
$$
\eta_\gamma(s) = \sum_{k=0}^\infty \gamma^k Pr(s_0 \rightarrow s, k, \pi)
$$
Generalize Proof of the Policy Gradient Theorem (episodic case)
I followed the steps in the proof on page 325 and added discounting.
$$
\begin{aligned}
\nabla v_\pi(s) &= \nabla \bigg[\sum_a \pi(a|s) q_\pi(s,a)\bigg] \text{,} \quad \text{for all} \; s \in \mathcal{S} \\
&= \sum_a \bigg[\nabla \pi(a|s) q_\pi(s,a) + \pi(a|s) \nabla q_\pi(s,a)\bigg] \\
&= \sum_a \bigg[\nabla \pi(a|s) q_\pi(s,a) + \pi(a|s) \nabla \sum_{s^\prime,r} p(s^\prime,r|s,a) (r + \gamma v_\pi(s^\prime))\bigg] \\
&= \sum_a \bigg[\nabla \pi(a|s) q_\pi(s,a) + \gamma \pi(a|s) \sum_{s^\prime} p(s^\prime|s,a) \nabla v_\pi(s^\prime)\bigg] \\
&= \sum_a \bigg[\nabla \pi(a|s) q_\pi(s,a) + \gamma \pi(a|s) \sum_{s^\prime} p(s^\prime|s,a) \sum_{a^\prime} \big[\nabla \pi(a^\prime|s^\prime) q_\pi(s^\prime,a^\prime) + \gamma \pi(a^\prime|s^\prime) \sum_{s^{\prime\prime}} p(s^{\prime\prime}|s^\prime,a^\prime) \nabla v_\pi(s^{\prime\prime})\big]\bigg] \\
&= \sum_{x \in \mathcal{S}} \sum_{k=0}^\infty \gamma^k Pr(s \rightarrow x, k, \pi) \sum_a \nabla \pi(a|x) q_\pi(x,a)
\end{aligned}
$$
$$
\begin{aligned}
\nabla J(\theta) &= \nabla v_\pi(s_0) \\
&= \sum_s \bigg(\sum_{k=0}^\infty \gamma^k Pr(s_0 \rightarrow s, k, \pi)\bigg) \sum_a \nabla \pi(a|s) q_\pi(s,a) \\
&= \sum_s \eta_\gamma(s) \sum_a \nabla \pi(a|s) q_\pi(s,a) \\
&= \sum_{s^\prime} \eta(s^\prime) \sum_s \frac{\eta_\gamma(s)}{\sum_{s^\prime} \eta(s^\prime)} \sum_a \nabla \pi(a|s) q_\pi(s,a) \\
&= \sum_{s^\prime} \eta_\gamma(s^\prime) \sum_s \mu_\gamma(s) \sum_a \nabla \pi(a|s) q_\pi(s,a) \\
&\propto \sum_s \mu_\gamma(s) \sum_a \nabla \pi(a|s) q_\pi(s,a)
\end{aligned}
$$
Steps leading to the REINFORCE update equation (13.8)
$$
\begin{aligned}
\nabla J(\boldsymbol{\theta}) &\propto \sum_s \mu_\gamma(s) \sum_a q_\pi(s,a,\boldsymbol{\theta}) \nabla \pi(a|s) \\
&= \mathbb{E}_\pi\bigg[\gamma^t \sum_a \pi(a|S_t,\boldsymbol{\theta}) q_\pi(S_t,a) \frac{\nabla \pi(a|S_t,\boldsymbol{\theta})}{\pi(a|S_t,\boldsymbol{\theta})}\bigg] \\
&= \mathbb{E}_\pi\bigg[\gamma^t q_\pi(S_t,A_t) \frac{\nabla \pi(A_t|S_t,\boldsymbol{\theta})}{\pi(A_t|S_t,\boldsymbol{\theta})}\bigg] \\
&= \mathbb{E}_\pi\bigg[\gamma^t G_t \frac{\nabla \pi(A_t|S_t,\boldsymbol{\theta})}{\pi(A_t|S_t,\boldsymbol{\theta})}\bigg]
\end{aligned}
$$
Discounted REINFORCE update:
$$
\begin{aligned}
\boldsymbol{\theta}_{t+1} &\overset{.}{=} \boldsymbol{\theta}_t + \alpha \gamma^t G_t \frac{\nabla \pi(A_t|S_t,\boldsymbol{\theta})}{\pi(A_t|S_t,\boldsymbol{\theta})} \\
&= \boldsymbol{\theta}_t + \alpha \gamma^t G_t \nabla \ln\pi(A_t|S_t,\boldsymbol{\theta})
\end{aligned}
$$

I dont understand why the $\gamma^t$ appears when you write the gradient with an expectation. Could you elaborate ? thank you
I agree with you with all the things up to that point
EDIT : to try to answer my own question, I would say that it is important to clarify what the expectation is based on :
\begin{align}
\sum\limits_s \mu_\pi(s) \sum\limits_a q_\pi(s,a) \nabla_\theta \pi(a|s) &= \mathbb{E}_{s \sim \mu_\pi} \big[ \sum\limits_a q_\pi(s,a) \nabla_\theta \log \pi(a|s)\big]\\
&= \mathbb{E_\pi} \big[\gamma^t \sum\limits_a q_\pi(S_t,a) \nabla_\theta \log \pi(a|S_t) \big]
\end{align}
in the first, we use the on-policy undiscounted state distribution, which is not exactly the same as the discounted state distribution, as my understanding of it.
See : https://proceedings.mlr.press/v202/che23a/che23a.pdf

With $\mathbb{E}\pi$ I meant the on-policy undiscounted expectation. Since $\mu\gamma$ is the discounted distribution, I believe we are left with a factor of $\gamma^t$ when rewriting as $\mathbb{E}_\pi$. However, this part is what I was (and still is) the most unsure about.

So yes I agree with what you've done, but like you I'm not 100% sure. As pointed out in the paper linked and in https://towardsdatascience.com/why-there-is-a-problem-with-the-policy-gradient-theorem-in-deep-reinforcement-learning-958d845218f1, I think it's something a lot of people overlook

Went back to this question a year after, and after carefully reading your derivation, I don't understand why you can't define :
$$\mu_{\gamma}(s) = \frac{\eta_\gamma(s)}{\sum_{s'} \eta_\gamma(s')}$$
Here is the start of your derivation :
\begin{align}
\nabla v_\pi(s_0) &= \sum_s \eta_\gamma(s) \sum_a \nabla \pi(a|s) q_\pi(s,a) \\
&= \sum_{s'} \eta(s') \sum_s \frac{\eta_\gamma(s)}{\sum_{s'}\eta(s')} \sum_a \nabla \pi(a|s) q_\pi(s,a) \\
&= \sum_{s'} \eta_\gamma(s') \sum_s \mu_\gamma(s) \sum_a \nabla \pi(a|s) q_\pi(s,a)
\end{align}
going from line 1 to line 2 is multiplying by $\frac{\sum_{s'} \eta(s')}{\sum_{s'} \eta(s')}$
going from line 2 to line 3 is using your definition of $\mu_\gamma(s)$ but I don't see why you also changed the $\eta(s')$ into $\eta_\gamma(s')$ ?
If you use the $\mu_{\gamma}(s) = \frac{\eta_\gamma(s)}{\sum_{s'} \eta_\gamma(s')}$ definition in the first place, you just straight away multiply by $\frac{\sum_{s'} \eta_\gamma(s')}{\sum_{s'} \eta_\gamma(s')}$ and I don't see any problem to finish the proof.

We just need to exchange the order of summation and then sample episodes in both space and time to perform gradient descent. My response is a bit late, but I hope it can still be of help to you.


text:
What is the difference between face identification, face verification, and face recognition?
I am confused. On some websites, face identification is just face detection, while face verification is finding the person's identification. They are both considered components of face recognition. But in some websites face verification is 1-to-1 matching of faces while face identification is 1-to-k face matching

Face identification is the process of determining whether a face in a given image belongs to a specific person.
Face verification is the process of verifying that a given face corresponds to a specific person.
Face recognition is the process of identifying a person from a given image.
One key difference between face identification and face recognition is that face identification is typically used to determine whether someone is who they claim to be, while face recognition is used to identify people regardless of who they are. Another difference is that face identification is often used as a security measure, while face recognition is more often used for things like tagging photos.
Another difference between face identification and face recognition is that face identification systems often require a person to be looking directly at the camera, while face recognition systems can often work with pictures of people taken from different angles.

I understand difference between face identification and face recognition but can you explain the difference between face identification and face verification?

There are two things you can do:

Find a face (not a specific face) in a frame (I think this is 1-k)
Recognition Verification a specific face in a frame (I think this is 1-1)

I want to know difference between face identification and face recognition.

identification: There is a face in this picture
verification: It's the face of person P.
recognation: identification + verification

So, face identification is face detection?

I don't think it well-defined. It depends on the context you've read this term. I think identification can be slightly more than just detection. Identification could mean to detect a specif group of faces (like a specific ethnicity).
I learned it that way that recognition is to detect if a face is in a frame and to distinguish what face exactly is in a frame.


text:
Is there an image classification dataset where the class depends on spatial relations?
My question is pretty much the one asked above. To clarify a bit further: I have only found datasets that do object localization and that also have relations between the objects annotated (like: "Here is the horse, here is the rider. The rider rides the horse". What I am looking for is a dataset where you can do classification based on spatial relations (Like: "I am a positive example, because the rider is above the horse. I am a negative example, because the rider is below the horse (or besides it, etc)").
Any help is appreciated

There is no specific image classification dataset that focuses on spatial relations. However, there are some datasets that include images with spatial relations annotations, such as the Visual Relationship Detection (VRD) dataset which is a collection of images with annotations of spatial relations between objects.
Here is a paper Identifying Spatial Relations in Images using Convolutional Neural Networks that uses pre-trained CNN to learn spatial relations between objects in an image, it makes use of SUN09 and a simplified synthetic dataset to trained the network. the network pays attention to a certain parts of images
while classifying them for spatial relations.

Thank you so much for your answer! I already had a look into the VRD dataset and I think it is suitable for learning relations. But my problem is a bit different in that I do not need to learn the spatial relations (my approach looks at important image parts and finds spatial relations just by the coordinates. Do you think it would be possible to get a subset from such datasets where the ground truth of an image depends on the relations? Thank you also for the linked paper as it gives me further ideas for my research.

Yes, I think it would be possible to get a subset from such datasets where the ground truth of an image depends on the relations. However, it is not clear how useful this would be, since the goal of the datasets is to learn generalizable models from data, and not to create models that only work on a specific subset of data.

Second, the ground truth of an image may not be entirely determined by the relations between objects in the scene, but may also be influenced by other factors such as lighting or the position of the camera.

Thanks for pointing that out. I think I need to explain my specific case a bit further: I am researching on Explainable AI that goes beyond visual explanations like LIME, GradCAM etc. and build explanations based on first order logic (like: "the image belongs to this class since the important objects A, B, C are in a particular spatial relation to each other"). I am aware that this topic might be very new and so there is not a lot of datasets that are usable for this particular purpose. At least no sets that go beyond toy datasets.


text:
100 layer neural network with 100 hidden units vs. 1 layer neural network with 100 hidden units
Suppose we have a neural network with 100 hidden layers. Each hidden layer has one hidden node, and the hidden nodes employ a universal basis function (e.g. tanh). Now we want to compare this network's approximation capacity to another network with one hidden layer with 100 hidden nodes (all using tanh). Can both of these networks approximate the same set of functions?
I have read from a few different sources that a neural network has to have "enough" hidden units, use a universal basis function, and have at least a single hidden layer to approximate any reasonable function to any degree of accuracy. However, I'm not sure what "enough" means. Intuitively, the network with one hidden layer that has 100 hidden units should satisfy this condition but I can't come up with any reasoning to back this up.

The answer to this is so broad as to be unsatisfying.  Those with sufficient experience training neural networks know that there isn’t a single architecture that solves a problem and there are some that seemingly never solve a problem.
Further a layer which is wider that the previous transforms the output of the previous to a higher dimension.  The alternative is true of smaller layers.  If the activation function draws a linear boundary and the data is not linearly separable in its native dimension transforming to a lower dimension (100 layers of 1 neuron) won’t likely be useful.  Whereas at a higher dimension it may be linearly separable (1 layer of 100).
Finally, ANN are universal approximators.  One can do MNIST classification with dense layers but it becomes a lot easier and more accurate with convolution.  Although a dense network can approximate the behavior of CNN there is enough stochasticness in training it isn’t likely to do nearly as good.  Neural networks can often do much better if we make assumptions about the problem space.  Which may mean a variety of dimensional transformations in a dense network.


text:
Detecting individual multiple documents in a pdf
I need to solve a problem whereas a scan of multiple documents (contracts, invoices, bank extracts) is stored into a PDF and I need to identify how many individual documents are contained in the PDF and which pages of the PDF belong to which document.
This scenario presents itself, for example, when a person feeds a bunch of documents into an automatic scanner that then creates a single PDF from these documents. Each document is just an image and might have one or more pages and may have different layouts.
What would be an intelligent AI approach to attacking this problem?

This is an interesting question.
Let's make the problem more abstract:
Suppose we have n docs(each page in the PDF is one doc). How can we classify these docs?
My approach:
This could be treated as an NLP task; my approach is. First, you have to extract texts from each doc, then compute the similarity between each doc, if the similarity is great than the threshold you give, they should be considered as same docs.
Useful link:
Compute similarity: https://towardsdatascience.com/calculating-document-similarities-using-bert-and-other-models-b2c1a29c9630
OCR: https://nanonets.com/blog/attention-ocr-for-text-recogntion/


text:
How does accuracy (for a single class of interest) of a segmentation network vary with increasing number of classes?
BACKGROUND:  I have a real world problem of developing a U-net-like model for segmenting lung tumors in lung CT images.
On the one hand, I can make this a two class problem:
 1. lung tumor
 2. not lung tumor

On the other hand, I can make this a multiclass problem (for example):
 1. lung tumor
 2. lung
 3. trachea
 4. heart
 5. blood vessels
 6. etc.

Ultimately, I'm only interested in getting the lung tumor right.  Thus, the "class of interest" (mentioned in the title) is lung tumor.  All other classes are not necessary for my analysis, BUT my intuition is telling me that including more classes may improve the model's ability to segment lung tumors.  My rationale is that not lung tumor is very heterogeneous category that can be difficult for a model to classify a pixel into, but giving the model a set of more specific categories for commonly occurring structures may be helpful to model learning.  I am aware that the multiclass approach results in more manual annotations (i.e., time, fatigue, and costs), but that is not the focus of this question.
SPECIFIC QUESTION:  Which approach -- the two class or multiclass -- will yield higher accuracy for lung tumor segmentation on CT images?
REQUIREMENTS:  I seek a response that sheds deep insight into this problem.  Responses founded on established mathematical principles are acceptable.  Responses founded on empirical evidence from credible literature references are acceptable.  The evidence need not be specific to lung tumors, but it should be directly relevant to segmentation tasks.
PRIOR RESEARCH:  I have scoured the literature for a head-to-head comparison of these two approaches, but have not found anything useful for lung tumor segmentation.
RELEVANCE TO THE AI COMMUNITY:  While my problem is related to lung tumor segmentation, I believe the problem type is of general interest to the broader AI community.  For example, one may be interested in segmenting peoples' eyes in facial images, and one can ask whether they should have two classes (eyes vs. not eyes) or multiple classes (eyes vs. nose vs lips vs. chin vs....).  Prior knowledge about which method is better would help people plan their annotation approach before embarking on a long and expensive annotation journey.

Does anyone have suggestions on how to improve this post in order to get answers?

On a pure theoretical basis there's no difference between a 2-class problem or a multi-class problem via more perceptrons in the first layer to correctly partition the multi-feature space as a preparation to fully match target outputs. But if some patterns from the problematic heterogeneous lung-tumor class are mostly different and identifiable from your other multi-classes patterns, then intuitively your NN's probability to misclassify one lung-tumor sample would be smaller (more accurate).

@mohottnad, I am unclear why the first layer needs more perceptrons.  Isn't the first layer the input layer, and so shouldn't the number of perceptrons not change whether it is binary or multi-class?

Also, I did not say that the lung tumor class is heterogeneous.  Rather, I said that the not lung tumor class would be heterogeneous because it would include everything else (e.g. normal lung, trachea, heart, blood vessels, etc.).  Thus the class would be difficult to learn.  In a multiclass setting, that "heterogenous" class would be broken down.  My guess is that giving these smaller more "homogeneous" classes would be easier to "learn".

I meant first hidden layer in a MLP above to have more perceptrons to try to correctly partition the features space for multi-class. You can search “linear machines” for multiclassifier. Of course you can also try CNNs. If you interested lung tumor class is reliable to differentiate from non lung tumor classes, then intuitively why bother with multiclassifier?

Maybe not a satisfactory answer but I hope this will give you at least a slightly new perspective on how to think about binary vs multiclass problems in a machine learning way.
Let's start from the math: Binary classification and multiclass classification are basically the same, i.e. every loss formulated for binary classification is a special case of the same loss generalized to n classes. If you see explicit classes for both in every main machine learning frameworks is just due to specific different implementations for boosting a bit performances. But if we look at the formula of the main loss used for classification, i.e. cross entropy, you'll see that there is no difference at all between 2 and n classes

Cros entropy: $$-\sum_{c=1}^{M}y_{o,c}log(p_{o,c})$$
Binary cross entropy (the sum between the 2 classes is just expanded): $$−(ylog(p)+(1−y)log(1−p))$$

Moving on to your rationale: Not lung tumor is an heterogeneous class, hence splitting it into specific classes might help the model learning better features for each one of them. I don't agree a lot with this rationale for a simple reason: first, if it's possible to lean good features for subclasses of a macro category then mapping all those classes back to the main class should be trivial to learn for any deep learning architecture. The opposite instead does not hold. This is why despite the same math behind, tasks with less classes are usually easier to tackle. The only AI area I can think about that leverage this kind of rationale is multi task learning, in which similar tasks like classification and object segmentation/detection are combined to leverage more ground truth data. But in this case the advantage is the combination of different losses that help learning hierarchical features.
So what to do: I personally feel that the right angle to look at the problem is investigating the amount of data at your disposal and their labels distribution. If your fine grained labels generate a sort of balanced distribution then I would go for multiclass, if not I would go for binary classification, even if the binary labels distribution is highly skewed, since for this scenario Focal loss (a weighted variation of cross entropy) works just fine.

thank you for taking the time to provide a response.  The fact that the binary and multiclass cross entropy loss functions take on similar forms is well-appreciated.  However, just because the functions are of similar form does not necessarily mean that they are going to be equal/similar.

Second, the problem of mapping fine labels (trachea, lung, blood vessels) to coarse labels (not lung tumor) is--I agree with you--easy.  But this isn't a mapping problem.  This is a learning problem.  Coarse labels in a hierarchy may be easier to learn when the fine labels share common features.  This is levered by various hierarchical approaches.  However, this approach breaks down when the fine labels do not share common features, as we have here.

about the second point I was indeed talking about learning in the answer. I don't agree with your last statement, hierarchical learning is not about shared features, actually learning differentiated features (good clustered embedding vectors) is the main goal in multi task learning for high level tasks. If I want to better detect faces I need to be able to detect different face elements first, like eyes or ears, which have nothing to do with each other. Once the model learn that, that the different feature layers will just learn to fire to the same output nodes related to the face class.

point is that you can also learn directly detect faces without clustered features, i.e. with features layers that detect a bit of everything instead of being specialized. So again, unless your dataset is perfectly balanced I don't see why you should make your life harder going down the multiclass rode.

********Two-Class Approach (Lung Tumor vs. Not Lung Tumor):
Simpler to Implement: Here, the model just needs to decide if a part of the image is a lung tumor or not. This approach is straightforward and focuses directly on what you care about.
Mixed Background Issue: The problem is that the "Not Lung Tumor" category includes a lot of different things, like lungs, heart, blood vessels, etc. This makes it harder for the model because it's dealing with a big, mixed bag of other stuff.
Multiclass Approach (Lung Tumor, Lung, Trachea, etc.):
More Specific Learning: By teaching the model to recognize different parts of the image (like the lung, heart, trachea), you're giving it more specific categories to work with. This could help the model better understand what a lung tumor is by knowing what everything else is too.
More Work: However, this method requires more effort because you need to label (or annotate) all these different parts in your training data, which can be time-consuming and expensive.
Which is Better for Lung Tumor Segmentation?
Depends on the Situation: If you have the time and resources to label all the different parts (like the lungs, heart, etc.), the multiclass approach could help the model learn better and potentially improve accuracy in finding lung tumors.
Quick and Focused: If you're short on time or resources, the two-class approach is simpler and might still work well, though it might struggle a bit with the mixed "Not Lung Tumor" category.
In summary, adding more classes can help the model learn better, but it's more work. If your main goal is just to find lung tumors and you're okay with some extra effort, the multiclass approach could be worth it.

Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.


text:
To train a mushroom vs. non-mushroom image classifier, which images should comprise the negative class?
I want to train a model that will identify if the image is mushroom and non-mushroom.
If the image is mushroom, I will use another existing model to categorize if its poisonous or edible.
I want to know what kind of images should I use in training non-mushroom category.

In training a mushroom vs. non-mushroom image classifier, which images should comprise the negative class?
Only you can answer this question.  The answer requires some careful thinking on your part, and it depends on what types of objects that you believe your classifier will encounter once deployed in the "real world".  If you are only going to present "vegetation" to your classifier, then you probably only need to curate a mushroom (positive class) and plant (negative class) dataset.  On the other hand, if there are no limits to what your classifier will encounter, then your training images for the non-mushroom class must include the visible universe (minus the mushrooms, of course).  Ideally, your positive and negative class images should be collected in the same manner, at least how they would be collected when implemented.


text:
Are masks needed for images that don't contain the object of interest in (binary) Image Segmentation tasks?
Total Dataset :- 100 (on case level)
Training :- 76 cases (18000 slices)
Validation :- 19 cases (4000 slices)
Test  :- 5 cases (2000 slices)
I have a dataset that consists of approx. Eighteen thousand images, out of which approx. Fifteen thousand images are of the normal patient and around 3000 images of patients having some diseases. Now, for these 18000 images, I also have their segmentation mask. So, 15000 segmentations masks are empty, and 3000 have patches.
Should I also feed my model (deep learning, i.e., unet with resnet34 backbone) empty masks along with patches (non empty mask)?

You should, absolutely.
Even more, if you have prior knowledge regarding the distribution of the disease (like 1 case every 1000) I would strive to get a training dataset close to that distribution, even if it means collecting and adding more control cases with empty masks.
At the end you'll probably end up with a really imbalanced dataset, the way to tackle this issue is using the focal loss.

Thank you for your response. But I tried this approach earlier and used the focal loss, but the result was unsatisfactory. I have used model  unet-resnet34. The tumor patch is tiny, and you can say it is a small dot on 512X512 images, which is hard to segment. Therefore, I'm using only mask pictures. And at this time stamp, I'm getting an IOU of 74%. Once the training is complete, I'll freeze all the layers except the last one and feed mixed images which are empty masks and non-empty masks. What do you say abo this approach?

I share with you my experience with TensorFlow 2.0 (TF2.0).  I don't know for other deep learning packages.  In TF2.0, you must pass x_train and y_train together during training, where x_train is the image and y_train is the mask.  (Similar holds for x_test and y_test, which must be passed together during evaluation.)  This means that you must have a mask for both images with tumor and images without tumor.  In your case, y_train would be a binary mask, since you have two pixel classes (tumor and no tumor).  For images with no tumor, the mask would be all 0's (what you call an "empty mask").  For images with tumor, the mask would be 0's everywhere but 1's where there is tumor (what you call the "patch").  Note that the masks are essentially images (or numpy arrays, if you prefer) of the same size as your medical images, except that the mask has binary pixel values according to tumor present (1's) or absent (0's).
Thus, the answer to your question, "Should I also feed my model empty masks along with patches (non empty mask)?", is Yes.  In fact, in TF2.0, you absolutely must. (A separate post can be started if you have questions about model performance.)


text:
Why does GridSearchCV model give worse results despite same parameters used with base model
I am trying to make prediction using random forest regression and then utilize GridSearchCV to tune hyperparameters(just 'n_estimators').  However results of GridSearchCV are worse than base model. Despite GridSearchCV parameter grid has same values with base model the results of GridSearchCV model is still worse. So, why is the RMSE of the GridSearchCV model worse than base model?
Here are models that I used:
Base Model:
rfr_rd_fi = RandomForestRegressor(n_estimators = 100,
                                  max_features=7,
                                  random_state = 3115)

Root Mean Squared Error for base model is 200.2
GridSearchCV model:
param_grid = {'n_estimators' : np.arange(10,1010,10, dtype=int),
              'max_features' : [7],
              'random_state' : [3115]}

cv_test= KFold(n_splits=10)

rfr_gs = GridSearchCV(RandomForestRegressor(),
                      param_grid=param_grid,
                      scoring='neg_mean_squared_error',
                      cv=cv_test, verbose=4, n_jobs=-1).fit(X_train, y_train)

Root Mean Squared Error for GridSearchCV model is 257.2

There are a number of possible reasons why this is the case. Here are a few things to check.
 1. Was training and testing done on the same dataset for your baseline model?
 2. Did you do cross validation with your baseline model?
 3. If so, did you use the same folds?

Bottom line:  To compare apples to apples, your baseline model and your Grid search should have the same pipelines, except for the hyperparameters, of course.  After you have done that, you can look at your grid search model where n_estimators=100) and you should have identical results as the baseline model.
On a separate note, you should consider nesting your hyperparameter tuning within your crossvalidation (see article).


text:
Are there better loss functions than MSE for maze solver using deep learning?
I am a newbie in reinforcement learning, and I was doing a project on solving an agent maze solver using deep Q Learning.  Currently, I am using the MSE loss function, but the agent is very slow or not even reaching the target.  Is there any better loss function to improve the agent performance?

We would really need to know more about your problem and setup to answer this more specifically. You didn't tell us   e.g. for how the agent is being trained, what the reward function is, etc.

The original arXiv DQN paper used the Mean-Squared Error (MSE) loss function. The loss of a sample experience tuple $(s, a, r, s')$ is as follows:
$$ \left([r + \gamma\max_{a'}Q(s', a'; \theta_{i-1})] - Q(s, a;\theta_i)\right)^2.$$
For completeness sake, the target values (expression in brackets) are generated with the previous weights of the network $\theta_{i-1}$ when determining the current weights $\theta_i$ via gradient descent.
In contrast to using MSE as a loss function, the Nature DQN paper instead clipped the error (expression inside outer parentheses) to be between $-1$ and $1$ (see Methods, Training algorithm for deep Q-networks, last paragraph). The authors state that clipping the error corresponds to using an absolute value loss function outside of the interval $(-1, 1)$ during gradient descent. In practice, this generally produces smaller gradients than those of MSE loss, and the authors state that this form of error clipping improved the stability of the DQN algorithm. Some deep learning libraries implement this loss function, or a similar variant, as the Huber loss function.  Also for completeness sake, the Nature paper replaced parameters $\theta_{i-1}$ with those of a separate target network, denoted as $\theta_i^{-}$, for further stability of the algorithm.
Without more details, it's difficult to determine if the loss function is the culprit of poor performance for your problem, but this answer is at least a starting point regarding better loss functions in practice.

you could also mention sparsity of rewards being an issue if OP is using them

Good point. It's kind of hard to know the extent of OP's issues with the problem. At least my answer provides guidance for the question in the title and body.
Just so I'm on the same page as you @DavidIreland, how would you incorporate sparse rewards as an issue into this answer? Does it relate to the loss functions, or is it a more general issue that I should discuss?

When I read that OP’s problem was a maze I assumed that it would be some kind of sparse reward (0 every step, 1 to reach the goal, for instance). In this case I don’t think it’s a problem with the loss function, so just something you could point out as to why it might not be working, though I think in terms of answering the actual question related to the loss you’ve provided a great answer.


text:
How to handle missing data for an ordinal feature variable for a deep learning model?
BACKGROUND:  I am developing a deep learning model in which one feature variable (out of many) is the grade of cancer, an ordinal variable.  Below is a breakdown of my data by grade:
GRADE       SAMPLES
--------------------
Grade I     746
Grade II    1128
Grade III   2908
Grade IV    1909
Unknown     4255

PROBLEM:  I need a solution to handle a large amount of missing data.
OPTIONS I HAVE CONSIDERED:

Because of the amount of missing data, just removing those study subjects is not an option (can't remove rows -- too many).
Because grade is an important predictor variable, removing grade as a feature is also not an option (can't remove the column -- too important).
I fear that imputation is not going to give me accurate results since so much of my data is missing.  I have considered multivariate imputation methods as well, but I'm still not convinced I can get meaningful results.  Maybe I'm wrong.
I can just treat grade like a nominal variable and make "Unknown" its own category, but then I won't be able to leverage the order information (can't have my cake and eat it too).

QUESTION:  Are there any other mathemagical options for this Goldie Locks?
By the way, I know I can try all of these options and see what works.  That is not what I'm looking for.  I'm really looking to see if there are better options that I've not considered yet.

A way is to build a model to predict the class (or the encoded class value) of the unknown Grade first, then use the predicted grade to build the desired prediction model.

@lpounng, what you describe is essentially multivariate imputation.  The link in the question post leads to an article that describes their version of it (which they call the IterativeImputer), as follows:  "It does so in an iterated round-robin fashion: at each step, a feature column is designated as output y and the other feature columns are treated as inputs X. A regressor is fit on (X, y) for known y. Then, the regressor is used to predict the missing values of y."


text:
Can independent datasets be artificially combined for multimodal learning (semi-synthetic data generation)?
BACKGROUND: To apply multimodal machine learning (ML), the various data modalities typically come from the same example (e.g., chest X-ray (modality 1) and cancer biomarkers (modality 2) come from the same patient (the example)).  The problem is that we often don't have that in public datasets.  Instead, datasets are more commonly independent (e.g., a dataset of chest X-rays from one set of patients and a dataset of cancer biomarkers from a second set of patients).
QUESTION:  Is there any validity to artificially creating "co-registered" datasets from such independent datasets for the ultimate purpose of leveraging multimodal ML (e.g., generate all possible pairs of chest X-rays and cancer biomarkers from different patients with cancer and generate the same for different patients without cancer)?
NOTE:  It is understood that this approach is non-canonical and has flaws, but that is not the question here.  I am more interested in learning whether this could be a second best option for researchers who lack co-registered samples but still want to develop multimodal ML models.  Please provide your response along with some justification as to why this would or would not be valid "second best option".

11/26/2022
New NOTE:  I thought it was implied in the question post that I am aware that the interaction between modalities is not available to be leveraged by multimodal ML since this is really the crux of the problem with fusing independent datasets.  However, the question still stands:  "Would this be a valid second best option?"

A keyword that may be useful to you is the concept of "semi-synthetic" data generation. It seems like that is what you are trying to do :)

One problem with such an approach is if you would know how chest X-ray and cancer biomarkers interact you would likely use that model or knowledge directly instead of using it to generate data. And aligning data among subjects without such knowledge might even give you less information than working with each of the two modalities separately since your model might pick up on some incidental relations. It's like having a regression problem but being unaware of the interaction term.


text:
What type of neural network architecture allows filtering out of unwanted sounds?
I have a use case where I will be inputting audio to a model, and the output of the model will be the same audio except with certain sounds removed (volume set to zero).  The dataset is generated by taking an audio file, duplicating it, and then zeroing out the unwanted sounds (usually a half second long).
I believe a neural network architecture is needed here with the input being the undisturbed audio and its spectrogram.  The output is then the modified/cleaned audio.
What model architectures would work for this use case?  I would potentially like to have this run real-time as a person is speaking.

Since you say that you "believe that a neural network architecture is needed here...", I am assuming that you are open to other options.  This approach doesn't utilize a neural networks, but I think it can potentially get the job done (with some caveats).  One approach is to

First isolate the unwanted sound segments that you want to "subtract"
Calculate the Fourier transform of each unwanted sound segment
Determine the components with the highest amplitudes in each unwanted sound segment
Determine if these dominant peaks have shared frequencies across the various unwanted sound segments
If so, this spectrum of frequencies is the "signature" of your unwanted sound
Once you have that, you can simply subtract those frequencies from your full sound recording to remove the unwanted sound
This is done by finding the Fourier transform of the full sound recording
Subtracting the frequencies (i.e., the signature) of the unwanted sound
Transforming back to the time domain to get the signal minus the unwanted sound

As implied, for this approach to work, the unwanted sounds will have to have a consistent set of dominant frequencies (i.e., a signature).  If you have multiple "types" of unwanted sounds, then this procedure can be repeated for each type (i.e., find the signature for each type).  If you have multiple types of unwanted sounds and you cannot distinguish them, a neural network may then be useful.
You will also have to make sure that those frequencies are not a significant component of your desired signal.  Otherwise, you will be removing an important part of your desired signal.  To do that, just follow the same procedure as described for the unwanted signal.
This code from MATLAB describes one possible implementation (albeit for a denoising application).


text:
How exactly does Keras calculate the validation accuracy?
After each epoch, Keras provides the following evaluations (depending on how the model is compiled):

train_accuracy
train_loss
validation_loss
validation_accuracy

Keras evaluates the performance of the model using the validation set at the end of each epoch.  But how does Keras do this?  Assume that we are performing binary classification and using a binary loss function.  Assume that there are 100 images in the validation set.
QUESTION:  Can the accuracy can be found by simply calculating accuracy for each item and then average them (/100)?
CONTEXT:  I want to implement a custom loss function that calculates the loss towards reducing validation loss (increasing validation accuracy). Therefore, I need to understand how the accuracy is calculated for the validation set.

Keras is a powerful tool that does many things. Some more automated than others. It would be best to read and understand the code, referring to the documentation if necessary (I presume you're using someone else's code?). In any case, we would need more information about what functions are being used before we can tell.

From what I understand, the reason for your question is that are trying to calculate a loss from the validation set. Please note that if you were to do such a thing, that is no longer a validation set, but a second train set. A validation set is specifically used to monitor the performance without learning from that set of data.

After each epoch, the model is applied to the features of the validation set (x_val) to predict the probability of the labels.  Then, the probabilities are thresholded to generate the predicted label (y_pred).  Finally, the accuracy is calculated as follows:
$$ Accuracy\ =\ \frac{Concordant\ samples}{Total\ samples} $$
, where concordant samples are those for which the predicted (y_pred) and true labels (y_val) agree.
Assume we have a binary classification task and 5 validation samples.  If y_pred = [0, 1, 1, 0, 1] and y_val = [0, 1, 1, 1, 0], then there are 3 concordant samples, giving an accuracy of 3/5 = 60%.

Thanks for your reply. But the thing is that I need a more deep explanation as I am writing a custom loss function that requires to optimized toward a value in each epoch. Do you have any idea?

Based on the information that you have provided, this is as deep an explanation as anyone would be able to provide you.


text:
Are there applications of Grenander's pattern theory in pattern recognition or for implementing algorithms?
I came across Grenander's work "Probabilities on Algebraic Structures" recently, and I found that much of Grenander's work focused on what he called "Pattern Theory." He's written many texts on the matter, and, from what I've seen, they seem like an attempt to unify some mathematical underpinnings of pattern representation. However, I'm not sure what this really means in practice, nor how it relates to results we already have in learning theory. The mathematical aspect of the work is really quite intriguing, but I am skeptical as to its practicality.
Are there any applications of Grenander's pattern theory? Either for getting a better theoretical understanding of certain methods of pattern recognition or for directly implementing algorithms?
Some links to what I'm referring to:

Wikipedia entry,
book,
first book of 3 set volume, and
research group, including fields medalist David Mumford.

Grenader's Pattern Theory sees AI from a different perspective from other techniques- one of pattern recognition (as opposed to algorithms, analogies to the human brain, etc...). Like anything that sees an existing problem from a radically different perspective, it's difficult to unpack all the implications.
It has considerable overlap with existing AI algorithms. For example, formulations with Bayes' theorem and information theory. The idea of basing AI on patterns is not unique to this theory. Terms like "signals" are thrown around in AI development (especially computer vision and time series analysis), and the most famous machine learning textbook is called Pattern Recognitin.
That being said, I can think that the way that this formula would impact AI is by giving us a series of tools for machine learning that would allow for handling of problems from a pattern recognition perspective. For example:

ways to decompose noisy images into separate image + noise signals with mathematical formulations for each
better ways of creating embeddings based on discrete signals in input data, and more mathematical operations that could be performed on each
finding a way to compress data by turning an image (essentially a very dense and large graph where each pixel is a node with an edge to adjacent ones) to compressed graphs where the nodes are signals.

There are a lot of different possibilities. One of the joys and hardships of AI and machine learning is that there are often many mathematical formulations for the same idea, and knowing which one to select and why is one of the great challenges of the field.


text:
Distinguishing between handwritten compound fraction and subtraction
I am working in a project named "Handwritten Math Evaluation". So what basically happens in this is that there are 11 classes of (0 - 9) and (+, -) each containing 50 clean handwritten digits in them. Then I trained a CNN model for it with 80 % of data used in training and 20 % using in testing of model which results in an accuracy of 98.83 %. Here is the code for the architecture of CNN model:
import pandas as pd
import numpy as np
import pickle
np.random.seed(1212)
import keras
from keras.models import Model
from keras.layers import *
from keras import optimizers
from keras.layers import Input, Dense
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
from keras.utils.np_utils import to_categorical
from keras.models import model_from_json
import matplotlib.pyplot as plt
model = Sequential()
model.add(Conv2D(30, (5, 5), input_shape =(28,28,1), activation ='relu'))
model.add(MaxPooling2D(pool_size =(2, 2)))
model.add(Conv2D(15, (3, 3), activation ='relu'))
model.add(MaxPooling2D(pool_size =(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation ='relu'))
model.add(Dense(50, activation ='relu'))
model.add(Dense(12, activation ='softmax'))
# Compile model
model.compile(loss ='categorical_crossentropy',
            optimizer ='adam', metrics =['accuracy'])
model.fit(X_train, y_train, epochs=1000)

Now each image in dataset is preprocessed as follows:
import cv2
im = cv2.imread(path)
im_gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)
ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
rect = rects[0]
im_crop =im_th[rect[1]:rect[1]+rect[3],rect[0]:rect[0]+rect[2]]
im_resize = cv2.resize(im_crop,(28,28))
im_resize = np.array(im_resize)
im_resize=im_resize.reshape(28,28)

I have made an evaluation function which solves simple expression like 7+8 :-
def evaluate(im):
    s = ''
    data = []
    im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)
    ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
    boundingBoxes = [cv2.boundingRect(c) for c in ctrs]
    look_up = ['0','1','2','3','4','5','6','7','8','9','+','-']
    i=0
    for c in ctrs:
        rect = boundingBoxes[i]
        im_crop = im_th[rect[1]:rect[1]+rect[3], rect[0]:rect[0]+rect[2]]
        im_resize = cv2.resize(im_crop,(28,28))
        im_resize = np.array(im_resize)
        im_resize = im_resize.reshape(28,28,1)
        data.append(im_resize)
        i+=1
    data = np.array(data)
    predictions = model.predict(data)
    i=0
    while i<len(boundingBoxes):
        rect = boundingBoxes[i]
        print(rect[2],rect[3])
        print(predictions[i])
        s += look_up[predictions[i].argmax()]
        i+=1
    return s

I need help extending this to compound fractions, but the problem is that the vinculum / is identical to the subtraction sign - when resized to (28, 28). So I need help in distinguishing between them.
This is my first question, so please let me know if any details are left.

Welcome to SE:AI!  We focus on the conceptual aspects, as opposed to programming questions.  Can I ask is there a way to ask the core conceptual question without so much of the code?

I though to give all the details

We appreciate earnestness—I was concerned that the question has received two close votes and no answers as yet.  (My thought was emphasizing the conceptual could facilitate getting the information that could help you solve your specific problem.  Right now, I have to wonder if including all of the imports and ancillary code is keeping people away.)

Ok I will edit the question

It seems like the question was in fact edited but the problem @DukeZhou indicated of "all of the imports and ancillary code" has not been addressed.  I suggest heavily editing the post to conform to SE:AI guidelines (see https://ai.stackexchange.com/help/on-topic and https://ai.stackexchange.com/help/how-to-ask).

To better distinguish the vinculum vs. minus, you have a few options.

First, you may add more examples of each in your training set.

Alternatively, you may develop a second classifier (vinculum vs. minus subclassifier) that operates in tandem with your first
"general purpose" classifier ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, -, /]) which you already have.  The subclassifier is triggered only
when the first classifier predicts either a vinculum or minus.
Since your subclassifier is expected to be more accurate than than
the first classifier for the subtask of distinguishing between
vinculum and minus, you use the prediction of the former as the
final verdict.  In summary, the first classifier is a selector for
either vinculum or minus, and the second (sub)classifier is a
discriminator for which vinculum or minus.

Note:  For the vinculum vs. minus subtask, you may perform some old fashioned image analysis.  First, derive a mathematic representation of the bar.  Next, extract the length of the bar and the angle the bar makes with respect to the horizontal.  Finally, these two tabular features can be used in one of the many options of machine learning algorithms at your disposal.  For this subtask, it is possible that this approach may yield better results than deep learning.