text:
Why are the fluctuations in this plot so extreme?
I am training a standard feed-forward, fully connect neural network on ~30,000 6d input vectors paired to 30,000 5d output vectors. The vectors capture position and speed of a classical mechanics system.
I am using PyTorch, and after much trial and error, have found the following neural network seems to work OK:
self.neural_net = nn.Sequential(
            nn.Linear(6,1000), nn.ReLU(),

            nn.Linear(1000, 1000), nn.Sigmoid(),
            nn.Linear(1000, 1000), nn.Sigmoid(),

            nn.Linear(1000, 5)
        )

Work "OK" means: the loss goes down pretty low, using a MSE loss function, batch size=300, lr=1e-4, SGD optimizer. See the top plot here:

During training, I feed the network a random selection of 400 of the 30,000 input vectors. As it trains the network's ability to recognize the 400 vectors fluctuates very wildly, as shown in the bottom plot.
Does anyone know why the fluctuations are so extreme? Does this have something to do with my network's structure? I have tried messing around with regularization and different activations, etc. to no avail.
I see the fluctuation envelope decreasing as the loss goes down, and it seems like if I just wait another 10,000 training epochs, I'll be good, so maybe it's just the gradient descent doing its job?

Try to lower learning rate and batch_size (64 or 128) and hidden layers which tend to cause vanishing gradient for sigmoid activation. Try ReLU or Leaky ReLU, and BatchNorm with Early Stopping.

Thanks very much. I'll give your suggestions a try. (At the moment, using [Leaky]ReLU throughout causes a loss=nan to occur.)


text:
Using YOLO to Detect Objects from Specific Directional Images Only
I am working on an object detection task using YOLOv, where I have a dataset containing both front-facing and side-facing images of the same object. My goal is to train the model to detect only the front-facing images and ignore or differentiate side-facing images during detection.
I attempted to treat side-facing images as negative samples for training, but the results were unsatisfactory.
Could anyone suggest a more effective approach to solve this problem? Specifically, I need a method to ensure that side-facing images are either not detected at all or are distinguishable from the front-facing ones.

source: Stack Exchange

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

sort: votes


text:
Approaches to reduce splotchiness on UNet output
I'm training a UNet to try and predict caustics, kind of as a toy problem.
An input image to the UNet is something like this:

It is trained on "ground truth" outputs like this:

It is currently predicting results like this:

I figure I've begun overfitting because the training loss is consistently dropping below the validation loss (I'm just using standard MSE over the pixels). So, my main question is: what part of the UNet architecture is producing this weird splotchiness? It almost seems like there's some visible grid in the predicted caustic...
It seems like the splotchiness is similar to the that seen on the images in this question.

Here's another example:

My UNet is very standard. I'm trying to think what part of it would produce this grid-like splotchiness and then how I can tackle that.

For reference, my code looks like this:
import torch
import torch.nn as nn
import torch.nn.functional as F

class UNet(nn.Module):
    def __init__(self, in_channels=3, out_channels=3, pool_size=2, pool_type=F.max_pool2d, learn_residual=False):
        super(UNet, self).__init__()

        self.pool_size = pool_size
        self.pool_type = pool_type
        self.learn_residual = learn_residual
        self.in_channels = in_channels
        self.out_channels = out_channels

        # Encoder
        self.enc1 = self.conv_block(in_channels, 64)
        self.enc2 = self.conv_block(64, 128)
        self.enc3 = self.conv_block(128, 256)
        self.enc4 = self.conv_block(256, 512)

        # Bottleneck
        self.bottleneck = self.conv_block(512, 1024)

        # Decoder
        self.upconv4 = self.upconv_block(1024, 512)
        self.dec4 = self.conv_block(1024, 512)  # 512 (from upconv) + 512 (from enc4)

        self.upconv3 = self.upconv_block(512, 256)
        self.dec3 = self.conv_block(512, 256)   # 256 (from upconv) + 256 (from enc3)

        self.upconv2 = self.upconv_block(256, 128)
        self.dec2 = self.conv_block(256, 128)   # 128 (from upconv) + 128 (from enc2)

        self.upconv1 = self.upconv_block(128, 64)
        self.dec1 = self.conv_block(128, 64)    # 64 (from upconv) + 64 (from enc1)

        # Final Output
        self.final_conv = nn.Conv2d(64, out_channels, kernel_size=1, stride=1) # No ReLU activation on final output

    def conv_block(self, in_channels, out_channels):
        return nn.Sequential(
            nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1),
            nn.ReLU(inplace=True)
            # Note: Removed MaxPool2d from conv_block
        )

    def upconv_block(self, in_channels, out_channels):
        return nn.Sequential(
            nn.ConvTranspose2d(in_channels, out_channels, kernel_size=2, stride=2),
            nn.ReLU(inplace=True)
        )

    def forward(self, x):
        # Encoder
        enc1 = self.enc1(x)    # [batch, 64, H, W]
        enc2 = self.enc2(self.pool_type(enc1, self.pool_size)) # [batch, 128, H/2, W/2]
        enc3 = self.enc3(self.pool_type(enc2, self.pool_size)) # [batch, 256, H/4, W/4]
        enc4 = self.enc4(self.pool_type(enc3, self.pool_size)) # [batch, 512, H/8, W/8]

        # Bottleneck
        bottleneck = self.bottleneck(self.pool_type(enc4, 2)) # [batch, 1024, H/16, W/16]

        # Decoder
        up4 = self.upconv4(bottleneck)                         # [batch, 512, H/8, W/8]
        concat4 = torch.cat([up4, enc4], dim=1)                # [batch, 1024, H/8, W/8]
        dec4 = self.dec4(concat4)                              # [batch, 512, H/8, W/8]

        up3 = self.upconv3(dec4)                               # [batch, 256, H/4, W/4]
        concat3 = torch.cat([up3, enc3], dim=1)                # [batch, 512, H/4, W/4]
        dec3 = self.dec3(concat3)                              # [batch, 256, H/4, W/4]

        up2 = self.upconv2(dec3)                               # [batch, 128, H/2, W/2]
        concat2 = torch.cat([up2, enc2], dim=1)                # [batch, 256, H/2, W/2]
        dec2 = self.dec2(concat2)                              # [batch, 128, H/2, W/2]

        up1 = self.upconv1(dec2)                               # [batch, 64, H, W]
        concat1 = torch.cat([up1, enc1], dim=1)                # [batch, 128, H, W]
        dec1 = self.dec1(concat1)                              # [batch, 64, H, W]

        # Final Output
        out = self.final_conv(dec1)                            # [batch, 3, H, W]

        if self.learn_residual:
            out += x[:, :self.out_channels, :, :]

        return out

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


text:
Training custom seq2seq transformer model without any tokenizer
I’m working on a custom seq2seq transformer model for translating between two sets of token IDs.
My input and translation token IDs range from 0 to 8191.
Example:
input_ids = [2034, 4043, ...., 3]  # length is 2048
translation_input_ids = [3042, 9123, ...., 3285]  # length is 2048

I’m using EncoderDecoderModel with BertConfig for both the encoder and decoder. My sequences are all of length 2048. Here are my questions:
Vocab Size: Given that my tokens range from 0 to 8191, do I need to set vocab_size to 8194 (i.e., 8192 + 2) to account for additional tokens like decoder_start_token_id and pad_token_id?
Handling decoder_start_token_id: How does the model handle the decoder_start_token_id? Is it correct to assign it to a value outside my token range (e.g., 8192)?
Padding Token: If I don’t need padding (since all my sequences are the same length), do I still need to provide a pad_token_id in the configuration?
Config Choice: Is using BertConfig the right approach for this task, or would something like BartConfig or T5Config be more appropriate?
Here is my current model structure:
batch_size = 2
vocab_size = 8192 + 2
embedding_size = 1024
context_window_length = 2048

device_num = 0
device = torch.device(f'cuda:{device_num}' if torch.cuda.is_available() else 'cpu')

# Initialize BertConfig for encoder and decoder with vocab_size=8192
config_encoder = BertConfig(
    vocab_size=vocab_size,
    hidden_size=embedding_size,
    num_hidden_layers=6,
    num_attention_heads=8,
    intermediate_size=embedding_size*4,
    max_position_embeddings=context_window_length
)
config_decoder = BertConfig(
    vocab_size=vocab_size,
    hidden_size=embedding_size,
    num_hidden_layers=6,
    num_attention_heads=8,
    intermediate_size=embedding_size*4,
    max_position_embeddings=context_window_length,
    is_decoder=True,
    add_cross_attention=True
)

config = EncoderDecoderConfig.from_encoder_decoder_configs(config_encoder, config_decoder)
model = EncoderDecoderModel(config=config)
model.config.decoder_start_token_id = 8192  # arbitrary number that is not between 0 and 8191
model.config.pad_token_id = 8193  # arbitrary number that is not between 0 and 8191
model.to(device)

Now when I run the model training, it the loss is stuck to 6.5 just after around 1000 iterations, (my epoch size is 19500 iterations) and stays over there.
Is this the correct way to run a seq2seq training without any tokenizer involved? Or am I doing something wrong here?
I do not need the tokenizer here because these sequence are coming directly from a generation process and those are not text. But do I still need to design a customer tokenizer to run with the BERTConfig?


text:
Moving tensor to cuda reduces precision
I am trying to move a tensor to cuda, but unfortunately, I am loosing precision. The first entry is 5.02e-5 which is converted to 0. Is there a work around?
vals = vals.to(torch.float64).to(device_of_t)

which is converted to 0 how are you checking it?

I’m printing it and dividing by it to check.

post the entire code to reproduce the problem

why precision matters in soft computing? If so, you can normalize your data into min of float 64 to max of float 64.


text:
How well can gene expression values be reconstructed?
My aim is to create an embedding with AI models and to reconstruct the original data as well as possible from the embedding.
My data is the CCLE dataset with 16,383 gene expressions from 1037 cell lines.
I created two different models:

A Transformer model with MultiHeadAttention (MSELoss of 0.4)
A simple VAE (MSELoss of 0.6)

The 0.4 means that the value per position of the 16,383 gene expressions deviates by 0.4 from the original data.
I tried a lot with hyperparameters but I can't get below this value.
Do you think this can be the truth, or is there perhaps an even better way to create an embedding from which you can get the best possible reconstruction?

Could you clarify what the input and output data was, beyond "gene expression"? E.g. what input and output vectors are, what the sequence represents - are these amino acid sequences for instance, and what starting sequences are you testing with? Is your loss from training data, or separate hold-out test sequences

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.


text:
The probability of selecting optimal action
I am reading Reinforcement Learning by Sutton. In chapter 2 they say that:

An advantage of $\epsilon$-greedy methods is that, in the limit as the
number of plays increases, every action will be sampled an infinite number
of times, guaranteeing that $N_t(a)\to \infty$ for all $a$, and thus ensuring that all
the $Q_t(a)$ converge to $q(a)$. This of course implies that the probability of selecting
the optimal action converges to greater than $1 − \epsilon$, that is, to near
certainty.

Does "optimal action" mean sequence of actions chosen such that in the end total reward is maximized?
Also how to mathematically see that whatever optimal action means, it is implied by $\epsilon-$greedy method that the probability of selecting optimal action converges to some quantity greater than $1- \epsilon$?

oh wait is it just that at some step the probability that a non optimal action is chosen is $\epsilon$ and thus the probability that the best action is chosen is $1 - \epsilon$.
And optimal action means at given step the action which has highest expected reward.

Hello, and welcome to AI Stack Exchange! This post seems to have 2 different questions being asked. To help you get a faster answer to each question, it is recommended that you ask these two questions in separate posts. See the 'needs more focus' section of https://ai.stackexchange.com/help/closed-questions for more details. Thank you for joining, and we hope to see more of your questions here soon!

Does "optimal action" mean sequence of actions chosen such that in the end total reward is maximized?

No, although that is conceptually related to the optimal policy for reinforcement learning. Chapter 2 is not about the full reinforcement learning problem, it is about bandit problems. As such there is no "sequence of actions" that can be taken. You can repeat the experiment and take lots of actions. But they are not in a sequence, in the sense of there being any dependency between them.
Instead the "optimal action" is the one that has the highest expected reward. In a simple bandit problem, where no rules about reward change, this is always the same action, and solving the bandit problem involves finding that action. Often there's a side-effect that you want to maximise reward received whilst learning (this is often viewed as "minimising regret" although S&B do not go into that), but that is not important to the paragraph you quoted.
This sentence from the book is a little clumsy:

This of course implies that the probability of selecting the optimal action converges to greater than 1−, that is, to near certainty.

I take it to mean this:
In the limit of infinite samples, the estimated values can be arbitarily close to the true expected rewards, thus the greedy action choice will also the optimal action choice, with a probability arbitrarily close to 1.
Within the $\epsilon$-greedy framework, the probabilty of selecting the greedy action is actually $1 - \epsilon + \frac{\epsilon}{|A|}$, which is higher then $1 - \epsilon$. The extra term appears because when behaving randomly you may still select the greedy action. That's just how the
$\epsilon$-greedy behaviour is defined (you could define it differently)
I think at the current stage of explanation, the author didn't want to distract with the logically correct probability and maths so just used "greater than $1 - \epsilon$" as shorthand.
The point is that if $\epsilon$ is relatively small, then in the limit of very large numbers of trials, the basic $\epsilon$-greedy framework guarantees close-to-optimal behaviour.


text:
How deep to go in recursion for value iteration
Value iteration is a recursive function which calls itself to infinity. However, I can not find any mentioning how deep the recursion should go. Or in other words how often should the function call itself during one iteration?
How would I end the recursion?
The formula goes like this.:

The and V(s) should call it selft infinately time before evaluating anything.

Value iteration is a dynamic programming algorithm used to compute the optimal policy for a Markov Decision Process (MDP) and in practice value iteration proceeds until the value function converges to a solution that is close enough to the optimal value function as stated on page 82 of Sutton & Barto's RL book.

value iteration formally requires an infinite number of iterations to converge exactly to $v_*$. In practice, we stop once the value function changes by only a small amount in a sweep.

Common choices for this small amount are small positive numbers such as $0.01, 0.001$, or even smaller which is influenced by how much accuracy is needed in the value function and how long you're willing to wait for convergence on a case by case basis.

I am not referring to iteration, but only to the recursion part. As far as I know there is not validation happening.

@JackMiles There is no recursion used in Value Iteration. Current best estimate values are used to resolve $V(s')$.

But doesn't V(s') exactly suggest this?

@JackMiles It's called value iteration (not value recursion) and policy update (not policy iteration or recursion) for a reason. Yes it calls itself but there's no intrinsic conflict here, and in value iteration we're not really dealing with value functions but iterated approximated values.

@JackMiles Yes, see foreverska's answer, you could create a method that recursed state values via the Bellman equation. That's not what value iteration does though. Such an approach would not be called value iteration, and would more like a tree search/evaluation.

Historically value iteration was tabular.  So the “function” V(s) was just looking up a value in a table.  As such, there is no recursion (per se).
There’s nothing that requires a table (or any other single step estimator).  If the estimator actually implemented recursion, there is fundamentally two ways for recursion to end.  First is that the environment is episodic and naturally ends on a long enough timeline.  The second is to discount future rewards, effectively making it episodic at infinity.  This doesn’t end recursion on its own but we must remember why we’re estimating V or Q, to decide on an action.  At some point, with finite rewards, the discount factor becomes so small that any further estimation won’t change action rankings.  This is a natural place to stop.  If rewards are not bounded, this no longer works and some other simplification is needed (setting an artificial limit to recursion depth).


text:
Linear / Quadratic Program for Solving MDPs
Aside from value iteration, we can use the following linear program to solve the optimal value function of an MDP.

I am planning to put some constraints on the policies class that I consider, for example, state 2 and state 3 have to select the same action, etc. I want to explicitly add the policy as an optimization variable, and I created the following program with linear objective but quadratic constraints (bilinear to be specific).

To me, the constraints are essentially performing a policy evaluation, and the optimal policy is found through the maximization. However, I put this optimization problem into Gurobi and the algorithm has a hard time to converge. I am not sure whether the proposed optimization is right. More specifically, I am not sure whether the weights in the objective function can be arbitrary. Can anyone shed some light on my problem?


text:
What is delta in state space model?
I have read about state space model and MAMBA. The state space model is described as:
$\dot{h}(t) = Ah(t) + Bu(t)$
$y(t) = Ch(t) + Du(t)$
Then the discretization is:
$\bar{A} = exp(\Delta A)$
$\bar{B} = (\Delta A)^{-1}(exp(\Delta A) - I)\Delta B$
From my understanding, $\Delta$ is small change over time i.e $dt$, and $t$ is just a scalar represent time. However, in state space model S4 and Mamba paper, they use $\Delta$ as a high dimensional matrix, which cause confusion to me. How can I understand this $\Delta$?


text:
Reinforcement Learning for Discrete State and Continuous Action Spaces
I was looking for a way to apply reinforcement learning to a discrete state and continuous action space, specifically algorithms and common methods of approaching this type of problem.
I have tried applying a version of DynaQ to two environments: Pendulum v1 and Swimmer v2, where I discretise the state space into bins between the lowest and highest values for pendulum v1 and seemingly achieve similar results for optimisation to the expected. This confirms my method works to a degree but I wanted to ask if there were any known algorithms/methods which are typical for this type of problem (discrete obs/cont acts) as I would like to ideally try multiple methods and compare?
(As an optional aside: I have attempted to do this for Swimmer v2, where the lower and upper bounds of the state space are +/- inf. I have chosen somewhat arbitrary boundaries of +/- ~1000 and maybe 7/8 bins as there are 8 components to the states so end up with ~1e7 states and achieve a reward of ~30 but according to this I should expected closer to 100, if anyone can offer any solutions?)

you could just have parameters for each state that would correspond to the parameters of a distribution and learn these parameters using a policy gradient method. since the state space is discrete (I am assuming you are also meaning it to be finite and "not too large" in the sense that you can work with it in memory and get decent coverage through env interactions in a reasonable amount of time) this would literally just be a lookup from state to parameters.


text:
VAE suffers from posterior collapse under all hyper parameters
I am trying to find a low-dimensional latent space representation for a bunch of simulated data. No matter what VAE architecture I try and no matter how I tweak it, the output of the VAE is always the same, where all values hover around 0; it does not reconstruct the input at all. As far as I can understand, the problem is known as posterior collapse in the literature.
I use ray.tune to find hyper-parameters that work and the lowest validation loss I ever get is val_loss ~ 1. I picked one hyper parameter configuration that yields a val_loss ~ 1 and used it to train a VAE in the script below. The data in this example is 15 dimensional and drawn from a correlated multivariate normal distribution.
import math
import os
import numpy as np
from scipy.stats import random_correlation, loguniform

import torch
from torch.utils.data import Dataset, DataLoader, random_split
from torch import nn

import time

class MDVAE(nn.Module):
    def __init__(
            self,
            n_data: int,
            n_latent = 0.3,
            n_hidden = 0.6,
            n_hidden_layers = 1,
            bias = True,
            # activation=nn.LeakyReLU(0.01),
            activation=nn.LeakyReLU(0.01),
    ):
        super().__init__()
        if isinstance(n_latent, float):
            n_latent = math.ceil(n_latent * n_data)
        if isinstance(n_hidden, float):
            n_hidden = math.ceil(n_hidden * n_data)

        if (n_latent > n_hidden) or (n_latent > n_data):
            raise ValueError

        endocer_layers = [nn.Linear(n_data, n_hidden, bias=bias), activation]
        for i in range(n_hidden_layers):
            endocer_layers.extend([nn.Linear(n_hidden, n_hidden, bias=bias), activation])

        self.encoder = nn.Sequential(*endocer_layers)
        self.mean_lay = nn.Linear(n_hidden, n_latent)
        self.log_var_lay  = nn.Linear(n_hidden, n_latent)

        decoder_layers = []
        for layer in endocer_layers:
            if layer == activation:
                continue
            decoder_layers.extend([nn.Linear(layer.out_features, layer.in_features), activation])
        decoder_layers.extend([nn.Linear(n_latent, n_hidden)])
        self.decoder = nn.Sequential(*decoder_layers[::-1])

    def encode_and_sample(self, x):
        hidden = self.encoder(x)
        mean = self.mean_lay(hidden)
        log_var = self.log_var_lay(hidden)

        # reparametrization trick
        std = torch.exp(0.5 * log_var)
        epsilon = torch.randn_like(std)
        z = mean + std * epsilon
        return z, mean, log_var

    def forward(self, x):
        z, mean, log_var = self.encode_and_sample(x)
        return self.decoder(z), mean, log_var

class VAE_Dataset(Dataset):
    def __init__(self, data: torch.Tensor):
        self.data = data

    def __len__(self):
        return self.data.shape[0]

    def __getitem__(self, idx):
        return self.data[idx], self.data[idx]

_PRR = lambda x: float(x.to('cpu').data.numpy())
def ray_train(config, cwd, n_data, n_epoch=5, show_progress=False):
    n_hidden = float(config.get('n_hidden', 0.6))
    n_latent = int(config.get('n_latent', 0.3))
    n_hidden_layers = int(config.get('n_hidden_layers', 2))
    batch_size = int(config.get('batch_size', 64))
    learning_rate = float(config.get('learning_rate', 1e-3))
    weight_decay = float(config.get('weight_decay', 1e-4))
    KL_factor = float(config.get('KL_factor', 1.0))

    train_ds = torch.load(os.path.join(cwd, 'train_ds.pt'))
    val_ds = torch.load(os.path.join(cwd, 'val_ds.pt'))
    x_val, y_val = val_ds[:]
    train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=True)

    mdvae = MDVAE(
        n_data=n_data,
        n_hidden=n_hidden,
        n_latent=n_latent,
        n_hidden_layers=n_hidden_layers,
    )

    loss_f = nn.MSELoss()
    optimizer = torch.optim.Adam(mdvae.parameters(), lr=learning_rate, weight_decay=weight_decay)

    if show_progress:
        raise NotImplementedError

    for epoch in range(n_epoch):
        for i, (x, y) in enumerate(train_loader):
            x_hat, mean, log_var = mdvae.forward(x)
            reconstruct = loss_f(x_hat, y)
            KL_div = - 0.5 * torch.sum(1 + log_var - mean.pow(2) - log_var.exp())
            loss = reconstruct + KL_factor * KL_div
            optimizer.zero_grad()
            if ~(torch.isnan(loss) | torch.isinf(loss)):
                loss.backward()
                optimizer.step()

        with torch.no_grad():
            x_val_hat, mean, log_var = mdvae.forward(x_val)
            reconstruct_val = loss_f(x_val_hat, y_val)
            KL_div_val = - 0.5 * torch.sum(1 + log_var - mean.pow(2) - log_var.exp())
            val_loss = reconstruct_val + KL_factor * KL_div_val

            print(_PRR(val_loss))
    return mdvae

def prepare_datasets(
        cwd,
        n_data,
        n_samples=20000,
        val_frac=0.1
):
    # reproduciple random numbers
    rng = torch.Generator()
    rng.manual_seed(3)
    np_rng = np.random.RandomState(3)

    # create a data-set with correlated variables
    eigs = np_rng.rand(n_data)
    eigs = eigs * (n_data / sum(eigs))
    corr = torch.from_numpy(
        random_correlation.rvs(eigs, random_state=np_rng)
    )
    chol = torch.linalg.cholesky(corr)

    data = torch.randn((n_samples, n_data), generator=rng, dtype=torch.float64)
    data = data @ chol  # correlated data
    data = (data - torch.mean(data, 0)) / torch.std(data, 0)  # normalized data
    dataset = VAE_Dataset(data.float())

    n_validate = math.ceil(val_frac * n_samples)

    # prepare datasets
    train_ds, val_ds = random_split(
        dataset,
        lengths=(len(dataset) - n_validate, n_validate),
    )
    torch.save(train_ds, os.path.join(cwd, 'train_ds.pt'))
    torch.save(val_ds, os.path.join(cwd, 'val_ds.pt'))

if __name__ == "__main__":
    param_space = {
        'batch_size': 32,
        'n_hidden': .8,
        'n_latent': 10,
        'n_hidden_layers': 4,
        'learning_rate': 0.0001,
        'KL_factor': 0.1,
    }

    n_samples = 20000
    n_data = 15
    n_latent = 5  # I make an assumption on the right latent space size, so this is kept fixed

    cwd = os.getcwd()
    if not (os.path.isfile(os.path.join(cwd, 'train_ds.pt')) and os.path.isfile(os.path.join(cwd, 'val_ds.pt'))):
        prepare_datasets(cwd, n_data, n_samples)

    mdvae = ray_train(param_space, cwd=os.getcwd(), n_data=15)

    cwd = os.getcwd()
    train_ds = torch.load(os.path.join(cwd, 'train_ds.pt'))
    val_ds = torch.load(os.path.join(cwd, 'val_ds.pt'))
    x_val, y_val = val_ds[:]

    y_hat, mean, log_var = mdvae.forward(x_val)

    print(y_val[:3])
    print(y_hat[:3])
    print(torch.std(y_hat, 0))

THe output of this is:
3.098008632659912
1.006637454032898
1.000028371810913
0.9986492991447449
0.9981059432029724
tensor([[ 1.8516, -0.3478,  2.6511,  1.9317, -0.1876,  0.4041,  1.6015,  0.3717,
          0.8461, -1.1814, -1.4392, -0.9345, -1.1823,  0.7851,  0.2900],
        [-0.6445, -0.0632, -1.0273,  0.5480,  1.1906,  0.5398,  0.0707,  0.2530,
          0.5554,  2.5385,  0.0644, -0.0644, -1.5081,  0.3629, -1.9816],
        [-0.8815,  0.1168, -0.9882,  0.7145, -0.0372,  1.1363,  0.3081, -0.6322,
          0.8425,  0.1894, -0.4679,  0.3413,  0.5822,  1.1235, -0.9265]])
tensor([[-0.0023, -0.0152,  0.0029,  0.0012, -0.0078, -0.0040, -0.0068,  0.0090,
         -0.0128, -0.0080,  0.0216,  0.0035,  0.0050, -0.0190, -0.0067],
        [-0.0005,  0.0043, -0.0106,  0.0028, -0.0019, -0.0043, -0.0063, -0.0039,
         -0.0142,  0.0004,  0.0255, -0.0042, -0.0013, -0.0173, -0.0056],
        [-0.0026,  0.0002, -0.0004, -0.0010,  0.0057, -0.0078,  0.0026, -0.0010,
         -0.0060, -0.0050,  0.0206,  0.0029,  0.0008, -0.0187, -0.0017]])
tensor([0.0024, 0.0102, 0.0073, 0.0030, 0.0120, 0.0046, 0.0099, 0.0062, 0.0089,
        0.0048, 0.0042, 0.0051, 0.0034, 0.0009, 0.0072])

My observations
The floats at the top are the validation loss and clearly it goes down and stabilizes around 1. From this I conclude that we are 'learning' something. If we then look at printed tensors, the first one is not at all like the second one and so the reconstruction does not work at all. Also note that the reconstruction tensor almost only contains values close to 0. The standard deviation of these reconstructed values is shown in the third tensor. I interpret these problems as posterior collapse.
For reference
the non-standard hyper parameter meanings are the following:

n_hidden: fraction of the input space dimensions that is used as the dimension of the hidden layers
n_latent: latent space dimension
n_hidden_layers: number of hidden layers; thinking is that maybe the network is not complex enough?
KL_factor: a factor to reduce the weight of the KL divergence in the loss; this is known as a  $\beta$-VAE

The data in this example are drawn from a correlated multivariate normal distribution (see prepare_datasets), my real data is far more complex and high dimensional.
Previous tries
Things I tried that led to the same issue of having reconstructions that are vectors of 0s and a loss of ~ 1:

I normalize my real data by subtracting the mean and dividing by standard deviation; also the case for the prepare_data function in this example
I tried having n_data = n_hidden = n_latent meaning that there is no bottleneck; this also lead to posterior collapse
ray tune parameter search space:

param_space = {
        'batch_size': tune.choice(list(2 ** np.arange(3, 9))),
        'n_hidden': tune.uniform(min_n_hidden, max_n_hidden),
        'n_latent': n_latent,
        'n_hidden_layers': tune.randint(1, 4),
        'learning_rate': tune.loguniform(1e-5, 3e-1),
        'KL_factor': tune.uniform(1e-5, 1),
    }

None of the combinations in this space lead to a good reconstruction and all outputs have been just vectors with numbers close to 0.
Questions
I am now at a loss; to me it seems like the VAE code is fine and yet none of the things I have read about posterior collapse have lead to a working VAE even on example data.

Why do I keep getting 0 vectors?
If the network is learning (loss going down and stabilizing around 1), why am I not getting a working VAE?
What should be my next steps in creating a working VAE?
The VAE is attractive since the latent space samples resemble a standard multivariate normal (MVN). For my project, these latent variables serve as independent variables in a conditional density estimation task and this is made easier if they are MVN. Are there perhaps other algorithms (than VAE) that I should look at to learn latent variable representations of my data?
Are there other encoder/decoder architectures that make sense for continuous tabular data? I now just use fully connected layers but maybe something more fancy would do the trick?

Since your ray tune already tried fine tuning smaller KL-factor down to 1e-5, have you tried even lower value or KL annealing? Also how many data size for your training? Are they images of good smooth variability quality? What's your current best reconstruction error? Sounds like your decoder is overfit.

Thanks for thinking along! My task involves continuous tabular. I've not tried annealing yet, but am skeptical since the reconstructions also do not work for very small beta. What makes you think the decoder is overfit?


text:
Is there a better way to do this type of optimization?
I have an image classification task that uses an object detection model as its basis. For each image, I get a vector of confidences (one value for each class), and I take the class with the highest confidence as the actual prediction for the image. I also have a weight for each class - all confidences of a class will be multiplied by a weight; this is used to improve accuracy when confidences for certain classes are abnormally low or high (as a result of the nature of the model). In addition, I also have a vector of "thresholds" (one for each class). These reduce to 0 any confidences that are below the threshold. Again, this is used to tune and improve accuracy.
I am currently working on a method to automatically tune these "class weights", using a ground truth labeled dataset. I try to adjust these class weights/thresholds such that my confidences (after weighting and thresholding) are calibrated so that the highest confidences for each image are aligned with the ground truth label as much as possible. I am currently using a gradient descent method based on cross-entropy (similar logic with normal classification model training) to tune these weights, but I am stuck on what to do with the thresholds. Anyone has any suggestions for technical ideas/algorithms that fit my task?

Even you trained your classifier using CE loss, in practice the usual overfitting, imbalanced training data and distribution shift may result in poorly calibrated predictions as discussed in a recent post here. For modern neural network classifiers Guo et al showed empirically that a simple temperature scaling as a single parameter variant of Platt scaling produces the lowest expected calibration error (ECE) on a variety of ANN classification problems in their 2017 paper On Calibration of Modern Neural Networks.

Through extensive experiments, we observe that depth, width, weight decay, and Batch Normalization are important factors influencing calibration... Modern neural networks exhibit a strange phenomenon: probabilistic error and miscalibration worsen even as classification error is reduced...  Nevertheless, simple techniques can effectively remedy the miscalibration phenomenon in neural networks. Temperature scaling is the simplest, fastest, and most straightforward of the methods, and surprisingly is often the most effective.

In summary calibrations usually do not affect the identity of the most probable class label, so these methods may have little impact on classification accuracy. However, they do improve calibration performance in terms of forecasting, for example, the effect of relatively high temperature (>1) in temperature scaling is to make the trained model's predictive distribution less peaky which you'd better employ to adjust your thresholds after you trained your model. Your class weights essentially acting as cost-structure should be only used in your reweighted CE loss during training. Of course you can also move calibrated ratios to loss as additional class weights in some distribution shift calibration cases as discussed in the linked post which your problem hopefully doesn't face.


text:
Mathematical calculation behind decision tree classifier with continuous variables
I am working on a binary classification problem having continuous variables (Gene expression Values). My goal is to classify the samples as case or control using gene expression values (from Gene-A, Gene-B and Gene-C) using decision tree classifier. I am using the entropy criteria for node splitting and is implementing the algorithm in python. The classifier is easily able to differentiate the samples.
Below is the sample data,
sample training set with labels
Gene-A    Gene-B    Gene-C    Sample
   1        0         38       Case
   0        7         374      Case
   1        6         572      Case
   0        2         538      Control
   33       5         860      Control

sample testing set labels
Gene-A    Gene-B    Gene-C    Sample
  1         6        394       Case
  13        4        777       Control

I have gone through a lot of resources and have learned, how to mathematically calculate Gini-impurity, entropy and information gain.
I am not able to comprehend how the actual training and testing work. It would be really helpful if someone can show the calculation for training and testing with my sample datasets or provide an online resource?

Of course, it depends on what algorithm you use. Typically, a top-down algorithm is used.
You gather all the training data at the root. The base decision is going to be whatever class you have most of. Now, we see if we can do better.
We consider all possible splits. For categorical variables, every value gets its own node. For continuous variables, we can use any possible midpoint between two values (if the values were sorted). For your example, possible splits are Gene-A < 0.5, Gene-A < 17, Gene-B < 1, Gene-B < 3.5, and so on. There is a total of 10 possible splits.
For each of those candidate splits, we measure how much the entropy decreases  (or whatever criterion we selected) and, if this decrease looks significant enough, we introduce this split.
For example. Our entropy in the root node is $-0.4 \log_2 0.4 - 0.6 \log_2 0.6 \approx 0.97$. If we introduce the split Gene-A < 0.5, we get one leaf with entropy $1$ (with 2 data points in it), and one leaf with entropy $0.918$ (with 3 data points). The total decrease of entropy is $0.97 - (\frac25 \times 1 + \frac35 \times 0.918) \approx 0.02$.
For the split Gene-A < 17 we get a decrease of entropy of about $0.3219$.
The best splits for the root are Gene-B < 5.5 and Gene-C < 456. These both reduce the entropy by about $0.42$, which is a substantial improvement.
When you choose a split, you introduce a leaf for the possible outcomes of the test. Here it's just 2 leaves: "yes, the value is smaller than the threshold" or "no, it is not smaller". In every leaf, we collect the training data from the parent that corresponds to this choice. So, if we select Gene-B < 5.5 as our split, the "yes" leaf will contain the first, fourth and fifth data points, and the "no" leaf will contain the other data points.
Then we continue, by repeating the process for each of the leaves. In our example, the "yes" branch can still be split further. A good split would be Gene-C < 288, which results in pure leaves (they have 0 entropy).
When a leaf is "pure enough" (it has very low entropy) or we don't think we have enough data, or the best split for a leaf is not a significant improvement, or we have reached a maximum depth, you stop the process for that leaf. In this leaf you can store the count for all the classes you have in the training data.
If you have to make a prediction for a new data point (from the test set), you start at the root and look at the test (the splitting criterion). For example, for the first test point, we have that Gene-B < 5.5 is false, so we go to the 'no' branch. You continue until you get to a leaf.
In a leaf, you would predict whatever class you have most of. If the user wants, you can also output a probability by giving the proportion.
For the first test point, we go to the "no" branch of the first test, and we end up in a leaf; our prediction would be "Case". For the second test point, we go to the "yes" branch of the first test. Here we test whether 777 < 288, which is false, so we go to the "no" branch, and end up in a leaf. This leaf contains only "Control" cases, so our prediction would be "Control".


text:
Should the range and initial values of weights and biases be adjusted to fit input and output data?
As a routine (in typical everyday  tasks) of a data scientist, should they usually decide about weights and biases range and initial values as a function of which data they are planning to insert as an input, and which type of data they expect to get in the output? Or we usually do not deal with such fine-tuning, and let the algorithm to do it?
One could answer that normalizing inputs solves the problem and no need to fit weights and biases, but I guess they depend also on expected output.
To summarize:

is it common to deal with weights and biases in everyday tasks or in most of the cases existing algorithms do it well?

what are the rules of thumb for how to decide about range and initial values of weights and biases?

is it common to deal with weights and biases in everyday tasks or in most of the cases existing algorithms do it well?

No; and it is no coincidence that you will not be able to find any reference to such a practice in any course or tutorial about neural networks. Such a practice would require a whole additional level of (business/SME) know-how in order to meaningfully apply neural networks to real-world problems, and fortunately this is not necessary.
The desired situation is for both weights & biases to remain in a relatively small* range around zero, among other reasons because this avoids the exploding & vanishing gradient problems, which is catastrophic for learning; trying to adjust for the scale of our inputs & outputs by scaling accordingly the model weights & biases is the wrong approach, and never followed.
[*How small? Well, see here and here for what a change in just the standard deviation of a zero-mean weight initialization from 1.0 and 0.1 to 0.01 can do to the model performance]
It is a well-established fact by now that neural nets work with normalized inputs, and, depending on the problem, normalized outputs as well; which answers to your objection:

One could answer that normalizing inputs solves the problem and no need to fit weights and biases, but I guess they depend also on expected output.

It depends indeed, but the solution here is to scale the output(s) as well, and not the weights.
Now, it's true that de-scaling the outputs back to their original range in order to be able to meaningfully compare them with the ground truth (and possibly compute more meaningful metrics, like the error on our true output range, and not the scaled one) is AFAIK seldom mentioned in introductory expositions; but this is indeed the correct thing to do (especially for regression problems, where metrics like MSE are scale-sensitive), instead of trying to manually intervene on the weights range. For details, see own answers in How to interpret MSE in Keras Regressor and ANN regression accuracy and loss stuck.

what are the rules of thumb for how to decide about range and initial values of weights and biases?

Range aside, the general rule, as already implied, is to initialize the biases with zeros and the weights with small random values around zero. Nevertheless, the exact details are an area of active research. Currently used initialization schemes that are already integrated into the relevant frameworks (Tensorflow, Keras, Pytorch etc) are the Glorot (or Xavier) and He initializations (for a nice overview, see Weight Initialization in Neural Networks: A Journey From the Basics to Kaiming).
Beyond these routinely-used approaches that have already reached the practitioner's workbench, and moving closer to the front of active theoretical research, the Lottery Ticket Hypothesis (finding "winning" weight initializations that require minimal training) is an ultra-hot topic lately.

ok no prob. Please tell me, you can add to your answer as part 3 of same question.
What about activation functions?  same 2 questions (is it common to deal with them as a routine, and if so what are thumb rules)

@Igor ReLU and its variants for almost all uses in the intermediate layers; output ones depend on the problem - sigmoid or softmax for classification problems, linear for regression ones.

With regards to output scaling, is it required because initially (first iterations) there might be a large difference between true outcome and prediction (due to very small weights) which can cause exploding gradients or diverging behavior?


text:
Reinforcement Learning for an environment that is non-markovian
I will start working on a project where we want to optimize the production of a chemical unit through reinforcement learning approach. From the SME's, we already obtained a simulator code that can take some input and render us the output. A part of our output is our objective function that we want to maximize by tuning the input variables. From a reinforcement learning angle, the inputs will be the agent actions, while the state and reward can be obtained from the output. We are currently in the process of building a RL environment, the major part of which is the simulator code described above.
We were talking to a RL expert and she mentioned that one of the thing that we have here conceptually wrong is that our environment will not have the Markov property in the sense that it is really a 'one-step process' with the process not continuing from the previous state and there is no sort of continuity in state transitions. She is correct there. This made me think, how can we get around this then. Can we perhaps append some part of the current state to the next state etc. More importantly, I have seen RL applied to optimal control in other examples as well which are non-markovian ex. scheduling, tsp problems, process optimization etc. What is the explanation in such cases? Does one simply assumes process to be markovian with unknown transition function?

The Markov Property is an extremely hard constraint that is often not met in real life application. Many RL methods will work fine without the Markov property. A simple way is to add previous states in the value and action fonctions for instance.
Given that, I think that you should either give more details on your specific problem or rephrase the theoretical part you are not understanding.

RL is currently being applied to environments which are definitely not markovian, maybe they are weakly markovian with decreasing dependency.
You need to provide details of your problem, if it is 1 step then any optimization system can be used.

what do you mean by 1 step? Currently yes, the simulator can take a set of input and render output. But there is nothing dynamic and time-dependent in it. Like I mentioned the next state (output) then is only being determined here by the next action (input) which I am free to choose and as such has got nothing to do with the previous state.

I view it as a generalization of the conditional Markovian case.
It does have the Markov property, in that the future state depends solely on the input at the given state, which probably is to be sampled from a stochastic policy, that is conditioned on the current state.
It seems to me to be a more general, simpler, and unconstrained case.


text:
Is it possible to predict $x^2$, $\log(x)$, or variable function of $x$ using RNN?
There were some posts that using RNN can predict the next point of the sine wave function with data history.
However, I wondered if it also works on all the functions of $x$, such as $x^2$, $x^3$, $\log(x)$, $\frac{1}{(x+1)}$ functions.

Yes, RNN can work on the functions you have mentioned. In fact, neural networks can approximate anything (Universal Approximation Theorem). This question also reminds me of Neural Turing Machine.
But, it would be a complete waste to use RNNs or NNs for such a task.

Neural networks cannot approximate "anything". They can approximate continuous functions and provided that certain conditions are met. It may be a good idea to provide a link to some article that provides more details.

Thank you. It would not be a waste because i need the thing that this assumption is true to build my own algorithm.

I know UAT doesn't cover it all but it is the only mathematical thing I can point out. Otherwise, I can say it is magic, because nobody can explain the unreasonable effectiveness of neural networks. It is a black-box which we frankly no nothing about. Hence, the need to run helter-skelter building bigger things or hitting any arrow that hits in the dark..

Recently, there has been some interesting research around topology which may explain this. But, then, that would be another Pandora's box. That is why, using UAT is a good idea since everybody knows that.


text:
What is the training accuracy of this model?

I’m trying to classifiy ECG signals using LSTM and MATLAB, the above plot shows that the training accuracy of the system is 100% but when I apply this code to calculate and get the accuracy I get only 20%
LSTMAccuracy = sum(trainPred == Labels)/numel(Labels)*100

Am I missing something here? Or there something wrong I did in my code?
Here is the configuration and the training code:
layers = [ ...
    sequenceInputLayer(1)
    bilstmLayer(100,'OutputMode','last')
    fullyConnectedLayer(5)
    softmaxLayer
    classificationLayer
    ]

options = trainingOptions('adam', ...
    'MaxEpochs',1750, ...
    'MiniBatchSize', 150, ...
    'InitialLearnRate', 0.0001, ...
    'ExecutionEnvironment',"auto",...
    'plots','training-progress', ...
    'Verbose',false);

net = trainNetwork(Signals, Labels, layers, options);

trainPred = classify(net, Signals,'SequenceLength',1000);

LSTMAccuracy = sum(trainPred == Labels)/numel(Labels)*100
figure
confusionchart(Labels,trainPred,'ColumnSummary','column-normalized',...
              'RowSummary','row-normalized','Title','Confusion Chart for LSTM');

The problem can be solved using the following line of code:
trainPred = classify(net, Signals,'SequenceLength','longest');

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:
How to handle the dead agent in multi-agent environment?
I try to implement deep reinforcement learning on a defender-vs-attacker problem, where agents can be destroyed by enemies. I am coding both the environment and the RL algorithm. The agent can observe his own state and other's. As we know, the input size of a network should be constant. Here comes the question: how to handle the observation of a dead agent? i.e., if agent1 dies, what should be the agent2's observation of agent1?
My first thought is to set the state to be zero. But I don't think it is a good idea. let's take the state of a agent as: [x, y, theta] and normalize it to [0, 1]. Apparently [0, 0, 0] is also a valid state. If we set the state to be other strange numbers like -1, How can the network realized this is not a normal input of alive agent?

How to handle the dead agent? Borrow him with honors. Or just keep a track on a separate variable (just guessing)

A dead agent is a state of that agent, assuming the episode continues.
Using the existing state or observation vector, and adding special values should work, but you could be making extra work for any value function approximator. The usual way to solve this is to expand the observation vector and add e.g. 1 for alive and 0 for dead, as a new feature.
If the agent is also removed from any possible interactions, you should also set other variables to some standard fixed values, because there should be only one observation vector matching a dead agent. Exceptions to that rule might might apply for static features describing a agent in general terms, such team affiliation (knowing it's your own or opposing team member that is out of action is important knowledge for predicting outcomes).
This also has the advantage of supporting extensions to the environment as your agents become more sophisticated. For example, if instead of being removed from the map, visiting a dead agent's location allowed scavenging resources (you may also need more observation features for whether this has been done already).

What do you mean by "making extra work for any value function approximator"? Do you think expanding the observation vector is better choice than adding special values?

@zhixin I mean that the function shape is usually more difficult to learn if it contains a big change at some special values, where otherwise it would have been simpler and smoother. An NN can learn the shape of e.g. a circle easily, but would find a circle with a single spike on one side slightly harder to learn.

Your environment should handle it, not the agent.
If I'm playing a game, and my player dies, then if I press buttons, nothing happens... it's not the player supposed to stop playing, it's the game that is supposed to stop you from doing anything
In other words, the network can still observe whatever is supposed to be seeing, it's the environment and your training algorithm that should take special cautions for such corner case

I guess you misunderstand my question. I mean how to handle observation of dead agents. Suppose we have 3 agents a, b, c. Agent a's obs is [a's state, b's state, c's state].  If b is destroyed, how should the a's obs be? By the way, the environment is created by myself, so how should the environment give the agent observation when some other agents die. And this is not a corner case, it is expected to see agents destroyed by others.


text:
Why can Variational Autoencoders (VAEs) approximate arbitrary distributions?
I am trying to reason to myself why is it that VAEs can approximate arbitrary probability distributions even though (|)
and (|)
are Gaussian.
I understand that the parameters are typically learned using neural networks =()
and =()
, where
and
denote arbitrary neural networks. Therefore, some ideas I had is perhaps the entire VAE is able to learn differentiable, change of variables mappings, which allows the VAE to ensure that, although
and
both characterize Gaussian distributions, they do so using some transformation of the original variables
and/or
and so effectively correspond to some arbitrary distribution rather than a Gaussian in
or
.
However, and I may just be unnecessarily picky here, but I can't quite convince myself why the Gaussian PDF itself, even with the change of variables mapping, will not act as a "bottleneck" in learning some rather complex non-Gaussian distributions. I was wondering if there is some universal approximation theorem in the case of VAEs, as there in feed-forward neural networks / RNNs etc.

The fact that you can approximate any distribution is given by the definition ELBO, which is a lower bound in order to learn $p(x)$
Theoretically speaking, if you are able to make that difference between $p(x)$ and ELBO $0$, than you can say that you have actually approximated $p(x)$ correctly
This usually doesn't happen, as it is an ideal/optimal solution, which is not what happens when you then introduce mathematical models, optimized with gradient descent, and moreover non convex
However, you might want to take a look at optimal transport, so you might get a better idea how to transform an arbitrary complex distribution, to a simple one
About the bottleneck, you can think that your NN is so powerful, that might learn a transformation between your more-complex-prior to a Gaussian, thus a bigger NN might learn the transformation $p_\text{data}\rightarrow p_\text{complex prior} \rightarrow p_\text{Gaussian}$

I see, thank you for your fast response. So are you essentially saying that the neural network may learn how to "morph" the space of $x$ using a differentiable mapping into some Gaussian distribution that it can then leverage from that point onward? This was my attempt at summarizing your last point.
Edit: thanks also for pointing me towards optimal transport.

@Joel yes, you can phrase it like that... you can even think that each layer learns a step towards that simpler distribution... however, OT transforms distributions, NN transform a sample of that distribution

I see @Alberto Sinigaglia, thank you. I am wondering, however, if the mapping of x to the parameterization of the latent space (usually Gaussian) is deterministic, then why can't we view the process as transforming the entire distribution itself?

On second thoughts, I think you mean that the parameters of $p(z|x)$ itself are usually dependent on $x$ and so the NN is technically mapping the sample $x$ to a different distribution - am I interpreting you correctly?

@Joel well, first, transforming samples to look like a distribution (VAE), it's much simpler that transforming in closed form the whole distribution (Opt Transport)... secondly, the fact that we are using a distribution in the latent space, it's not always necessary... you can achieve a pseudo-VAE with just a naive AE with a L2 regularization on the latent vectors


text:
Why can AI not understand numbers- even basic ones?
I asked Bard to "Give me a challenging mathematical riddle to which the answer is 23"
It answered: "Here is a challenging mathematical riddle to which the answer is 23:
I am the sum of the first 7 positive odd numbers. What number am I?
The answer to this riddle is 23. The first 7 positive odd numbers are 1, 3, 5, 7, 9, 11, and 13. Their sum is 23."
This is clearly untrue?

Perhaps because the foundation models of most leading AIs was trained  on "tokens" (string fragments) not numeric types?

Models don't really understand math. If they produce a correct answer, it's because that's the most probable answer given the data they were trained on. We've had a similar question for ChatGPT. This should apply to all models that are just trained to predict the most likely output.

Does this answer your question? Why is ChatGPT bad at math? It's not really a duplicate but I think the question are very related that they can be closed a such given that the answers will most likely be all similar or refer to the same reasons

You are not alone in having unrealistic expectations of a technology labelled as "AI" that appears to have very general capabilities regarding text. After all, it will always respond to any input, and the tone of responses for most of the fine tuned LLMs (of which Bard is one) give an air of confidence.
The latest releases of chatbots and related technology are all based on large language models (LLMs), where the core training process is to predict the next word, or more precisely a token which can be just part of a word, given all the text that has already been seen. This trained model is then leveraged to emulate chatting or other text generating processes. For each use case, the model can be fine tuned for the type of content and utility, and can also be "pre-prompted" to establish the nature of the content. The fine tuning and pre-prompting  doesn't radically change the core nature of an LLM as a text prediction engine though.
During training, typically on immense amounts of data far larger than human experience of a single person can cope with, the LLM does learn some approximations of logic and facts, in as much as they help it predict the next token. Some of these learned capabilities are robust, useful and effective. Some less so. The more complex a real-world constraint or fact is, the less likely the LLM will have modelled it with an accurate approximate function internally.
The riddle problem you set in the question is exposing the limitations of the approximate internal models. The LLM is capable of generating a grammatically correct riddle, plus an explanation, plus meet the constraint placing a specific answer in the text. It is not capable of analysing its own output and figuring out that it has made an incorrect statement. That is, it has either no model or a weak approximate model for constraints on what should be in the mathematical statements in the riddle. From what you have written I would say "weak model" here, because the number produced by resolving the puzzle is still the correct order of magnitude, and that probably is not a coincidence.
Expect future iterations of AI chatbots and assistants to get better at this kind of thing - in my opinion this will happen purely because it is an expectation many people will have of "useful AI". Simply scaling the LLMs will only help a little, so future work is probably looking at adding further models or integrations that can fact-check outputs, or better understand and model requests (beyond representing as streams of tokens).
In the meantime, you are always recommended to fact check results from LLMs. Do not assume any output is factually correct, always validate anything where you need it to be correct or true.

