CNN Back Propagation without Sigmoid Derivative I'm new to CNN and trying to study some MATLAB sample codes (cause I need to know the internal calculation). I recently realized that the sample code I'm using doesn't multiply error by sigmoid's derivative in back propagation. The feed forward process has sigmoid as last layer's activation function so from my understanding, back propagation error = (outputs - target) * sigmoid's derivative(outputs). However, the author intentionally disabled this multiplication with the following code:
if cnn.loss_func == 'cros'
    if cnn.layers{cnn.no_of_layers}.act_func == 'soft'
        cnn.CalcLastLayerActDerivative = 0;
    elseif cnn.layers{cnn.no_of_layers}.act_func == 'sigm'
        cnn.CalcLastLayerActDerivative = 0;
    end
end

My reference code.
When cnn.CalcLastLayerActDerivative = 0, error is defined just as (outputs - target). I tried to initialize cnn.CalcLastLayerActDerivative = 1 so that sigmoid's derivative is considered in back propagation but then I got worse error rate. I'm not sure whether it's just because sigmoid's derivative is in the range [0,0.25] or I'm not understanding back propagation correctly. Does anyone know why this is happening and whether I should add sigmoid's derivative in my calculation?
Thanks!

error is defined just as (outputs - target)

This is the correct gradient for cross-entropy loss function with Sigmoid as the last layer.

For squared (quadratic) loss $$(y-f(x))^2,$$ the gradient is, as you said, $$(y-f(x))f'(x)$$ (constant $2$ is removed), but for binary cross-entropy loss $$y\text{log}f(x) + (1-y)\text{log}(1-f(x)),$$the gradient is $$yf'(x)/f(x) - (1-y)f'(x)/(1-f(x)),$$
since for Sigmoid we have $f'(x)=f(x)(1-f(x))$, by substitution the gradient becomes
$$y(1-f(x)) - (1-y)f(x)=y-f(x)$$
 To distinguish between these two gradients, author sets cnn.CalcLastLayerActDerivative = 0 to be checked later in an if statement in bpcnn.m file as follows (comments don't exist in the original code):

...
else
  % error = (f(x) - y)
  er = ( cnn.layers{cnn.no_of_layers}.outputs - yy);
...
if cnn.CalcLastLayerActDerivative ==1
    % change the error from (f(x) - y) to f'(x)(f(x) - y)
    er =applyactfunccnn(cnn.layers{cnn.no_of_layers}.outputs,cnn.layers{cnn.no_of_layers}.act_func, 1, er);
end

which means gradient is $(y-f(x))f'(x)$ for quad and $(y-f(x))$ for cros (bad variable name!).

As a side note, author only allows Sigmoid for cross entropy which means only binary classifier is supported (multi-class classifier requires SoftMax).

error('cross entropy is implemented only when last layer is sigmoid');

EDIT

Thanks to @Edison for pointing out that error and gradient were not handled  the same as loss values in the code, which substantially changed the final answer.


Dropout without the averaging The final step in dropout regularization is to multiply the weights by the dropout probability. This is motivated by analogy to bagging: averaging the weights of multiple nets. But it isn't truly that because of Jensen's inequality.

The effect of the averaging is basically just to shrink the weights by some factor. Why average at all? Has anyone tried simply not averaging, and controlling the magnitude of the parameters with e.g. L2 regularization?  One would still get the benefits of preventing codependence, but one would need to regularize some other way.

With dropout, the sum of activations feeding each neuron during training and testing needs to be roughly the same. The problem is that during training some neurons are masked out, and this does not happen during testing. Therefore there has to be a scaling difference between train and test phases using dropout, otherwise each neuron would be receiving larger magnitude inputs and the network as a whole could behave very differently.

This is not mathematically the same as averaging the weights - although the effect is compared intuitively to bagging many related neural networks and averaging their output. Instead, it is usually explained that dropout scales the neuron activations to have same expected total magnitude when dropout is active (during training) vs inactive (during testing/prediction).

Applying L2 or any other kind of parameter-controlling regularisation technique will not help with this, because these do not change any of the activations between train and test phases.

You either treat training activation values as canonical and adjust activations to be lower at test time (multiply by keep probability $p$), which is "classic dropout" or you treat test activation values as canonical and multiply activations by $\frac{1}{p}$ during training, which is called "inverse dropout". The latter approach is nowadays more common, as it is possible to completely ignore dropout layers during prediction that way.


Are Correlithm Objects used for anything in the industry? This refers to a system described in a book by Nick Lawrence titled "Correlithm Object Technology". The author coined the term "correlithm" as a combination of "correlation" and "algorithm".

The Correlithm Objects ("COs") described in the book are, in the author's view, "primary data tokens" in biological neural systems and are central to "all high-level data representation, storage, and manipulation". In addition to this the author describes the COs as "important mathematical objects of the statistics of bounded, high-dimensional spaces".

Considering these descriptions it seems like these COs could be useful for AI. What I was curious about is whether they are actually used anywhere in the industry, and if so, in what kind of situations?

Looks like lots of marketing around a specific case of what everyone was already doing to sound bigger than it is.  So, are people implementing mathematical frameworks in software to extract and use the structure within data?  Absolutely.  Are people using the word 'Correlithm' when doing it?  Not as far as I've seen.


Data Exploration Led Conversion to Ordinal Variable [I encountered this question at an interview few weeks ago and I am still not clear.]

If all the values in a categorical column fuel_mileage come from the set {poor, good, very_good}, then we can make the column ordinal due to universal and ordered relationship amongst {poor, good, very_good}, so this is kind of obvious.

However, imagine the label column in this same dataset is engine_longevity, so that we are studying all other variables in the context of their relationship with it. During data exploration, it turns out that another categorical column, manufacturer, all of whose values come from set {H, S, J, K}, has a very strong correlation with label engine_longevity, so much so that the choice of H, S, J, K in a given sample essentially dictates the label. Therefore, as for as this data set is concerned, H, S, J, K have an ordered relationship with respect to label engine_longevity. The question is:

Will you make column manufacturer ordinal? If yes, how strong should the relationship between manufacturer and the label engine_longevity be? And what metric will you use to measure it?
If you will not make manufacturer column ordinal, why?
More generally, should the choice of making a column ordinal come from the mutual-relationship of values within that column alone? Or, the relationship of values in a column with the label should be taken into consideration?

If there is no hard-and-fast rule, I would like to know how the community here will approach this situation.

You are describing highly correlation features. The most common way to measure the correlation between two variables measured on at least an ordinal scale is the Spearman rank-order correlation coefficient.
Generally if two features are near perfectly correlated, one feature can be dropped from analysis.


Best machine learning algorithm for understanding specific conditional structures Being still a bit new to neural networks, I wish to use some form of machine learning but I am not sure which one is most suited.

I have a toy-example of what I am working on. Tips are much appreciated.

Assuming there are chunks of data (not pixels of an image :)) which all are based on a root variable, being 'a'.
This 'a' is the 'thing' and the other variables are aspects.

Also assuming it's a finite set of things and that each thing 'a' has four ('b', 'c' and 'd') aspects.

I wish to make a program that learns that thing ('a':1) often occurs with ('b':0.2), ('c' :0.8), and ('d':0.3).
There is a catch however, I want there to be a certain conditional structure underlying the co-occurrence of a and the respective values of (b, c, d).

For instance:

('a':1) often     (prob. 0.7) occurs with ('b':0.2), ('c':0.8), and ('d':0.3)
('a':1) sometimes (prob. 0.6) occurs with ('b':0.2), ('c':0.5), and ('d':0.7)
('a':1) sometimes (prob. 0.4) occurs with ('b':0.3), ('c':0.4), and ('d':0.2)
('a':1) rarely    (prob. 0.2) occurs with ('b':0.5), ('c':0.3), and ('d':0.8)

For a given ('a':2) this would all be different.
There is no implied necessity for the values of b, c and d to sum to 1. They are mere placeholder values representing aspects (0.2 could be green, 0.8 purple, etc.).

I want to make sure the program understands that there is an inherent dependency between b, c and d, as well as an 'a':x given (b, c, d).

If anyone has suggestions based on this, I would be very grateful.
Thanks!

EDIT: I am working in python and was thinking about dictionaries as the data-structure, but if there are better solutions (also depending on the algorithm perhaps) I am all ears.

To me, this describes conditional probabilities. And the first thing that springs to my mind when it comes to learning conditional probabilities is a Bayesian network (BN), which is an example of a probabilistic graphical model. In fact, BNs can be fitted using Maximum Likelihood Estimation (MLE), which corresponds to counting occurrences (similar to "Naive Bayes", if you have used that). An excellent book on this is Barber.

Another model that corresponds to your probabilistic problem, and which is more neural network-like, is a Restricted Boltzmann Machine (RBM):

(source)

Once again, this involves learning conditional probabilities, like $p(v_0 | h_0, h_1, h2), p(v_1 | h_0, h_1, h2), p(v_2 | h_0, h_1, h2), \ldots$. This is a nice introduction to them. Unlike neural networks, which are trained using backpropagation and are not probabilistic, RBMs are indeed probabilistic and are trained using Contrastive Divergence.


How do we implement a custom loss that backpropagates with PyTorch? In a neural network code written in PyTorch, we have defined and used this custom loss, that should replicate the behavior of the Cross Entropy loss:

def my_loss(output, target):
    global classes

    v = torch.empty(batchSize)
    xi = torch.empty(batchSize)

    for j in range(0, batchSize):
        v[j] = 0
        for k in range(0, len(classes)):
            v[j] += math.exp(output[j][k])

    for j in range(0, batchSize):
        xi[j] = -math.log( math.exp( output[j][target[j]] ) / v[j] )

    loss = torch.mean(xi)
    print(loss)
    loss.requires_grad = True
    return loss

but it doesn't converge to accetable accuracies.

You should only use pytorch's implementation of math functions, otherwise, torch does not know how to differentiate them. Replace math.exp with torch.exp, math.log with torch.log.

Also, try to use vectorised operations instead of loops as often as you can, because this will be much faster.

Finally, as far as I can see, you are merely reimplementing a log loss in pytorch, any reason why you don't use one that has been implemented by default? (see here or here)

[EDIT]: If after having removed math operations and implemented a vectorised version of the loss, it still does not converge, here are a few pointers on how to debug it:

Check that the loss is correct by calculating the value manually and compare it with what the function outputs
Compute the gradient manually and check that it is the same as the values in loss.grad, after running loss.backward() (more info here)
Monitor the loss and the gradient after a few iterations to check that everything goes right during the training


