The most used loss function in tensorflow for a binary classification? I am working on a binary classification problem using CNN model, the model designed using tensorflow framework, in most GitHub projects that I saw, they use "softmax cross entropy with logits" v1 and v2 as loss function, my questions are:

1- Why this loss method is the most used one?

2- What is the type of this function because of the name I am confused about its type.

3- What is the equation for the function? in tensorflow website, the equation is not available.

I think there is some confusion here. Softmax is usually an activation function which you will use in your output layer, and cross-entropy is the loss function that you will use.

Softmax

This activation function outputs the probability for each class, it is defined as

$\sigma(\bf{y_i}) = \frac{e^{\bf{y_i}}}{\sum_{j}{e^{\bf{y_j}}}}$.

For example, in a problem with 2 class labels. Then if we have some outputs from our neural network like $y$=[3, 4] then we can get the probability of each output classes by using the softmax function on these outputs.

import numpy as np

x = [3, 4]
np.exp(x)/np.sum(np.exp(x))

  [0.26894142, 0.73105858]

You will see that these are probabilities and do sum to 1.

Then we can get the class of the input by seeing which probability is higher. In this case class 2 has a probability of 73.11%, so the predicted class label, $\hat{y} = 1$.

Cross-entropy

Cross-entropy is a loss function that is used for classification tasks. For binary classification it is defined as

$H(p, q) = -y\log(p) - (1-y)\log(1-p)$.

Let's assume that the real class of the above example is 0, $y=0$. Then we made a mistake and you can see that

$H(p, q) = -0\log(0.26894142) - (1-0)\log(1-0.26894142) = 0.313$.

That is the loss that is used for backpropagation.


How to code a simple forward propagation of recurrent neural networks? I know the theory behind recurrent neural networks or RNN but I am confused about its implementation. This is an rnn equation I got from the web,

I tried to code the forward propagation alone in python's numpy

import numpy as np

outputs = 5
inputs = 3

# Input value
# (batch_size,seq_len, vocab_len)
X = np.ones((10,3,3))

# Initializing rnn weights and hidden states
Wxh = np.random.rand(outputs,inputs)
Whh = np.random.rand(outputs,inputs)
Why = np.random.rand(outputs,inputs)
h = np.zeros((1,inputs))

# Forward propagation
def rnn(x,h):
    h = np.tanh(np.dot(Whh,h.T) + np.dot(Wxh,x.T))
    y = np.dot(Why,h.T)
    return y,h

for i in X:
    _,h = rnn(i,h)

But I get a broadcasting error. How do we implement the forward propogation of rnn?

In other words, what does the forward pass of a RNN look like. You read about using the inputs plus values from the previous node (here it will be prev_s)
First initialise the weights, than perform the foreward pass. I highlighted what you was looking for.

U = np.random.uniform(0, 1, (hidden_dim, T))
W = np.random.uniform(0, 1, (hidden_dim, hidden_dim))
V = np.random.uniform(0, 1, (output_dim, hidden_dim))

 for i in range(Y.shape[0]):
        x, y = X[i], Y[i]

        layers = []
        prev_s = np.zeros((hidden_dim, 1))
        dU = np.zeros(U.shape)
        dV = np.zeros(V.shape)
        dW = np.zeros(W.shape)

        dU_t = np.zeros(U.shape)
        dV_t = np.zeros(V.shape)
        dW_t = np.zeros(W.shape)

        dU_i = np.zeros(U.shape)
        dW_i = np.zeros(W.shape)

        # forward pass
        for t in range(T):
            new_input = np.zeros(x.shape)
            new_input[t] = x[t]
            mulu = np.dot(U, new_input)
            mulw = np.dot(W, prev_s)
            add = mulw + mulu
            s = sigmoid(add)
            ***mulv = np.dot(V, s)***
            layers.append({'s':s, 'prev_s':prev_s})
            prev_s = s

So the '*  *' area can be roughly translated: mulv = np.dot(V, s) are the weights multiplied with the current state. (same as before, s==input_vector) but the difference is that the s will be calculated with weights from previous output and current input i.e.

mulu = np.dot(U, new_input)
mulw = np.dot(W, prev_s)
add = mulw + mulu
s = sigmoid(add)

Thats why we have 3 initial weights in the first place.


What does it mean to have 1 degree of freedom in ANOVA test? So I used python to run multi-factorial ANOVA analysis on a data set. I first used a ols.fit() and then the anova_lm function. I realized for the variables I am analyzing their degree of freedom is 1. Does that mean only 1 value out of my data is extracted and used for calculation? Why is the residual df so high?

import pandas as pd
from statsmodels.multivariate.manova import MANOVA
import statsmodels.api as sm
from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm
from statsmodels.graphics.factorplots import interaction_plot
import matplotlib.pyplot as plt
from scipy import stats

#Some quick df transformation
#We want to analyze the candy sales numbers with respect to the flavors

formula = 'Sales ~ Mango+Raspberry+Chocolate+Dark_Chocolate+Ice_cream+Cherry'

model = ols(formula, df).fit()

aov_table = anova_lm(model_MS_ro, typ=2)

print(aov_table)

****ANOVA Results****

                      df    sum_sq   mean_sq          F    PR(>F)
Mango                1.0  0.008512  0.008512   2.325284  0.130999
Raspberry            1.0  0.006025  0.006025   1.645954  0.202998
Chocolate            1.0  0.049506  0.049506  13.524418  0.000412
Dark_Chocolate       1.0  0.007233  0.007233   1.976095  0.163447
Ice_cream            1.0  0.018032  0.018032   4.926093  0.029117
Cherry               1.0  0.024460  0.024460   6.682116  0.011444
Residual            85.0  0.311140  0.003660        NaN       NaN

Well having datasets handy would have made it easier to do it myself and explain better.

But let me try to help you get started assuming you are asking to interpret the results rather than trying to solve model/platform/library specific implementation, if not don't bother reading ahead.

As I understood its the linear fitting model here which you are calling with 2-way ANOVA test.So in Linear models each independent variable uses 1 degree of freedom, if there are 6 then 6 DF. Rest are just actually number of observations available which it shows in residual/error. That's why the sum_sq and mean_sq values are same in your output for variables but different for residual.

I found it much more easier to try 2-Way ANOVA  in excel first and trying to interpret the results. please try this link with excellent explanation I found long back-

https://statisticsbyjim.com/anova/two-way-anova-excel/

https://statisticsbyjim.com/hypothesis-testing/degrees-freedom-statistics/

Few more links which clearly explains-

https://support.minitab.com/en-us/minitab-express/1/help-and-how-to/modeling-statistics/anova/how-to/two-way-anova/methods-and-formulas/methods-and-formulas/

https://www.graphpad.com/support/faq/the-anova-table-ss-and-df-in-two-way-anova/

https://people.richland.edu/james/ictcm/2004/twoway.html

And If you are statistically challenged like me and have to brush up to use it effectively in ML, please follow these-

https://www.youtube.com/watch?v=NF5_btOaCig


Is a multi-layer perceptron exactly the same as a simple fully connected neural network? I've been learning a little about StyleGans lately and somebody told me that a Multi-Layer Perceptron, MLP, is used in parts of the architecture for transforming noise. When I saw this person's code, it just looked like a normal 8-layer fully connected network (i.e. linear-->relu-->linear-->relu-->...)
Last year, I read Hands-on Machine Learning with Scikit-Learn, Keras, and TensorFlow 2 by Aurelien Geron and he talks about MLPs. When I read about it, I interpreted his description as that an MLP is not exactly the same as a vanilla fully connected neural network. I didn't fully understand the text and don't have the book anymore so, unfortunately, can't recall exactly what I read so I might have been completely wrong in my understanding of what he wrote.
Is an MLP the same thing as very basic fully connected network?

Yes, a multilayer perceptron is just a collection of interleaved fully connected layers and non-linearities.
The usual non-linearity nowadays is ReLU, but in the past sigmoid and tanh non-linearities were also used.
In the book, the MLP is described this way:

An MLP is composed of one (passthrough) input layer, one or more layers of TLUs,
called hidden layers, and one final layer of TLUs called the output layer (see
Figure 10-7). The layers close to the input layer are usually called the lower layers, and the ones close to the outputs are usually called the upper layers. Every layer except the output layer includes a bias neuron and is fully connected to the next layer.

("TLU" stands for threshold logic unit)


How to make our own environments for reinforcement learning? How can we make our own environments for reinforcement learning? I have a heavy doubt that a game engine is used.. And if a game engine is used, which game engine is used that can be downloaded for free?

Classical reinforcement learning (e.g. vanilla Q-learning) is not normally applied to games. There are some environments to play (and do research) on classical RL benchmarks (e.g. balancing pole), like OpenAI Gym. It can be extended to your own benchmark environments.
Games, nevertheless, are a standard testbed for deep reinforcement learning algorithms. Probably the most popular approach is to use Atari games. I believe they were popularized among the RL practitioners after the landmark article Playing Atari with Deep Reinforcement Learning by DeepMind. You can find multiple implementations of RL environments on top of the Atari emulator in github (e.g. this and this).
Another popular deep RL game-based testing environment is Starcraft. DeepMind open-sourced their Starcraft RL environment.


Flipping the labels in a classification problem Let us say A- we have a binary classifier with labels 1 as healthy and 0 as sick. The precision we got is 100% and the recall is 70%.
Now let us say B-we flip the labels where 0 is healthy and 1 as sick.
Are Precision and recall  get flipped in their values if you flip the labels? So in the new case, recall is 100% and precision is 70%?
Or the (70,100) values in the first case belonged to class 1 aka, healthy people and I should calculate the precision and recall for class 0?
My understanding was that  we were able to recall 70% of sick people and out of every people who we detected as sick, no person was healthy (precision as 100%).  But I am confused now

To begin with, I don't quite get it why you want to flip them. In the binary case, you flip Negatives and Positive, so True Negative becomes True Positive and so do FP/FN. Hence you flip specificity/true negative and sensitivity/recall values, so overall accuracy and F1 stay the same.


hidden layer weights calculation I've been working on neural network for a while and I built simple network from scratch with python but before using TensorFlow, I would like to have a complete understanding of it.

Here is my question :