Surely if any AI 'Chatbot' is to become popular it should be trained specifically in Mathematics in addition to the huge amounts of assorted data?

@bensvensson ChatGPT is already extremely popular already, so no that does not appear to be a requirement. In addition the training process for LLMs cannot so easily be adapted to feed in factual information. It doesn't learn in anything like the same way as humans. Things that might seem easy to you from outside of understanding the system are often anything but - https://xkcd.com/1425/ is relevant

Ah, splendid, I forgot how pertinent xkcd can be, so thanks for that. While I'm no computer whizz, couldn't there be multiple 'engines' as it were, involved in answering queries, thus there would be one overseeing the entire process, and a separate ones which specialise in specific topics. This would also allow for a factchecker...

@bensvensson Yes that might be a valid architecture moving forward, and I touch on that in the penultimate paragraph.


text:
How to label missing/default values for a named entity recognition dataset
I am building the training dataset for a named entity recognition model, with 2 tags: Name and Category and I am using a pre-trained spaCy model.
Given a document, the model needs to extract the name and category of several items.
However, the name or category can be missing or contain a value which means it is absent.
In this case, should I tag or ignore the entities?
For example, given the following document:
The first item's name is: X and category is: N/A
The second item's name is: --- and category is <specifiy-item-category>
The third item's category is: Y

should I also tag N/A as the Category (first line) or --- as the Name  or <specifiy-item-category> as the Category (the second line) ?
Specifically, I am not interested in empty values and in the end I am going to ignore them anyway. Should the model be agnostic or not of empty meaning?

NER models are usually trained on non overlapping sequence of tokens. And this is pretty much the only rule followed (even though it's not strictly required to do so, but at the risk of complicating training and hindering performance).
In your specific case I see a couple of possibilities:

if "N/A", "---" and  are systematic values used in the data when a name or a category is not given, then you could implement a preprocessing step to identify them, and skip both training and inference for them since you already know how to retrieve them manually.

if you don't know in advance all the possible values people will use to label something as not known (for both name and category), you can skip their labelling all at once. A similar case holds for image segmentation, you don't have to label background, rather you label all areas of interest, and the model will automatically learn to label everything that is not of interest as background. In your case, the model will learn only name and category as entities and it will ignore everything else (or label everything else as 'non entity', depending on your code implementation).


text:
can't find a viable import class for keras.utils.Sequence
I am using Google Colab. tensorflow version = 2.8.0, and keras is the same.  I am trying to get a BalancedDataGenerator(Sequence) class created, but I can't get keras.utils.Sequence to load.
from tensorflow.python.keras.utils import Sequence
from tensorflow.python.keras.utils.np_utils import Sequence
from tensorflow.python.keras.utils.all_utils import Sequence

I've tried it taking out "python", or taking out "tensorflow.python", or searching as to where it is now currently located, but haven't found it.
The errors I get are:

ImportError: cannot import name 'Sequence' from
'tensorflow.python.keras.utils'
(/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/utils/init.py)
AttributeError: module 'keras.utils' has no attribute 'Sequence'
ImportError: cannot import name 'Sequence' from 'keras.utils'
(/usr/local/lib/python3.7/dist-packages/keras/utils/init.py)
ImportError: cannot import name 'Sequence' from
'keras.utils.all_utils'
(/usr/local/lib/python3.7/dist-packages/keras/utils/all_utils.py)

NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

When I go /usr/local/lib/python3.7/dist-packages/keras/utils and read all_utils.py I see 'from keras.utils.data_utils import Sequence' as an option and it says that this all_utils.py module is 'used as a shortcut to access all the symbols.  Those symbols werd exposed under init, and was causing some hourglass import issue.'
If I read data_utils.py I do see the Sequence module inside there.
if I type from 'keras.utils.data_utils import Sequence' in a cell and run it it looks like it was accepted.  However, if I put that in the balanceddatagenerator.py and import the class I created that uses Sequence, and then import the class I get this error.  I don't know how to overcome this.
How get this installed and imported successfully?

I just had an error message that indicated that "from imblearn.keras import balanced_batch_generator" was not finding Sequence in keras.utils.Sequence.  And I know that keras.utils.data_utils.Sequence is where it is located.  So, it looks like since I have an updated version of Keras, that they put Sequence in a different place. Why they seem to keep moving Sequence's location, I don't know, but it is causing problems between versions.

I just found that imblearn also has a tensorflow version of balanced_batch_generator, so switch to tensorflow.keras.utils.Sequence and use the imblearn.tensorflow.balanced_batch_generator, and it seems to import without any errors.  Now let's see if I can finally make progress.

Hi Jennifer. Welcome to our community! It seems that you were having a programming issue. This type of question is off-topic and better asked on Stack Overflow, even though it's in the context of AI. We focus on the theoretical, philosophical and social aspects of AI. See https://ai.stackexchange.com/help/on-topic

That is funny, @nbro, I was told over there that I should post all my Machine Learning and Data Science questions here in Stackexchange.

Some people think that our site also covers programming issues, but that's not the case. Programming questions should really be asked on Stack Overflow. There you will find many programming questions related e.g. to Keras and TensorFlow.

imblearn.keras.balanced_batch_generator was pointing to an old version of keras.utils.Sequence and causing this error.
I just found that imblearn also has a tensorflow version of balanced_batch_generator, so I switched to tensorflow.keras.utils.Sequence
and the imblearn.tensorflow.balanced_batch_generator, and it seems to import without any errors.
Now let's see if I can finally make progress.


text:
Weights initialization once the Neural Network is trained
I am trying to understand how weights are initialized in a Neural Network using Keras deep learning framework and what happens if I train a Neural Network and then I want to train it again: are the weights of the previous training stored in some way and are the weights of the same Neural Network in the new training initialized based on the latest training ?
By "one training" I mean all the updates that the weights have received after all the epochs that are set.
To tell all the story: so far I have made my analysis assuming that with each new training (leaving the neural network architecture unchanged) the weights were initialized without taking into account the previous training. The concern come out when I wanted to use K-fold cross-validation procedure for which the dataset is split into K parts and K-1 parts are used for training while the remaining is left for testing. Iteratively this method uses all the K parts both for training and for testing. It is advised to do this in case of few data because in this way you can train the model K times each time with a different train/test split and you can produce significantly better estimates.
This can be achieved with the following code (I am dealing with natural language processing and so the code is based on those kinds of tasks):

#Whatever is needed to prepare data to be fed to our Neural Network

....

#Build keras model and fit

classes=2 # 3 or 4

embedding_dim = 10

X_train, X_test, y_train, y_test = train_test_split(padded_sequences, Y, test_size=.2)

# Define per-fold score containers
acc_per_fold = []
loss_per_fold = []

# Merge inputs and targets
inputs = np.concatenate((X_train, X_test), axis=0)
targets = np.concatenate((y_train, y_test), axis=0)

num_folds=3

# Define the K-fold Cross Validator
kfold = KFold(n_splits=num_folds, shuffle=True)

# K-fold Cross Validation model evaluation
fold_no = 1
for train, test in kfold.split(inputs, targets):
    model = Sequential()#1.Define a model
    model.add(Embedding(len(tokenizer.word_index)+1, embedding_dim, input_length=max_length, name='embeddings'))
    model.add(LSTM(64))
    model.add(Dense(1,activation='sigmoid'))
    #Dense(classes,activation='softmax') for 4 classes
    print(model.summary())

    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
#sparse_categorical_crossentropy for 4 classes

    # Generate a print
    print('------------------------------------------------------------------------')
    print(f'Training for fold {fold_no} ...')

    model.fit(X_train, y_train,  batch_size=128,  validation_data=(X_test, y_test),verbose=2, epochs=15)

    # Generate generalization metrics
    scores = model.evaluate(X_test,y_test,verbose=0)
    print(f'Score for fold {fold_no}: {model.metrics_names[0]} of {scores[0]}; {model.metrics_names[1]} of {scores[1]*100}%')
    acc_per_fold.append(scores[1] * 100)
    loss_per_fold.append(scores[0])

    # Increase fold number
    fold_no = fold_no + 1

After the first 15 epochs in which the Neural Network is trained on the first fold, the Neural Network is then trained on the second and again after 15 epochs it is trained on the third.
So, as said before, we do this because we can train the model K times each time with a different train/test split and you can produce significantly better estimates. So this assumes that after the first 15 epochs for the training on the first set of data, the weights are just updated towards better estimates in the second run of 15 epochs for the training on the second set of data and so on ?
In other words: if training K times is better than training once (in case of few data and under the limit of no-overfitting), does this mean that in some way the information of the previous training is stored and used for the second training and so on in order to be optimized more and more ?
In case of my specific example, I am interested in the weights of the embedding layer to have dense vector representation of my sequence of symbols. So I want to understand this for all the weights of a Neural Network in general but particularly on the embedding layers weights (I think that it is the same story for all the weights of all layers of the NN).
Anyway, besides the cross-validation procedure, if some kind of information is stored and kept from one training to the other, does it mean that each time I run my Neural Network training, this is influenced by the previous training ? For example, if I train this simple NN:
# define model
model = Sequential()
model.add(Dense(10, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# fit the model
model.fit(X_train, y_train, epochs=150, batch_size=32, verbose=0)

First on a set of data and then on an other, in the second training with the new set of data, the weights are initialized on the basis of the previous training ?
On one side, this seems to me to have no sense and not to be possible, but on the other if it is not so, I do not find the meaning of using cross-validation technique (at least in the way it is done using Keras in the code I attached).
I tried to search what is the default initializer in Keras and I found this thread in Stack Overflow What is the default weight initializer in Keras?. I can find all the ways to initialize them but I am not able to find explanations about if these initializers take into account information from the previous trainings.
For example, as suggested in the answer to Stack Overflow question,  for most of the layers, such as Dense, convolution and RNN layers, the default kernel initializer is 'glorot_uniform' and the documentation says that:

Draws samples from a uniform distribution within [-limit, limit], where limit = sqrt(6 / (fan_in + fan_out)) (fan_in is the number of input units in the weight tensor and fan_out is the number of output units).

But is this uniform distribution conditioned by the weights of the previous trainings ? Or by default this is not taken into account and there is some parameter to set to consider the previous  ones ?
I think I miss something of important but I hope to have been clear and I thank you in advice.
Note: I took the code and emphasized sentence about cross-validation from this blog post: How to use K-fold Cross Validation with TensorFlow 2 and Keras?

This question seems to be about how 1-2 libraries implement certain functionalities, i.e. k-fold cross-validation and weight initialization. Questions about libraries and how they implement things are off-topic here and they are better suited for Stack Overflow. We focus on the theoretical aspects of AI. Your question could be made on-topic if you don't focus on Keras or sklearn, but ask a general question about k-fold cross-validation and how it's usually done. That seems to be the crux of your post anyway. You can leave the code if you think it's helpful to answer the theoretical question.

By the way, welcome to our site! If you have some time, it might be a good idea to read https://ai.stackexchange.com/help/on-topic.

@nbro I think that the question falls under "Implementation questions in the context of understanding the theoretical topics are on-topic", as described in our on-topic page. OP seems to have some methodology questions regarding the relation of CV and weight initialization, and they use Keras just to demonstrate them. FWIW, I can only testify that this question would be closed as off-topic in Stack Overflow (it is not about programming).

Maybe this would be more appropriate for Data Science SE, but with so many partially overlapping SE sites, no wonder new users feel disoriented regarding which one is more appropriate for their question(s). Still, I don't think it is off-topic here (otherwise of course I would not have answered it).

@desertnaut Yes, I agree that this question could fall into the context of "understanding the theoretical topics", unless the OP wants to understand the details of the software libraries, so I think we should leave it open (for now). As a rule of thumb, though, I usually say that, if you can ask your question without any code, then it's more like your question is on-topic here. I also agree that the overlap between our sites can be a mess. The sites should collaborate more in order for them to focus more on specific types of questions.

Thank you for the welcome @nbro. Since the Keras tag was available, I thought that I could do this question with code. And since my question is about specificities of neural networks (or better also CV in context of neural network ), I chose AI SE to ask it.

@Manuela Ok. Just keep in mind that programming questions are off-topic here. We focus on the theoretical/conceptual problems.

Regarding your first code snippet, there is no weight storing or continuation of training between the different CV folds whatsoever: each model is trained anew with the respective training data of each fold and validated on the validation data. Notice that this is exactly the idea behind cross validation - models trained on different folds are completely independent and they do not "communicate" in any manner. Should the models of the different sub-folds "communicated" and shared information between them, like, say, each model start training from the point the previous one finished, we would not talk about CV anymore (notice that such an approach could be valid if you purpose is something different than CV, but is is not a valid CV procedure).
The situation is slightly different in your 2nd code snippet; after it finishes, you could go on, either by running more epochs on the same data, or even training with different data. All you would need is to simply add model.fit statements sequentially:
# your 2nd snippet already run
# you can either continue with, say, 50 more epochs:
model.fit(X_train, y_train, epochs=50, batch_size=32, verbose=0)

# or continue training the model with different data, say X_train2 & y_train2:
model.fit(X_train2, y_train2, epochs=150, batch_size=32, verbose=0)

# of course you can do both, sequentially

What is the difference between your two code examples? In the first one, your model is redefined each time the loop executes; this defines a new model each time with the weights re-initialised, so no continuation of training. And, as already said, this is exactly the correct approach for cross validation.
But in the 2nd case, you have a single model, which you can just keep training sequentially (either with the same or even with different data, as shown above).
There are even cases where you would want to continue training the model outside of the current script; in such cases, you would save the model, and load it again later (or in another script altogether) in order to continue training it. This will work OK as long as you do not proceed to redefine/reinitalise the model. You can have a look at the documentation on Save and load Keras models for more.

I am not able to find explanations about if these initializers take into account information from the previous trainings.

They most certainly don't; if there is already existing information from previous runs (like in the cases I describe above), we do not even talk about weight initialization, i.e. there is no need for it - we already have the "initial" weights from the previous run. Random weight initialization in the sense discussed in the Stack Overflow thread you have linked above makes sense only for new models, like the ones produced in each run of the loop in your first code snippet above.

Thank you for the answer ! So I didn't understand CV. Thus its purpose is to have a better and more reliable estimate of the accuracy of the model and not also to train del model on a larger dataset? You just "enlarge" the dataset for the testing and not for training in practice ? While, instead, using the second strategy adding model.fit, you enlarge also the dataset for training and the NN both trains on more examples and tests on more examples...

@Manuela More or less, yes and yes. There are at least two different reasons to use CV. The 1st is essentially what you describe (get an estimate of the final model performance); the 2nd is for hyperparameter tuning, when it is combined with a grid search. And as is, your 2nd code snippet has nothing to do with CV.


text:
At what point are MCTS results discarded in AlphaZero Training?
Regarding the AlphaZero paper, it is not clear to me when the Monte Carlo Tree Search (MCTS) results will be cleaned up.
I assume this has to happen at some point, since mixing results could lead to lower quality results? Imagine in the self-play the Neural Network (NN) is updated to a new version and evaluates certain patterns differently by detecting a new trick. Many iterations must follow to outperform the old best choice (visit-count). I imagine discarding old MCTS results should be done about between an episode and the next NN weight updates.
I feel that a wrong decision here could have a strong negative impact on the overall learning process.

You're right, it would not be great to keep training on games played early during the selfplay process, since they would just hurt the network playing strength.
The AlphaZero paper (free access) itself does not elaborate, but it has this sneaky sentence:

Unless
otherwise specified, the training and search algorithm and parameters are identical to AlphaGo Zero

The AlphaGo Zero paper (free access) is more helpful:

Optimization: [...] Each minibatch of data is sampled uniformly at random from all positions of the most recent 500,000 games of selfplay.

For context, in total AlphaGoZero played 4.9 million games, so the buffer rotated about 10 times.
As another example, KataGo started with a small buffer early in training and then gradually grew it:

Samples are drawn uniformly from a growing moving window of the most recent data, with window size beginning at 250,000 samples and increasing to about 22 million by the end of the main run. See Appendix C for details.

(note that this is counting positions, not games like the AlphaZero paper)


text:
Is there a standard term for the following flaw in the data?
I wonder if following characteristic of data has some standard "professional" or scientific term associated with it.
Let's assume that I have a set of dog/cat images labeled 0 for a cat and 1 for a dog. My purpose is to extract (classify) as many true dog images as possible, with the smallest amount of non-dog images. But the problem with data is that:

some dogs images are labeled as 0
some cats images are labeled as 1
some images are of other animals or non-animal objects and can have both labels 1 and 0

Is there any specific term describing such cases, or they are  just "noisy" data?

This is referred to as label noise (or noisy labels), and it is indeed different from just "noisy data"; quoting from the 2014 paper Classiﬁcation in the Presence of Label Noise: a Survey:

In this survey,
it is assumed that each training sample is associated with an
observed label. This label often corresponds to the true class of
the sample, but it may be subjected to a noise process before
being presented to the learning algorithm. It is therefore
important to distinguish the true class of an instance from its
observed label. The process which pollutes labels is called
label noise and must be separated from feature (or attribute)
noise which affects the value of features.

You may also find the (preprint) paper Label Noise Types and Their Effects on Deep Learning useful; there, the authors propose a taxonomy of different types of label noise:

Label noise types can be subdivided into
three main groups as follows.
• Uniform noise: Flipping probability of label from its
true class to any other class is equally distributed. Many
works in literature use synthetic uniform label noise by
just flipping labels randomly for a given percentage of
data instances [21]–[24].
• Class-dependent noise: Flipping probability of label
depends on the true class of the data instance. This
is mostly represented by a confusion matrix and can
be designed in different ways. The easiest way is to
attain inter-class transition probabilities just random [25],
so that there is still class dependence since transition
probabilities are given according to classes but without
any correlation to class similarities. In a more structured
way, noise transition matrix can be designed in a way that
similar classes have a bigger probability to be flipped to
each other [26]–[30]. Some works use pairwise noise,
in which transition from one class can only be defined
to one another class [31]–[35]. Work of [36] checks the
popularity of classes and constructs transition matrix so
that mislabeling happens from popular class to unpopular
class or vice versa.
• Feature-dependent noise: The probability of mislabeling
depends on features of instances. In order to generate
feature-dependent noise, features of each instance should
be extracted, and their similarities to other instances from
different classes should be evaluated. Unlike uniform and
class-dependent noise, there are much fewer implemen-
tations of synthetic feature-dependent label noise. One
particular work in this field is [37], where data is clustered
with the kNN algorithm, and labels are flipped randomly
for clusters of data. This method provides concentrated
noise in the feature space. But, this type of synthetic noise
doesn’t evaluate the instance similarities and therefore
different from our proposed approach. Alternatively, in
case there is a surrounding text for each image in the
dataset, some works create noisy labels from the interpretations of these texts [38]–[41], assuming surrounding
texts are related to features of data. But this approach is
restricted to datasets with surrounding user-defined texts,
which is not the case for most of the time.

Other possibly useful resources on the subject:

A Comprehensive Introduction to Label Noise (conference paper, 2014)
A Little Talk on Label Noise (blog post, 2018)
Data Noise and Label Noise in Machine Learning (blog post, 2021)


text:
YOLO - does the Intersection over Union is actually a part of Non Maximum Suppresion
In the Stack Overflow thread Intersection Over Union (IOU) ground truth in YOLO they say that in YOLO actually the IoU (intersection over union) is used twice:

during training to compare ground truth box to predicted box

during the usage of already trained YOLO network this technique is being used to eliminate overlapping boxes which include same object many times.

As far as I know eliminating overlapping boxes is being done by process called Non Maximum Suppression (NMS). Thus I wonder maybe the IOU is a part of a NMS process?

IoU is used as an threshold parameter for NMS. You just need to choose how much common space two boxes have to have to be considered as overlapping and to merge them. Details are in this link.

Thank you sir!
p.s I have asked here a few more questions about YOLO which I am trying now to use in my research. I would be more than glad if you could help me there also.

IoU is way to measure how much two areas overlap. As said in the comments IoU is used as a threshold parameter for NMS, so You could decide how much common space two areas have to have to be considered as overlapping.

According to first YoLo paper IoU is used first time to calculate confidence score for each bounding box according to the formula:

Confidence is 5th parameter in prediction vector: (x, y, w, h, c) predicted for every bounding box and it tells how good the bounding box is according to ground truth. Thanks to usage of IoU, confidence gives the full information about bounding box position, dimensions and its class probability.

Using NMS algorithm to merge overlapping boxes is very popular technique in object detection algorithms. However in original YoLo architecture You can see that output is a tensor of SxSx(B*5+C) where SxS are grid dimensions, B is number of bounding boxes per grid and C is number of possible classes. 5 is there because it's the length of prediction vector. It means that output tensor is constant, and You have to analyze the results it gives by yourself.

Authors claim that

Non-maximal suppression can be used to fix these multiple detections. While not critical to performance as it is for R-CNN or DPM,
non-maximal suppression adds 2- 3% in mAP.

Reading the VOC 2007 Error Analysis section of the Yolo paper also might be useful.
Hope it helps.


text:
How do you interpret this train vs test accuracy scores? is the model under or over fitting?

What does this difference in train and test accuracy mean?

For the Logistic Regression Classifier and SVC, the train_accuracy and test_accuracy are very similar; thus, there is no evidence for over or under fitting.  However, the KNN, shows a train_accuracy that is lower than your test_accuracy, which suggests overfitting.  Alternatively, depending on your sample size, it is entirely possible that this variation is within the expected range of error.
In machine learning, the model is trained on one dataset (i.e., the training data), but performing model evaluation (i.e., estimate "real world" performance by calculating accuracy or some other metric) on the same dataset that you trained on would be biased.  Instead, the evaluation is performed on an independent identically distributed dataset (i.e. the test data) to get an unbiased estimate of model performance.  However, it is still important to compare accuracy (or whatever performance metric you choose) between training and testing to assess for over- or underfitting.
Overfitting means that your model "fit" the training data very well and achieves high accuracy on that data.  However, the model performs poorly (or less well) on the test data.  This is because the model not only "fits" the signal in the training data but also the noise.  The problem is that noise is random and it will surely be different in the test set.  Therefore, the model doesn't generalize well to other datasets.
Underfitting means that the model performs poorly on both datasets.  This is typically because the model does not have the "expressivity" or "capacity" to learn the signals within the data or that the data is random and has no "signal" to speak of (and, therefore, nothing from which to learn).

From the roc_auc metric it does not show the model is in the overfit zone.
It tells that on average of 10 predictions 8 predictions could be right and  2 predictions could go wrong.
Now , it depends on the application domain to decide the accuracy you are expecting from the model

Actually, in the strict sense, you cannot decide overfitting here, because overfitting is tested using losses, not metrics like accuracy.

For the downvote, why? This is correct :)

To my knowledge overfitting is a phenomenon that's unbound to any metric - it only means that your model is overly specific to the training data. You can identify an overfitted model with accuracy as well as with loss, no?

@Chillston no, the definition is to only use loss (since that is what the model is being trained on), not any metric, because metrics measure different things.

Interesting, do you have an intuition why this is defined so strictly? Where is the definition from? Not trying to be cheeky - just interested because its not super intuitive to me.


text:
What does that mean if my precision, F1-score are very high, but my ROC AUC score is around 0.5?

What does it mean when my precision and so on are so high and the roc auc score is around 0.5?

How many examples of each category do you have?

do you mean sample size?

Broken down by category…I’m curious about how much imbalance you have in your data.

i think i did something wrong. sorry for the disturbance

If you are certain that your calculations are correct, then there may be class imbalance in your dataset.  The metrics precision, f1_score, and accuracy are dominated by the model's performance on the major class in your dataset.
For example, if your dataset has $99$ examples of the positive class and $1$ example of the negative class and your model predicts positive $100$% of the time (no matter what), then

$Precision = \frac{TP}{(TP + FP)} = \frac{99}{(99 + 1)} = \frac{99}{100}$
$F_1 score = 2\times\frac{TP}{(2 \times TP + FP + FN)} = 2 \times \frac{99}{(2 \times 99 + 1 + 0)} = 198/199$
$Accuracy = \frac{(TP + TN)}{(TP + TN + FP + FN)} = \frac{(99 + 0)}{(99 + 0 + 1 + 0)} = \frac{99}{100}$

These give a false impression that your model is performing very well, but in fact, all it is doing is calling every example positive. This is why it is imperative to have a clear understanding of your performance metrics, what they mean, and what their limitations are.  In general, it is also a good idea to examine multiple performance metrics, if appropriate.
In this example, specificity will reveal problems in the model and/or data.

$Specificity = \frac{TN}{(TN + FP)} = \frac{0}{(0 + 1)} = \frac{0}{1}$

Given all possible positive-negative example pairs in your dataset, the roc_auc is the proportion of those pairs for which the model correctly identifies the positive example.  Thus, the AUC is more sensitive to class imbalance than those other metrics.
Your results also indicates that your model is not doing any better than the flip of a coin because an AUC of $0.5$ indicates the expected performance of a dummy (naïve) classifier.


text:
How to incorporate domain knowledge into a semantic segmentation network?
I'm working on a semantic segmentation project, and want to add some domain knowledge to the system. I want to ensure that for segmentation, there can only be one group of pixels that are predicted as a certain class. An example would be segmentation of the face. When we want to segment different parts of the face, assuming regular faces, we have the following domain knowledge: one group of pixels can be predicted as the mouth; one group can be predicted as the nose; two groups of pixels can be predicted as the eyes.
A possible solution I currently think of is as follows:
Suppose we find two groups of pixels, both groups having pixels with the highest probability for being the nose. Then, we would take the average score for each pixel group to obtain two average scores. Then we compute the probabilities again and select the group with the highest probability to be the nose. The group of pixels having the lower probability will then be predicted to be the class having the second best probability. Doing it in this way, we apply some post-processing (look at scores and determine what is our final prediction, using some logic). However, it does not seem to be learned by the neural network (or maybe it is learned implicitly, by looking at the final predicted class obtained from the postprocessing and updating the weights accordingly).
Are there any solutions, or could you point me to any papers which describe this problem and add domain knowledge to the neural network such that it incorporates this information and becomes better at predicting?

You could come up with a loss function which describes your constraints.
PS: your example with the face recognition (where a CNN would find eyes no matter how nonsensical they are placed) is probably solved by visual transformers

Thank you very much for your answer. Do you also have an example of the loss function? I currently think of counting the number of groups found and applying a penalty if the number of groups exceeds the allowed number. However, is this the most viable strategy?
Further, regarding visual transformers, they are very interesting. However, one of the constraints is also that we should be able to run inference in real time. From what I know, ViTs are currently not capable of doing that (yet).


text:
Can an AI have awareness?
I was thinking about the following:
According to Sir Roger Penrose "No computer has any awareness of what it does.".
Now some context to his statement:
Penrose’s argument in summary in his book (The emperor's new mind: concerning computers, minds, and the laws of physics) is that:

We don’t understand physics well enough to use it to describe our brains.
We don’t understand the mind well enough to create a framework capable of accommodating human consciousness.
Since our minds do not operate ‘computationally’ and are non-algorithmic, our intelligence, therefore, can’t be recreated by computers (Source).

Let's look at the latest controversy started by a Google engineer who claimed that Lambda is self aware. But, then I came across Searle’s Chinese Room and realized that Lambda is not self aware.

Then there are other self learning AIs like OpenAIFive that was really good in playing DOTA2.
But how do we know this AI understands what it's doing? If not then is there a possibility that a hypothetical AGI in future might be aware of its actions?
As a note I will add that the brain is still a big mystery and there's no unanimous  consensus on a single definition of consciousness. And I think every definition out there is just a combination of thought and faith/belief. And Penrose as of lately does however thinks that there might be a connection between consciousness and quantum mechanics.

Are kids conscious?
I always say that if a human can do it, and AI will be able to do it. There is nothing special to us: no magic, no soul. We will eventually crack intelligence. I personally don't even bother with the term "conscious". It doesn't mean much.

Whilst I agree with the sentiment, this answer doesn't really engage with the question as asked. Stack Exchange is not a discussion forum, and we're not looking for opinions.

@NeilSlater Aren't you asking in the wrong place then?

I am not asking anything. The OP has given references that they are trying to understand and resolve, and a good answer would reference and/or address them, or bring up equivalent references that the OP could use to understand the subject further. This answer so far expresses a personal opinion. I don't disagree with it, but I think it is not a good quality answer for our site in its current form.

You are assuming functionalism. So this answer is just an opinion. I know this is an extremely popular viewpoint, especially among scientists, but you shouldn't make the mistake to think somehow that it's a "proven fact" or anything of the sorts.


text:
CNN leaf segmentation throught classification of edges how to improve
I am trying to design a CNN that can do pixel wise segmentation of edges leaves in dense foliage agriculture images. Such as these:

On the basis of this article https://arxiv.org/pdf/1904.03124.pdf, two classes are defined, such as the external contours and the internal contours of the leaves boundaries. In addition, a multi-scale approach is used trough a unet like architecture and a auxiliary loss was used to learn the edge detection at different scales (which corresponds to the main idea of this article https://arxiv.org/pdf/1804.01646.pdf). I learn the network using a mIoU loss whose weight varies depending on the class and the scale. Finally, my last activation layer is a clipped ReLU. The results are starting to be good :

However, the network is not able to reconnect some internal edges. The following image shows a broken inner edge that does not continue to the next part (in blue) :

So I'm looking for some paper, git, codes, whatever that can improve the reconstruction of missing edges (inside or outside the CNN).