How can I compare the contribution of two predictors in two different sorts for machine learning algorithms? I'm new to machine learning and try to clarify my problem in research. I just wonder if I can compare the importance of two different variables in two different sorts. For example, A and B are two variables that I want to compare their contribution to ML accuracy. The rest of the variables (like C, D, and E) for each sort are the same.

If I got the results that the rest of the variables (C, D, and E) contributes more (obtain a higher importance score) in the model predicted by A and the rest, can I just say B performs better for ML and its contribution is more significant than the model with A and the rest?

No you can not.
You are forgeting about variable interactions (C-A,D-A,E-A, etc...)  that could favor A.

You could answer following qestion: If I were to measure information in my variables via variability how would I proceede? For example PCA

The more information you have in the variable(predictor) the higher the chance that it will contribute more to accuracy


Predicting next element of a sequence given small amount of data I have data of bank branches and amount of revenue they have generated in a month. The data looks like this:

I am tasked to find the expected revenue for the branch for the next month using machine learning. Initially I was planning to use LSTM networks for such analysis, but I doubt its possible with such small amount of data.
I personally think machine learning is an overkill for such task. What would be the most appropriate way to predict the revenue for next month? I thought about increasing the amount of data by treating every branch as equal and using the row corresponding to each branch as separate instance for training (but I doubt that is a correct approach).
Any advice would be appreciated

You might find the link helpful.
https://towardsdatascience.com/how-to-model-time-series-data-with-linear-regression-cd94d1d901c0


Keras: LSTM unit nums vs timesteps I am using Keras LSTM (Tensorflow backend) to fit a time series model. Here is my model:

model = Sequential()
model.add(LSTM(LSTM_nums, input_shape=(timestep,data_dim)))
model.add(Dense(2))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)

I am wondering is there a constraint or rule of thumb (or best practice) for setting the LSTM_nums value with a given timestep? I.e. should there be a relationship between LSTM_nums ~ timestep ? Thanks!

The n_neurons (LSTM_nums) on the first input layer should match those of n_features or in your case data_dim.

Further reading: LSTM Neural Network for Time Series Prediction


Integrate remote sensing & GIS data in a CNN I am trying to develop a CNN model that takes as input a set of parameters derived from satellite imagery.

What are the necessary steps to go through in order to make the Geotiff data acceptable as a valid input for a keras model?

Keras functional API can be used to create models with multiple inputs (E.g. : Image, Categorical and Numerical).

From Geotiff, you can extract :

TIFF
CSV with other features

TIFF will be an input to stack of CNN layers and other features will feed dense layers. Output from both of these is combined in a dense layer.

Example : https://www.pyimagesearch.com/2019/02/04/keras-multiple-inputs-and-mixed-data/


How to determine number of Neurons setup in Convolutional Neural Networks? Im new to deep learning and still learning on how to train my neural networks from the scratch. Sometimes I watch tutorial on YouTube or even online courses on the MOOC platforms. The base of a Convolutional Neural Networks usually has Conv2D and MaxPooling layers to make the input much more smaller and easy to be trained.

The thing is sometimes this tutorial online use the setup of Conv2D with higher number of neurons follows with other Conv2D with smaller one. Such as 1st layer is Conv2D(512...), 2nd layer is Conv2D(256....) and etcetera. Another tutorial setup is the increasing one, such as starts with Conv2D(16....), then Conv2D(32....) and increasing. These teacher doesn't tell us why they code the setup as so.

How do we know which setup to use?
Is there any differences between them? Can't find on the net how to refer this case.
If there's a paper that already described this case I would like to have it thanks.

A typical best practice is to read related papers or check github projects with information about similar architectures and how other ML engineers structured them. Usually, you can see some certain patterns in CNN architectures like increasing the depth along the pipeline (or number of channels) while decreasing width and height of results in each layer. Since the search space is huge and time so limited, it is usually better to rely upon already made architectures, and use, if possible, transfer learning to shorten training cycles. On the other hand, if you are doing pure research and want to create your top-notch, new state-of-the-art CNN architecture, you will need to carry out a very big effort on CNN architecture engineering as well as hyperparameter tuning, luckily on a systematic basis.


Understanding neural network probability I'm training a CNN model with two classes to predict. I know it gives me a probability for one class and for the other one, and I also know I can get the predicted label, but I don't the results given. Isn't the sum of the output for each evaluated input supposed to be equal 1.0? For instance:

[[0.2858745  0.85059494]
 [0.2858745  0.85059494]
 [0.6040499  0.5927084 ]
 [0.8403308  0.291448  ]
 [0.04195209 0.95504093]
 [0.79433376 0.21279709]
 [0.79433376 0.21279709]
 [0.01326967 0.9891382 ]
 [0.0153821  0.9867737 ]
 [0.79433376 0.21279709]
 [0.01617167 0.98520505]
 [0.01351487 0.98596036]
 [0.01473185 0.9846144 ]
 [0.00896762 0.9899838 ]
 [0.00936404 0.9893628 ]]

Is there something I didn't get?

Code:

model_05_01 = Sequential()
model_05_01.add(Conv1D(filters=16, kernel_size=12,
                 input_shape=(x_train.shape[1], 1)))
model_05_01.add(MaxPooling1D(pool_size=4))

model_05_01.add(Conv1D(filters=32, kernel_size=12))
model_05_01.add(MaxPooling1D(pool_size=4))

model_05_01.add(Conv1D(filters=16, kernel_size=12))
model_05_01.add(MaxPooling1D(pool_size=4))

model_05_01.add(Flatten())

model_05_01.add(Dense(16, activation='relu'))
model_05_01.add(Dense(2, activation='sigmoid'))

model_05_01.compile(loss='logcosh', optimizer='adam',
              metrics=['accuracy'])

What you have declared here is actually a 2-class multi-label classifier. It is trying to learn the probability of being each class independently where they are not mutually exclusive.

While that is a valid classifier problem to solve, I'm guessing what you mean is that you have an exclusive 2-class problem: it's either positive, or negative. In that case, you want the final layer to be Dense(1, activation='sigmoid')


unit-testing Machine Learning models I have been asked to unit-test my machine learning model(not the code that made the model). Since we wouldn't actually know what predictions models make, how to carry out the unit-testing to check the model's predictions against? How is this done?
EDIT 1:
The machine learning model I have is trained on tabular data of patients. let's take an example of cancer prediction(I am not allowed to disclose the actual one, but this example is very close). It takes multiple reading from various tests as inputs and outputs how close or how risky a patient is to get cancer.
EDIT 2:
Is there any way, like testing for range of value for every set of inputs (or) adversarial inputs(inputs that are sure model will fail on) or extreme input cases. What ate the best practices for this?

In the above case, you can collect annotated data ( which is not seen by the model during training) and validate the predictions made by the model.
And another way is if you are a domain expert or have sufficient knowledge on the data, you can tweak the input values and test the predicted output with your expected output.


Python Text Classification - Data that does not fit into any category I am having a lot of trouble finding any kind of answers to this problem i am facing.
I have a few text classifiers that i am testing out, and they work well for data that does fit into any predefined category, but if I input, lets just say "fhjakdlfsah", it will still assign it to some category because i guess the predict_proba functionality has to add up to 1 for all of the categories.
Is there something i am missing here? I am having such a hard time finding a solution to this, and I would imagine it is a very common thing to deal with. Right now i am working with gradientboosting from sklearn, and tried wrapping it in a onevsrestclassifier as suggested by others, but it is still having the same thing where all probabilities are adding up to one, and it is getting assigned the highest probability
Basically I am looking for a solution that can say either, yes this fits into one of these categories, or no, this does not fit into any of these categories.
Any help would be greatly appreciated as I am getting quite stuck here
Thanks!

The main assumption in supervised learning is that the training set is a representative sample of the space of all possible instances for the problem.
This is why in the regular classification setting there is simply no way to consider a "not in any class" option: what the model learns is to distinguish between the classes seen in the training set, nothing else. In the standard one-vs-rest multi-class setting It's important to understand that the model doesn't predict whether an instance is likely to belong to "class A" in general, it only predicts whether the instance is more likely to belong to class A compared to any other known class. This is a common misunderstanding, but one-vs-rest is not an answer to the problem of "not in any class".
There are options to deal with this problem, but one must design the system so that the model can deal with such cases. In general the problem is actually open-set classification: in regular classification the set of possible classes is closed, as mentioned above. The open-set classification setting is much less standard and usually harder, since the model has to make a prediction about something it does not know from its training data. Here are a few options:

Treat the problem as an outlier detection problem: before running the regular classifier, eliminate abnormal instances which don't look like the regular training set cases.
Multi-label classification: in multi-label classification an instance may belong to zero, one or several classes. A binary classifier is trained for every class, and an instance may belong to none of the classes. This is more flexible than multi-class classification, but it's still closed-set classification: for example the model is supposed to have seen examples in the training set which don't belong to any class.
One-class classification is a type of classification where the model learns to identify a single class "in general", not by contrast to any other class. This is proper open-set classification: if a model is trained for every known class and every model predicts the instance as negative, then the instance doesn't belong to any known class.


What's the best metric for evaluate an estimator for a multi class problem with class imbalance dataset? accuracy, precision, f1, ROC are good for binary single class problem.

but for more complex problem (imbalance multi-class problem), what should i use?

Do you have any recommendation?

One standard metric is the top-1 or top-5 test error rate. For instance, for top-5, your model predict 5 most likely labels, and if none of the 5 labels is the ground truth label, you mark this instance as an error. This is usually a standard metric when people working with the ImageNet data. See example here usage here. This metric does not explicitly count for that the classes are imbalanced.

Another powerful metric is the Mean Average Precision (mAP), where you calculate the Average Precisoin (AP) for each class and then average among all classes. In this metric the smaller classes get equal weights compared to larger classes. The idea comes from the information retrieval community.  This metric is also suitable for multi-label classification. See example usage here.


Is RL applicable to environments that are totally RANDOM? I have a fundamental question on the applicability of reinforcement learning (RL) on a problem we are trying to solve.

We are trying to use RL for inventory management - where the demand is entirely random (it probably has a pattern in real life but for now let us assume that we have been forced to treat as purely random).

As I understand, RL can help learn how to play a game (say chess) or help a robot learn to walk. But all games have rules and so does the ‘cart-pole’ (of OpenAI Gym) – there are rules of ‘physics’ that govern when the cart-pole will tip and fall over.

For our problem there are no rules – the environment changes randomly (demand made for the product).

Is RL really applicable to such situations?

If it does - then what will improve the performance?

Further details: - The only two stimuli available from the ‘environment’ are the currently available level of product 'X' and the current demand 'Y' - And the ‘action’ is binary - do I order a quantity 'Q' to refill or do I not (discrete action space). - We are using DQN and an Adam optimizer.

Our results are poor - I admit I have trained only for about 5,000 or 10,000 - should I let it train on for days because it is a random environment?

thank you Rajesh

Taking your question title literally:

  Is RL applicable to environments that are totally RANDOM?

The answer would be no. In a totally random environment there is little to nothing that can be learned.

However, you don't actually have a totally random environment. You have some quantities that fluctuate a lot in a way that you don't understand. Otherwise your environment behaves very logically - if there is demand for Y items, and you have X in stock, then you will end up with X - Y in stock if X > Y, or 0 in stock and Y - X incomplete orders otherwise. This is a very structured rule that you can definitely associate rewards with and learn. Presumably you have costs for ordering and holding stock and opportunity costs for not supplying orders.

A simple variant of your situation is used as a toy example in Sutton & Barto: Reinforcement Learning, an Introduction in chapter 4, called "Jack’s Car Rental" where the goal is to optimise stock at two locations with random demand occurring at either location. This toy problem is made easier by defining the distribution, and using it in a model-based way. But that is not necessary in general.

  Is RL really applicable to such situations?

In your case, probably yes. Although you do have to assume that "totally random" is just a way of phrasing "highly variable" and not literally 0 one day, 30 million the next day and 7.5 the next day. There are going to be limits to the orders and they will follow some distribution.

With very high variance then you are likely to find it hard to reach a balance point of costs for holding stock vs costs for missing orders, but in principle this is solvable and RL is a reasonable tool to attempt the solution.

  If it does - then what will improve the performance?

Check whether any fluctuations in demand depend on any variables that you can collect and add those variables to the state. For instance, if demand has some weekly or seasonal variation, then those parts of the date should be part of your state representation.

Understanding and predicting the distribution of demand, even if not the exact values, would also help with simulations and planning algorithms. The RL algorithm will over time learn the likely distribution, but it can only base its own predictions on state variables that you have let it observe.

You might be able to go one better: If demand is mostly independent of the stock levels that you hold, then you can separate the problem of predicting it and use more robust supervised learning to add a "predicted demand" feature to the state. The RL will on top of this prediction learn the costs associated with just trusting this prediction vs allowing for some extra incoming orders just in case etc.


Logistic regression GD implementation in Python I am implementing logistic regression in Python with the regularized loss function like this:

 But the gradient algorithm works bad. Read the bold text first, please! Just paste the code cell by cell

import numpy as np, scipy as sp, sklearn as sl
from scipy import special as ss
from sklearn.base import ClassifierMixin, BaseEstimator
from sklearn.datasets import make_classification
import theano.tensor as T

Here is the loss function: (scipy is to "clip" the logarithm's arg near 1)

def lossf(w, X, y, l1, l2):
     w.resize((w.shape[0],1))
     y.resize((y.shape[0],1))

     lossf1 = np.sum(ss.log1p(1 + ss.expm1(np.multiply(-y, np.dot(X, w)))))
     lossf2 = l2 * (np.dot(np.transpose(w), w))
     lossf3 = l1 * sum(abs(w))
     lossf = np.float(lossf1 + lossf2 + lossf3)
     return lossf

Here is the gradient function:(??PROBLEM HERE?? -see the end)

def gradf(w, X, y, l1, l2):
    w.resize((w.shape[0],1))
    y.resize((y.shape[0],1))

    gradw1 = l2 * 2 * w
    gradw2 = l1 * np.sign(w)
    gradw3 = np.multiply(-y,(2 + ss.expm1(np.multiply(-y, np.dot(X, w)))))
    gradw3 = gradw3 / (2 + (ss.expm1((np.multiply(-y, np.dot(X, w))))))
    gradw3 = np.sum(np.multiply(gradw3, X), axis=0)
    gradw3.resize(gradw3.shape[0],1)
    gradw = gradw1 + gradw2 + gradw3
    gradw.resize(gradw.shape[0],)
    return np.transpose(gradw)

Here is my LR class:

class LR(ClassifierMixin, BaseEstimator):
    def __init__(self, lr=0.0001, l1=0.1, l2=0.1, num_iter=100, verbose=0):
        self.l1 = l1
        self.l2 = l2
        self.w = None
        self.lr = lr
        self.verbose = verbose
        self.num_iter = num_iter

def fit(self, X, y):
    n, d = X.shape
    self.w = np.zeros(shape=(d,))
    for i in range(self.num_iter):
        g = gradf(self.w, X, y, self.l1, self.l2)
        g.resize((g.shape[0],1))
        self.w = self.w - g
        print "Loss: ", lossf(self.w, X, y, self.l1, self.l2)
    return self

def predict_proba(self, X):
    probs = 1/(2 + ss.expm1(np.dot(-X, self.w)))
    return probs

def predict(self, X):
    probs = self.predict_proba(X)
    probs = np.sign(2 * probs - 1)
    probs.resize((probs.shape[0],))
    return probs

Here are the tests:

X, y = make_classification(n_features=100, n_samples=100)
y = 2 * (y - 0.5)
clf = LR(lr=0.000001, l1=0.1, l2=0.1, num_iter=10, verbose=0)
clf = clf.fit(X, y)
yp = clf.predict(X)
yp.resize((100,1))
accuracy = int(sum(y == yp))/len(y)

This doesn't converge. But if I replace my gradw3 with theano:

gradw3 = get_gradw3(w,X,y)

where:

w,X,y = T.matrices("wXy")
logloss = T.sum(T.log1p(1 + T.expm1(-y* T.dot(X, w))))
get_gradw3 = theano.function([w,X,y],T.grad(logloss,w).reshape(w.shape))

it converges to 100% accuracy. That means, my gradw3 is implemented wrong, but I can't find a mistake.

Actually, i have finally made it work. I dont know, what exactly was the crucial change, but here's the extract of my changes:

replaced all np.multiply with *
Decreased learning rate and regulizers
Applied np.nan_to_num to exponents

So here is the final code:

def lossf(w, X, y, l1, l2):
    w.resize((w.shape[0],1))
    y.resize((y.shape[0],1))

    lossf1 = np.sum(ss.log1p(1 + np.nan_to_num(ss.expm1(-y * np.dot(X, w)))))
    lossf2 = l2 * (np.dot(np.transpose(w), w))
    lossf3 = l1 * sum(abs(w))
    lossf = np.float(lossf1 + lossf2 + lossf3)
    return lossf

def gradf(w, X, y, l1, l2):
    w.resize((w.shape[0],1))
    y.resize((y.shape[0],1))

    gradw1 = l2 * 2 * w
    gradw2 = l1 * np.sign(w)
    gradw3 = -y * (1 + np.nan_to_num(ss.expm1(-y * np.dot(X, w))))
    gradw3 = gradw3 / (2 + np.nan_to_num(ss.expm1(-y * np.dot(X, w))))
    gradw3 = np.sum(gradw3 * X, axis=0)
    gradw3.resize(gradw3.shape[0],1)
    gradw = gradw1 + gradw2 + gradw3
    gradw.resize(gradw.shape[0],)
    return np.transpose(gradw)
class LR(ClassifierMixin, BaseEstimator):
    def __init__(self, lr=0.000001, l1=0.1, l2=0.1, num_iter=100, verbose=0):
        self.l1 = l1
        self.l2 = l2
        self.w = None
        self.lr = lr
        self.verbose = verbose
        self.num_iter = num_iter

    def fit(self, X, y):
        n, d = X.shape
        self.w = np.zeros(shape=(d,))
        for i in range(self.num_iter):
            print "\n", "Iteration ", i
            g = gradf(self.w, X, y, self.l1, self.l2)
            g.resize((g.shape[0],1))
            self.w = self.w - g
            print "Loss: ", lossf(self.w, X, y, self.l1, self.l2)
        return self

    def predict_proba(self, X):
        probs = 1/(2 + ss.expm1(np.dot(-X, self.w)))
        return probs

    def predict(self, X):
        probs = self.predict_proba(X)
        probs = np.sign(2 * probs - 1)
        probs.resize((probs.shape[0],))
        return probs


Why do we have to remove most common words for text analysis? I am trying to do sentiment analysis the task is to classify racist tweets from other tweets. And I have read many articles and many have mentioned to remove the most common 10 words from the column because their presence will not of any use in classification of our text data.

So these are my top 10 most common words on my dataset.

[('love', 4271),
 ('day', 3572),
 ('amp', 2709),
 ('happy', 2651),
 ('u', 1840),
 ('time', 1771),
 ('im', 1770),
 ('life', 1756),
 ('like', 1700),
 ('today', 1591)]

If I remove these will my classification model be more accurate?

Similarly they are also recommending to remove the top 10 rare words from the column.

I want to know why? Any help

The simplest way to explain why it may be advantageous to remove the most common words is that they don't give us much information. In your case of classifying racist tweets, words like "and", "a", "the", etc. don't help the classifier and may act as noise which negatively impacts performance.

I would not say that the removal of the n most popular words will guarantee that the model will be more accurate, but it is a parameter you can explore. Outside of complete removal of the most common words, you may want to look into techniques like subsampling.


How to define a distance measure between two IP addresses? I have IP addresses as feature and I would like to know how much two IP addresses are similar to each other to use the difference in an Euclidean distance measure (in order to quantify the similarities of my data points). What tactic can I use for this?

If I understood them correctly, both Jeremy and Edmund's (first) solutions are the same, namely, plain euclidean distance in a 4-dimensional space of IP addresses.BTW, I think a very fast alternative to euclidean distance would be to calculate a hamming distance bit-wise.

Edmund's first update would be better than his second. The reason is simple to state: his 2nd update tries to define a distance measure by considering a non-linear function of the coordinates of a 4D vector. That however will most likely destroy the key properties that it needs to satisfy in order to be a metric, namely

Injectivity: $d(IP_1,IP_2)=0 \iff IP_1=IP_2$,
Symmetry: $d(IP_1,IP_2)=d(IP_2,IP_1)$, and
Triangular inequality: $d(IP_1,IP_2)\leq d(IP_1,IP_3)+d(IP_3,IP_2)\,\forall IP_3$.

The latter is key for later interpreting small distances as close points in IP space. One would need a linear (in the coordinates) distance function. However, simple euclidean distance is not enough as you saw.

Physics (well, differential geometry actually) could lead to a nice solution to this problem: define a metric tensor $g$. In plain english, give weights to each pair of coordinates, take each pair difference, square it and multiply it by its weight, and then add those products. Take the square root of that sum and define it as your distance.

For the sake of simplicity, one could start trying with a diagonal metric tensor.

  Example: Say you take $g=\begin{pmatrix}1000 &0 &0 &0 \\0 &100&0&0\\0&0&10&0\\0&0&0&1\end{pmatrix}$ $IP_1=(x_1,x_2,x_3,x_4)$ and
  $IP_2=(y_1,y_2,y_3,y_4)$. Then the square of the distance is given by
  $$d(IP_1,IP_2)^2=1000*(x_1-y_1)^2+100*(x_2-y_2)^2+\\ \,+10*(x_3-y_3)^2+1*(x_4-y_4)^2$$
  For $IP_1=192.168.1.1,\,IP_2=192.168.1.2$ the distance is clearly 1.
  However, for $192.168.1.1$ and $191.168.1.1$ the distance is
  $\sqrt{1000}\approx 32$

Eventually you could play around with different weights and set a kind of normalization where you could fix the value of the maximal distance $d(0.0.0.0,FF.FF.FF.FF)$.

Furthermore, this set up allows for more complex descriptions of your data where the relevant distance would contain "cross-products" of coordinates like say $g_{13}*(x_1-y_1)*(x_3-y_3)$.

EDIT: While this would be a better "weighting" method than using those other I addressed, I realize now it is actualy meaningless: As Anony-Mousse and Phillip mention, IP are indeed 32 dimensional. This means in particular that giving the same weight to all bits in say the 2nd group is in general not sound: One bit could be part of the netmask while the other not. See Anony-Mousse answer for additional objections.


Why margin-based ranking loss is reversed in these two papers? For knwoledge graph completion, it is very common to use  margin-based ranking loss

In the paper:margin-based ranking loss is defined as

$$ \min \sum_{(h,l,t)\in S} \sum_{(h',l,t')\in S'}[\gamma + d(h,l,t) - d(h',l,t')]_+$$

Here $d(\cdot)$ is the predictive model, $(h,l,t)$ means a positive training instance, and $(h',l,t')$ means a negative training instance corresponding to $(h,l,t)$.

However, in the Andrew's paper, it defines

$$ \min \sum_{(h,l,t)\in S} \sum_{(h',l,t')\in S'}[\gamma + d(h',l,t') - d(h,l,t)]_+$$

It seems that they switch the terms $d(h',l,t')$ and $d(h,l,t)$.

My question is that

does it matter to switch $d(h',l,t')$ and $d(h,l,t)$? it's real strange definition. Thanks

In this paper, $d$ denotes "dissimilarity" which should be minimized for positive samples.

In this paper, $d$ ($g$ in the paper) denotes "similarity" which should be maximized for positive samples (or equivalently $-g\left(T^{(i)}\right)$ should be minimized)


``Hierarchial features extraction'' in Multilayer Perceptron models I am referring to plain neural networks, MLPs.
I got to read the paper by Glorot and Bengio (2010), Understanding the difficulty of training deep feedforward neural networks.

Therein I read an interesting statement: ``Deep learning methods aim at learning feature hierarchies with features from higher levels of the hierarchy formed
by the composition of lower level features''.

What does that mean in the framework of MLPs? As far as I could understand, such a features extraction and abstraction process is made feasible by the use of convolutional layers, hence more advancer Deep Learning models.

In MLP, you can imagine it as similar to finding features in different spaces.
Like for n features you come up with k feature by using PCA, similarly here MLP is converting the n features into k* features in different space.


Number of Nodes in Isolation Forest I am currently reading this paper on Isolation Forest.

At page 3, there is a definition of Isolation Tree and there are a couple of sentences that I don't understand:

  Given a sample of data X = {x1, ..., xn} of n instances from a
  d-variate distribution, to build an isolation tree (iTree), we
  recursively divide X by randomly selecting an attribute q and a split
  value p, until either: (i) the tree reaches a height limit, (ii) |X| =
  1 or (iii) all data in X have the same values.

Here, I would like to understand what do the points (ii) & (iii) mean. In the point (iii), when the author says 'all data in X have same values', does it mean the same anomaly score?

And there is also a block of text which I am failing to understand completely. Can someone help me understand this text too..

  Assuming all instances are distinct, each instance is isolated to an
  external node when an iTree is fully grown, in which case the number
  of external nodes is n and the number of internal nodes is n − 1; the
  total number of nodes of an iTrees is 2n − 1

Thanks

(ii) means after splitting the data you end up with a single instance in the node, so there is nothing left to do

(iii) means after splitting the data all the samples in the node are identical ("duplicates"), and again there is nothing left to do

  Assuming all instances are distinct, each instance is isolated to an
  external node when an iTree is fully grown, in which case the number
  of external nodes is n and the number of internal nodes is n − 1; the
  total number of nodes of an iTrees is 2n − 1

Isolating an instance means splitting the data until that instance ends up in a leaf by itself. If you have $n$ instances, you will need $n$ external ("end") nodes. One for each instance. The internal nodes are all the nodes before the end nodes, including the root that contains all the instances. One can show that their number is $n-1$. For example, if you have $n=4$ you can split it in (4)-(2,2)-((1,1)(1,1)). So you have $1+1+1+1=4=n$ end nodes, and $1+2=3=n-1$ inner nodes.

Of course, the splits don't have to be equal. It could also be something like (4)-(3,1) Where you then split the (3) into (2,1) and the (2) into (1,1) But the counts will be the same. You'll have 3 inner nodes (4),(3) and (2) and 4 external nodes. (Best to draw it out)

The total is therefore the number of end nodes plus the number of inner nodes $n +(n-1) = 2n-1$.


What is the difference between "offline trained model" and "pretrained model"? I am confused that both are same or not, and then how can I differentiate with the online training model.

Every pre-trained model is an offline-trained model, but not the
  reverse.

Offline training is any training that leaves the model unchanged when new observations arrive, i.e. it has an end. Online training constantly updates the model with the help of new, incoming observations without using the previous training points, although, having a limited memory of previous samples compared to all seen samples is OK. Therefore, training offline periodically on all the training points, no matter how frequent, is not the same as online training.

We can use offline training for a model that supports online training, however, to train online, the model must allow such training. For example, common implementation of SVM cannot be trained on N new data points without re-using the previous training points. In contrast, Bayesian models are natural candidates for online learning, as they are trained by updating the belief (model) upon new observations, i.e. posterior update.

Pre-training is an offline training followed by a main, task-specific training, hence the prefix "pre".

For example, a $512 \times 512 \times 3 \rightarrow 128$ dimensionality reduction model is pre-trained on a large dataset of RGB images (either supervised, or self-supervised). Then, the pre-trained model is used to reduce the dimension of our [new] images to $128$, which is then being fed to the main, task-specific model.

Note that the word "self-supervised" (supervised by input itself) is currently used for models that try to reconstruct, or predict the whole, or part of the input as close as possible; e.g., auto-encoders, or some language models such as Word2Vec.


How to find the values that never matched in a full join Hello I have done this full join query:

SELECT
  F.date AS Campaign_Date,
  F.Store AS AS Campaign_Store,
  F.IMPRESSIONS,
  F.CLICKS,
  F.SPEND,
  C.Date AS visitors_Date,
  C.ANNEE_MOIS,
  C.ANNEE,
  C.MOIS,
  C.Store AS visitors_Store,
  C.FREQUENTATION_Store
FROM
  `project.campaigns`  F
FULL JOIN
  `proiject.visitors` C
ON (
  C.Store= F.Store
  AND C.Date =F.date
  )

And now, I want to retireve that values of column FRQ_Store that never matched any of those Campaign_Store ! In other words, how can I retrieve all the value of two columns that never matched in a full join query ?

Thank you!

What you appear to want is a left outer join on NULL, this will give you only the rows of the left table which do not have a match with the right table:

SELECT    F.Store
FROM      `project.campaigns`  F
LEFT JOIN `project.visitors`   C
ON (
      F.Store = C.Store
  AND F.Date  = C.Date
  )
WHERE C.Store IS NULL


Ordinal feature in decision tree I am curious if ordinal features are treated differently from categorical features in decision tree, I am interested in both cases where target is categorical or continuous.

If there is a difference, could you anybody point to good source with explanation and any packages (R or Python) supporting it?

As per my knowledge, it doesn't matter for a decision tree model whether the features are ordinal or categorical. The goal is to create a model that predicts the value of a target variable by learning simple decision rules inferred from the data features. Decision trees describe patterns by using a list of attributes.

For a more detailed explanation, I am providing here some links which you will find helpful for such queries.
http://www.ibm.com/support/knowledgecenter/SS3RA7_17.0.0/clementine/nodes_treebuilding.html

http://scikit-learn.org/stable/modules/tree.html

http://www.ryerson.ca/~rmichon/mkt700/SPSS/Creating%20Decision%20Trees.htm


Dimensionality reduction convolutional autoencoders I don't understand how convolutional autoencoders achieve dimensionality reduction. For FFNN based autoencoder, the reduction is easy to understand: the input layer has N neurons, and the hidden ones have M neurons, where N is greater than M.
Instead, in a convolutional autoencoder, the input image is wide and thin, and it becomes small and thick. It results in an amount of information that is greater than the initial ones.
I report a practical example to explain better what I mean:
# INPUT: 28 x 28 x 1 (wide and thin)

conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img) #28 x 28 x 32
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) #14 x 14 x 32
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1) #14 x 14 x 64
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) #7 x 7 x 64
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)

# OUTPUT: 7 x 7 x 128 (small and thick)

In this example, we start from a 28x28 single-channel image (784), and the encoder output will be 7x7x128 (6272). Is this a dimension reduction?

Why don't you use a lower number of filters in the last convolution? Instead of 128 you can just choose whatever number you want, e.g. 10.
Also, normally after the convolutional (and pooling layers), you flatten the output (therefore losing the spatial information) and then project with a dense layer onto the final representation space. You can control the dimensionality of the representation space with the shape of the matrices of the last dense layer.


How to extract and classify data from a column in excel? I have a column in an Excel sheet that contains a lot of data separated by || delimiters. The data can be classified to some classes like Entity, IFSC codes, transaction reference id, etc.

A single cell looks like this:

EFT INCOMING||0141201||NHFI0141201||UTR||SBIN118121948660 M S||some-name ||some-purpose||TRN REF NO:a1b2c3d4e5

Not every cell has the same number of classes or even the same type of classes.
Another example:

COMM/CHARGES/FEES||CHECK/REF.6546644473||BILPAY CCTY BEARING C||00.00||00012||18031358||BLPY||TRN REF NO:a1b2c3d4e5

I tried extracting this information using regular expressions and am able to get a list of ref-ids or IFSC codes extracted as a single list. But I need to break a cell to multiple cells with individual information. If some cell does not has that class data, it shall remain blank.

I also tried using named entity recognition but the same problem arises, I get the list of entities as output, not the breakdown.

Please help me in identifying what kind of problem this is? A text classification? And what would be the approach to solve it?

You need to perform a few preprocessing steps.

Convert your excel file into some sort of text file (csv is possibly the easiest)
Manipulate the file with python, either directly reading the file or with libraries such as the csv python module or pandas.

Last word of advice: Regular expressions are wonderful, but I think you might be using them for the wrong task, I strongly recommend you to take a programmatic approach.


Downsample GPS track I am working with GPS track files (list of X and Y coordinates). I have tracks with a high sampling rate and want to downsample the track for easier handling.

The obvious way would be to create a new list of points, and to keep only (for example) every 100th point of the track. The problem is that this could remove important extremes, such as curves.

Do you know algorithms, which allow to downsample the two dimensional array, while keeping the extremes (and dropping datapoints without much value, such as points which are in a straight line).

It would be great if there would be a solution in python, but I'm gratefule for all kind of help.

Thanks!

Ramer–Douglas–Peucker algorithm sounds like what you are looking for!
https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm
This algorithm allows you to simplify a sampled path. Basically, given a maximum allowed error, it proceeds by evaluating if a sequence of samples can be represented well enough (wrt the error) with a straight line between the first and the last point: if all the points in-between them falls under the error, all of them can be removed, otherwise it tries again with a shorter sequence.
This means that, setting a reasonable error value, on a straight road it will go for strong downsampling, while on an hairpin turn it may even decide to not downsample at all.
It seems there is a ready-to-go implementation in python https://github.com/fhirschmann/rdp
Eventually, you may find this talk from EuroPython 2016, about "Handling GPS Data with Python" by F. Wilhelm (I learn about the algorithm from this)


Given a target vector and a feature vector, how to computer the weight In page 13 of the slide, given $t$ and $X$ as following. I don't understand how we get $w$.

$$t=[t^{(1)},t^{(2)}, \ldots, t^{(N)} ]^T$$
$$X=\begin{bmatrix}1, x^{(1)} \\ 1, x^{(2)} \\ \vdots\\1, x^{(N)} \end{bmatrix}$$

Then:

$$w=(X^TX)^{-1}X^Tt$$

The least square problem is to minimize $$\|Xw-t\|^2$$

Differentiating it with respect to $w$ and equating it to $0$, we have

$$2X^T(Xw-t)=0$$

Hence, we have

$$X^TXw-X^Tt=0$$

That is $$X^TXw=X^Tt$$

$$w=(X^TX)^{-1}X^Tt$$


calculation of average ROC in IMageNet paper? The IMageNEt paper Image Net. presents the Average ROC curve for the 16 classes in the imagenet data, visit image figure. 8 in the paper. what is the known function to compute this ROC plot. As ROC plot is for a binary classification problem. Is this average ROC plot made in the picture average of ROC's for all the 16 categories. Any Help would be appreciated.

I would guess it is using the one vs all approach. You measure the auc of the ROC on each class adding the probabilities of all other classes to create the other class. Then you average among classes the obtained areas under the curve.


StanfordTokenizer will be deprecated in version 3.2.5 Warning I was testing the StanfordNERTagger using the NLTK wrapper and this warning appeared:

DeprecationWarning: The StanfordTokenizer will be deprecated in version
3.2.5. Please use nltk.tag.corenlp.CoreNLPPOSTagger or
nltk.tag.corenlp.CoreNLPNERTagger instead.
super(StanfordNERTagger, self).__init__(*args, **kwargs)

My code looks like this:

from nltk import word_tokenize, pos_tag, ne_chunk
from nltk.tag import StanfordNERTagger

sentence = "Today George went to school and met his friend Peter."

# stanford's NER tagger 3 entity classification
st = StanfordNERTagger('/home/hercules/Desktop/PhD/Tools/stanford-ner-
     2017-06-09/classifiers/english.all.3class.distsim.crf.ser.gz',
     '/home/hercules/Desktop/PhD/Tools/stanford-ner-2017-06-09/stanford-
     ner.jar',
     encoding='utf-8')

tokenized_text = word_tokenize(sentence)
classified_text = st.tag(tokenized_text)

print("Stanford NER tagger:")
print(classified_text)

I tried to use CoreNLPNERTagger but I could not find any examples or documentation.
I only found this link:
where it gives something like an example in the comments of the
class CoreNLPNERTagger(CoreNLPTagger)                                      (I found it by searching the keyword "CoreNLPNERTagge")

I tried to follow that example with no use.
I think I should start (if that is the correct term) the coreNLP server first but if is that the case I don't know how.

If anyone got any idea or advice I would be grateful.

@imoutidi, I also encountered the same deprecation warning.

After digging around a bit, it looks like the new/replacement package can be imported with the following:

from nltk.tag.stanford import CoreNLPNERTagger

However, when trying to run the tag() method I end up getting an unexpected HTTP connection refused error. I haven't figured out if this is an environment configuration issue (missing .jar path) or an issue with the NLTK codebase. I tried temporarily turning off my local firewall and that didn't help, so I'm guessing it is one of the other two possibilities.

I hope the python import statement is at least some help.


Layer weights don't match in keras This question uses the following code:
Xtrain = np.random.rand(400,1)
ytrain = f(Xtrain)
Xval = np.random.rand(200,1)
yval = f(Xval)

model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(10, activation='relu'),
    #tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(1, activation='relu')
])

model.compile(optimizer='adam',
              loss=tf.keras.losses.MeanSquaredError()
             )

model.fit(Xtrain, ytrain, epochs=500, verbose=0)

Using the command model.layers[0].get_weights() I get the following output:
[array([[-0.43412966, -0.51346564, -0.14263666,  0.8693182 , -0.4930619 ,
          1.249465  , -0.3924656 , -0.48984256, -0.55827504,  0.11134321]],
       dtype=float32),
 array([ 0.        ,  0.        ,  0.        ,  0.34663308,  0.        ,
         0.36201355,  0.        ,  0.        ,  0.        , -0.11139664],
       dtype=float32)]

And using model.layers[1].get_weights() I get this:
[array([[-0.04339373],
        [ 0.19533908],
        [-0.2295354 ],
        [ 0.903574  ],
        [-0.17581558],
        [ 0.7272965 ],
        [-0.69347996],
        [ 0.02008992],
        [-0.30351916],
        [-0.29846227]], dtype=float32),
 array([0.29466572], dtype=float32)]

I don't understand why the outgoing weights from layer 0 (second array in the model.layers[0].get_weights() list) don't match the incoming weights for layer 1 (first array in the model.layers[1].get_weights() list). And why is there an outgoing weight from layer 1? Isn't that supposed to be the final layer?

Expected shape of parameter arrays
Each layer has two arrays:

one for the weights, which has a shape of (num_inputs, num_outpus)
one for the biases, which has a shape of (num_outputs)

Here the num_inputs is the number of input features to that layer and the num_outputs is the number of outputs from that layer (this is what you select when instantiating a layer).
Output of .get_weights()
Another important thing to note is the usage of .get_weights(). In fact there are to ways to use it:

From the model, i.e. model.get_weights(): This will return a flattened list containing all parameter arrays in order. For example, it could look like this: [layer1_weights, layer1_biases, layer2_weights, layer2_biases, ...]

From a layer, i.e. layer.get_weights(): This is what you used. Here it will return the parameter arrays for a given layer. For example model.layers[1].get_weights() will return the parameter arrays for layer1. If layer1 has biases then this will return two arrays, one for the weights and one for the biases.

I took the liberty of changing your code a bit to make this a bit more clear.
import numpy as np
import tensorflow as tf

f = lambda x: 2*x

Xtrain = np.random.rand(400, 5)  # 5 input features
ytrain = f(Xtrain)
Xval = np.random.rand(200, 5)  # 5 input features
yval = f(Xval)

model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(10, activation='relu'),  # this layer has 5 inputs and 10 outputs
    tf.keras.layers.Dense(1, activation='relu')  # this layer has 10 inputs and 1 output
])

model.compile(optimizer='adam',
              loss=tf.keras.losses.MeanSquaredError()
             )

model.fit(Xtrain, ytrain, epochs=1, verbose=0)

# I will be calling .get_weights() directly from the model,
# so we expect 4 arrays: 2 for each layer.

print('First layer weights:', model.get_weights()[0].shape)
print('First layer biases:', model.get_weights()[1].shape)
print('Second layer weights:', model.get_weights()[2].shape)
print('Second layer biases:', model.get_weights()[3].shape)

The output:
First layer weights: (5, 10)
First layer biases: (10,)
Second layer weights: (10, 1)
Second layer biases: (1,)


Catboost Categorical Features Handling Options (CTR settings)? I am working with a dataset with large number of categorical features (>80%) predicting a continuous target variable (i.e. Regression). I have been reading quite a bit about ways to handle categorical features. And learned that one-hot encoding I have been using in past is really bad idea especially when it comes to lots of categorical features with many levels (read these post, and this).

While I've come across methods like target-based encoding (smoothing) of categorical features often based on mean of target values for each feature perhaps this post/kernel in Kaggle. Still I am struggling to find a more concrete way till I found CatBoost an open-source gradient boosting on decision trees released last year by Yandex group. They seem to offer extra statistical counting options for categorical features likely much more efficient than simple one-hot encoding or smoothing.

The problem is the documentation is not helpful how to set CTR settings. I have tried different ways but it just does not work. The doc says CTR setting as simple_ctr, to be given as (CTR setting section):

['CtrType[:TargetBorderCount=BorderCount][:TargetBorderType=BorderType][:CtrBorderCount=Count][:CtrBorderType=Type][:Prior=num_1/denum_1]..[:Prior=num_N/denum_N]',
 'CtrType[:TargetBorderCount=BorderCount][:TargetBorderType=BorderType][:CtrBorderCount=Count][:CtrBorderType=Type][:Prior=num_1/denum_1]..[:Prior=num_N/denum_N]',
  ...]

Here is a super simple example, the data looks like this:

import pandas as pd
import catboost
data = [{'profit': '342','country': 'holland','account': 'Jones LLC', 'saving': 150, 'debt': -60, 'age': 28},
         {'profit': '875','country': 'germany','account': 'Alpha Co',  'saving': 200, 'debt': -10, 'age': 42},
         {'profit': '127','country': 'italy','account': 'Blue Inc',  'saving': 50,  'debt': -300,  'age': 38 }]
df = pd.DataFrame(data)

Here is a simple Catboost Regressor:

X_train = df.drop(['profit'],axis=1)
Y_train = df['profit']
categorical_features_indices = [0,2]

train_pool = catboost.Pool(X_train, Y_train, cat_features=categorical_features_indices)

model = catboost.CatBoostRegressor(
    depth=3,
    iterations=5,
    eval_metric='RMSE',
    simple_ctr=None)

model.fit(train_pool);

The simple_ctr, one of CTR settings, is the problem!! It is pity because it looks like it the package offers various methods, so far no way to access them.

UPDATE Aug. 9th, 2018: A few days ago I raised this problem to Catboost developer, see here, and they opened a ticket for it to provide a tutorial.

I found out that in order to set the ctr parameters and all the components one should pass a list of strings, each string should contain the ctrType and one of its component:

The first word of the string should be a ctrType for example Borders: (click here for catboost parameters)
Then one component of the ctrType should follow. For example TargetBorderType=5.
All together 'Borders:TargetBorderType=5'.
Repeat the procedure to set an other component and add the new string to the list.

Example with two components set:

simple_ctr = ['Borders:TargetBorderType=Uniform', 'Borders:TargetBorderCount=50']


AttributeError: 'DataFrame' object has no attribute 'Values' I am running the following code:
import pandas as pd
import matplotlib.pyplot as plt
from pandas import read_csv

df = read_csv('demo.csv')
df
plt.hist(df.values)
plt.show()

lower_bound = 0.1
upper_bound = 0.95
res = df.Values.quantile([lower_bound,upper_bound])
res

and then I get the following error:
AttributeError: 'DataFrame' object has no attribute 'Values'

You tried to use .Values with a capital v instead of .values. Changing the capital v to a lowercase v should solve fix the error you're getting.


Check similarity of table/csv of Product Names We've got a list of approximately 18,000 product names (they're from 80-90 sources, so quite a few that are similar but not duplicates - these were picked as DISTINCT from a table) unfortunately there are different ways of expressing these names. We have to try and normalize the dataset so we present our users with more meaningful names.

For example, a list like this:

Canon EOS 5D Mark III
Canon EOS 5D mk III
Canon EOS 5DMK3
Canon EF 70-200mm f/2.8L IS II USM Lens
Canon EF 70-200mm f/2.8L IS II USM Telephoto Zoom Lens
Canon EF 70-200mm f/2.8L IS USM Lens
Canon EF 70-200mm f/4L USM Lens

I'd like to be able to assess those strings and collapse them into something like this:

Canon EOS 5D Mark III
Canon EF 70-200mm f/2.8L IS II USM
Canon EF 70-200mm f/2.8L IS USM Lens
Canon EF 70-200mm f/4L USM Lens

But I'd like to know how similar two strings are to be able to determine this. I do realise that the F2.8 IS II and IS USM may be a bit hard, but thought I'd throw it in.

The real product names are far less exciting (they're parts for the farm equipment we stock).

We also store these names in a Postgres (9.5) database table. Examples i've seen compare two lists, but we don't have a master product list to do that unfortunately.

Your problem is known as detection of near-duplicate documents, i.e. you have strings that are similar but not exact duplicate. The most common approaches are using cosine similarity and Jaccard similarity. You could check this page for more information

First you have to convert your strings to a vector of features, this features could be tf-idf vectors of all the tokens (words) that appear in the database, could also be a vector of n-grams. For a discussion of semantic and syntactic features you can look here.

Finally you would have to check the similarity of every pair of documents, in your case 18000. The brute force approach is O(n^2), so this could be unfeasible. A common technique to deal with this issue is to use fingerprinting (hashing) together with Locality Sensitive Hashing (LSH).

You can find an introduction and a general discussion of the whole topic in Chapter 3 of Mining Massive Datasets


What are good toy problems for testing Transformer architectures? I am testing various variants for Transformers and Transformer architectures. But training on full language tasks is a rather time consuming affair. What are good toy problems to test if a transformer (or alternative thereof) is working at all? I am looking for simple problems that can preferably be synthetically created and can be trained with really small setups (few layers, small embedding sizes etc.) in a short time. Ideally this should be problems that play to the strengths of transformers but would be hard to solve for, say, a fully connected feed-forward network. Tasks that can be applied to just an Attention Layer would be useful, too.

I would recommend the pre-processed datasets for language translation in the Stanford NLP group: https://nlp.stanford.edu/projects/nmt/

There are three datasets with sizes 0.13Mb, 4.5Mb and 15Mb - something for everybody  :)

Translation is something that is very difficult to solve for normal FF-networks and something that transformers dramatically improved due to attention mechanisms.

Also check out the related explanations of those ideas in The Annotated Transformer and the Illustrated Transformer


which loss function (if any) optimizes the calibration graph The calibration graph is the predicted versus actual probability(see http://scikit-learn.org/stable/modules/generated/sklearn.calibration.calibration_curve.html). Is it possible to optimize the linearity of that curve in terms of a loss function? Does the log-loss actually optimizes this curve in terms of KL divergence?

Log-loss optimizes your predictions in terms of their probability so in essence, yes it should be optimizing your calibration curve.  i.e. if you predict probability of 0% but it's actually labelled as the true class, this is penalised more than probability of 5% being true when it is the true class and so forth.


Describing the data cleaning process The term "Data Cleaning" is used to describe outlier checking, date parsing, missing value imputation to structuring datasets (organizing data values within a dataset) to facilitate analysis.

The latter is commonly referred to as "Data Tidying" but what about the former? I have seen outlier checking, date parsing and missing value imputation referred to as "soft processing", however I wonder if this is a commonly used term.

If I describe a component of data cleaning as "soft processing" is it reasonable to assume that knowledgeable people will know that I am referring to outlier checking, date parsing and missing value imputation?

In the field of Data Science, is there a commonly accepted phrase or term that umbrellas outlier checking, date parsing and missing value imputation.

If you're not modifying anything you can call it validation. If you are, you can call it preprocessing.


Which supervised machine learning algorithms assume normally distributed feature variables? I want to understand the assumptions made by supervised machine learning models.

I've heard it said many times that 'you need to make sure your feature variables are normally distributed for your ML models to work.' However, when I looked up the assumptions for Linear Regression, I found many conflicting viewpoints.

This post and this post mention normality of error distributions. The latter even says that features do not need to be normally distributed - just the errors.

Statistics solutions says that features do need multivariate normality, as do a lot of top beginner ML courses such as Machine Learning A-Z.

Wikipedia says that features do not need to be normally distributed, but it has a major influence on the precision of estimates.

Which algorithms assume that the feature variables are normally distributed? For ones that don't, why is it beneficial to scale your features so that they are more normally distributed?

If you are distinguishing between Statistics and Machine Learning, then you need to define boundary between the two, and that boundary is going to be opinion-based. It is a matter of definition which algorithms fall into Machine Learning and which under Statistics.
I'll try to give some examples without committing to one or the other.

Linear regression expects the errors (residuals) to be normally distributed. This comes from the maximum likelihood and the $x^2$ term in the normal distribution's PDF.
Logistic regression expects the log-odds of class membership to be linear. This is given for two normally distributed classes with equal variance. It follows from the Bayesian probability.
Linear discriminant analysis expects two normal-multivariate distributed classes with the same covariance matrix.

You can apply these algorithms even if your data violate the assumption, but then the results (in statistics: parameter estimates) won't be (quite) correct. How much they'll deviate from the real values depends on the extent of the assumption violation.


Train a GAN on "before and after" images of dental surgeries I want a GAN to train on "before and after" images of dental surgeries; so that it can generate "after" pictures for fresh patients.

Input images are like these: https://img.webmd.com/dtmcms/live/webmd/consumer_assets/site_images/article_thumbnails/slideshows/cosmetic_dentistry_slideshow/650x350_cosmetic_dentistry_slideshow.jpg

Now being a web developer, I am getting into AI.

I have read a hell lot on Basic ML, AI, GANs, VAE, StyleGANs, etc.

Made small projects on Tensorflow, etc.

I have my basics and theoretical knowledge in place. But I dont know how to get started practically on the above idea I have.

What should be my approach?

Following are my questions:

How many "before and after" images will I need?

What architecture should I use? i.e. StyleGAN, VAE?

As I will need my custom model; training a fresh model would need a good GPU. What platform should I go for? Colab, Floyhub, or something else

One approach that comes to my mind is that -
Process the image, do the color segmentation or something.

Add a static tooth image to area where tooth is missing. Then feed this image to a GAN, so that it can polish that image to look real.

Please guide me in the right direction. Sorry if any of my question is nonsense.

It's a very specific problem and there's no right or wrong solution.  I'll just write what I'd do in your position and hope that it is useful.

  How many "before and after" images will I need?

You'll need a lot of images to consistently get good results, in the range of tens or preferably hundreds of thousands.

  What architecture should I use? i.e. StyleGAN, VAE?

StyleGAN seems to be the most popular option for these sorts of tasks these days, so I'd stick with this.

  As I will need my custom model; training a fresh model would need a good GPU. What platform should I go for? Colab, Floyhub, or something else

The platform you'll choose depends on whether or not you're willing to pay. Colab, Floydhub and Kaggle are the only free options with GPU afaik, but also offer paid versions (1, 2). All have their pros and cons. For example colab, which I'm more familiar with, offers only 12-hour sessions (after that it disconnects you and you have to reconnect).

  Approach

Regarding the process you said, that's an interesting approach. I would have gone with something more basic like a segmentation mask, but I think your idea is better.


is there a way to customize my loss function to increase recall in one class only? First, thank you.

I'm using Tensorflow 1.13 with Python 3. I'm trying to create a CNN classifier on data with 4 classes, one-hot encoded. However, the insights I need it to produce are really around spotting either classes 0 or 3. If it tells me a data row is of class 1 or 2, that's of little use to my goals. Output from sklearn classification_report is below and I want to increase recall on classes 0 and 3 during model training. Precision would be nice, too.

               precision   recall   f1-score  support

          0       0.13      0.42      0.20      1484
          1       0.15      0.74      0.24       696
          2       0.81      0.34      0.48     12325
          3       0.13      0.23      0.17      1495

avg / total       0.65      0.35      0.41     16000

I'm relatively new to both python and TF, having gone through several tutorials, and am just now starting to build my own models. I've not really been able to stretch myself out past capabilities discussed in any of the learning courses I've watched. I know that Keras, for example, has custom loss function capability, but I'm not sure I understand how to integrate it into what I've already done. Relevant bits from my existing project are below.

# my current loss function, what I want to
# replace because just plain accuracy isn't
# terribly helpful
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true,logits=y_pred))

# setting up the trainer
optimizer = tf.train.AdamOptimizer(learning_rate=0.0001)
train = optimizer.minimize(cross_entropy)

# run it
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for i in range(steps):
        batch_x, batch_y = dataHelper.next_batch(100)

        sess.run(train,feed_dict={x:batch_x,y_true:batch_y,hold_prob:0.5})
        if (i + 1) % 100 == 0:
            print('on step {}'.format(i))
            preds_decoded = tf.argmax(y_pred,1)
            trues_decoded = tf.argmax(y_true,1)
            final_predictions, true_values = sess.run([preds_decoded, trues_decoded],
                                    feed_dict={x:price_data_only_test,y_true:test_labels,hold_prob:1.0})
            print(classification_report(y_true=true_values,y_pred=final_predictions))
```

What you want to do to boost the performance of one of your classes is to add class weights to your loss function. This will essentially tell your network to pay more attention to some classes (note that this might deteriorate performance on the rest of the classes).

How to compute the weights.

To see what weight you'll give to each class you can either put it yourself or you can let scikit-learn help you. The problem is that your data is imbalanced. This means that you have much more samples in class 2 than you have in classes 0, 1 and 3. Through the use of class weights you can make it so that your model pays more attention to the under-represented classes, so that they appear balanced. Scikit-learn has a function that calculates what weights you should give each sample.

from sklearn.utils.class_weight import compute_class_weight
weight_array = compute_class_weight('balanced', np.unique(y) y)  # y is the array containing your labels

weight_dict = dict(zip(np.unique(y), weight_array))  # dictionary containing your weights

Keras

In keras you can also add class weights in the fit method (which trains the model).

This is easier as you just need to pass a dictionary with the weight of each class (i.e. what we calculated previously)

model.fit(X_train, y_train, class_weight=weight_dict, ...)

TensorFlow

In TensorFlow you can do this simply by using this softmax_cross_entropy loss instead of the one you are currently using. This supports class weights (weights attribute).

This requires you to pass a Tensor with a shape of (batch_size,) where its elements are the weight you want to pass to each sample. What you need to do is to see what samples batch_y has and give them the respective weight!

batch_weights = [weight_dict(x) for x in batch_y]  # assuming that your batch_y is a numpy array that is not one-hot encoded

# if it is one-hot encoded:
batch_weights = [weight_dict(x) for x in np.argmax(batch_y, axis=1)]  # assuming batch y is a numpy array

# if batch_y is not a numpy array you need to get it through
batch_y_arr = sess.run(batch_y)

Then pass the batch_weights to the weights attribute of the loss (through a placeholder using the feed_dict - like you do with batch_x and batch_y)


When is it useful to measure the Frobenius norm of a matrix? In Deep Learning section 2.5 the author review some measures for the size of vectors and matrices.
When in general is it useful for someone to know these?
For instance they give the example of the Frobenius Norm which is analogous to the L2 Norm for matrices. Is this a metric that is more used in academics?

It is basically a similarity measure for matrices.
There are multiple uses for Frobenius Norm, some examples could be classifying covariance matrices for action recognition and low-rank bilinear pooling.


Create an RNN on text sources with different lengths I want to create an RNN to generate a new text based on many examples of existing texts of a certain format in the training data.
The type of texts in the training data consists of 3 segments, like so:
Example text 1:
[Segment 0, ~20 characters]
[Segment 1, ~200 characters]
[Segment 2, ~400 characters]
It is worth mentioning that the segments are all still alphanumerical, but of varying structure. Segment 1 contains more numbers and Segment 2 has more letters with references to occurrences in Segment 1.
I have training data with many instances of such example texts, but with varying length in each segment.
In most RNN text generation examples I have seen before the training set consists of a single long text which is split into segments of a fixed length for training, and I guess that could be done in my case as well by simply concatenating all of the different examples into one long string. Then dividing that long string into sequence pairs of "input" and "target" strings of a fixed length, say 100 characters and use them for training.
My question is if I can not instead take advantage of the fact that I have the text already split up into segments which resemble what I want to generate, and use each example as a batch for training.
In my opinion feels like it would be easier for the RNN to learn the relations between the different text sequences if it got fed data (input + target string) that was belonging to one example at a time, as opposed to data (input + target) spanning over the entire training set.
Is there any aspect that I am overlooking which would favour simply concatenating all training data into one large string?
Many thanks in advance!

I think that if you append a token <EOS> (end of sentence) at the end of each sentence when you merge, this would not be a problem, because the RNN would learn to cut sentences and to generate independently if you shuffle your data and train with several shuffles.
However, as you say your data is heterogeneous, you might consider to first run some clustering algorithm and then train your RNN to generate conditionally to a cluster. To build the training dataset, you can merge all data for each cluster. This is a bit more complex than simply "automatic generation" but it depends on what your goal is.


Which classifier performs better when using 'class_weight'? I have used the 'class_weight' method to balance my multi-class classification problem, using Logistic Regression, Random Forest, and XGBoost classifiers. Among these three methods, logistic regression's performance for the minority classes is substantially higher than the other two models. Could someone please explain why does LR beat decision-tree-based classifiers in this scenario? Thank you.

Very good question! I think the plain answer is: no. To my experience this usually depends on the data. Even different boosting methods/tools will perform differently. It often depends on try and error.

What can make a difference is using L1/L2 regularization, since it can shrink features which are „not useful“ to make a good prediction.

I cannot think of a theoretical reason here. I‘m curious if someone comes up with a more theory based answer!


Has Gradient decent any effects on the actual predictions or designed for practical convenience? If we consider Batch Gradient, Stochastic Gradient, Mini-batch Gradient, will they affect on the actual predictions? as we know they always attempt to reach a local minimum.

will they affect on the actual predictions?

Yes. The type of gradient descent that you use has an impact on how the model is trained. Therefore, it may lead to different predictions as the trained models may be different.

as we know they always attempt to reach a local minimum

Yes, each gradient descent technique has the same goal, which is to optimize a cost function by following its gradient. However, as you point out, gradient descent may end up in different local minima and some of them may be better than others. And each gradient descent technique is more or less prone to finding the global minimum, which may lead to a difference in predictions.

I would also recommend that you experiment with these different approaches and look at how the training progresses. This may help you gain intuition about the impact of each technique. Also, I find this blog to be quite useful to better understand the differences: https://towardsdatascience.com/gradient-descent-algorithm-and-its-variants-10f652806a3


How is vector A converted to single value scalar in Andrew Ng's course? In Andrew Ng's deep learning course on Coursera, how is a single scalar value obtained from a flattened image (feature vector)?
First there is $w.T$ of shape $(1, n_X)$ which is multiplied by $X$ of shape $(n_X, 400)$, so by the laws of linear algebra, the remaining vector is of shape $(1, 400)$. This is then passed through Sigmoid to form vector A, and the shape remains at $(1, 400)$. How is this vector then converted to a binary scalar value ($0$ or $1$) for prediction $y$-hat?

$n_X$ is the number of feature, $400$ is the number of data.
Each of the entry of $A$ is the output of the sigmoid layer, it is between $0$ and $1$. We can then decide a threshold (typically $0.5$) such that if it is at least the threshold, we map it to $1$, otherwise, we map it to $0$.


Random Forest command in R for selected variables All I can find online is how a random forest is run on all variables in a dataset using the period:
RF <- randomForest(sale ~ ., data = TrainSet, importance = TRUE)
What if I only want to apply the RF to selected variables in the dataset? Would I need to drop the variables first?
I tried the following, but get an error:
RF <- randomForest(sale ~ v1,v2,v3, data = TrainSet, importance = TRUE)

You have to do

RF <- randomForest(sale ~ v1 + v2 + v3, data = TrainSet, importance = TRUE)

this is the formula notation for R. It doesn't make a lot of sense for random forest models, but it is how it works.


What is the difference between multi-layer perceptron and generalized feed forward neural network? I'm reading this paper:An artificial neural network model for rainfall forecasting in Bangkok, Thailand. The author created 6 models, 2 of which have the following architecture:

model B: Simple multilayer perceptron with Sigmoid activation function and 4 layers in which the number of nodes are: 5-10-10-1, respectively.

model C: Generalized feedforward with Sigmoid activation function and 4 layers in which the number of nodes are: 5-10-10-1, respectively.

In the Results and discussion section of the paper, the author concludes that :

Model C enhanced the performance compared to Model A and B. This suggests that the generalized feedforward network performed better than the simple multilayer perceptron network in this study

Is there a difference between these 2 architectures?

Well you missed the diagram they provided for the GFNN. Here is the diagram from their page:

Clearly you can see what the GFNN does, unlike MLP the inputs are applied to the hidden layers also. While in MLP the only way information can travel to hidden layers is through previous layers, in GFNN the input information is directly available to the hidden layers.

I might add this type of connections are used in ResNet CNN, which increased its performance dramatically compared to other CNN architectures.


How can I measure the reliability of the specificity of a model with very small train, test, and validation datasets? Stats newbie here. I have a small dataset of 646 samples that I've trained a reasonably performant model on (~99% test and val accuracy). To complicate things a little bit, the classes are somewhat unbalanced. It's a binary classification problem.

Here is my confusion matrix on training data

[[387   1]
 [  1  73]]

on testing data:

[[74  1]
 [ 0 10]]

on validation data:

[[85  1]
 [ 0 13]]

Training Specificity: .986
Testing Specificity: .909
Validation Specificity: .928

My thoughts are that testing and validation have a very low specificity while training has a comparatively high specificity. However, given that only one sample is missed in both the testing and validation datasets, what is my real-world specificity? Is there a better generalizability measure? Is there something akin to a p-value that relates the reliability of the specificity given the size of the negative sample class?

Thanks!

Real world data is "test dataset", right?
Data has to be divided in such a way that train-validation see part of data more than once while test data will be seen only once. In that sense, if the model is robust enough, it will perform well even on the test dataset. The assumption is that test data is as close as possible to real-world data.


what can be done with the following data? I have scraped the rating that a customer gave in the following categories namely overall rating score, value for money, seat comfort,staff service, catering and entertainment from an airline forum. I would like to know what quality information I can deduce from such ratings about an airline. I have rating of nearly 300 customers who traveled in an airline.

I think you're looking at it the wrong way. First, you need to have a question and then you may find out whether the data could potentially hold the answer to that question. Right now, you're asking us to what questions this data may hold the answer. And that is a very difficult question!

But to provide you with at least some ideas:

Find out what people deem important (the correlation between overall rating and the other ratings in (linear) regression analyses)
Find out the relation between certain characteristics of people (age/gender/nationality, if available) and their overall rating (or whatever rating available)

Basically, the relation between certain variables and their outcomes. But make sure you have a clear question before you start looking for answers.


Store's unseen items sales forecasting I am working on sales forecasting problem.I am able to provide data about which items got sold and not sold to the algorithm.How to provide algorithm information about items that are not present in the store.Is there any way we could encode this information in data or any other algorithms accepts this kind of information.Currently, I am using Neural Networks and Random Forest to forecast
Sales.

If you use R the package forecast provides neural networks algorithm as nnetar() function and it has xreg option. You could use your stock data as regressor. Examples and details: http://pkg.robjhyndman.com/forecast/reference/nnetar.html

Or you could use var models because there is relation between stock and sales e.g. if you have low stock your sales would be min(y,stock) where y is the true demand to your products. It also applies in other direction too if you have high y then you will be out of stock fast.

But I'll suggest you to take look forecast package function nnetar() (in R).


Use of Random Forest algorithm in PySpark for imputation I am wondering how to use Random Forest algorithm for imputing missing values in a dataset. It is supposed to work well with missing values but I am not sure how those missing values are dealt with and how RF imputation works in PySpark.

You can do the following: use all the other features as input and the missing data as the label.

Train using all the rows that have the column filled with data and classify the others that don't. Use the values predicted by the Random Forest as the value of that field on the subsequent models and transformations.


When is it convenient dropping duplicates when performing Time Series Prediction? I have this Dataset.

 Head(df):

          ï..date   store item  qty    unit_price item_category
 1220   2017-03-15    38  295   255      13.30            13
 1453   2017-03-15    41   43   291      10.08            13
 3181   2017-03-15    75  186   324      28.59            13
 3541   2017-03-15    42  295   133      13.30            13
 3624   2017-03-15    56  127   969      55.23            13
 4657   2017-03-15    52   75   121      27.23            13
 4702   2017-03-15    13  413    75      18.55            13

There are clearly a lot of duplicates in the date column (since it depends on the stores and the items , that was expected), and since my task is to make a prediction and return predicted values in numeric of the qty variable - the target prediction variable "qty", represents the total quantities sold at the ITEM/DAY level- i think i can drop the duplicated dates (I'll be using the ARIMA model) and keep only the date , item and qty column for my prediction .

I wanted to know if my approach was right, any suggestion would be much appreciated , Thank you.

According to me, time-series ML Model are bit different than other routine ML  models. As time-series model is based upon the sequence of previous values, dropping a Date may impact your solution.

Specially in this case where sale is definitely dependent upon the particular day.
For eg consider store 38 is located in Area where on "2017-03-15" is holiday. So sale of store 38 will be comparatively high in comparison to other store and so on.

Rest you can try two different models one with Date and another without Date and compare the results as well.


Why do trained RL agents still display stochastic "exploratory" behavior on testing data? I am training a PPO2 RL model using stable baselines. One thing I found is that a trained agent will still display some stochastic behavior on test data, as shown by the predict method of PPO2; the deterministic flag should be set to True to see deterministic (un-stochastic) behavior.

I understand that when training an RL agent, there is some amount of random exploration so  that an agent can learn the optimal value-policy network. However, I thought that once an agent was trained, and used to act (predict) on new test data, the model parameters would be used and no exploration would take place. Therefore, when testing, I thought the agents actions and subsequent rewards would be deterministic (once the test data is static).

Why is the trained agent exploring on test data / Why is the trained agent still stochastic when predicting test data? To what degree is the trained agent stochastic (will it follow its model predictions 90% of the time and guess the other 10%)?

The PPO approach directly generates stochastic policies. Its output is some probability distribution function over the action space.

This is not the case for all RL algorithms, but is common for many Policy Gradient methods.

In some cases you may want this behaviour to continue. Paper-Scissors-Stone is a classic example of a game where a stochastic policy is optimal, and there are other examples from game theory.

Also, the agent many have learned on-policy (I'm not sure in case of PPO without looking it up), in which case the stochastic behaviour should match with expected value predictions. This is similar conceptually to using SARSA to generate an $\epsilon$-greedy policy. The value functions match following that policy, although if $\epsilon$ is low enough you may take a reasonable guess that a fully greedy policy is optimal.

  To what degree is the trained agent stochastic (will it follow its model predictions 90% of the time and guess the other 10%)?

To the degree that the output of the policy is stochastic. It will always "follow its model predictions".

Switching deterministic on actually stops the agent from following the model, and typically will select the mode (highest probability density) of the action distribution.

Unlike SARSA, with Policy Gradient methods, there is not always access to a "greedy" policy that picks according to action values. So instead, your deterministic flag is likely picking greedily according to action probability.

In some cases (e.g. Actor-Critic) you may also have a value based estimator for V(s) or Q(s,a), and it could be used instead, but that is usually considered secondary to the policy function (in addition, using this on a large or continuous action space is very inefficient compared to a real-valued policy function)

Also note in some cases e.g. DDPG (which stands for Deep Deterministic Policy Gradients), a policy gradient method can use a deterministic policy with exploration added as a behaviour policy, making the algorithm off-policy. DDPG should not exhibit exploration during testing as PPO is for you. You may find DDPG behaves closer to your original expectations because of this.


Group neural networks outputs using Keras/Tensorflow I am trying to group the outputs of my neural network, in order to have them perform a separated classification.

Let's take the example where the groups are constituted of two nodes and we previously have four output nodes, then the network should look like this :

How can I achieve this using Keras or Tensorflow ? I was thinking that I need to implement a custom layer and do the operation inside of it, but I wonder if there is an easier solution using the Keras Functional API (or lower level Tensorflow ?) :)

Using the keras functional API will get you what you need.

I'm assuming that you are currently using the standard keras sequential model API, which is simpler but also restricts you to a single pipeline. When using the functional API, you do need to keep track of inputs and outputs, instead of just defining layers.

For the example in your question:

from keras.layers import Input, Dense
from keras.models import Model

# Left side sub-model:
L1 = Input(shape=(2,))
L2 = Dense(2, activation='softmax')(L1)

# Right side sub-model:
R1 = Input(shape=(2,))
R2 = Dense(2, activation='softmax')(R1)

# Combining them together:
merge = concatenate([L2, R2])

# Some additional layers working on the combined layer:
merged_layer_1 = Dense(4, activation='relu')(merge)

# Output Layer:
output = Dense(2, activation='softmax')(hidden1)

# Defining the model:
my_model = Model(inputs=[L1, R1], outputs=output)

The depths of the layers are made up, but you should get the general idea.


How to extract the reason behind a prediction using TensorFlow? I created a CNN using TensorFlow2 and trained it as a binary classifier. Is there a way to extract the influence of each pixel upon the prediction?

I am trying to obtain a mask similar to the following:

[updated]

You are looking for GradCam.

Code:

https://fairyonice.github.io/Grad-CAM-with-keras-vis.html
https://tf-explain.readthedocs.io/en/latest/index.html

Paper: https://arxiv.org/abs/1610.02391

This is a good resource I found
https://christophm.github.io/interpretable-ml-book/


Is it advisable to rerun LASSO multiple (2) times? I have a dataset with large (4500 variables) and 3000 observations (n < p case). I am using LASSO (from the glmnet package in R) to reduce dimensionality of the problem. I have couple of questions.

1) After I ran LASSO the first time, the number of variables with non-zero coefficients goes down to ~80 (which seems reasonable). But the correlation matrix shows that some of these variables are very highly correlated themselves (FORMS distinct groups if you plot it using the corplot package. So I ran LASSO exclusively on these variables whose coefficients were non-zero during the 1st run. This time, the algorithm picked up even less number of variables.I was expecting that they would remain more or less similar. I want to know if running LASSO multiple times is advisable or it introduces bias of any form in anyway that interferes with interpretation/prediction ?

2) My second question is about the magnitude of the coefficients. Some of the coefficients, in absolute value, turns out to be very close to 0 (~1E-14) while some are larger. I mean the spread is very large. Does it indicate that the variables with this small coefficients are making the model worse in any way. Is it advisable to get rid off those variables and rerun LASSO ?

Thoughts/comments are most welcome.

Thanks

Wow I'm surprised that no one has given this one a go. I'm very very late to the party but I'll attempt an answer just for information's sake.

Before I attempt to answer your questions I'd like to say that any situation in which the number of features exceeds the number of observations by 50% there may be some instability in the results.

What I'd recommend is to attempt a number of feature selection approaches and compare the results. Personally I'd start with a PCA to reduce the number of features such that n > p and go from there. But that's just me.

1) I'm not surprised by more estimates being zero at all. In the first lasso regression with this many features a large amount of these features are likely to be noise (no prediction power) meaning that any feature with a small capability of prediction would be selected in the first cut of variables. Running a secondary lasso regression might not be completely advisable (logically, can't really think of any theoretical issues at current) but it's likely that some of the stronger predictors are being uncovered on this second iteration.

2) The estimates in lasso regressions are quite difficult to interpret (apart from zero and non-zero values obviously), but what I'd recommend is to check whether you've scaled your features before running the lasso reg? As my understanding is that a lasso reg is a penalised version of the ridge regression, and the ridge regression needs the features to be scaled as otherwise features represented by larger numbers are given higher coefficients. If you have just ignore this, but if you want to interpret the estimates I'd switch to a Linear Reg/GLM not a lasso reg.

Hopefully this helps.


Unbalanced multiclass data with XGBoost I have 3 classes with this distribution:

Class 0: 0.1169
Class 1: 0.7668
Class 2: 0.1163

And I am using xgboost for classification. I know that there is a parameter called scale_pos_weight.

But how is it handled for 'multiclass' case, and how can I properly set it?

scale_pos_weight is used for binary classification as you stated. It is a more generalized solution to handle imbalanced classes. A good approach when assigning a value to scale_pos_weight is:

sum(negative instances) / sum(positive instances)

For your specific case, there is another option in order to weight individual data points and take their weights into account while working with the booster, and let the optimization happen regarding their weights so that each point is represented equally. You just need to simply use:

xgboost.DMatrix(..., weight = *weight array for individual weights*)

You can define the weights as you like and by doing so, you can even handle imbalances within classes as well as imbalances across different classes.