Lets say you have 3 layers you have 3 weights to update :

1) --> the weight between the outputlayer and the hiddenLayer2

2) -->  the weight between the hiddenLayer2 and the hiddenLayer1

3) -->  the weight between the hiddenLayer1 and the inputLayer

For the 1) the calculation is quite simple we got :

weight_3 += LEARNING_RATE * ((2*(target - output)) * sigmoid'(output) * layer2)

For the 2) the calculation is more complicated  and we got :

weight_2 += LEARNING_RATE * ((2*(target - output)) * sigmoid'(output) * weight_3) * sigmoid'(hiddenLayer2)

I need help for the 3rd part, I tried to calculate and find on internet but not a lot of people uses 2 hidden layer when they work from scratch.

I also tried to resolve the chain rule but its too long and I can't resolve.

Does someone know the formula to get the weight between the hiddenLayer1 and the inputLayer ?

Thank you so much in advance

Here is a good example of how to implement forward and backward propagation, in numpy, but there should be similar functions in tf.


Equivalence of Tidy Data and Third Normal Form In Hadley Wickham's "Tidy Data" paper, he states that

  In tidy data:

  Each variable forms a column.
  Each observation forms a row.
  Each type of observational unit forms a table.

  This is Codd’s 3rd normal form (Codd 1990), but with the constraints
  framed in statistical language, and the focus put on a single dataset
  rather than the many connected datasets common in relational
  databases.

Codd's Third Normal Form can be described as:

A table is in 1st normal form if

It stores information in rows and columns where one or more columns, called the primary key, uniquely identify each row.
Each column contains atomic values, and there are no repeating groups of columns.

A table is in 2nd normal form if

The table is in 1st normal form, and
All the non-key columns are dependent on the table’s primary key.

A table is in 3rd normal form if

It is in 2nd normal form, and
It contains only columns that are non-transitively dependent on the primary key

I am trying to understand how these two sets of rules are equivalent.

I believe that the first Tidy Data rule maps to 1st normal form.  Specifically, "Each variable forms a column" maps to "Each column contains atomic values, and there are no repeating groups of columns".

I believe the Tidy Data rule "Each observation forms a row" maps to "All the non-key columns are dependent on the table’s primary key" and also "It contains only columns that are non-transitively dependent on the primary key".

I suspect that "Each type of observational unit forms a table" maps to "The table stores information in rows and columns where one or more columns, called the primary key, uniquely identify each row", but I'm least sure about this mapping.

I believe my above analysis is not (quite) correct, so I'm hoping someone can clarify the link between these two sets of rules.

My question is: How are the three aforementioned Tidy Data rules equivalent to Third Normal Form?

I believe it goes a like this, based on thinking about this myself:

Hadley's rule 1 & 2 (call it TR1 & TR2) is equivalent to 1NF with the exception that Hadley's rule 1 would permit repetition of columns. All we are saying in TR1 & TR2 is that we have a single value in each cell of our table, although I think that is dependent on how you interpret observation. If an observation is an instance of a specific object then this is a stronger statement, although TR3 suggests that is not the case I think.

The combination of Hadley's rule 1, 2 and 3 is equivalent to 3NF. Using this link which also explains transitive dependence:

The table with UnitCode, UnitName, CourseCode, CourseName is in 2NF but violates TR3 since course and unit are different objects. The tables in 3NF where course and unit are separated and linked by foreign key comply with TR1, TR2 and TR3.

Let's say we want to prove implication and assume we have TR1, TR2 and TR3. Since those say nothing about repeating columns I think we have to either assume no repetition of columns or accept that these are not equivalent. Let's assume no repetition of columns. From the definitions TR1 & TR2 => 1NF. If we have a table that is not in 2NF then TR3 is violated using the logic in the example above. Therefore we have 2NF. Similarly if we have a table that is not in 3NF then TR3 is violated, again using the logic above. Therefore TR1 & TR2 & TR3 => 3NF.

I think someone could probably do a more thorough proof with more time.

HTH


What are the methods to ensure that the population split for A/B test is random? Before launching an A/B test, what are the methods to ensure that the population split in control and target group is random for a particular label say, purchase rate.

Just split them 50/50 (or another percentage, depending on what you want). Once you want to test a specific metric, make sure that if you can only generate that for a portion of the A group because of some specific property (ie you need information C for this metric) to compare it only to the people in group B that also have information C to avoid bias.

Example: I have 200,000 customers. I split them 50/50. I'm testing my new recommendation method against a random advertisement. However my recommender only works if they have searched in the last month. After getting back the results, I will have to check that against the control group filtered to have only people that have searched in the past month. People that have searched in the past month are much more likely to have a stronger kind of connection to you, so if you would check against the whole group you would overestimate the performance of your recommender.

Alternatively you could already split up the groups knowing whether they have information C or not, but you might want to test different things so splitting before hand can give you extra info, at the small cost that your groups might be slightly unequal, but for a big sample size this is not an issue at all.


Is it possible to train a machine learning trainer? That's just a weird thought experiment I recently came up with. I am new to this field actually. Anyway, here is the idea:

Suppose we have data D, and a machine learning algorithm A1. Then we use A1 to train D to get the hypothesis H. I denote this process as follows:

A1(D) = H

Then, suppose we have a machine learning algorithm trainer A2, which we can use to train a better machine learning algorithm, A1*, by inputing all the row data, D, the hypothesises trained by the machine learning algorithms, H, and the algorithms itself A1:

A2(D,H,A1) = A1*(actually there should be a lot of H and A1)

And this process can go on and on. Hopefully you can get my idea. Sorry in advance if that doesn't make sense to you since English is not my native language. So do you guys think it is possible to train, let's say, a algorithm A3, which can be used to train a machine learning algorithm trainer?

I would try using a Genetic Algorithm to estimate optimal parameters for training. You would need to figure out the objective function of parameters of training algorithm $A_2$ which minimalised fitting $A_1(D)$. Those parameters could be regularization coefficients or numer of layers/neurons, or any other parameters describing your training algorithm.

There is also algorithm called bayesian regularization. It is very similiar to Levenberg - Marquardt training, but it is computing optimal regularization parameters $\alpha$ and $\beta$ by computing effective numbers of network's parameters.

I hope I understood your question well.
Regards, Max


Merging multiple data frames row-wise in PySpark I have 10 data frames pyspark.sql.dataframe.DataFrame, obtained from randomSplit as (td1, td2, td3, td4, td5, td6, td7, td8, td9, td10) = td.randomSplit([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1], seed = 100) Now I want to join 9 td's into a single data frame, how should I do that?

I have already tried with unionAll, but this function accepts only two arguments.

td1_2 = td1.unionAll(td2)
# this is working fine

td1_2_3 = td1.unionAll(td2, td3)
# error TypeError: unionAll() takes exactly 2 arguments (3 given)

Is there any way to combine more than two data frames row-wise?

The purpose of doing this is that I am doing 10-fold Cross Validation manually without using PySpark CrossValidator method, So taking 9 into training and 1 into test data and then I will repeat it for other combinations.

Stolen from: https://stackoverflow.com/questions/33743978/spark-union-of-multiple-rdds

Outside of chaining unions this is the only way to do it for DataFrames.

from functools import reduce  # For Python 3.x
from pyspark.sql import DataFrame

def unionAll(*dfs):
    return reduce(DataFrame.unionAll, dfs)

unionAll(td2, td3, td4, td5, td6, td7, td8, td9, td10)

What happens is that it takes all the objects that you passed as parameters and reduces them using unionAll (this reduce is from Python, not the Spark reduce although they work similarly) which eventually reduces it to one DataFrame.

If instead of DataFrames they are normal RDDs you can pass a list of them to the union function of your SparkContext

EDIT: For your purpose I propose a different method, since you would have to repeat this whole union 10 times for your different folds for crossvalidation, I would add labels for which fold a row belongs to and just filter your DataFrame for every fold based on the label


Format for X_train in keras using theano I want to try out Keras (Theano backend) for regressions after already using sklearn.

For this I uses this nice tutorial http://machinelearningmastery.com/regression-tutorial-keras-deep-learning-library-python/
 and tried to replace the training data there with my own.

import numpy
import pandas
import pickle
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

[X, Y] = pickle.load(open("training_data_1_week_imp_lt_15.pkl", "rb"))

X_train, X_test, y_train, y_test = train_test_split( X, Y, test_size=0.5, random_state=42)
scaler = StandardScaler()
scaler.fit(X_train)  # Don't cheat - fit only on training data
X_train = scaler.transform(X_train)

X_test = scaler.transform(X_test)

print (X_train.shape)

# define base mode
def baseline_model():
    # create model
    model = Sequential()
    model.add(Dense(8, input_dim=8, init='normal', activation='relu'))
    model.add(Dense(1, init='normal'))
    # Compile model
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model

# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# evaluate model with standardized dataset
estimator = KerasRegressor(build_fn=baseline_model, nb_epoch=100, batch_size=5, verbose=0)

estimator.fit(numpy.array(X_train),y_train)

However, i get the following error:

Exception: Error when checking model target: the list of Numpy arrays that you are
passing to your model is not the size the model expected. Expected to see 1
arrays but instead got the following list of 6252 arrays: ...

The format of X is the usual sklearn format: print (X_train.shape) = (6252, 8)

How do I format my input X correctly.

What I tried was transposing but this did not work.

I also already searched the web but could not find a solution/explanation.

Thanks!

EDIT: here is a small sample file https://ufile.io/8a428

[X, Y] = pickle.load(open("test.pkl", "rb"))

I solved this (still banging my head against the wall):

estimator.fit(numpy.array(X_train),numpy.array(y_train))

this works. I am not sure why. The error message is very misleading IMHO.


Using constants in input of a ML Model I'm currently building a binary classifier.
My input is a sequence of 32 time-steps.
Certain time-steps of the input will be constant (ex: t-0 will always be 0, t-5 will always be 9, etc)
Does it make sense to add these time-steps as features into the model? Im thinking its not since: The model will have to pay attention to these features and they will add a type of noise/bias into the model - since there isnt any new information to be gained from them. Am i thinking about this correctly?

You are thinking about this correctly. If data doesn't vary between your outcomes then it doesn't need to be included.
That being said, if you are using time series techniques such as trend decomposition to feature engineer, then changing the structure of your data could complicate interpretation (ie: what is a moving average if you've removed data points?).
In that light I'd say that you should not build these quirks into the code you write, and that you should keep it as general as possible except where absolutely necessary. This is related to the concept of writing "DRY" code where you don't repeat yourself.
Personal opinion: Classifiers, like software, should try not to make assumptions about data if possible. This gives you a better chance of being able to reuse it or share it.


Tensorflow - simple multi-layer perceptron not stabilizing around mean of normally distributed y-values I'm building an FX trading model where I'm trying to predict the +/- movement of a currency pair 5 minutes into the future.  I've had some promising results adapting the model as a classifier (i.e., buy if the currency pair is expected to increase by more than some threshold, sell if the pair is expected to decrease by some threshold, or do nothing otherwise), but when trying to create even the simplest multi-layer perceptron regression model, the mean of predicted values bounces around the mean of actual y values (as expected, this is very close to zero).

The two images below depict the train predictions (green) versus the actual y_train values (blue) after two different epochs after a number of epochs had already been completed.  Rather than move towards the true y mean in some stable way, the prediction mean flies right past it and will seemingly do this over and over with more epochs.

So far, I've tried:

Using a smaller learning rate
Adding more epochs.  The training set has almost 300k items in it, so the classification model is able to learn in only about 5 epochs, but increasing this to 50 or 100 for regression still doesn't seem to help.
Changing the loss function from mean squared error to mean absolute error
Changing the optimizer from Adam to other options like RMSProp (eventually will predict the same value for all items), Adagrad, Adadelta, Nadam, etc.
Using elu or leaky relu instead of relu.  Also, if I don't add 1 to the y values before training, the model ends up predicting the same value for every item after only a few epochs, which might be an extension of the same problem.
A bias constraint on the output node to keep it close to zero (the true y mean).  Ultimately, it doesn't matter and the model still pushes away from zero.
Batch normalization

This is happening even with a very simple model such as this:

self.model = models.Sequential()
self.model.add(layers.Dense(self.num_layers, activation="relu" input_shape=(self.x_train.shape[1],)))
self.model.add(layers.Dense(1))
self.model.compile(optimizer=self.optimizer, loss='mean_squared_error', metrics=["mae"])

Any suggestions as to how I can stabilize the model?

Try deeper network. Something like this.

self.model = models.Sequential()
self.model.add(layers.Dense(self.num_layers, activation="relu", input_shape=(self.x_train.shape[1],)))
self.model.add(layers.Dense(self.num_layers//2, activation="relu"))
self.model.add(layers.Dense(self.num_layers//4, activation="linear"))
self.model.add(layers.Dense(1))
self.model.compile(optimizer=self.optimizer, loss='mean_squared_error', metrics=["mae"])


PCA vs.KernelPCA: which one to use for high dimensional data? I have a dataset which contains a lot of features (>>3). For computational reasons, I would like to apply a dimensionality reduction. At this point I could use different techniques:

standard PCA
Kernel PCA
LLE
...
My problem is to choose the right approach since the number of features is so high that I cannot know beforehand what the distribution of points is like. I could do it only if I have 3D data, but in my case I have much more than that.

I know for example that if the set of points was lineary seperable I could use standard PCA; if it was somehow a sort of concentric circles like shape, then KernelPCA would be a better option.

Therefore how can I know beforehand which dimensionality reduction technique I need to use for high dimensional data?

It can be hard to choose - because it's hard to visualize. However, you probably have a specific goal right? Maximizing some kind of score.

Why don't you try a Grid Search applied to your dimensionality reduction decision? See this.

I'm interested in reading other, more theoretical answers to this question, though.


gpu pytorch code way slower than cpu code? I have the following pytorch code in a jupyter notebook:

import torch
t_cpu = torch.rand(500,500,500)
%timeit t_cpu @ t_cpu

Which outputs:

422 ms ± 3.93 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

And the following code, which took like 100 times as long:

import torch
t_gpu = torch.rand(500,500,500).cuda()
%timeit t_gpu @ t_gpu

which outputs:

31.8 ms ± 550 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Clearly, the reason the gpu version took longer is because it did 1000 loops each.

Why did it do 1000 loops each, given that I didn't specify this at all?

ps. side question: what does the "@ t_cpu" do in "%timeit t_cpu @ t_cpu"?

TL;DR
GPU runs faster than CPU (31.8ms < 422ms).
Your results basically say: "The average run time of your CPU statement is 422ms and the average run time of your GPU statement is 31.8ms".
The second experiment runs 1000 times because you didn't specify it at all. If you check the documentation, it says:

-n: execute the given statement  times in a loop. If  is not provided,  is determined so as to get sufficient accuracy.

Number of runs
The %timeit function estimates the run time of a statement.
It does that by executing the statement multiple times and measuring the average run time of it.
The number of times it runs the experiment is $N\times{R}$, where $N$ is the number of loops and $R$ is the number of times it repeats it.
If you do not specify $N$ and $R$, the default $R$ is $7$ and $N$ may vary.
In your case, you did not specify any. So, in the first experiment, you had $R=7$ and $N=1$. The second experiment was $R=7$ and $N=1000$.
$N$ varies depending on how long it takes to run a single loop. If your statement is time-consuming, %timeit will execute fewer loops, whereas fast statements are executed more often. This is the case because it will give a better accuracy of the actual run time of your statement.
Results
You should not consider the time to run a statement based on the time that it takes to run the %timeit function. As you noticed, the number of loops may vary. Instead, try to interpret the printout.
For example, you had:
422 ms ± 3.93 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

422 ms ± 3.93 ms per loop: this is the mean average time and standard deviation it takes to run your statement one time. A loop is a single execution of your statement. Your statement is what you have defined when calling the function (in your case it is t_cpu ).
of 7 runs, 1 loop each: this is the number of times that %timeit ran your statement to calculated the average execution time. If you think that running the t_cpu only $7$ times is not reliable enough to estimate the $422ms$ then you may increase $N$ and $R$.
Documentation
I really don't mind answering your questions here. However, if you want faster clarifications to your answers, a good resource to read is usually the documentation of the function itself.
The documentation of the %timeit function can be found here.


Classification Accuracy in Keras I'm using two different functions to calculate the accuracy of my deep learning model and I am confused which one is which.

The first one is

    loss, accuracy = model.evaluate(x_train, y_train, verbose=0)
    print('Accuracy: %f' % (accuracy))
    print('Loss: %f' % (loss))

    Accuracy: 0.731495
    Loss: 1.136258

And the second way is to obtain the accuracy:

test_accuracy = history2.history['val_acc']

Accuracy is 0.731 and test_accuracy is around 0.21
Which is one is my models' accuracy

The accuracy given by Keras is the training accuracy. This is not a proper measure of the performance of your classifier, as it is not fair to measure accuracy with the data that has been fed to the NN. On the other hand, the test accuracy is a more fair measure of the real performance. As there is a big gap between them, you are overfitting very badly, and you should regularize your model. Consider using dropout or weight decay.


Interested in Mathematical Statistics... where to start from? I have been working in the last years with statistics and have gone pretty deep in programming with R. I have however always felt that I wasn't completely grasping what I was doing, still understanding all passages and procedures conceptually.

I wanted to get a bit deeper into the math behind it all. I've been looking online for texts and tips, but all texts start with a very high level. Any suggestions on where to start?

To be more precise, I'm not looking for an exaustive list of statistical models and how they work, I kind of get those. I was looking for something like "Basics of statistical modelling"

When looking for texts to learn advanced topics, I start with a web search for relevant grad courses and textbooks, or background tech/math books like those from Dover.

To wit, Theoretical Statistics by Keener looks relevant:
http://www.springer.com/statistics/statistical+theory+and+methods/book/978-0-387-93838-7

And this:
"Looking for a good Mathematical Statistics self-study book (I'm a physics student and my class & current book are useless to me)"
http://www.reddit.com/r/statistics/comments/1n6o19/looking_for_a_good_mathematical_statistics/


What does 'lm' mean in the lmplot() in seaborn? I was learning seaborn, but could not find the full name of seaborn.lmplot. What is 'l' and 'm' in the name 'lmplot'.

"lm" = "linear model". As reported in the docs:

  Plot data and regression model fits across a FacetGrid. [...] It is intended as a convenient interface to fit regression models across conditional subsets of a dataset.


How to define person's gender from the fullname? Given person's name, e.g. 'Adjutor Ferguson'.

How to define is it a male or female?

One solution came to my mind:

     I have found Person NLP training dataset here mbejda.github.io.

      And via a machine learning software like Apache Mahout, train it and provide real data.

But I am not sure about the accuracy of the results.

May be another approach exist? (e.g. scikit-learn.org)

That dataset looks like a good starting point. Keep in mind that when you make your own dataset from those datasets you'll want to keep the male to female ratio balanced if you want it to predict both well.

It should not matter what machine learning software you use (Apache Mahout, scikit-learn, weka, etc.). Pick one that fits your language of choice since speed will probably not be too much of a concern with the smallish dataset size. As for features, you'd generally use ngrams as your baseline for NLP classification tasks. If you use ngrams here you won't end up with anything very interesting because the model won't generalize to any unseen names. I'd suggest as a feature baseline that you try character ngrams, and maybe something like syllable ngrams for something slightly more advanced (for syllable tokenization see https://stackoverflow.com/questions/405161/detecting-syllables-in-a-word).


Difference between using BERT as a 'feature extractor' and fine tuning BERT with its layers fixed I understand that there are two ways of leveraging BERT for some NLP classification task:

BERT might perform ‘feature extraction’ and its output is input further to another (classification) model
The other way is fine-tuning BERT on some text classification task by adding an output layer or layers to pretrained BERT and retraining the whole (with varying number of BERT layers fixed

However, if in the second case, we fix all the layers and add ALL the layers from the classification model will be added, 1st and 2nd approaches are effectively the same, am I right?

No, approaches 1 and 2 are not the same:

In approach 1 (feature extraction), you not only take BERT's output, but normally take the internal representation of all or some of BERT's layers.

In approach 2, you train not only the classification layers but all BERT's layers also. Normally, you choose a very low learning rate and a triangular learning rate schedule to avoid catastrophic forgetting.

There are many scientific articles studying how to best use BERT in transfer learning scenarios. This one may be a good starting point: https://www.aclweb.org/anthology/W19-4302/


How to determine if problem is solvable by neural network? I want to identify subtle patterns in images using a convolutional neural net. I have seen several examples where people gave up reasoning that the pattern is not dominant or consistent enough to be picked up by a neural network. Since tuning these networks is very time consuming I wonder: What are methods to validate that the problem is solvable/the pattern can be recognised (or at least give an indication of the same)?

Background: My problem

My goal is to identify subtle differences in products based on their images. I want to break down a product image into a large variety of describing attributes. Now I need to find out which product attributes are well suited for identification.

For example, I want to distinguish collar types in clothing images. Therefore I need to figure out if the human understanding of collar types (waterfall, V-shaped, round etc.) are distinct and consistent enough to be properly identified by a neural net with human level performance.

Of course images always differ a little and the solvability is clearly related to the data available. But before collecting and cleaning hundreds of thousands of images I would like to find out if results will be any good.

Neural network will pick up on any patterns as long as your environment is not fully stochastic.

Here is what authors of Using Recurrent Neural Networks to Frecasting of Forex used to determine the amount of stochasticity:

page 7, bottom. "Hurst Exponent".


Implement the following loss function without interrupting the gradient chain registered by the gradient tape I have spent five days trying to implement the following algorithm as a loss function to use it in my neural network, but it has been impossible for me. Impossible because, when I have finally implemented, I get the error:

  No gradients provided for any variable: ['conv1_1/kernel:0',
  'conv1_1/bias:0', 'conv1_2/kernel:0'

I'm implementing a semantic segmentation network to identify brain tumours. The network is always returning very good accuracy, about 94.5%, but when I have plotted the real mask with network outputs, I see that the accuracy is 0% because it doesn't plot any white dot:

By the way, the mask image only has values between 0.0, black, and 1.0, white, values.

So, I have decided to implement my own loss function, which has to do the following:

In a nutshell, sum mask image to output and count how many 2.0 values are in this sum. Compare this count of 2.0 values with the count of 1.0 values in the mask image, and get the error.

More detailed:

Converts the output's value of the model into 0.0 or 1.0.
Sum that output to the mask (which values are also 0.0 or 1.0).
Counts how many 2.0 are this sum.
Counts how many 1.0 are in the mask.
Returns the different between them.

My question is:

Is there a already tensorflow function that do that?

Now, I get that network output because I'm using Euclidean Distance. I have implemented the loss function using tf.norm:

def loss(model, x, y):
  global output
  output = model(x)
  return tf.norm(y - output)

And then, I use this loss function to tape gradients:

def grad(model, inputs, targets):
  with tf.GradientTape() as tape:
      loss_value = loss(model, inputs, targets)
  return loss_value, tape.gradient(loss_value, model.trainable_variables)

Maybe, what I am trying to do is another kind of distance.

Your intuition about counting the 2 is pretty good. For this type of problem, you can use the dice loss function wich use your idea but sligthly differently.

the values of predicted and ground truth components (each pixels) are either 0 or 1, representing whether the pixel bayond to the label (value of 1) or not (value of 0). Therefore, the denominator is the sum of total labeled pixels of both prediction and ground truth, and the numerator is the sum of correctly predicted boundary pixels because the sum increments only when predicted and ground truth match (both of value 1).

as cited here:

For example, if two sets A and B overlap perfectly, DSC gets its maximum value to 1. Otherwise, DSC starts to decrease, getting to its minimum value to 0 if the two sets don ‘t overlap at all. Therefore, the range of DSC is between 0 and 1, the larger the better. Thus we can use 1-DSC as Dice loss to maximize the overlap between two sets.

The dice loss is commonly used in segmentation task because it is resilien to class inbalance (eg too much background)

you can implement it in tensorflow keras like this;

def dice_coef(y_true, y_pred):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return (2.0 * intersection + 1.0) / (K.sum(y_true_f) + K.sum(y_pred_f) + 1.0)

def dice_coef_loss(y_true, y_pred):
    return 1-dice_coef(y_true, y_pred)

(in the implementation, we use a smoothing termes (here it's 1, the lower the value is the better) The smoothing therme is here to avoid dividing by 0 if the input as no label in it)


where to store embeddings for similarity search? I've asked on stackoverflow already (here), but I figured that the approach of storing embeddings in an ordinary postgres-Database might be flawed from the very beginning. I will shortly etch out the application again:

text corpora (few hundred thousand documents, containing a few paragraphs)
embeddings create with BERT (for each paragraph)
Application: similarity search (retrieve similar paragraphs and reference to the document)

I've seen tutorials about creating embeddings with BERT etc. and it all works. The Crux I have is how to manage having a few million embeddings and searching for similar ones. Where to store them, plus the additonal information (raw text related to the embeddings and document which contains the text).
So the question is:
How does one store a few million embeddings (768-Dimensional numpy arrays) in an efficient and searchable way without using cloud-environments (data privacy reasons)?
Is Tensorflow Records the right answer?
Is it in the end a relational database?
Is it something different? It's my first NLP task and I might simply not know the obvious answer. However, searching on stackexchange and google didn't provide a solution.

There's Milvus search engine that utilizes several prominent Approximate KNN libraries such as FAISS, ANNOY and HNSW. It also handles several bookkeeping, clustering, data integrity and other tasks that you probably don't want to handle yourself. All for a performance price ofc, but if you don't want to pay it, you can always pick one of the "barebones" libraries.


Error analysis for better accuracy I have historic error of time series.  I want to analyze error series to improve forecast series. Are there any methods to do this?

NARMAX Methodology and Residual analysis both address this issue. Search for the following articles:(Error = Residual = Noise)

Chaotic Time Series Prediction with residual Analysis Method Using Hybrid Elman–NARX Neural Networks, Muhammad Ardalani-Farsa (2010)
Orthogonal Least Squares Methods and their Application to Non-Linear System Identification, S. Chen, S. A. Billings, W. Luo (1989)
Any article working on NARMAX, NARMA and Residual Analysis. Remember in NARX and NAR there is no error estimation and analysis.

Notice in general you can follow this steps:

Estimate a time series and calculate Error or Residuals using any .
Consider errors or residuals as a new time series. Try to estimate Error-Time-Series. Now you can add this estimations to your initial model.
You can do this residual analysis as many times as you need. In practice 2 or 3 times suffices. Remember in practice, residual time series are noisy and SNR in this time series is so small. So you should use some Noise-Robust methods for residual analysis.


ReLU for combating the problem of vanishing gradient in RNN? For solving the problem of vanishing gradients in feedforward neural networks, ReLU activation function can be used.

When we talk about solving the vanishing gradient problem in RNN, we use a more complex architecture (e.g. LSTM). In both of these, activation function is tanh. Can't we use ReLU instead of tanh in RNNs for solving vanishing gradients too rather than opting for a more complex architecture?

Can't we use ReLU instead of tanh in RNNs for solving vanishing gradients too rather than opting for a more complex architecture?

Yes you can. I'd expect it to help a little with vanishing gradients, but not be as effective generally as a LSTM is. Also, without some kind of bounded function in the feedback loop, a basic RNN using ReLU will suffer very easily from exploding values, resulting in very large errors as the feedback is run over multiple time steps - so you may need some adjustment like gradient clipping to account for that.

If you already have a problem to analyse, have prepared your data and decided on a top level architecture (e.g. how many recurrent layers, whether this is a seq2seq type model etc), then it should be relatively easy to compare LSTM with a basic RNN using ReLU. It is likely different hyperparameters would suit each architecture so you may need to do a search, and you could compare best possible results for each model, or look at things such as number of parameters or training time.

I expect many researchers have already tried this comparison, although the examples I found on a quick search appeared to use tanh for the simple RNN.

Even using ReLU in simple RNN architecture, I suspect that by most measures and in most problems, the LSTM or GRU architectures will perform better. There will be some edge cases where RNN performs better, such as if your goal is to get to a certain accuracy quickly during training and not necessarily to get the best accuracy in the end.


Can we use Binary Cross Entropy for Multiclass Classification? In this link, the author has implemented a CNN which classifies 15 classes and has used Binary Cross Entropy as the loss function. But since it's multiclass classification, is it valid to use Binary Cross Entropy? Or should we use Categorical Cross Entropy instead?

Use categorical_crossentropy when it comes for Multiclass classification, Because multiclass have more than one exclusive targets which is restricted by the binary_cross_entrophy.

binary_cross_entrophy is used when the target vector has only two levels of class.

In other cases when target vector has more than two levels categorical_crossentropy can be used for better model convergence.


Why is there a big drop off in performance in my GBM? I'm working on an employee attrition predictive model using sklearn's GradientBoostingClassfier. I have 9,000 observations, which I split 50/50 for training and testing. I have another set of 1,200 observations that I use for a final validation. All 10,200 observations were obtained in similar fashion.

I carried out a grid search with 5-fold cross-validation in order to obtain a suitable set of hyper parameters. The results for my test set are good and very stable. However, there is a big drop off in performance when use my final validation data.

Results for the test set

->  Precision: 0.836 / Recall: 0.629 / Accuracy 0.874

Results for the final validation set

->  Precision: 0.149 / Recall: 0.725 / Accuracy 0.484

At first I thought this could be the caused by data leakage, but even after removing "suspicious" features, there is still a big drop off when comparing the test results with the final validation results.

Surely I'm doing something wrong, but I'm at a loss as to what exactly. Here are the relevant lines of code (nothing fancy):

> X = pd.read_csv('train_test.csv')
> y = X.pop('Target')
> X_final = pd.read_csv('final_validation.csv')
> y_final = X_final.pop('Target')

> X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)

> gb = GradientBoostingClassifier(n_estimators=300, max_depth=5, learning_rate=0.2)
> gb_model = gb.fit(X_train, y_train)

> # test set
> y_pred = gb_model.predict(X_test)
> precision, recall, fscore, support = score(y_test, y_pred, average='binary')

> # final validation set
> y_hat = gb_model.predict(X_final)
> precision, recall, fscore, support = score(y_final, y_hat, average='binary')

Any thoughts?

I would first suggest trying to plot the results during training. How do your metrics (or at least the loss) vary over the training process for training and cross-validation datasets?
The loss at each iteration is appended to your GBC object gb_model in the train_score_ attribute.

Normally, when there is such a big gap between training and test data, it indicates that you are overfitting to your training data, and that the model does not generalise well to unseen data. You could think about doing  shuffling your data, in order to balance the training/validation/test datasets - [if you are looking at a time-series problem, you should be careful as to how you do this].


3D point reconstruction from 2D images I know this must exist, but I'm having enormous trouble finding the right search terms.

Say I have a bunch of labelled 3D points, and I capture multiple 2D images of it. If I want to reconstruct the 3D points, are there well-established algorithms/libraries for doing this?

This is presumably the basis for 3D facial recognition, which is a well-established field of research, but the general case (i.e. non-faces) doesn't seem to have an obvious literature that I can find.

One way I can see for approaching this is an optimisation problem where each 2D image establishes a minimum distance constraint between each point, and one can reconstruct the 3D points by minimising the distance required to satisfy these constraints. This does feel like one of those problems that has a sneaky linear algebra solution, though.

Does this class of problem have a literature I can search? Does it have existing libraries? (I'd be really surprised if there wasn't something in OpenCV, but I don't really know what I'm looking for)

Are you familiar with multi-view geometry? There's a classic book on it: Multiple View Geometry in Computer Vision by Hartley and Zisserman. A good starting point is the wiki article on 3D reconstruction from multiple images.

Essentially, yes, the idea is to solve an optimization problem. Let the object be a point set $P$ in 3D. In the general case, we have $N_C$ cameras $\{C_i\}_{i=1}^{N_C}$, such that
$$
q_i = \mathcal{I}_k\mathcal{E}_k p_i
$$
$$ (x_i,y_i)=(q_{i1}/q_{i3},q_{i2}/q_{i3}) $$
where $p_i\in P$ is the $i$th point in homogeneous coordinates, $\mathcal{I}$ is the intrinsic camera parameters matrix, $\mathcal{E}$ is the extrinsic camera parameters matrix, and $q_i$ is the $i$th projected point with image coordinates $(x_i,y_i)$.
(Check out the camera matrix and camera calibration for more.)

To solve this problem depends on how much information you have (especially regarding the camera parameters) and what assumptions you make (e.g. perspective vs orthographic projection equations). Note there can be some inevitable ambiguity, depending on the number of cameras/images and their view-points, as well as the number/location of corresponding points. If you have some prior knowledge of the 3D structure itself, that can also be used.

In the two-view case, the
 (grandly named) fundamental matrix and the essential matrix, allow estimation of the 3D point positions (up to translational scaling) based on the epipolar constraints. With more views, one can for example do triangulation via direct linear transformation estimation.

A nice place to start might be the PhD thesis of Daniel Martinec on Robust Multiview Reconstruction.

Two libraries of potential interest are OpenCV and OpenMVG.

Now, entirely separate from this view as a classical computer vision problem is the machine learning approach. The idea is to complement (or sometimes replace) the geometric relations from multiview geometry (which can be brittle due to noise, measurement error, etc...) with data-driven estimations. Some starting points might be:

SurfaceNet: An End-to-end 3D Neural Network for Multiview Stereopsis
DeepMVS: Learning Multi-view Stereopsis
MVSNet: Depth Inference for Unstructured Multi-view Stereo
Learning a Multi-View Stereo Machine
Learned Multi-Patch Similarity

Such approaches may be more in line, I suppose, with the average users of a "Data Science" site :)

Note that these approaches tend to assume that no point-wise correspondence is known. This makes the problem significantly harder. In your case, however, it seems this is not the case, though you haven't given an explicit formalization. Just keep this mind when looking at these various methods, as an easier approach may be preferable.


Looking for second opinion on checking that attribute in data frame is unique using r Check whether attribute name in city is unique (i.e. each city has a different name).  data_frame is city with a column called name.

length(unique(city$name))
rapply(city,function(name)length(unique(name)))
unique(city$name)

multiple options are available in R.Apart from your mentioned function.

dplyr

duplicated


is Batch Norm a little bit stochastic by default? Using full-batch gradient descent, stacking 100 layers and using alpha 0.0001 results in steadily decreasing error.

However, after I implemented Batch Norm, the same scenario results in fluctuations. My implementation was verified by several people, so now I am wondering - why batch norm adds this stochastisity effect?

In fact, I am no longer able to stack 100+ layers, only approximatelly 10 layer before stochasticity becomes very apparent and hard to control.

What's more interesting -it seems to get worse with smaller learning rate! 0.4 is good, and 0.0001 results in smaller updates (as expected) but larger relative fluctuation (caught me by surprise).

Why is it so?

Edit: Just tried 100 layers (each 63 neurons). It's very noisy, but I am able to more-or-less steadily reduce the error. It's less stochastic if I set learning rate to 10 ("ten", lol!) and will get only noisier if it's say 0.5 which is incredibly weird

Of course, this learning rate is so high that the error does occasionally snap to a high value, but seems to work with 100 layers...

Notice - I am using full-batch gradient descent, with 50 elements

Edit:

Using 100 LSTM units, stacked like a pillar onto each other.

Number of timesteps is 50. Feature size 50 (lstm state has dimension of 50), because my sentence has 50 distinct characters, each character is encountered only once per epoch.

Performing backpropagation after 50 timesteps Using vanilla gradient descent, all fancy things like accelerated momentum or L2 norm, dropout are disabled.

I am sure there is no error, but interested if anyone can tell why Batch Norm has this stochastic property, if that's common with people.

While I am no expert in batch normalization, I have noticed that in the paper they include a constant, epsilon (ε) to the minibatch variance for numerical stability (in the normalize step from this paper, page 3). So while your math might be right and verified by others, the actual computation could still be wrong.

This issue appears here. The solution on the bottom is to add variance clipping.

And with a stab at why you might be seeing it more with smaller learning rates. Well that might be just because you're taking a lot more steps to get to the same accuracy so that's just many more steps of numerical instability.
EDIT: I now realize when you say full-batch gradient descent you mean you are doing the entire dataset before updating the weights (just standard gradient descent). This means that with a smaller learning rate you will have to be doing more steps... so I'm sure that explains the learning rate difference.


Closed Domain Question Answering which doesn't answer Questions I've been exploring Closed Domain Question Answering Implementations which have been trained on SQuAD 2.0 dataset. Ideally, it should not answer questions which the context text corpus doesn't contain answers to. But while implementing such models using the Haystack repo or the FARM repo, I'm finding that it always answers these questions even when it shouldn't. Is there any implementation available that takes into account the fact that it shouldn't answer questions when it doesn't find a suitable answer.
References:

https://colab.research.google.com/github/deepset-ai/haystack/blob/update-tutorials/tutorials/Tutorial3_Basic_QA_Pipeline_without_Elasticsearch.ipynb#scrollTo=KS4nTwxIbRb6

https://github.com/deepset-ai/FARM

https://github.com/deepset-ai/haystack

https://huggingface.co/deepset/bert-large-uncased-whole-word-masking-squad2

https://colab.research.google.com/drive/1UrKlHlf68hD3wwQDTctx2cQs6FMUxLLH?usp=sharing#scrollTo=J4jxYsxaG77O

Question Answering model of simpletransformers takes data with is_impossible option in the training phase. Also during prediciton, it does not generate answers for all questions since the model learns which questions should be answered.
You can find details below:
https://pypi.org/project/simpletransformers/


How to reduce the GPU consumption size while using Elmo Model? I am performing an NLP task using Elmo model. Whenever I load the Elmo model, it occupies the 15 GB of my GPU memory. How can I reduce it ?
Below is my code
import tensorflow.compat.v1 as tf
import tensorflow_hub as hub
tf.disable_eager_execution()
from tensorflow.compat.v1.keras import backend as K
sess = tf.Session()
K.set_session(sess)

elmo_model = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True)
sess.run(tf.global_variables_initializer())
sess.run(tf.tables_initializer())

def ElmoEmbedding(x):
    return elmo_model(inputs={
                    "tokens": tf.squeeze(tf.cast(x, tf.string)),
                    "sequence_len": tf.constant(batch_size*[maxlen])
              },
              signature="tokens",
              as_dict=True)["elmo"]

And then I am passing the ElmoEmbedding in the Lambda layer as below
input_text = Input(shape=(maxlen,), dtype=tf.string)

embedding = Lambda(ElmoEmbedding, output_shape=(maxlen, 1024))(input_text)

x = Bidirectional(LSTM(units=512, return_sequences=True,
               recurrent_dropout=0.2, dropout=0.2))(embedding)

.....

What do I need to change in the above code ?

I understand that you are doing only inference, so you shouldn't need your model to be trainable, and therefore you can set the parameter trainable to False when invoking hub.Module.


Using K-fold cross-validation in Keras on the data of my model I would like to use K-fold cross-validation on my data of my model.

My codes in Keras is :

a = np.array(
[[283, 95, 72, 65],
[290, 100, 80, 72],
[120,170,130,122],
[100,230,110,200],
[300,100,200,500]]
)
X = a[:,0:2]
Y = a[:,3]

from sklearn.model_selection import KFold, cross_val_score
k_fold = KFold(n_splits=3)
model = models.Sequential()
model.add(Dense(12, input_shape=(3,)))
model.add(LeakyReLU())
model.summary()

cross_val_score(model,X,Y)

But, It makes this error:

  If no scoring is specified, the estimator passed should have a 'score' method. The estimator  does not.

And when I select a scoring parameter as:

cross_val_score(model,X,Y, scoring= 'accuracy')

It makes another error:

  TypeError: Cannot clone object '' (type ): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods.

How can I use K-fold cross-validation on this model?

Thank you

The cross_val_score seems to be dependent on the model being from sk-learn and having a get_params method. Since your Keras implementation does not have this, it can't provide the necessary information to do the cross_val_score. Try the manual k-fold cross validation found here: https://machinelearningmastery.com/evaluate-performance-deep-learning-models-keras/


How to evaluate performance of a time series model? I trained a LSTM network on a time series dataset. Predictions seem to follow the dataset. In fact, they are nearly a right shifted form of real values. Thus, in my opinion, it doesn't provide any predictive capability since only shifting a signal in time results in low RMSE but is not useful.

How to properly evaluate a time series model?

The best summary on evaluating time series forecast is probably explained in detail on Rob Hyndman's site. I typically use the mean absolute percentage error which is baked in Keras. However, what I found in a different setting is that the MAPE prevents the neural network from converging if combined with the Adam optmization. I had much better success with the rooted mean square error (RMSE). Since you have poor experience with that maybe you could use the symmetric MAPE.


Multiple XGBoost models or just 1 for a cetain type of category? I am building a model to predict, say house prices. Within my data I have sales and rentals. The Y variable is the price of either the sales or rentals. I also have a number of X variables to predict Y, such as number of bedrooms, bathrooms, meters squared etc.

I believe that the model will firstly make a split on the variable "sales" vs "rentals" as this would reduce the loss function - RMSE - the most.

Do you think it is best to train 2 models one for "sales" and the other for "rentals"? The RMSE for the model is quite high and this is in part due to the incorrect "Sales" predictions.

This is the main advantage of ML: if the variable has any predictive value (that is not included in another variable), it should be used by the model. So, generally speaking it doesn't really make sense to handpick your variables to make different versions of your model (however it make sense to handpick some you want to throw). That would just be equivalent to picking the first binary split in your first tree... you are not achieving much with that.
Edit: ok, it seems that the target is not really well defined as you aggregate things that are monthly payments and things that are the house value. In that case it make sense to have two models. (Honestly it would even make more sense not to aggregate those two distinct dataset in the first place).


How do I interpret loss and accuracy per epoch while training a CNN? I am really new to Neural Networks, and I am training a CNN for image classification, and while training, I get the following:

which tells me the training loss and accuracy and validation loss and accuracy, soemone please correct me if i am wrong.
So, my question is:
What are pecisely these quantities? And is there a way to understand if I a, doing well with my problem looking at them?

"loss" refers to the loss value over the training data after each epoch. This is what the optimization process is trying to minimize with the training so, the lower, the better.
"accuracy" refers to the ratio between correct predictions and the total number of predictions in the training data. The higher, the better. This is normally inversely correlated with the loss, but not always.

The "validation" counterparts are the same concepts but computed over the validation data, which is not used for training and hence "unseen" to your model. If the training loss and accuracy are good but the validation counterparts are bad, it means that your model is "overfitting", as it can't generalize to unseen data.
Normally, these measures are plotted together into a training and validation loss plot and a training and validation accuracy plot, so that you can better evaluate their behavior over time. You can see some examples on how to interpret different trends here.
In your case, the training is not finished, so we can't be sure. From what we can see, the accuracy is less than 30%, so it is bad, but maybe more training leads to improvements.


Is Max Pooling and Conv used on anything else but images? Can you think of any domain of application, other than 2D images, where it could make sense to use max pooling or convolution?

Because the ONNX format allows for non 2D inputs. On the operators page (https://github.com/onnx/onnx/blob/master/docs/Operators.md#MaxPool) they say

  dimensions for image case are (N x C x H x W), where N is the batch
  size, C is the number of channels, and H and W are the height and the
  width of the data. For non image case, the dimensions are in the form
  of (N x C x D1 x D2 ... Dn)

And I did a search, and couldn't find an application where non images.

As @Aditya mentioned, we can use 1D Convolutions and Max Pooling for text classification as well. It has been used in sentiment analysis and gives quite good performance too. See here and here.

Another useful application is in signal processing. Classifying data from sensors of all kinds is a task for CNNs.

  You can develop a Human Activity Recognizer using 1D Convolutions. See
  here.

But, why not use an RNN instead of a CNN?

RNNs require a higher level of data preprocessing and have low inference speed if you are running them on a smartphone ( or any other IoT device ). CNNs are pretty fast in this case.

Audio classification using MFCC is performed using 1D Convolutional NN. See here.

  2D Convolutions are mainly used in image concerned ML tasks. They could extract spatial features from the 2D arrays ( an image ). In some cases, you can use them on 2D data which is not an image.


ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() Firstly I have a pandas series of recommended product  (recmd_prdt_list). In this series there is a possibility of presence of deleted products. So as to remove deleted products from the recommended products, I did the following :

recmd_prdt_list = user_lookup['Recommended items']
recmd_prdt_list

0      PLV08, PLPD04, PBC07, 555, PLF02, 963, PLF07, ...

1      123, 345, R922, Asus009, AIMAC, Th001, SAM S9,...

2      LGRFG, LG, 1025, COFMH, 8048, BY7, PLHL4, 569,...

3      COFMH, 5454, 8048, 1025, LG, len123, Th001, PL...

4      LGRFG, AIM-Pro, 569, Asus009, PLHL3, PL04, PLH...

5      PLV08, PLF09, PLF02, PBC04, PLF07, AIM-Pro, PL...

type(recmd_prdt_list)

pandas.core.series.Series

DataFrame of product status

product_status

ItemCode  Status DeletedStatus
AIMAC     2      True
AIM-Pro   2      True
SAM S9    2      True
SH MV     2      True
COFMH     2      True
LGRFG     2      True

type(product_status)

pandas.core.frame.DataFrame

first_row = user_lookup['Recommended items'][0]
first_row

'PLV08, PLPD04, PBC07, 555, PLF02, 963, PLF07, HG8, jealous21, 4'

type(first_row)

str

Converting the str to list

first_row_list = list(first_row .split(","))
first_row_list

['PLV08',
 ' PLPD04',
 ' PBC07',
 ' 555',
 ' PLF02',
 ' 963',
 ' PLF07',
 ' HG8',
 ' jealous21',
 ' 4']

From the first row i took first itemcode to check the deleted status :

product_details = product_status.loc[product_status['ItemCode'] == 'PLV08']
product_details

ItemCode   Status   DeletedStatus

PLV08       2            False

type(product_details)

pandas.core.frame.DataFrame

product_details['DeletedStatus']

693    False

Name: DeletedStatus, dtype: bool

So as to check the deleted status of each product in the respective row and save to a new list. I wrote the following code :

itemcode = 'PLV08'
activ_product = []
if itemcode in product_status['ItemCode'].values:
    print(itemcode)
    product_details = product_status.loc[product_status['ItemCode'] == itemcode]
    print(product_details)
    if product_details['Status'] == 2 & product_details['DeletedStatus'] == 'False':
        activ_product.append(itemcode)

Error :

PLV08
     ClientId ItemCode  Status  DeletedStatus
499      2213    PLV08       2          False
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-35-9507e1ada5f7> in <module>()
      5     product_details = product_status.loc[product_status['ItemCode'] == itemcode]
      6     print(product_details)
----> 7     if product_details['Status'] == 2 & product_details['DeletedStatus'] == 'False':
      8         activ_product.append(itemcode)

~/.virtualenvs/sysg_python3/lib/python3.5/site-packages/pandas/core/generic.py in __nonzero__(self)
    951         raise ValueError("The truth value of a {0} is ambiguous. "
    952                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 953                          .format(self.__class__.__name__))
    954
    955     __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

How to get solve of this error?

First of all, to make logical test in Python, you should not use & for a single values equalities (see this) and you should not use question marks around boolean values False and True.

Now, concerning you specific error : When writing product_details['Status'] and product_details['DeletedStatus'] you get each time a Series, which you cannot test for a logical and between them. If you have unique item codes, you can use:

if product_details.iloc[0]['Status'] == 2 and product_details.iloc[0]['DeletedStatus'] == False:
    activ_product.append(itemcode)

It will simply select the first row of product_details and subset the desired column so that the result is a single value and you can compare it.


How to filter out numbers from text documents I have several text files that have numbers in them.  It would be very difficult to filter out all the numbers by creating a Stop Word txt file with every number from 1 to 2010, plus very inefficient use of resources.  Is there a way in Orange to parse out all numbers from text documents?

Yes, orange has regex abilities as seen here.  If you're unfamiliar with regex here's a link regarding regex with numbers.  The idea is pretty straight forward.  Use regular expression to find all numbers, then replace the numbers with something like a blanks string a placeholder word like "NUMBER".


Why validation loss worsens while precision/recall continue to improve? I'm training a neural network on 'easy' dataset with ~15k examples.
Network overfits pretty fast.

The thing I cannot understand that after 5th epoch validation loss is starting to worsen, while precision and recall are continue to improve for 10 more epochs.
(loss = binary cross-entropy)

Diving deeper:

I've checked if there is a lot of predictions around probability ~0.5, but it is not:

Also, there is a plot of percent of correct predictions based on prediction probability. There is some pattern here, but the number of elements is quite small to make conclusions.

So, my question is: why can it happen, and what to do about it?

What I found while digging down:

As neural network continues to train, it gets more confident in predictions. And while it overfits at certain scenarios, it makes log_loss to increase exponentially, since it was more confident while mistaken.

Meanwhile, if I look at mean average error, I would see that it continues to decrease, which indicates that network still learns something. Which at the end make f1_score to increase.

One important thing I noticed: it will not always hold true, because there is a tradeoff between overfitting/learning new, and MAE is weaker metric in binary outcomes.


Do you know this S curve's name? I'm making a ad-sales predictor and try to make it fit by using the S curve's below.

$ ()= \frac{}{1+} (1−^{−}) $

$f(x)$ is an ad-sales
$x$ is an advertise cost
both $a$ and $b$ are fitting parameters.

It looks like logistic function curve. I actually don't know a name of it though I have to use it for our client order. Do you know a name of the function or any text referring it?

From a quick search, the function you provided doesn't seem to have a particular name.

Are you sure the function belongs to Sigmoidal curves? The function does not remind too much of an S-shape.


best approach to embed random length sequences of words as a fixed size vector without having a maximum length? I have a dataset of sentences in a non-English language like:

word1 word2 word3 word62

word5 word1 word2

and the length of each sentence is not fixed.
Now, I want to represent each sentence as a fixed sized vector and give it to my model and i want to keep as much information as possible in the embedding, and i don't want to have a maximum length for sentences because important information might happen in the end.
The only two approaches I can think of so far are:

Convert them to one hot vector and add them

Convert them to a word embedding and then add them

Is there any better way? What is the best approach to represent a variable length sentence without losing information from it (like having a maximum length for each sentence - I want all the words in the sentence to affect the embedding)?

May be Universal Sentence Encoder might work for you if it can encode words in language you want.
https://tfhub.dev/google/universal-sentence-encoder/4


Reasoning behind using Deep Learning on non-local data I understand the using of deep learning for data that have "local" structure, for example, images/videos/texts, as the convolutional layers reduce the amount of dimensions.
However, I saw that some people use it on non-local data, as on databases for example, here or here on the titanic database.
My question is: as just one hidden layer with enough neurons within can theoretically creates as many dimensions as we want, why would one use several hidden layers/deep learning instead of just using a single bigger hidden layer?

It appear that I forgot one point of the ANN, at least forgot one of its effects : the activation function.

It is true that for linear activation, multi-layer can be reduce to a single-one, but with a non-linear function,

  a two-layer neural network can be proven to be a universal function approximator.

Sources

However, it is true that I dont understand now why to use more than two hidden layers...


What are "VGG54" and "VGG22" derived from the VGG19 CNN? In the paper Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network by Christian Ledig et al., the distance between images (used in the loss function) is calculated from feature maps extracted from the VGG19 network. The two used in the article are (a bit confusingly) called VGG22 and VGG54.

What are these feature maps?

What do the designations "22" and "54" mean?

Reading the article, it seems like they define VGG54 as the loss calculated from the euclidean distance between the $\phi_{5,4}$ feature maps derived from both the high and low resolution images using the VGG19 network. Where $\phi_{i,j}$ is defined as "the feature map obtained by the j-th convolution (after activation) and before the i-th max-pooling layer within the VGG19 network".


Error is occurring setting an array element with a sequence I have the columns in my Data Frame as shown below:
 Venue             city                Venue Categories

  Madison         London      [1, 1, 1, 1, 0, 0, 0, ...,0,0]
  WaterFront      Austria     [0, 1, 1 0, 0, 0,  0, ....0,1]
  Aeronaut        Marvilles   [0, 0, 0, 0, 1, 1, 1, ....1,1]
  Aeronaut        Paris       [0, 1, 1, 0, 0, 0, 0, ....1,1]
  Gostrich        New York    [0, 0, 1, 0, 0, 0, 0, ....1,0]

I am passing this data to my machine learning model , but model.fit is not accepting the input , My code is shown below , that I am trying ,
labelencoder = LabelEncoder()
dff['Venue']=labelencoder.fit_transform(dff['Venue'])
dff['city']=labelencoder.fit_transform(dff['city'])

Let's say , if I want to increase the number of features . I want to add more columns , then I again write everything for each feature just like shown below , if i want to add type column and owner column
city = dff['city'].values
owner = dff['owner'].values
type = dff['type'].values
categories = dff['Venue Categories'].values
labels = np.array(dff['Venue'].values)
data = np.array([make_sample(city[i], owner[i], type[i] categories[i]) for i in range(len(city))])

This will looks weird , I want to make it global , means there should not need to touch the code if we may increase the number of columns .

I managed to make it work, by combining the city column with the venue categories column into a 2D (numpy) array which can be used by the RandomForestClassifier of sklearn.
Example code:
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import numpy as np

def make_data(df, target_column='Venue', categories_column='Venue Categories'):
    categories = df[categories_column].values
    other_features = []
    for col in df.columns:
        if col in [categories_column, target_column]: continue
        other_features.append(df[col].values);
    return np.array([[col[i] for col in other_features]+categories[i] for i in range(len(categories))]), np.array(df[target_column].values)

labelencoder = LabelEncoder()

dff['Venue'] = labelencoder.fit_transform(dff['Venue'])
dff['city'] = labelencoder.fit_transform(dff['city'])

data, labels = make_data(dff, 'Venue', 'Venue Categories')

train_data,test_data,train_labels,test_labels = train_test_split(data,labels,test_size=0.20)

model = RandomForestClassifier()
model.fit(train_data,train_labels)

Note make sure that Venue Categories column has same number of elements for each row of data, else a new problem will arise again. If needed fill with dummy values


Text classification into thousands of classes Could somebody point me to a paper or code that is about classifying texts into potentially thousands of categories (topics)? I do have data based on Wikipedia and the number of categories is really big (thousands), so looking for some solutions that should work.

In general this doesn't work well, since it's almost unavoidable that the classifier won't be able to distinguish all the categories from each other. I'd suggest tying to reduce the number of categories (for instance discard the least common ones).

In any case I'm not aware of any specific model to deal with a high number of classes, it's regular text classification. I'd suggest to start with a robust method such as decision trees, but there are many options.


Do larger numbers of hidden layers have a bigger effect on a classification model's accuarcy? I trained different classification models using Keras with different numbers of hidden layers and the same number of neurons in each layer. What I found was the accuracy of the models decreased as the number of hidden layers increased, however, the decrease was more significant in larger numbers of hidden layers. The following graph shows the accuracy of different models where the number of hidden layers changed while the rest of the parameters stay the same (each model has 64 neurons in each hidden layer):

My question is why is the drop in accuracy between 8 hidden layers and 16 hidden layers much greater than the drop between 1 hidden layer and 8 hidden layers, even though the difference in the number of hidden layers is the same (8).

Neural Networks try to approximate the function which maps the given image to its label. Changing the number of hidden layers essentially means changing the number of parameters, which would be used to approximate that input-label mapping. These parameters include weights and biases ( in the case of Dense layers ).
Depending upon the number of parameters, the model might,

Underfit, meaning it has lesser parameters than that required to solve the problem. This would result in a decrease in the model's accuracy over the test dataset.

Overfit, meaning the model has excessive parameters and now it tends to memorize the given training dataset. In this case, the model's accuracy over the training dataset would be much higher than that over the testing data.

Apart from these cases, if the model's loss, as well as accuracy ( or any metric to measure the performance of the model ), seems to improve both on the testing and training data, up to a certain number of epochs, we say that the model has generalized itself.
It depends on the problem you're trying to solve. CNNs like InceptionV3, MobileNets, ResNets have hundreds of hidden layers as they are trained on huge image datasets like CelebA or ImageNet. So, they require millions of parameters to generalize themselves and obtain a better accuracy over the testing data.
You may find a clearer definition of overfitting here.

My question is why is the drop in accuracy between 8 hidden layers and
16 hidden layers much greater than the drop between 1 hidden layer and
8 hidden layers, even though the difference in the number of hidden
layers is the same (8).

Probably the no. of parameters in those hidden layers were just up to the mark for the model to generalize itself. As the no. of hidden layers was increased to 16, the model had excessive parameters and it overfitted the training data.

How do we determine the no. of hidden layers required by a model?

One way to determine is to perform hyperparameter optimization and to search for the best possible combinations of hyperparameters ( learning rate, dropout rate ).


Varying the number of neighbors in nearest-neighbors regression For nearest-neighbors regression, it is plausible to increase the number of neighbors used to predict $f(x)$ when there are many data points near $x$.

What is a good algorithm for varying the number of neighbors used?

"A good algorithm for varying the number of neighbors" is probably quite data-dependent. But there is, for example, the fixed radius approach.


Replace column in a dataframe with another column based on index I have the following dataframe A:
        score   content  content_preprocessed   for_analysis
1         5       a             aaa                 True
2         5       b             NaN                 False
3         1       c             ccc                 True
4         1       d             ddd                 True

I need to replace content_preprocessed column with the following new column from dataframe B:
        content_preprocessed
1              aaz
3              ccf
4              ddo

which contains new values for content_preprocessed where for_analysis is set to True. The values for content_preprocessed should remain NaN if the corresponding for_analysis is False.
How do I do that?

Try this:
A.loc[A.for_analysis & (A.index.isin(B.index)) , content_preprocessed] = B.content_preprocessed


What is the difference between cache() vs prefetch() in tensorflow? I have gone through the TensorFlow documentation.
What is the difference between cache() vs prefetch() in TensorFlow?
When should I use the cache() function and when should I use the prefetch() function?

The tf.data.Dataset.cache transformation can cache a dataset, either in memory or on local storage. This will save some operations (like file opening and data reading) from being executed during each epoch. The next epochs will reuse the data cached by the cache transformation.
Prefetch overlaps the preprocessing and model execution of a training step. While the model is executing training step s, the input pipeline is reading the data for step s+1. Doing so reduces the step time to the maximum (as opposed to the sum) of the training and the time it takes to extract the data.
Source


Training Image classifier on tiny dataset If we have a tiny dataset in terms of images per class where we only have one image available for a given class e.g. a Flag, will data augmentation strategy work or what should be the strategy of training in this case?

CNNs require a large number of images to be trained properly. The most common workaround when having a small training set is to use a pre-trained model, which has been trained on a huge dataset (e.g. ImageNet). Through this you take a highly effective model, drop its final layers (which are used for classification) and keep all the previous (which are used for feature extraction). Then you add your own final layers and only train them. This allows you to train a model with much less parameters (because most of them have already been trained), which can be done on smaller datasets.

Another thing you can do is data augmentation. This usually consists of affine transformations (rotations, translations, scaling, shifting, etc.), flipping the image, brightness/contrast adjustments, noise induction, etc. The problem with this is that strong augmentations might ruin the image. This limits you in terms of how many and how strong augmentors you use.

That being said, in terms of image classification a tiny dataset could be considered one with a few hundred samples. If you have just one image per class, I'm not sure that you can ever effectively train a CNN without overfitting. My suggestion is, if possible, obtain more data.


Why doesn't training RNNs use 100% of the GPU? I wonder why training RNNs typically doesn't use 100% of the GPU.

For example, if I run this RNN benchmark on a Maxwell Titan X on Ubuntu 14.04.4 LTS x64, the GPU utilization is below 90%:

The benchmark was launched using the command:

python rnn.py -n 'fastlstm' -l 1024 -s 30 -b 128

How can I diagnose   what the bottleneck is?

I get about this same utilization rate when I train models using Tensorflow. The reason is pretty clear in my case, I'm manually choosing a random batch of samples and calling the optimization for each batch separately.

That means that each batch of data is in main memory, it's then copied into GPU memory where the rest of the model is, then forward/back propagation and update is performed in-gpu, then execution is handed back to my code where I grab another batch and call optimize on it.

There's a faster way to do that if you spend a few hours setting up Tensorflow to do batch loading in parallel from pre-prepared TF records.

I realize you may or may not be using tensorflow under keras, but since my experience tends to produce very similar utilization numbers, I'm going out on a limb by suggesting that there's a reasonably likely causal link to draw from these correlations. If your framework is loading each batch from main memory into the GPU without the added efficiency/complexity of asynchronous loading (which the GPU itself can handle), then this would be an expected result.


Unable to overfit using MLP I'm building a 5-class classifier with a private dataset. Each data sample has 67 features and there are about 40000 samples. Samples of a particular class were duplicated to overcome class imbalance problems (hence 40000 samples).

With a one-vs-one multi-class SVM, I am getting an accuracy of ~79% on the validation set. The features were standardized to get 79% accuracy. Without standardization, the accuracy I get is ~72%. Similar result when I tried 50-fold cross validation.

Now moving on to MLP results,

Exp 1:

Network Architecture: [67 40 5]
Optimizer: Adam
Learning Rate: exponential decay of base learning rate
Validation Accuracy: ~45%
Observation: Both training accuracy and validation accuracy stops improving.

Exp 2:
Repeated Exp 1 with batchnorm layer

Validation Accuracy: ~50%
Observation: Got 5% increase in accuracy.

Exp 3:

To overfit, increased the depth of MLP. A deeper version of Exp 1 network

Network Architecture: [67 40 40 40 40 40 40 5]
Optimizer: Adam
Learning Rate: exponential decay of base learning rate
Validation Accuracy: ~55%

Thoughts on what might be happening?

Some things that could be happening:

You do not have enough parameters in your hidden Layer to learn. Try something like [67, 512, 5] or expand deeper with something like [67, 1024, 256, 5]. The idea is that your hidden layer may be too small to capture interactions between the attributes given the amount of training data you have.

A rule of thumb that I have seen is:

Nh=Ns/(α∗(Ni+No))

Nh = number of hidden neurons.
Ni = number of input neurons.
No = number of output neurons.
Ns = number of samples in training data set.
α = an arbitrary scaling factor usually 2-10.

Which in your case gets you to like 5000 neurons, but since you are oversampling your classes, I would also suggest under-sampling instead, and that gets you maybe to around 1024, which is fairly standard. The [67, 512, 5] configuration is pure gut reaction.

Learning rate is too high. Try starting with lower LR (like 10e-5). Sometimes this works with Adam.
Weight Initialization : double check you are using Xavier.


Matrix notation in Sutton and Barto On pg. 206 of Barto and Sutton's Reinforcement Learning, there is a curious statement about the result of a scalar product:

As I interpret it, A is the expectation of a scalar product of two d-dimensional vectors: which should be a scalar, right? So how do they get a dxd-matrix from it? Is it a shorthand for a scalar matrix (diagonal with the repeated coefficient, namely this scalar product)?

In Sutton & Barto, vectors are considered column vectors by default. So if you have this kind of product:
$$\mathbf{a}\mathbf{b}^T$$
where $\mathbf{a}$ and $\mathbf{b}$ are $d$ dimensional vectors, it does not calculate the scalar product. Instead it treats both vectors as matrices and calculates a matrix product, which will be a $d \times d$ matrix because you are multiplying a $d \times 1$ matrix by a $1 \times d$ matrix.
Worthing noting that the scalar product can also be calculated as a $1 \times 1$ matrix if follow the same matrix multiplication rules but with the first vector transposed instead:
$$\mathbf{a}^T\mathbf{b}$$
which leads to multiplying a $1 \times d$ matrix by a $d \times 1$ matrix. This is why the value function approximation can be written as $\mathbf{w}^T\mathbf{x}_t$ (there is a small liberty taken of assuming a $1 \times 1$ matrix is the same as a scalar value in terms of notation).


Understanding `get_combination_wise_output_matrix` when investigating a multi-label classification problem I am currently working on a multi-label classification problem. I am using the scikit-multilearn library (further reading here)

I understand that train / test split is important for these types of problems - ensuring label combinations are represented well in both (scikit-multilearn implements their own split method for this purpose).

My current goal is to understand how this works so that I can evaluate it properly - I am reading from here primarily.

I am a little confused by the output of the get_combination_wise_output_matrix method. I understand at a high level it is giving me a breakdown of counts of label combinations for whatever order I specify.

Here is an example output I have observed:

The first question I have is does (5, 5): 1, mean anything other than the label at index 5 appears once, and this number is repeated for... reasons?

Additionally I haven't yet found any literature evaluating how / when to use different orders (my current approach to this is to qualitatively evaluate a few different options that seem reasonable given the specific problem I am trying (desperately) to solve

Thanks in advance for any help!

So order here means how many possible combinations of labels you want to compare (e.g., order=1 would by how often does each label appear, order=2 would be how often any two combinations of labels appear with values like 5,5 meaning "rows that only have a label in index 5 and no other label, and where the max order should be the number of labels you have --> 23 in your case. If you set order=23 and if you had a row that only had a label in index position 4, one of our results would be a vector of 4 repeated 23 times.

I think what's weird for me is that I would expect to see the "only one label in index 4" presented as [0, 0, 0, 0, 1, 0, ,,, 0, 0] (== len 24) instead of [4, 4, 4, ... 4, 4] (== len 24).

Odd presentation for sure. Kind of like sparse matrix notation.


GPU vs TPU for convolutional neural networks (NLP) I am testing ideas on IMDB sentiment analysis task by using word embeddings + CNN approach. What could explain a significant difference in computation time in favor of GPU (~9 seconds per epoch) versus TPU (~17 seconds/epoch), despite supposedly superior computational power of a TPU over GPU?

I don't think it makes a lot of sense to compare a generic TPU to a generic GPU. There is probably a factor of 10 or greater between a low-end GPU and the best GPUs on the market in terms of compute capability. Google has itself developed 3 generations of TPUs, each more powerful than the last.
I haven't kept up with the latest developments, but last I was aware, TPUs don't necessarily dominate over GPUs in all aspects. They may provide more compute per watt, but that's not the same as compute per unit or compute per dollar.
More specific to the architecture details, google has it's own documentation on exactly what sorts of computation TPUs are good at, versus what is more suitable for a GPU.


Why do we need data scientists if there are websites like this? Very basic question about data science as I'm a beginner. I noticed that there are sites like illustrated below, that help visualize and analyze data so that the business owner can have a better understanding of their business.
My question is that if there are sites like this, why are data scientists needed? What can they offer that sites like this can't?

Without having a complete knowledge of the features on that website I would say:

Data visualization is only one part on data scientist (ds) pipeline from data understanding thought model validation and model production as per CRISP-DM, and that site seems to be focused on that.

Introductory courses on data science most of the time work with "ready to use" data frames (Iris, Titanic) that do not reflect the real way in which data must be preprocessed and aggregated to the correct level (ex. you may have accounts level data that needs to be aggregated to user's level to predict user's default) also, in many cases you have multiple sources of data stored in a variety of places like relation tables or non structured information that will need to be queried and joined beforehand.

The latter role is commonly attributed as a "data engineer", however in small to medium projects the line between the two roles are less discernible. It is important to note, however, that this line should be fuzzy, as more often than not choices made by data scientists require work from a data engineering perspective, - as other answers below have touched more upon.
And this gives a wrong impression of simplicity on the task.
Besides, the above step would need for you to have at least basic understanding of the data you are working on in order to create "meaningful" features that otherwise will end just by aggregating by min, max and average (sometimes useful but not sufficient)

Finally, a trained model in a notebook would hardly being helpful if you cannot use that model in an completely deployed way (having a AWS lambda for example)


Deep Learning for non-continuous dataset I am working with this dataset which is record of student academic details and I want to predict the student's performance.

since the dataset is non-continuous I cannot apply CNN on this dataset.

How can I apply Deep learning on this kind(non-continuous) of dataset. I searched online but could not find anything relevant

Thank you!!

Deep Learning excels in problems where the data is relatively unstructured. Stacked layers help find conceptual features that can be used to infer rules.

Your dataset seems very structured at first glance. And, as you pose, it doens't look like it needs specialised layers that exploit sequential or spatial relations.

Neural network wise, this would warrant one or two fully connected layers, connected to an output layer (shaped to your wishes). However, typically, problems like these are tackled with a more biased approaches (eg Decision Tree Learners).


ML Models: How to handle categorical feature with over 1000 unique values I am trying to build an ML Classification model on a data set that contains quite a few categorical columns. However, few of them have over 1000 unique values. I am concerned that if I run one-hot encoding or pandas get dummies on them, it will simply result in too many features to work with.

So, I tried to find the top N unique values that account for, say, 90% of the underlying data and group the rest of them under say, 'Other' or 'miscellaneous'. But that's making the 'Other' or 'miscellaneous' value as the most prominent one. I am concerned that this might skew the model/results. Any pointers as to how I should handle such a scenario?

If necessary, there are other methods of encoding categorical features:

Label encoding (might need some judgement regarding implied ordering)
Target encoding
Hashing trick

A handy python package is Category Encoders: link

I would suggest first investigating if your model needs categorical variables to be encoded - lightgbm and catboost are examples of ML algorithms which support categorical variables.


Modify value of data in Orange I am new to Orange and it seems very useful for conducting exploratory data analysis.
One question I have is - can we use a widget to modify some values in the dataset?
For example, several numerical columns have some values of 0 which does not make sense and should be a "missing data". I do not want to ignore the line as some other column data are important. Also not all 0 should be replaced with "missing data" as for some column, a 0 is a valid data.
Can we do that with a widget? I could not figure it.
Thank you for the help!

You can accomplish this using a Feature Constructor widget with some conditional logic. For example, if looking at the Titanic survival data set, some fares are marked $0. This does not make sense since tickets have a non-zero value. You can create a new variable called fare_new where valid fares are copied over but 0 values are replaced with missing values. See screenshot below: