approach for predicting machine failure using maintenance history I have been struggling with this problem for a while now and I finally decided to post a question here to get some help. The problem i'm trying to solve is about predictive maintenance.

Specifically, a system produces 2 kinds of maintenance messages when it runs, a basic-msg and a fatal-msg, a basic message indicates that there is a problem with the system that needs to be checked (its not serious), a fatal-msg on the other hand signals that the machine must be shut down and the fatal problem be addressed before it is allowed to run again. All maintenance messages have a unique message-id associated with them.

For example:

Basic-msgs:

112234: Check Valve 3

162746: Main switch power fluctuating

Fatal-msgs:

7432955: Valve 3 Broken

3358399: Startup Failed

Some key Details:
A basic-msg can occur without a fatal-msg, but a fatal-msg must occur with a corresponding basic-msg. Every time the machine is run it gets a unique run-id.

Problem: By looking at the basic-msgs occurred in the previous 4 runs of the machine, predict what fatal-msgs could occur in the next run. I have the data for previous 8 years (includes basic-msgs, fatal-msgs, corresponding run-ids.

I am stuck with this for about 3 weeks now, can someone guide me about a machine learning approach I can take to solve this, and what tools should i use.

Thanks!

Sequence labeling seems like a good fit for your problem. Your features could be the frequency of each kind of basic-msg in the past 4 runs (but maybe it would work better normalized by unit of time).

There are a good few libraries for Conditional Random Fields for example.


Should I fit my parameters with brute force I am running analysis on data for this type of sensor my company makes. I want to quantify the health of the sensor based on three features using the following formula:

sensor health index = feature1 * A + feature2 * B + feature3 *C

We also need to pick a threshold so that if this index exceeds the threshold, the sensor is considered as bad sensor.

We only have a legacy list which shows about 100 sensors are bad. But now we have data for more than 10,000 sensors. Anything not in that 100 sensor list is NOT necessarily bad. So I guess the linear regression methods don't work in this scenario.

The only way I can think of is the brute force fitting. Pseudo code is as follows:

# class definition for params(coefficients)
class params{
  a
  b
  c
  th
}

# dictionary of parameter and accuracy rate
map = {}

for thold in range (1..20):
   for a in range (1..10):
      for b in range (1..10):
        for b in range (1..10):
           # bad sensor list
           bad_list = []
           params = new params[a, b, c, thold]
           for each sensor:
             health_index = sensor.feature1*a+sensor.feature2*b+sensor.feature3*c
             if health_index > thold:
               bad_list.append(sensor.id)
           accuracy = percentage of common sensors between bad_list and known_bad_sensors
           map[params] = accuracy

# rank params based on accuracy
rank(map)
# the params with most accuracy is the best model
print map.index(0)

I really don't like this method since it is using 5 for loops which is very efficient. I wonder if there is a better way to do it. Using existing library such as sk-learn perhaps?

If you don't know which of the 10,000 sensors are good and which are bad, the data from those 10,000 sensors is useless for training a regression line / classifier.  You need labelled data, where you know both the value of the features and the health of the sensor.

Moreover, to be effective, you probably need your training set to contain both healthy sensors and bad sensors (where you know which ones are healthy and which ones are bad); it's not enough to just have data from bad sensors, because that doesn't tell you what healthy sensors look like.  In a pinch you could use one-class classification, but I don't recommend it -- your results will probably be poor, and you'll probably be better off obtaining labelled data from both healthy sensors and bad sensors.


the ratio of training set, validation set and testing set in K-fold cross validation Suppose I have 100 samples, then I want to use 5-fold CV, is the ratio of training set: validation set: testing set is 84:16:16? Is the number of data in validation set should always equal to testing set?

For K-fold cross validation, you would not need a separate validation set. In your example of 100 samples, if you have train:test set ratio to be as 80:20 and follow the 5-fold CV algorithm, the train set (80 samples) will be split into five 16 sample subsets ($s_i$) as in $\{s_1, s_2, s_3, s_4, s_5\}$. At each iteration, the model will select any 4 subsets out of the 5 subsets, based on your selection criteria, and use the left-out set as the validation set.
Once the training is completed, you can test the performance of the final model with the held-out test set (20 samples) to get an unbiased estimate of the performance of the model.
It is not necessary to have same number of samples in validation set and test set.


Dimension reduction for logical arrays I have measurements of 4 devices at two different points of time. A measurement basically consists of an array of ones and zeros corresponding to a bit value at the corresponding location:

whos measurement1_dev1_time1

Name                         Size               Bytes  Class      Attributes

measurement1_dev1_time1      4096x8             32768  logical

I assume that for a specific device the changes between time 1 and 2 of the measurements are unique. However, since I am dealing with 32768 bits at different locations, it is quite hard to visualize if there is some kind of dependency.

As every bit at location xcan be regarded as one dimension of an observation I thought to use PCA to reduce the number of dimensions.

Thus, for every of the 5 devices:

I randomly sample n measurements at point t1and t2 seperatly
I prepare an array as input for pca() with m*n columns (m< 32768; its a subset of all the observed bits, as the original data might be too big for pca) and 4 rows (one row for each device).
On this array A I calculate the pca: ``[coeff score latent] = pca(zscore(A))```
Then I try to visualize it using biplot: biplot(coeff(:,1:2), 'score', score(:,1:2))

However, this gives me really strange results. Maybe PCA is not the right approach for this problem? I also modified the input data to do the PCA not on the logical bit array itself. Instead, I created a vector, which holds the indices where there is a '1' in the original measurement array. Also this produces strange results.

As I am completely new to PCA I want to ask you if you either see a flaw in the process or if PCA is just not the right approach for my goal and I better look for other dimension reduction approaches or clustering algorithms.

Standard PCA only sees your data as real values and hence doesn't really provide the best low rank approximation. There exist versions of PCA designed for boolean data types. Personally I think the best paper covering the whole broad topic of generalised PCA is Generalized Low Rank Models. In section 5.2 they specifically cover Boolean PCA.


running scikit-learn on Sypder with Python 3.5 I am trying to print a accuracy score and get this message:

<function accuracy_score at 0x0000018A76046840>

So I am wondering if I am missing a package or is something installed incorrect?

Code:

from sklearn import datasets
from sklearn.metrics import accuracy_score
from sklearn import svm
iris = datasets.load_iris()
X = iris.data
y= iris.target
#print (iris.keys)
print('**********')
#print (iris['feature names'])
print('**********')
#print (iris)
#print (iris['data'].shape,'legth: ',len(iris['target']))
clf = svm.SVC(gamma=0.001, C=100)
clf.fit(X,y)
y_hat=clf.predict(X)
accuracy_score(y, y_hat)
score = accuracy_score
print (score)

The reason is that you are printing the object of function. Instead call the function. Suppose the name of your function is func. You are doing something like

print(func)

instead try to call it by adding parenthesis.

print(func())


Deep Learning with Spectrograms for sound recognition I was looking into the possibility to classify sound (for example sounds of animals) using spectrograms. The idea is to use a deep convolutional neural networks to recognize segments in the spectrogram and output one (or many) class labels.  This is not a new idea (see for example whale sound classification or music style recognition).

The problem that I'm facing is that I have sound files of different length and therefore spectrograms of different sizes. So far, every approach I have seen uses a fixed size sound sample but I can't do that because my sound file might be 10 seconds or 2 minutes long.

With, for example, a bird sound in the beginning and a frog sound at the end (output should be "Bird, Frog"). My current solution would be to add a temporal component to the neural network (creating more of a recurrent neural network) but I would like to keep it simple for now. Any ideas, links, tutorials, ...?

RNNs were not producing good enough results and are also hard to train so I went with CNNs.

Because a specific animal sound is only a few seconds long we can divide the spectrogram into chunks. I used a length of 3 seconds. We then perform classification on each chunk and average the outputs to create a single prediction per audio file. This works really well and is also simple to implement.

A more in-depth explanation can be found here:
http://ceur-ws.org/Vol-1609/16090547.pdf


Calculating mean of data frame inside a series object I have a data frame with following structure:

df.columns

Index(['first_post_date', 'followers_count', 'friends_count', 'last_post_date','min_retweet', 'retweet_count', 'screen_name', 'tweet_count',  'tweet_with_max_retweet', 'tweets', 'uid'],
      dtype='object')

Inside the tweets series, each cell is another data frame containing all the tweets of an user.

df.tweets[0].columns

Index(['created_at', 'id', 'retweet_count', 'text'], dtype='object')

I want to perform calculation on the tweets of each users, for example, finding average number of retweets of each user, the tweet with maximum retweets etc.

How can I do these?

Edit
link to sample data

In the absence of a MultiIndex (the Right Way$^\mathrm{TM}$), the apply method can do what you want; e.g.

df.assign(
    max_retweet=df.tweets.apply(lambda x: x.retweet_count.argmax('retweet_count')),
    avg_retweet=df.tweets.apply(lambda x: x.retweet_count.mean())
)

       avg_retweet  max_retweet
26662     0.045476          187
32316     0.821538          427
25879     0.633681          583
43411     0.112465          421
28840     0.472222            8

Also, don't share data with pickle; it's dangerous.


What is a basic object detection/localization ML algorithm that can be used for my relatively simple image set? I have a relatively simple object localization task. I have an image set that are either uniform or contain an 'object' consisting of a black circular shape at some position in the image. The labels include whether or not it contains the object, and the position of the object (center coordinates + length/width). YOLO feels like overkill and I'm having a hard time figuring out how to get it to work, but I'm having a hard time finding a simpler model that would work.

So, the following techniques are used for object detection (other can YOLO, R-CNN and other Deep Learning Techniques):

Haar Cascade Classifier

This was the first facial detection algorithm to be successfully used and is popular until today. Proposed by Viola and Jones in their paper Rapid Object Detection using a Boosted Cascade of Simple
Features (2001, CVPR) this algorithm uses Haar-like features from integral images and a cascade of boosting classifiers to detect objects in images. Lienhart proposed in 2002 a improvement to this algorithm in his paper Empirical Analysis of Detection Cascades of Boosted Classifiers for Rapid Object Detection

It can be easily trained using OpenCV Object Detection Module in Python, C and Java, also available in Matlab Computer Vision Tool Box as vision.CascadeObjectDetector and probably many other platforms.

Max-Margin Object Detection

Proposed in the paper Max-Margin Object Detection by Davis E. King, creator of dlib, where it has been implemented, this algorithm has implementations for face detection using HoG Features and CNNs features.

The abstract of the paper goes:

  Most object detection methods operate by applying a binary classifier
  to sub-windows of an image, followed by a non-maximum suppression step
  where detections on overlapping sub-windows are removed. Since the
  number of possible sub-windows in even moderately sized image datasets
  is extremely large, the classifier is typically learned from only a
  subset of the windows. This avoids the computational difficulty of
  dealing with the entire set of subwindows, however, as we will show in
  this paper, it leads to sub-optimal detector performance.

  In particular, the main contribution of this paper is the introduction
  of a new method, Max-Margin Object Detection (MMOD), for learning to
  detect objects in images. This method does not perform any
  sub-sampling, but instead optimizes over all sub-windows. MMOD can be
  used to improve any object detection method which is linear in the
  learned parameters, such as HOG or bag-of-visual-word models. Using
  this approach we show substantial performance gains on three publicly
  available datasets. Strikingly, we show that a single rigid HOG filter
  can outperform a state-of-the-art deformable part model on the Face
  Detection Data Set and Benchmark when the HOG filter is learned via
  MMOD.

I think these are the two most relevant outside of Deep Learning, reading the related work fromt he 3 cited articles might give you some light on how to proceed.


FP16, FP32 - what is it all about? or is it just Bitsize for Float-Values (Python) What is it all about FP16, FP32 in Python? My potential Business Partner and I are building a Deep Learning Setup for working with time series. He came up with "FP16 and FP32" while finding a GPU. It looks like he's talking about Floating Point values in 16 vs 32bit. (Our data points look like this: "5989.12345", so I'm pretty sure 16bit ain't enough.)

Is FP16 a special technique GPUs use to improve performance or is it just a fancy term for using 16-bit float values instead of 32 standard floats?

FP32 and FP16 mean 32-bit floating point and 16-bit floating point. GPUs originally focused on FP32 because these are the calculations needed for 3D games. Nowadays a lot of GPUs have native support of FP16 to speed up the calculation of neural networks. If you look at some benchmarks (https://blog.slavv.com/titan-rtx-quality-time-with-the-top-turing-gpu-fe110232a28e) you will see that GPUs that support FP16 are almost twice as fast calculating FP16 than FP32. Taking into account that newer cards that support FP16 (like NVidia 2080 series) are also about 20% faster for FP32 compared to their predecessor (1080) you get an increase of 140% to train FP16 neural networks compared to FP32 on previous cards.

But there is a caveat. Your neural network needs to be written using FP16 and it should also have the same accuracy. FP16 have lower accuracy by design, because they have much less bits to represent the same number. Some choose to use mixed precision models in order to be fast and accurate (https://hackernoon.com/rtx-2080ti-vs-gtx-1080ti-fastai-mixed-precision-training-comparisons-on-cifar-100-761d8f615d7f). But you see in the last link that the speed is faster for mixed precision, but is not 2 times faster as when you use pure FP16. You could also theoretically use FP32 weights and convert some of them to FP16 weights, but the accuracy could fall.

So in the end you need to understand whether you could rewrite your neural network to use FP16 fully or partially. If you cannot then you do not get any additional benefits from FP16 compatible cards.

The maximum value for FP16 is 65504 and the minimum is 5.96 × 10−8. The best precision for 5989.12345 will most likely be 5988.0 (played with bits on https://www.h-schmidt.net/FloatConverter/IEEE754.html) If this precision and magnitude is not enough for you then you could scale your data before training to fit FP16 and then train with double the speed. Or use mixed precision models where you have FP32 as input and then reduce precision in later layers.


What does high variance mean in a binary classification machine learning model? My understanding of high variance is that the targets are spread widely around. The output values are "all over the place".

In a binary classification model, there can only be 2 outcomes. I am at a loss when visualizing what high variance mean in a binary classification model.

You have correctly intuited that variance isn't as useful a concept in this case. Statisticians typically look at the binomial deviance instead (see here for a thorough technical development).

If you do want to think about variance, we can recognize that many binary classifiers output an estimate of the label $\hat{Y}$. It helps here to think of a randomized classifier which estimates $P(Y=1|X=x) = E[Y|X=x]$ (call this estimate $\hat{p}(x)$) and generates $\hat{Y} = 1$ with probability $\hat{p}(x)$, $\hat{Y} = 0$ otherwise. We have $$var(\hat{Y}|X=x) = E[(\hat{Y} - E[\hat{Y}|X=x])^2|X=x].$$ This will be smaller when the classifier is more certain about the classification once it sees the covariates. I.e., if $\hat{p}$ is always either 1 or 0, then the variance is 0 (but of course the bias will be huge). If there are equal numbers of each class in the training data and the classifier is able to learn nothing from the covariates, then $\hat{p} = \frac{1}{2}$ for any $x$ and so the variance is $\frac{1}{4}$--a coin flip, as we'd expect.


Is there a name for this form of sampling? I had an idea to combine oversampling and undersampling in the following way:

Compute the average number of individuals in each class. For classes with a number of individuals greater than this average, undersample the class so that the number of individuals in this class equals the average. For classes with a number of individuals lower than this average, oversample the class so that the number of individuals in this class equals the average. Overall, all classes then have a number of individuals equal to the original average.

However, I cannot see any literature on this idea. Does this idea exist and does it have a name so that I may read about it?

It is called resampling and is quite famous for dealing with imbalanced datasets. See, for example, https://www.kaggle.com/rafjaa/resampling-strategies-for-imbalanced-datasets for implementation.

Congrats on good intuition!


Merging sparse and dense data in machine learning to improve the performance I have sparse features which are predictive, also I have some dense features which are also predictive. I need to combine these features together to improve the overall performance of the classifier.

Now, the thing is when I try to combine these together, the dense features tend to dominate more over sparse features, hence giving only 1% improvement in AUC compared to model with only dense features.

Has somebody come across similar problems? Really appreciate the inputs, kind of stuck. I have already tried lot of different classifiers, combination of classifiers, feature transformations and processing with different algorithms.

Thanks in advance for the help.

Edit:

I have already tried the suggestions which are given in the comments. What I have observed is, for almost 45% of the data, sparse features perform really well, I get the AUC of around 0.9 with only sparse features, but for the remaining ones dense features perform well with AUC of around 0.75. I kind of tried separating out these datasets, but I get the AUC of 0.6, so, I can't simply train a model and decide which features to use.

Regarding the code snippet, I have tried out so many things, that I am not sure what exactly to share :(

This seems like a job for Principal Component Analysis. In Scikit is PCA implemented well and it helped me many times.

PCA, in a certain way, combines your features. By limiting the number of components, you fetch your model with noise-less data (in the best case). Because your model is as good as your data are.

Consider below a simple example.

from sklearn.pipeline import Pipeline
pipe_rf = Pipeline([('pca', PCA(n_components=80)),
                    ('clf',RandomForestClassifier(n_estimators=100))])
pipe_rf.fit(X_train_s,y_train_s)

pred = pipe_rf.predict(X_test)

Why I picked 80? When I plot cumulative variance, I got this below, which tells me that with ~80 components, I reach almost all the variance.

So I would say give it a try, use it in your models. It should help.


Classification report question I need some help to interpret the 2 classification reports of the same logistic regression. The only difference between them is the size of test_size. Even though my second classification report has a much higher f1-score, because of its low test_size (0.1), it is an inferior algorithm than my first classification report?
I am not wholly sure how to interpret the relationship between the f1-score and support values. But from what I understand, the support values should all be close to each other and f1-score be as close to 1 as possible. In that case does that make the algorithm I developed a bad one then?

Support is the number of occurrences of each label in the ground truth. For example in the results with test size=0.1, class P has only 3 samples. Based on this, if the support values are not close to each other, it only means that your data is unbalanced. This documentation might be helpful.


Image classification with different number of training image for each class I'm trying to train neural network for image classification with 4 different classes:

Cars (22k training examples)
Building (8k training examples)
Pedestrian (5k training examples)
Trees (1k training examples)

The problem is that the number of training images is biased toward one/two class(es).

I'm wondering if there is a way to train neural network in terms of the number of training examples per class?

Do I have to limit the number of training examples per class to the minimum number of all classes?

Sometimes it could be the case that one class is simply more common than others. That really bears thinking about, usually. I would guess that is not really the case for what you describe (i.e. it's rather arbitrary what kind of things people would want to classify images of in production later), so let's ignore that case (although it may affect performance on a test set depending on what's the most common in the test set). In general though, you would try out some strategies (e.g. no oversampling whether some extent of oversampling with data augmentation) and test their performance on a realistic test set.

Definitely don't throw away any images. Normally we struggle to create realistic images in data augmentation - here you already have them! Thus, a much more appealing approach is to use a data generator that sample evenly (or in whatever proportions make the most sense) from each class to create batches of training data (with some data augmentation such as slight rotations, changes in colors/saturation etc.).


How to convert sequence of words in to numbers which are input to RNN/LSTM? I am watching online videos and tutorials about use of RNN/LSTM for NLP but none of them explain how to convert the sequences of words into digitized input to the neural networks?

I am looking for intuitive understanding but answers with python code are also welcome.

For example, how do I input; "grass is green and sun is hot" to my RNN/LSTM?

from keras.preprocessing import Tokenizer
samples = ["grss is green and sun is hot"]
tokenizer = Tokenizer(num_words=1000)
tokenizer.fit_on_texts(samples)
sequences = tokenizer.texts_to_sequences(samples)

The Keras library uses it's tokenizer function but you have other well known libraries like nltk, gensim to convert them into sequences and pass it into your neural network. There are other ways like TF/IDF and CountVectorizer in Sklearn for more simpler algorithms. num_words takes the most 1000 frequent words and tokenizes them.

Link : Keras text processing


My first machine learning experiment , model not converging , tips? I wanted to recreate the model mentioned in this paper:https://arxiv.org/pdf/1610.09204v1.pdf . I am using keras with tensorflow backend, and a gtx 1050ti.

I am an ML beginner, and thought this would be a good way to get a hands on feel for things. However, My model is not converging(loss is same as first epoch).
This is what I read from that paper:

  The first convolutional layer re- ceives an input of 56px by 56px
  images with RGB channels. It uses 32 filters of size 5×5×3, stride 1
  and then sampled with max pooling of size 2 × 2, stride 1. The second
  convolutional layer has 64 filters of size 5×5×32, stride 1 and a max
  pooling of size 2 × 2, stride 1. The results of the second max pooling
  provide the first fully-connected layer with a vector of length 12,544
  (14 × 14 × 64) which are used by 512 neurons. The final
  fully-connected output layer uses a 20-wide softmax [21] which
  represents the probability of each respective 20 class labels. This
  architecture is similar to the LeNet model [3], but with using
  rectified linear unit (ReLU) [22] activation functions instead of
  sigmoid activation functions. We also use dropout [23], a technique to
  prevent overfitting, with a keep probability of 0.5 for the
  fully-connected layers.

and my code is:

model = Sequential()
model.add(Convolution2D(32, 5, 5, border_mode='same', input_shape=(70,52, 3))) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Convolution2D(64, 5, 5, border_mode='same')) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten()) model.add(Dense(output_dim=512)) model.add(Activation("relu"))
model.add(Dense(output_dim=2))
model.add(Activation("softmax")) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, nb_epoch=70, batch_size=500,verbose=1)

the full code can be found here : https://gist.github.com/harveyslash/5c98f9fdab0d53a2a48f477a52d8588d
I have scrapped the data from goodreads
Help appreciated !

EDIT

I forgot to actually ask what i wanted. Since its my first experiment, i would like to ask what are some things that I should do to make my model converge.

You may try Stochastic Gradient Descent optimizer with a learning rate decay and nesterov momentum. You can also try a different batch_size. Also you are missing drop out layers between the fully connected layers which the authors used.

Try

...
# flatten the conv layers
model.add(Flatten())
model.add(Dropout(0.5))

# fully connected 512
model.add(Dense(output_dim=512))
model.add(Activation("relu"))
model.add(Dropout(0.5))

# fully connected output layer
model.add(Dense(output_dim=2))
model.add(Activation("softmax"))

# compile
model.compile(loss='categorical_crossentropy',
              optimizer=SGD(lr=10e-4,
                            decay=10e-6,
                            momentum=0.99,
                            nesterov=True),
              metrics=['accuracy'])

# train
model.fit(x_train, y_train,
          nb_epoch=70,
          batch_size=64,
          verbose=1)

In my experience this usually helps a lot. If you get this setting to converge, you may try rmsprop and adam.


When to use Multinomial Naive Bayes? I am working on a text classification problem, and plan on using Naive Bayes based model.

In which cases should I consider using Multinomial Naive Bayes?

For Text classification Multinomial Naive bayes is a good choice to start with.

We can start with MNB,
when

1. There is no dependence between the predictors
2. Good if class is not unblanced

(Here is an approach to handle unbalanced data with MNB )

I would start with bag of words and then TFIDF vectors to check the model performance before moving further.


How to get a output of a hidden layer of a single-layer LSTM How can get the hidden layer outputs in a simple one-layer lstm?

cat("Building Model\n")

model <- keras_model_sequential() %>%
  layer_lstm(units = 64, dropout = 0.2, input_shape = c(seqlength, length(chars))) %>%
  layer_dense(units = length(chars), activation = "softmax") %>%

  compile(loss = 'categorical_crossentropy',
          optimizer = optimizer_sgd(lr = 0.001,
                                    decay = 1e-6,
                                    momentum = 0.9,
                                    nesterov = T),
          metrics = c('accuracy'))

summary(model)

cat("Training \n")

history <- model %>%
  fit(train,
      trainLabels,
      epochs = 6,
      batch_size = 16,
      validation_split = 0.2)

I found this guid but I don't know how to fit it to this simple model and what is data.

model <- ...  # create the original model

layer_name <- 'my_layer' intermediate_layer_model <- keras_model(inputs =
                  model$input,outputs = get_layer(model, layer_name)$output)
intermediate_output <- predict(intermediate_layer_model, data)

Can anybody give a sample of this?

You'll definitely want to name the layer you want to observe first (otherwise you'll be doing guesswork with the sequentially generated layer names):

model <- keras_model_sequential() %>%
  layer_lstm(units = 64,
             dropout = 0.2,
             input_shape = c(seqlength, length(chars)),
             name = "lstm") %>%
  ...

The rest is pretty straightforward:

lstm_layer_model <- keras_model(
  inputs = model$input,
  outputs = get_layer(model, "lstm")$output
)
lstm_output <- predict(lstm_layer_model, new_data)

So if you want to know the LSTM neuron values for a given case, define that case's features as new_data and run the last line.


While downsampling training data should we also downsample the validation data or retain validation split as it is? I am dealing with class imbalance problem.
In this case, I am down sampling the majority class lables in the training set.

Among training, validation and test splits, the majority class in training split is down-sampled, and test split is retained as it is. However, should the validation split be downsampled according to the training-set or should it be retained as it is?

This is because the validation set controls the training process.

I would recommend not to downsample the validation set. In the end you care about performance on the test set with the skewed class distribution. Therefore your validation set (used for hyperparameter selection, early stopping etc.) should have the same distribution in my opinion.

Have you considered upsampling the minority class? By downsampling you loose training data, which might contain valuable information and therefore might harm the learning process.


ELMo - How does the model transfer its learning/weights on new sentences Word2vec and Glove embeddings have the same vector representation for every word in the corpus and does not take context into consideration.
For eg:

The dog does bark at people
The bark of the tree is hard.

In the above examples, Word2vec and Glove create one vector for the word "bark". But using Elmo, there would be two different representations for the word "bark" as it considers context. So, I am trying to the mechanism behind how Elmo gives a vector for a new sentence in the data set?
Say, we have a pre-trained model and it has different vectors for various words. When I have to use this model on a new sentence, does Elmo produce a new vector with completely new vectors? Then is the fine-tuning of the model always happening as it is applied to new data? If that is the case, when is it considered to be a completely trained model?

ELMo does not lookup the embeddings from a pre-precomputed table as Word2Vec and GloVe. Embeddings from ELMo are hidden states of an LSTM-based language model, i.e., they are computed on the fly when you give a sentence to the network.
ELMo even does use standard word embeddings as the LSTM input. Words are treated as character sequences and those are processed with a 1-dimensional CNN that provides vector representations of the words. The word representations are then passed into two two-layer LSTMs which are trained as a forward and a backward language model repsectively. What you get as a contextual word embedding is a an average of the LSTM states.


partial numerical array - pattern matching I have a linear numerical array source and I want to find/match test array as pattern :

source = [39,36,23,21,28,36,30,22,34,37]
test = [36,23,21,28]

we can use brute force or similar method for finding the exact match, by checking test array from index 0 to len(source)-len(test)

but in our problem, we can accept this pattern too ( order is important )

test = [36,24,21,28] // changed 23 to 24

since we have many different ways of solving this problem ( maybe fuzzy!),
I want to know the title of some solutions.

According to your definition (consecutive, order matters, max +/-2 difference), it's not a fuzzy matching case. It's just a minor variant of searching a subsequence:

for i=0 to len(source)-len(test) {
  j=0
  while (j<len(test)) && (abs(source[i+j]-test[j]) <= 2) {
    j++
  if (j == len(test)) {
     // match found
  }
}

This is the simple version, in case efficiency is an issue there is a more efficient version using dynamic programming.


neg_mean_squared_error in cross_val_score The string "mean_squared_error" appears to be deprecated in cross_val_score now, and it's saying to use neg_mean_squared_error. Is this metric literally just the negative of the MSE?

You are right, neg_mean_squared_error is simple -1 * mean_squared_error. This is because a convention in the Scikit-learn api that all the scorers follow.
According to scikit-learn documentation (some emphasis added):

For the most common use cases, you can designate a scorer object with the scoring parameter; the table below shows all possible values. All scorer objects follow the convention that higher return values are better than lower return values. Thus metrics which measure the distance between the model and the data, like metrics.mean_squared_error, are available as neg_mean_squared_error which return the negated value of the metric.


Plot misclassfied values in matplotlib I have two lists. One is target and another is predicted values in a binary classification. The target is always zero. And I have to plot the results of the prediction against target in such a way, that when I get a value of 1 in prediction, then i need to plot it with a black dot on a square plane. The plane should contain white area (which means it has both target and prediction as zero) and black dots (in case of target zero but prediction as one). How can I achieve this? Thanks for the help in advance.
Something like this :

Well since the points would be shown when the target is zero and the predicted value is $1$, then all the plots would be located in the same place and would overlap. So I think you can give a random value for these points and then plot them.

If the white space is the density of $(0,0)$ values and depicts the ratio of $(0,0)$ points, then you should change the size of the figure or the points. Do you want to do so?

import numpy as np
import matplotlib.pyplot as plt
predicted = list([random.choice([0,1]) for i in range(100)])
target = np.zeros(100)

#adding a random value to the predicted and random values
randomed_predicted = [random.random() for i in range(predicted.count(1))]
randomed_target = [random.random() for i in range(len(randomed_predicted))]
plt.figure(figsize=(5,5))
plt.gca().axes.get_yaxis().set_visible(False) #hiding the yaxis labels
plt.gca().axes.get_xaxis().set_visible(False) #hiding the xaxis labels
plt.scatter(randomed_target, randomed_predicted, c="black")


Convert Pandas Dataframe with mixed datatypes to LibSVM format I have a pandas data frame with about Million rows and 3 columns.  The columns are of 3 different datatypes. NumberOfFollowers is of a numerical datatype, UserName is of a categorical data type, Embeddings is of categorical-set type.

df:

Index  NumberOfFollowers                  UserName                    Embeddings        Target Variable

0        15                                name1                      [0.5 0.3 0.2]       0
1        4                                 name2                      [0.4 0.2 0.4]       1
2        8                                 name3                      [0.5 0.5 0.0]       0
3        10                                name1                      [0.1 0.0 0.9]       0
...      ...                               ....                       ...                 ..

I would like to convert this pandas data frame into the LibSVM input format.

Desired Output:

0 0:15 4:1 1:0.5 2:0.3 3:0.2
1 0:4 5:1 1:0.4 2:0.2 3:0.4
0 0:8 6:1 1:0.5 2:0.5 3:0.0
0 0:10 4:1 1:0.1 2:0.0 3:0.9
...

One solution I found was using:

https://scikit-learn.org/stable/modules/generated/sklearn.datasets.dump_svmlight_file.html

It takes input as a NumPy array or a sparse matrix.

The UserName has a million unique values, so calling pd.get_dummies on this column and storing it as a dense NumPy array is not a solution and will not fit in the memory.

So, I know it may be done using sparse matrices, but, then I don't know how to convert the above data with mixed data types into a sparse matrix and then use sklearn.datasets.dump_svmlight_file.html.

In reality, I have many columns with mixed data types and I need to convert them into libSVM format. But, all the columns fall in one of the above three types.

Thanks in advance for any thoughts on how to solve the above problem.

As you've mentioned you can use the function by sklearn, I don't see the problem using it (perhaps I'm missing something)
import pandas as pd
from sklearn.datasets import dump_svmlight_file

def df_to_libsvm(df: pd.DataFrame):
    x = df.drop('label', axis=1)
    y = df['label']
    dump_svmlight_file(X=x, y=y, f='libsvm.dat', zero_based=True)

Regarding the categorical features with 10^6 unique categories, you can use a simple embedding for it into a binary vector.
One way to do it will be to map each username to a unique integer number.
Then you can convert the number to binary representation; that way you'll have a simple embedding of approximately size 20 (2^20=1,048,576) i.e. this feature is represented by 20 binary features.
Of course if the usernames are all unique, they probably shouldn't be a feature (identical to id).


Training Dataset for Sentiment Analysis of Movie Reviews I am currently working on sentiment analysis using Python. I wanted to find whether reviews given for a movie is positive or negative based on sentiment analysis. I have found a training dataset as provided in this link.
This dataset have reviews and a score with 1 indicating review is positive and 0 indicating movie review is negative, but it has less number of records. I have a test dataset which I will predicting based on training set. My test dataset has complex and long words for which my python ML model sometimes gives positive result for a negative reviews (returning result as 1 for negative review). I am looking for better dataset to train my model, so that my model can predict well. Could you please advise me any good/large and effective training dataset to use for this scenario? If you could share any links that would be great. The training data can be in format as 1 for positive reviews and 0 for negative reviews or even polarity like pos for positive reviews or neg for negative reviews.

You can use the SAR14 dataset of 234K IMDb movie reviews. The construction of the SAR14 dataset is detailed in the paper "Sentiment Classification on Polarity Reviews: An Empirical Study Using Rating-based Features".


WEKA Multilayer Perceptron with large dataset I'm new to data mining using WEKA.

I was trying out datasets with a large dataset (2000+ attributes with 90 instances) and left the default parameters as it is.

Why is Multilayer Perceptron running long on a dataset with 2000+ attributes?
K-Nearest Neighbour does a better job in terms of speed given the same dataset.

How does the hiddenLayer in MLP affect the speed and accuracy of the training set?

What is the most recommended way in running such large dataset, or is there none?

That is a lot of attributes.  Moreover, WEKA's default MLP size of the hidden layer is "a", where the following preset sizes are given:

a = (features + classes) / 2
i = features
o = classes
t = features + classes.

It is expected that the MLP will take longer.  Some things you can try:

use dimensionality reduction such as PCA to reduce the number of features
reduce the number of folds if you are using k-fold cross validation
reduce hidden layer size (which will probably negatively affect accuracy)
use another model such as a SVM, which would be a lot faster

I recommend going through this high level introduction to neural networks.  It is important to gain some intuition about the models you are using and the options you have available before tackling your problem.


Is plant classes or any crop is present in Imagenet datasets? I am working on a project, which is based on plants. I only want to know that whether Imagenet datasets include tree, crop classes or not.

Yes, there are many kinds of plants in Imagenet. image-net.org has a search feature. For example, http://image-net.org/search?q=tree


Value Updation Dynamic Programming Reinforcement learning Regarding Value Iteration of Dynamic Programming(reinforcement learning) in grid world, the value updation of each state is given by:

Now Suppose i am in say box (3,2). I can go to (4,2)(up) (3,3)(right) and (1,3)(left) and none of these are my final state so i get a reward of -0.1 for going in each of the states. The present value of all states are 0. The probability of going north is 0.8, and going left/right is 0.1 each. So since going left/right gives me more reward(as reward*probability will be negative) i go left or right. Is this the mechanism. Am I correct? But In the formula there is a summation term given. So I basically cannot understand this formula. Can anyone explain me with an example?

The probabilities you describe refer only to the go-north action. It means that if you want to go north, you have 80% chance of actually going north and 20% of going left or right, making the problem more difficult (non-deterministic). This rule applies to every direction. Also, the formula does not tell which action to chose, just how to update the values. In order to select an action, assuming a greedy-policy, you'd select the one with the highest expected value ($V(s')$).

The formula says to sum the values for all possible outcomes from the best action. So, supposing go-north is indeed the best action, you have:

$$.8 * (-.1 + 0) + .1 * (-.1 + 0) + .1 * (-.1 + 0) = -.1$$

But let us suppose that you still don't know which is the best action and want to select one greedily. Then you must compute the sum for each possible action (north, south, east, west). Your example has all values set to 0 and the same reward and so is not very interesting. Let's say you have a +1 reward to east (-0.1 for the remaining directions) and that south already has V(s) = 0.5 (0 for the remaining states). Then you compute the value for each action (let $\gamma = 1$, since it is a user-adjusted parameter):

North: $.8 * (-.1 + 0) + .1 * (-.1 + 0) + .1 * (1 + 0) = -.08 - .01 + .1 = .01$
South: $.8 * (-.1 + .5) + .1 * (-.1 + 0) + .1 * (1 + 0) = 0.32 - .01 + .1 = .41$
East: $.8 * (1 + 0) + .1 * (-.1 + 0) + .1 * (-.1 + .5) = .8 - .01 + .04 = .83$
West: $.8 * (-.1 + 0) + .1 * (-.1 + 0) + .1 * (-.1 + .5) = -.08 - .01 + .04 = -.05$

So you would update your policy to go East from the current state, and update the current state value to 0.83.


Why are mBART50 language codes in an unusual format? I am trying to use mBART for multilingual translation(about 30 languages) but I am facing an issue with using it as I am currently using langid to identify the languages then load mBART and translate all the words based on the language code that has been identified. But mBART uses this odd format for language codes for example:
en_XX -> English
hi_IN -> Hindi
ro_RO -> Romanian

Whereas Langid outputs them in this format:
af, am, an, ar, as, az, be, bg, bn, br

I cannot seem to find any documentation on how to interpret the mBART language code as even the research paper does not include it.

It encodes the language and its regional variant, the same way as locales are encoded. hi_IN then means Hindi as spoken in India, en_US would mean American English, en_GB British English. My guess is that en_XX means English in general.
Anyway, the first part of the locale code is the ISO 639-1 language code which is the same as langid uses.
Btw. langid works fine for documents, but not that well for isolated sentences. For isolated sentences, pre-trained FastText classifier delivers much better results.


Cross-validation of a cross-validated stacking ensemble? let me begin by saying that I understand how to build a stacked ensemble by using cross-validation to generate out-of-fold predictions for the base learners to generate meta-features. My question is about the methodology when cross-validating the entire stacked ensemble to check generalization error.

To eliminate any confusion, I'm going to call the cross-validation to generate out of fold predictions for the base learner CV A, while I'll call the cross-validation of the entire stacking ensemble CV B.

When I do CV B, is it valid to do CV A just once and use those out of fold predictions for the entire CV B process? Or do I have to keep doing CV A and generate new out of fold predictions during each fold of CV B?

Normally, I'd think that there'd be some data leakage in the first method, but one could also reason out that since the out of fold predictions are taken, well, out of fold, that issue is taken care of. The main reason I'm asking this is because doing the second method would surely remove any data leakage but there would be an order of magnitude of additional computational complexity involved.

I posted this same question on Reddit and someone was kind enough to answer

By https://www.reddit.com/user/patrickSwayzeNU

  This

    When I do CV B, is it valid to do CV A just once and use those out of fold predictions for the entire CV B process?

    Normally, I'd think that there'd be some data leakage in the first method, but one could also reason out that since the out of fold predictions are taken, well, out of fold, that issue is taken care of.

  Yes, your data set created by CV A is now "good as new".


You are running into that error because your X and Y don't have the same length (which is what train_test_split requires), i.e., X.shape[0] != Y.shape[0]. Given your current code:

>>> X.shape
(1, 6, 29)
>>> Y.shape
(29,)

To fix this error:

Remove the extra list from inside of np.array() when defining X or remove the extra dimension afterwards with the following command: X = X.reshape(X.shape[1:]). Now, the shape of X will be (6, 29).
Transpose X by running X = X.transpose() to get equal number of samples in X and Y. Now, the shape of X will be (29, 6) and the shape of Y will be (29,).


Size of folds in k-fold cross-validation When evaluating results using cross-validation, several strategies can be adopted, as using 5 or 10 folds, or doing leave one out cross-validation, as well as doing a 80/20 split.

Under which are general conditions should I try one or another?

I think cross-validation is almost always superior to a simple train-test split. The only problem is computation time (for a 5-fold cross validation you have to train your model 5 times).

If your problem allows it, you should always use cross-validation.

The only reason you shouldn't use cross-validation is if your model takes too long to train (e.g. state-of-the-art image recognition networks require weeks of training on GPU clusters).


Neural Networks: Predicting probabilities of the possible values of y, instead of just predicting y I have a true value y that I'd like to predict with a regression, but I'm interested in the probabilities that y will be different values. Y is theoretically continuous but in the dataset it is rounded to integers. Let's say y could be 0-9. I want 10 probabilities, one for each possible value. I tried doing this categorically, with a neural network having 10 output nodes, this hurts the predictions since we lose the relationships between categories, 1 is closer to 2 than it is to 10.

Example toy problem:

y is the weight of an object in pounds. The dataset has Y values rounded s.t. y can be from 0 to 9 pounds. Predict the probabilities that y will be 0-9 pounds based on the features X.

Example output: [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1,] (ten # summing to 1)

I'd like to be able to accomplish this with Keras.

Here are two approaches that use the idea of havng your prediction be a distribution over your continuous variables rather than a single value.

"Easy" but computationally expensive

Independently train K regressors.  You now have K samples from a distribution, and you can

Big downside - you have to train lots of networks, and run lots of networks at inference time.

For some details "Confidence and prediction intervals for neural network ensembles"

Harder

Have one network explicitly predict a parametrized (e.g. Gaussian/ mixture of gaussians) distribution.  For details see "Mixture density network", for example

So in the case of a single Gaussian, the network would output a mean and standard deviation, and so you have an estimate of the uncertainty.

A possible danger here is that you might need to be clever about how to train the variance parameter.  You could imagine a case where a network does a good job on the means but is either over- or under- confident in its predictions.


How to use a custom objective function? The following is what I'm trying to accomplish:

I have a charity contact data set. Each contact has features such as sex, age and so on, which we define as X.

Now we are doing a solicitation campaign: we send mails to certain contacts, with a cost of 5 dollars per person, and they can respond by making a donation of however much money they wish. For the training set, we have the persons that have received the mails, and Y_amount is the amount they donated (0 if they didn't do so). People who didn't receive the mails couldn't make unsolicited donations.

Our objective is to maximize the amount raised by the campaign after deducting the cost. However, here comes the tricky part: how could I write a loss function out of this problem and implement it within python?

My original thought was to train a neural network that gives the confidence of generating net revenues from each person Y_confidence on X using the following loss function (the money we could raise if I follow the rule):

Y_decision = 1 if Y_confidence > 0.5
Y_decision = 0 if Y_confidence < 0.5
Loss = -SUM(Y_decision * (Y_amount - 5))

Do you know how to implement it or do you have a better idea of solving this problem?

You can break the problem into two separate problems:

How much is a person likely to donate? That is a regression problem.
How to maximize the amount raised by the campaign after deducting the cost? That is contraine optimization problem.

Each separate problem can be solved with existing tools without creating a custom loss function.


Class imbalance and statistics I have a dataset with 5000 observations with class 0 and 300 observations with class 1. I would like to run some statistical analysis, for example on the average length of strings, the number of words, ...
I was calculating the average. Do you think it makes sense to calculate and compare these values with a similar difference In classes?

The answer to this question really depends on the insights you are after with these statistics.
Generally speaking however, it is wise to investigate per class in such a situation. Taking your examples this would entail comparing average string lengths for both classes. This will help you understand how both classes are (dis)similar given the dataset.


Why is pre and post silence important when collecting speech data? I've been wondering why it's important, when collecting speech data for AI machine learning, to collect pre and post silence for speech data? Is it just so the machine learns and understands the difference between "silence", "background noise", and actual "speech"?

Silence is often useful when segmenting the raw data into suitably sized samples for the machine learning methods. It is practical to run recording for several minutes at a time, but often the input to the machine learning model should for example be 1 second clips. Each clip should avoid cutting of a word midways.
Having silence allows to segment words or phrases using a simple threshold algorithm.


How many neural networks can a GPU run simultaneously? I am developing an application that would take feed from multiple IP cameras and run 3 different neural networks on it. I want to determine the number of such IP cameras that can I process parallelly on my GPU.

This is sort of like asking "how long is a rope?". To be able to answer how many neural networks you can run simultaniously on a single GPU one would require a lot of information. For example:

How much memory does your GPU have?
Are we talking training or inference?
If we are talking training, is there a performance requirements and demand for a specific number of samples per batch?
How big is each image that should be processed?
How big is the neural network?
Datatype: float16 or float32 or something else?
Deeplearning-framework used
E.t.c.

If you were to provide this information, one might be able to guess how many you can run in parallell, but it would still be hard.

Best way is if you simply try running your network and monitor the memory usage on your GPU, using for example nvidia-smi, and find the peak memory usage and then simply check of many of that fits on the total GPU-memory. However note that you need to check the specifications for the framework e.t.c. to verify how they allocate GPU-memory (for example many frameworks allocate all the memory that's avaliable)


Can I train a CNN to detect the number of objects without localizing them first? So I was trying to search but couldn't find any answers. I was wondering if it possible to train a model to detect the number of items of interest in a photo without having bounding boxes or dots to locate the objects in the training set.

For example say I wanted to count something simple like street poles in a photo, would it be possible with just the photos in the training set and the number of poles as the target only. So no bounding boxes or points labeled for the training data targets.

There are different approaches to achieve your goals:

1.) Provide only number of objects in an image in order to train an object detector.
This is called weak supervision or weak labeling.

Some works that utilize this approach:

https://arxiv.org/pdf/1711.05282.pdf : Their approach is to train an object detector on image proposals, together with object counts.
https://hal.inria.fr/hal-02393688/file/1912.00384.pdf : Object detectors can be trained by providing only one categery label per image

2.) There are several works focusing on counting objects:

For example the following works focus on counting arbitrary objects:

https://arxiv.org/pdf/1903.02494.pdf
https://arxiv.org/pdf/1604.03505.pdf

For density estimations there are several works counting people:

https://arxiv.org/abs/1903.00853

Depending on the setup, it could also be used for your application,


The proper way to codify Na in a list in R I am trying to impute missing timeseries present in different dimentions, row by row, on the whole date set. I showed the type of return of na.kalman() and it happens to be a tribble, I am not so keen on R, so I thought converting it to a plain vector will do it, something like array(as.data.frame(imputed_slice))

library("imputeTS")
library(readr)

slice <- read_csv("slice.csv")
little_slice <- slice[c(750: 850)]
for (row in 1:nrow(little_slice)) {
  ts <- little_slice[row,]
  if(any(is.na(ts)))
     {
       imputed_ts <- na.kalman(ts, model = "auto.arima")
       little_slice[row,] <- array(imputed_ts)
  }
}

#529, 811, 812 missings

Looking for warning, I see something like:

  imputeTS: No imputation performed for column 64 because of this Error
  in na.kalman(data[, i], model, smooth, nit, ...): Input data needs at
  least 3 non-NA data point for applying na.kalman

For every row, so I tried to force other columns to be NA

Like this:

slice$`811` <- NA
slice$`812` <- NA
slice$`822` <- NA
slice$`832` <- NA

To sum up, why na.kalman() not recognizing my NAs?

I made it work by coercing the timeserie to numerics

little_slice <- slice[c(750: 850)]
for (row in 1:nrow(little_slice)) {
  ts <- as.numeric(little_slice[row,])
  if(any(is.na(ts)))
     {
       imputed_ts <- na.kalman(ts, model = "auto.arima")
       little_slice[row,] <- imputed_ts
  }
}


PySpark Filter shows only 1 row I have a 12 GB .csv file and I am trying to filter rows based on some value in a column under "AppName"

Doing this in Pyspark:

display(df.filter(df.AppName=="23954ec32332dfgd"))

only yields 1 row as opposed to at least 1400 rows.

Any ideas ?

Can you try this:

df1 = df.filter(df.AppName=="23954ec32332dfgd")
print df1.count()


What are interleaved layers of convolutions? In the FaceNet paper, they describe the Zeiler&Fergus model like this:

  [...] the
  Zeiler&Fergus model which consists of multiple interleaved
  layers of convolutions, [...]

What do they mean by interleaved? How does that compare to the inception layers?

Especially, as the Zeiler&Fergus paper states

  We use standard fully supervised convnet models
  throughout the paper, as defined by (LeCun et al.,
  1989) and (Krizhevsky et al., 2012).

  [...]

  The top few layers of the network are conventional
  fully-connected networks and the final layer is
  a softmax classifier.

The Oxford dictionary explains "interleave" as

  Place something between the layers of (something)

or in the context of telecommunication, as

  Mix (digital signals) by alternating between them.

In the FaceNet paper, Schroff et. al describe their first architecture as

  multiple interleaved layers of convolutions, non-linear activations, local response normalizations, and max pooling layers

which means they create a CNN by "altering between" these layer types (e.g. Conv -> Pool -> Conv -> Pool -> ..). Or with the wording of the first definition, you "place one layer" of each type "between" the other existing layers (i.e. start with a couple of Conv layers, then place Pool layer between two Conv layers, and so on).
The final architecture would look somewhat like this:

Conv -> ReLu -> LRN -> Pool -> Conv -> ReLu -> LRN -> Pool -> Conv -> ...

So, this "interleaved" CNN is a network where the different layer types are applied in series, layer-by-layer.
As a comparison, in "inception" type CNN, you apply the different layer types in parallel (the graphic below is 1 layer):

              +---> Conv 3x3 ---+
              |                 |
prev layer ---+---> Conv 5x5 ---+---> next layer
              |                 |
              +---> Pool 3x3 ---+


Multiple filtering pandas columns based on values in another column I have a pandas dataframe df1:

Now, I want to filter the rows in df1 based on unique combinations of (Campaign, Merchant) from another dataframe, df2, which look like this:

What I tried is using .isin, with a code similar to the one below:

df1.loc[df1['Campaign'].isin(df2['Campaign']) &
        df1['Merchant'].isin(df2['Merchant'])]

The problem here is that the conditions are independent eg : I want to check if (A,1) from df2 is in df1, but with the above condition, since I am checking all the list, not row by row, it would return all rows in df1 where Campaign column is A OR Merchant column is 1.

Do you have any suggestion for this multiple pandas filtering?

Bit late but my preferred solution to this is

# verbetim from @tuomastik

import pandas as pd

df1 = pd.DataFrame({"Random numbers 1": pd.np.random.randn(6),
                "Campaign": ["A"] * 5 + ["B"],
                "Merchant": [1, 1, 1, 2, 3, 1]})

df2 = pd.DataFrame({"Random numbers 2": pd.np.random.randn(6),
                "Campaign": ["A"] * 2 + ["B"] * 2 + ["C"] * 2,
                "Merchant": [1, 2, 1, 2, 1, 2]})

# modification

def pair_columns(df, col1, col2):
   return df[col1] + df[col2]

def paired_mask(df1, df2, col1, col2):
   return pair_columns(df1, col1, col2).isin(pair_columns(df2, col1, col2))

identical = df1.loc[paired_mask(df1, df2, "Campaign", "Merchant")]


How can my Pytorch based GAN output pure B&W with no grayscale? My goal is to create simple geometric line drawings in pure black and white. I do not need gray tones. Something like this (example of training image):

But using that GAN it produces gray tone images. For example, here is some detail from a generated image.

I used this Pytorch based Vanilla GAN as the base for what I am trying to do. I suspect my GAN is doing far too much work calculating all those floats. I'm pretty sure it is normalized to use numbers between -1 and 1 inside the nn? I have read it is a bad idea to try to using 0 and 1 due to problems with tanh activation layer. So any other ideas? Here is the code for my discriminator and generator.

image_size=248
batch_size = 10
n_noise = 100

class Discriminator(nn.Module):
    """
        Simple Discriminator w/ MLP
    """
    def __init__(self, input_size=image_size ** 2, num_classes=1):
        super(Discriminator, self).__init__()
        self.layer = nn.Sequential(
            nn.Linear(input_size, 512),
            nn.LeakyReLU(0.2),
            nn.Linear(512, 256),
            nn.LeakyReLU(0.2),
            nn.Linear(256, num_classes),
            nn.Sigmoid(),
        )

    def forward(self, x):
        y_ = x.view(x.size(0), -1)
        y_ = self.layer(y_)
        return y_

Generator:

class Generator(nn.Module):
    """
        Simple Generator w/ MLP
    """
    def __init__(self, input_size=batch_size, num_classes=image_size ** 2):
        super(Generator, self).__init__()
        self.layer = nn.Sequential(
            nn.Linear(input_size, 128),
            nn.LeakyReLU(0.2),
            nn.Linear(128, 256),
            nn.BatchNorm1d(256),
            nn.LeakyReLU(0.2),
            nn.Linear(256, 512),
            nn.BatchNorm1d(512),
            nn.LeakyReLU(0.2),
            nn.Linear(512, 1024),
            nn.BatchNorm1d(1024),
            nn.LeakyReLU(0.2),
            nn.Linear(1024, num_classes),
            nn.Tanh()
        )

    def forward(self, x):
        y_ = self.layer(x)
        y_ = y_.view(x.size(0), 1, image_size, image_size)
        return y_

What I have so far pretty much consumes all the available memory I have so simplifying it and / or speeding it up would both be a plus. My input images are 248px by 248px. If I go any smaller than that, they are no longer useful. So quite a bit larger than the MNIST digits (28x28) the original GAN was created over. I am also quite new to all of this so any other suggestions are also appreciated.

EDIT: What I have tried so far. I tried making the final output of the Generator B&W by making the output binary (-1 or 1) using this class:

class Binary(nn.Module):
    def __init__(self):
        super(Binary, self).__init__()

    def forward(self, x):
        x2 = x.clone()
        x2 = x2.sign()
        x2[x2==0] = -1.
        x = x2
        return x

And then I replaced nn.Tanh() with Binary(). It did generate black and white images. But no matter how many epochs, the output still looked random. Using grayscale and nn.Tanh() I do at least see good results.

The output is going to be "continuous" if you don't use it for "classification".

You can follow few approaches here:

This would be the simplest approaches but you probably need some post processing to fill holes in lines:

Define a Threshold to make the image binary;
Redefine the last layer to output a image with 2 channels and define your final image by taking the index of the maximum channel value for each pixel (as it was a classification problem)

You can also redefine your problem:

PS:Every time a mention "line" from now on I actually mean line segment.

Given that your images are a set of geometric figures, which can all be broken down to lines and any line can be described by a set of 4 numbers, you can use a RNN to generate the set of lines (pair of points) that can be drawn easily afterwards.

To do so you need to rewrite your training set:

Define a line width for your images
Based on that line width you can set a number of line orientations possible (for example, with width of one pixel you can only draw lines in 4 orientations 0, 45, 90 135 degrees)
Use that to find every point that define line (start and end) by finding points where these orientation changes
To transform the set of points into a set of lines you can check if there is a black pixel in that orientation that is in the interval.

Binary Layer Issue

Your binary layer implements a Heaveside Step function which will kill the gradient since it's derivative is 0 for all values other than x=0. In x=0 the derivative is actually not defined (limit goes to infinity).

Thus your weights can't update, so every forward pass will filter data with random weights, causing random effects to the output.

Check this answer to understand more about why we don't use Heaveside Step function as an activation function on neural networks


How is an ASR's output compared to ground truth for validation? I am curious how it is done as I am interested in doing something similar. I have some manually transcribed data that contains tags for multiple speakers. I want to compare how well the out of the box ASRs (Google, AWS Transcribe) are able to diarise speakers (or in simple words identify and transcribe audio with multiple speakers). I want to compare it to the ground truth data I have and come up with a comparison metric.
I can use Levenshtein Distance or something like Ratcliff-Obershelp similarity as a metric. But I am trying to learn if there is a more standard way of doing this?

The answer I was looking for is Word Error Rate. It is the most standard way of comparing ASR transcriptions wrt ground truth. It is less granular than what I had in mind, it is basically Levenshtein Distance on a word level rather than a character level.
jiwer in python has a few other metrics and is easy to use.


confusion on outliers I am not able to distinguish the outliers - When to go with std. dev and When do we need to go with Median.
My understanding on std. dev. is - if the data is away from mean by more than 2 std dev. we consider that as outlier.
Similarly for Median, we say that any data that is not in-between q1 and q3, we say again that as outlier.

So am confused which one to choose.

Can you guys help me understand.

It completely depends on the context of the data that is being considered. For example, $2\sigma$ from the mean ($\mu$), depends on the distribution of the data. What is the value of $\mathbb{P}(-2\sigma < X - \mu <2\sigma)$.

Also, there are many methods for outlier detection, and all of them depend on the context. Hence, you cannot say which method should be used by taking it outside the context. You should do some experiments by these methods over the data, and then base on the real outliers sample, decide which method is proper for the current data.


Predicting Intent to do X with a confidence score or intent percentage score? I have a data set like:

did_purchase  action_1_30d action_2_20d action_2_10d ....
   False            10          20            100
   True            ....etc

Where did_purchase shows whether the customer purchased or not, and the columns indicate the volume of actions taken before the purchase (or non-purchase) event.

So, for the first row the customer did 10 of action_1 within 30 days of the purchase event, but didn't purchase in the end.

I have been using sklearn's LogisticRegression to predict the did_purchase false/true, and can get about 89% accuracy, which is nice.

However, I'd like a percentage intent score instead. So it could say user-321 has a 46% chance of purchasing in the next 10 days.

What would be a good algo/approach for this?

You could use the probabilities output by LogisticRegressions predict_proba method.

Almost all classifiers give you a probability in sklearn. One exception is the support vector classifier which will give you a points distance to the decision hyperplane, which can be interpreted as a confidence (you can get probabilities for the support vector classifier, but it is through a computationally costly cross validation process).


What are limitations of decision tree approaches to data analysis? The major limitations of decision tree approaches to data analysis that I know of are:

Provide less information on the relationship between the predictors and the   response.
Biased toward predictors with more variance or levels.
Can have issues with highly collinear predictors.
Can have poor prediction accuracy for responses with low sample sizes.

Are there any others? Are they robust to traditional statistical assumptions such as homogeneity, normality, independence?

Simple decision trees have some limitations listed below. Fortunately, some of these can be fixed used ensemble learning techniques (think bagging, boosting...).
Concerning limitations :

Trees tend to overfit quickly at the bottom. If you have few observations in last nodes, poor decision can be taken. In this situation, consider reducing the number of levels of your tree or using pruning.

Trees can be unstable because small variations in the data might result in a completely different tree being generated.

Decision trees perform greedy search of best splits at each node. This is particularly true for CART based implementation which tests all possible splits. For a continuous variable, this represents 2^(n-1) - 1 possible splits with n the number of observations in current node.

For classification, if some classes dominate, it can create biased trees. It is therefore recommended to balance the dataset prior to fitting.
Also, Some distributions can be hard to learn for a decision tree. An example below (XOR) :


Applying activation on part of the layer in Keras Context
I am trying to implement the YOLO algorithm in Keras. What I have so far is the following network:
i = Input(shape=(image_height,image_width, image_channels))
rescaled = Rescaling(1./255)(i)
x = Conv2D(16, (1, 1))(rescaled)
x = Conv2D(32, (3, 3))(x)
x = LeakyReLU(alpha=0.3)(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Conv2D(16, (3, 3))(x)
x = Conv2D(32, (3, 3))(x)
x = LeakyReLU(alpha=0.3)(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Flatten()(x)
x = Dense(256, activation='sigmoid')(x)
x = Dense(grid_width * grid_height * anchor_number * (5 + class_count))(x)
x = Reshape((grid_width, sgrid_height, anchor_number, (5 + class_count)))(x)

Which supposed to output, for each grid cell and anchor box, a vector of $(p(c), b_x, b_y, b_h, b_w, class_0, class_1, ..., class_n))$
The problem
Some of the output vectors, namely $p(c)$, $b_x$ and $b_y$, are limited to be between 0 and 1, so they should pass through a sigmoid activation. The part of $class_0, class_1, ..., class_n$ is a classification, so it should pass through a SoftMax activation. So I need a way to specify what part of the output needs to use which activation.
TL;DR: How do I apply different activation functions to different parts of the network output?

YOLO architecture uses the softmax activation function determining the classes of objects in bounding boxes in the output layer. But from the code you have shared, sigmoid is used in the last layer for prediction. It seems that you're trying to implement YOLOv3.
Please look at this paper original paper. This will help to give a better understanding.
Authors of YOLOv3 have refrained from softmaxing the classes since the method rests on the assumption that classes are mutually exclusive. For example, if there are classes like “cat” and “animal” in the dataset and one of the objects in the bounding boxes is a cat, this assumption fails, because a cat is also an animal. Instead, independent logistic classifiers predict each class score and a threshold is used to perform multilabel classification for objects detected in images. An element belonging to a certain class will not be influenced by the decision of that element belonging to another class (binary cross-entropy loss).


What to use in setting up a Speech to Text engine in production? So i have the task to study the feasability of setting up a Speech-To-Text engine in a production environnement, and i've been researching on this topic, so I tried Google's Speech-To-Text API and there is a quota on how many transcribtions you can use daily => That's a no-no. Second alternative, CMU Sphinx ( Open Source pretrained model for that purpose ), just tested it, and the output is messy for the LiveSpeech feature ( Perhaps i need to adjust some parameters first ). Third option, using the available deep learning tools to create my own SpeechRecognition engine ( I have tons of recorded conversations of people speaking French ).

Question : What should i use for my case? building an engine from scratch using neural networks seems complicated for a beginner in ML like me, using CMU Sphinx seems easy, but i have a feeling it's not. Any advice on what approach should i follow?

This would be a gift to the world. I certainly hope someone is working on it even as we speak (ha ha). Let's both keep looking.


Why is PCA often used before t-sne for problems when the goal is only to reduce dimensionality? Ex: Matlab's t-sne tutorials frequently use PCA

https://www.mathworks.com/help/stats/tsne-settings.html

" Process Data Using t-SNE

Obtain two-dimensional analogs of the data clusters using t-SNE. Use the Barnes-Hut algorithm for better performance on this large data set. Use PCA to reduce the initial dimensions from 784 to 50. <- (1) Why are we using PCA here to reduce dimensions to 50 first at all if we are going to use t-sne after PCA to reduce to 2 dimensions anyway?

Matlab Tutorial Code: https://www.mathworks.com/help/stats/tsne-settings.html

rng default % for reproducibility

Y = tsne(X,'Algorithm','barneshut','NumPCAComponents',50);

figure gscatter(Y(:,1),Y(:,2),L)

1) See question bolded above

2) What would you have googled to find this out?

I had googled "Why is PCA often used before t-sne for problems when the goal is only to reduce dimensionality? "

t-SNE is computationally expensive, more than PCA. Many examples might use PCA just to simplify the problem.

Moreover, it is explained here:

  If the data set is high dimensional, doing principal component
  analysis is recommended because otherwise the curse of dimensionality
  can be an issue. TSNE makes the assumption of local linearity which
  might not hold in high dimensions where the manifold may be varying
  and PCA can help alleviate this issue by reducing the dimensionality
  of the data.

In other words, it seems that t-SNE suffers from high dimensionality of your data. It is typically used to represent a manifold in 2-3 dimensions, not tens or hundreds like, for example, PCA. It's mostly a visualization tool IMHO.

On the googling part, I suggest you to search only for the most relevant elements of your search, not for articulated sentences. I googled something like: "why pca before tsne" and it was enought to find useful stuff. Browsers don't need syntactic coherence, just the right keywords.


Text classification with neural network number of input neurons I am classifying documents I have around 4000 of them that I am trying to categorise into 5 categories. I am using a bag of words model which equates to about 18,000 unique words (features) and therefore I have an input layer of a neural network with 18,0000 inputs which doesn't seem right.

It is taking a huge amount of memory to try and train this network and so much time it will never converge!

Is there a way of reducing the number of input neurons seeing as a large portion of this data will be nulls?

Yes, actually what usually people do is to map the unique tokens in a space with fixed dimensionality, obtaining what is called "words embeddings".

And actually, using already trained word embeddings like GloVE is usually it's a best practice: those vectors are trained on huge datasets like Wikipedia or Common Crawl. A nicety of those vectors is that, thanks to the way they are built, they include also the relation between the words, a sort of semantic.
This is the way I definitely suggest you to start.


Supervised learning approach - creating my own labels Scenario - I have data that does not have labels but I can create a function to label the data based on behavior and deploy the model so I don't have to keep labeling the data. Is this considered machine learning ?

Objective: Classify accounts with Volume spikes based on high, medium or low labels to deploy on big data (trillions of lines of data) .

Data: The data I have includes the following attributes:
Account, Time, Date, Volume amount.

Method:

Create a new feature column called spike and create a pandas function to ID a spike greater than 5.  Is this feature engineering ?
Next I create my label column and  classify it as low medium or high spike.
Next I train a machine learning classifier and deploy it to label future accounts with similar patterns in big data.

Thoughts on this process ? Is this approach correct for machine learning ?

The approach you're describing might be good but the main question is how the automatic labeling works. You say you can "create a function to label the data based on behavior": what is the behavior? Do you manually decide the label or is entirely automatic? If it's completely automatic and general enough so that it can work for any instance, then you don't need any ML since you can directly apply your function. On the other hand if it's specific to a subset of data (or requires some amount of manual decision) then it makes sense, and the challenge will be whether the features are informative enough to predict the label for fresh instances from a different subset. ML assumes that your test instances come from the same distribution as the training set.

  Create a new feature column called "label" that classifies accounts based on a user defined function that IDs spikes in volume. Is this feature engineering ?

It's not feature engineering if it's the class that you will try to predict later with new instances. Feature engineering would be selecting particular features, for example instead of volume amount you could have minimum, maximum and mean volume, or discretizing the time, etc.


compare log and division of numbers What is the difference between (a) taking the logarithm of a set of numbers (b) dividing the set of numbers by an integer. Both appear to reduce the scale of set of numbers, so can they be used interchangeably?

Both of these are feature transformation techniques but they do different things due to their different mathematical properties. In general it is good to normalize the data we are working with, this is usually done with a linear transformation. However, the logarithm is useful when the range of the data is too large.

For example [1, 10, 100, 1000, 10000, 100000], these data points have too large of a range. If we normalize this data with linear transformation using the max, we get: [1.e-05, 1.e-04, 1.e-03, 1.e-02, 1.e-01, 1.e+00]. You can see that the lower values in this data are all essentially 0. However, with the logarithm of this vector we would get [0, 2.303, 4.605, 6.908, 9.210, 11.513]. We can see that these values now have a much better spread.

x = [1, 10, 100, 1000, 10000, 100000]
x_trans_lin = [t/max(x) for t in x]
x_trans_log = [np.log(t) for t in x]

plt.figure(figsize=(10,5))
plt.subplot(1,2,1)
plt.scatter(range(len(x)), x)

plt.subplot(1,2,2)
plt.scatter(range(len(x)), x_trans_lin, label='linear')
plt.scatter(range(len(x)), x_trans_log, label='log')
plt.legend()
plt.show()


How to save and test CNN model on test set after training My CNN model is trained on the training set and validated on the validation set, now I want to test it on test set, here is my code:

x_img = tf.placeholder(tf.float32, name='x_img')
y_label = tf.placeholder(tf.float32, name='y_label')
reshape = tf.reshape(x_img, shape=[-1, img_x, img_y, img_z, 1], name='reshape')

def CNN_Model(input):
    conv1 = conv_layer(reshape, num_channels, n_f_conv1, name="conv1")
    max_pool1 = maxpool_layer(conv1, name="max_pool1")
    conv2 = conv_layer(max_pool1, n_f_conv1, n_f_conv2, name="conv2")
    max_pool2 = maxpool_layer(conv2, name="max_pool2")
    shape = 4*4*4*64
    flattened = tf.reshape(max_pool2,shape=[-1, shape], name='flattened')
    fc = fc_layer(flattened, shape, n_node_fc, name="fc")
    dropout1 = dropout(fc, keep_rate, name="dropout1")
    output_layer = output(dropout1, n_node_fc, num_classes, name="output_layer")
    return output_layer

def train_CNN(input):
    train_predict = CNN_Model(x_img)
    with tf.variable_scope("cross_entropy", reuse=tf.AUTO_REUSE):
        lose = tf.nn.softmax_cross_entropy_with_logits_v2(logits=train_predict, labels=y_label, name='cross_entropy')
        cost = tf.reduce_mean(lose, name='reduce_mean_cost')
        tf.summary.scalar("cost", cost)

    with tf.variable_scope("optimization", reuse=tf.AUTO_REUSE):
        optimizer = tf.train.AdamOptimizer(learning_rate, name='AdamOptimizer').minimize(cost)

    init = tf.global_variables_initializer()
    print("Starting session...")
    with tf.Session() as sess:
        sess.run(init)
        all_time = 0
        batch_size = 120
        batch = 0
        print("Starting training...")
        for epoch in range(num_epochs):
            train_batch = train_data[batch:batch_size]
            batch += batch_size
            batch_size += batch_size
            start_time = time.time()
            ep_loss = 0
            for data in train_batch:
                X = data[0]
                Y = data[1]
                _, c = sess.run([optimizer, cost], feed_dict={x_img: X, y_label: Y})
                ep_loss += c
            end_time = time.time()
            all_time += int(end_time-start_time)
            print('Epoch', epoch+1, 'completed out of',num_epochs,'loss:',ep_loss,
                  'time usage: '+str(int(end_time-start_time))+' seconds')

        correct_predict = tf.equal(tf.argmax(train_predict, 1), tf.argmax(y_label, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_predict, tf.float32), name='reduce_mean_acc')
        print("Validation accuracy:", accuracy.eval({x_img:[i[0] for i in validate_data],
                                               y_label:[i[1] for i in validate_data]}))
        print("Test accuracy:", accuracy.eval({x_img:[i[0] for i in test_data],
                                               y_label:[i[1] for i in test_data]}))

I have a test dataset stored as test_data like train_data in the code above, tried to do it in more than one way, but I did not succeed, can anyone share a testing code with me, ofcourse based on my code?

I don't know exactly where you have the problem but according to comments, take a look at the following line.

_, c = sess.run([optimizer, cost], feed_dict={x_img: X, y_label: Y})

feed_dict is used for passing data to your network. As you can see, X is the training data. You can replace it with the test data. You should also change the y_labels to the labels of the test data.


How do two perceptrons produce different linear decision boundaries? I'm trying to visualize how two perceptrons converge to two different decision boundaries (which is ultimately used to create the classifier for the non-linearly separable data).

 Source: https://tdb-alcorn.github.io/2017/12/17/seeing-like-a-perceptron.html

I don't understand how the second perceptron creates a different decision boundary when it has the same input as the first perceptron? I know the weights can be initialized differently but does this second perceptron classify something else? Shouldn't the decision boundaries be converge to be the the same ultimately?

 Source: https://tdb-alcorn.github.io/2017/12/17/seeing-like-a-perceptron.html

In the article which you linked, it seems that none of the two perceptrons are trained on data, you just have two perceptrons with different weights w0 and w1. If they have different weights, then they will have two different decision boundaries.
You can see that, looking at the code that plots the decision boundaries:

ax.plot(t0, decision_boundary(w0, t0), 'm', label='Perceptron #0 decision boundary')
ax.plot(t1, decision_boundary(w1, t1), 'g', label='Perceptron #1 decision boundary')

where decision boundaries is

def decision_boundary(weights, x0):
    return -1. * weights[0]/weights[1] * x0


What is the rationale for discretization of continuous features and when should it be done? Continous feature discretization usually leads to lose of information due to the binning process. However most of the Top solutions for Kaggle Titanic are based on discretization(age,fare).

When should continuous features be discretized ? Is there any criteria and pros and cons on accuracy.

One reason to discretize continuous features is to improve signal-to-noise ratio.  Fitting a model to bins reduces the impact that small fluctuates in the data has on the model, often small fluctuates are just noise. Each bin "smooths" out the fluctuates/noises in sections of the data.


Why does changing the cluster number change the plot in Kmeans? This might be a dumb questions but I can't find the answer to it. I don't have the perfect mathematical understanding of kmeans, so apologies if it is.

I'm just wondering why I see a different plot when I change the number of clusters in a kmeans plot?

Here's the code that I'm using:

set.seed(1)
k <- kmeans(data, centers = x)
plotcluster(data, k$cluster)

I vary x to see how the plot looks like. Below are the results for x = 3 and x = 4. Apologies for the poor formatting.

I'm wondering why both plots look different if I'm only varrying the number of clusters. Is it because the principle components that are being show, dc1 and dc2, are different as you change x, so as to maximize the variance displayed?

Another quick question is - can you determine the cluster # by how "neat" the clustering looks in the plot? I know there are various methods of doing it, I'm just wondering if the plot is indicative of good/bad clustering as well.

any help is appreciated!

plotcluster changes the projection based on the result that you give. See the documentation for more details.

Because of that you cannot compare the two plots.


back propagation in CNN I have the following CNN:

I start with an input image of size 5x5
Then I apply convolution using 2x2 kernel and stride = 1, that produces feature map of size 4x4.
Then I apply 2x2 max-pooling with stride = 2, that reduces feature map to size 2x2.
Then I apply logistic sigmoid.
Then one fully connected layer with 2 neurons.
And an output layer.

For the sake of simplicity, let's assume I have already completed the forward pass and computed δH1=0.25 and  δH2=-0.15

So after the complete forward pass and partially completed backward pass my network looks like this:

Then I compute deltas for non-linear layer (logistic sigmoid):

$$
\begin{align}
&\delta_{11}=(0.25 * 0.61 + -0.15 * 0.02) * 0.58 * (1 - 0.58) = 0.0364182\\
&\delta_{12}=(0.25 * 0.82 + -0.15 * -0.50) * 0.57 * (1 - 0.57) = 0.068628\\
&\delta_{21}=(0.25 * 0.96 + -0.15 * 0.23) * 0.65 * (1 - 0.65) = 0.04675125\\
&\delta_{22}=(0.25 * -1.00 + -0.15 * 0.17) * 0.55 * (1 - 0.55) = -0.06818625\\
\end{align}
$$

Then, I propagate deltas to 4x4 layer and set all the values which were filtered out by max-pooling to 0 and gradient map look like this:

How do I update kernel weights from there? And if my network had another convolutional layer prior to 5x5, what values should I use to update it kernel weights? And overall, is my calculation correct?

A convolution employs a weight sharing principle which will complicate the mathematics significantly but let's try to get through the weeds. I am drawing most of my explanation from this source.

Forward pass
As you observed the forward pass of the convolutional layer can be expressed as
$x_{i, j}^l = \sum_m \sum_n w_{m,n}^l o_{i+m, j+n}^{l-1} + b_{i, j}^l$
where in our case $k_1$ and $k_2$ is the size of the kernel, in our case $k_1=k_2=2$. So this says for an output $x_{0,0} = 0.25$ like you found. $m$ and $n$ iterate across the dimensions of the kernel.
Backpropagation
Assuming you are using the mean squared error (MSE) defined as
$E = \frac{1}{2}\sum_p (t_p - y_p)^2$,
we want to determine
$\frac{\partial E}{\partial w^l_{m', n'}}$ in order to update the weights. $m'$ and $n'$ are the indices in the kernel matrix not be confused with its iterators. For example $w^1_{0,0} = -0.13$ in our example. We can also see that for an input image $H$x$K$ the output dimension after the convolutional layer will be
$(H-k_1+1)$x$(W-k_2+1)$.
In our case that would be $4$x$4$ as you showed. Let's calculate the error term. Each term found in the output space has been influenced by the kernel weights. The kernel weight $w^1_{0,0} = -0.13$ contributed to the output $x^1_{0,0} = 0.25$ and every single other output. Thus we express its contribution to the total error as
$\frac{\partial E}{\partial w^l_{m', n'}} = \sum_{i=0}^{H-k_1} \sum_{j=0}^{W-k_2} \frac{\partial E}{\partial x^l_{i, j}} \frac{\partial x^l_{i, j}}{\partial w^l_{m', n'}}$.
This iterates across the entire output space, determines the error that output is contributing and then determines the contribution factor of the kernel weight with respect to that output.
Let us call the contribution to the error from the output space delta for simplicity and to keep track of the backpropagated error,
$\frac{\partial E}{\partial x^l_{i, j}} = \delta^l_{i,j}$.
The contribution from the weights
The convolution is defined as
$x_{i, j}^l = \sum_m \sum_n w_{m,n}^l o_{i+m, j+n}^{l-1} + b_{i, j}^l$,
thus,
$\frac{\partial x^l_{i, j}}{\partial w^l_{m', n'}} = \frac{\partial}{\partial w^l_{m', n'}} (\sum_m \sum_n w_{m,n}^l o_{i+m, j+n}^{l-1} + b_{i, j}^l)$.
By expanding the summation we end up observing that the derivative will only be non-zero when $m=m'$ and $n=n'$. We then get
$\frac{\partial x^l_{i, j}}{\partial w^l_{m', n'}} = o^{l-1}_{i+m', j+n'}$.
Then back in our error term
$\frac{\partial E}{\partial w^l_{m', n'}} = \sum_{i=0}^{H-k_1} \sum_{j=0}^{W-k_2} \delta_{i,j}^l o^{l-1}_{i+m', j+n'}$.
Stochastic gradient descent
$w^{l(t+1)}_{m', n'} = w^{l(t)}_{m', n'} - \eta \frac{\partial E}{\partial w^l_{m', n'}}$
Let's calculate some of them
import numpy as np
from scipy import signal
o = np.array([(0.51, 0.9, 0.88, 0.84, 0.05),
              (0.4, 0.62, 0.22, 0.59, 0.1),
              (0.11, 0.2, 0.74, 0.33, 0.14),
              (0.47, 0.01, 0.85, 0.7, 0.09),
              (0.76, 0.19, 0.72, 0.17, 0.57)])
d = np.array([(0, 0, 0.0686, 0),
              (0, 0.0364, 0, 0),
              (0, 0.0467, 0, 0),
              (0, 0, 0, -0.0681)])

gradient = signal.convolve2d(np.rot90(np.rot90(d)), o, 'valid')

array([[ 0.044606,  0.094061], [ 0.011262,  0.068288]])

Now you can put that into the SGD equation in place of $\frac{\partial E}{\partial w}$.

Please let me know if theres errors in the derivation.

Update: Corrected code


How to reduce dimensionality of audio data that comes in form of matrices and vectors? I'm working on a project involved with identifying different types of sounds (such as screams, singing, and bangs) from each other.  We've got our data a reasonable number of different transformations (e.g.: spectrograms, chromagrams, MFCCs, etc.), but since most of our features are 2-dimensional matrices (some are actually 1-dimensional vectors), we'd like to reduce this information in some way, so that the machine learning we're hoping to do takes a "feasible" amount of time.  However, I don't quite know enough about math and statistics to make an educated decision on this.

Our data consists of small sound files from ~1-10 seconds long.  There are recordings of screams, singing, bangs (and other man-made noises), and birds (and other natural noises).  We are hoping to be able to differentiate and identify each source type from the others.  See https://github.com/BenSandeen/surveillance_sound_classifier/blob/master/Project.ipynb for the different plots we have made to guide our selection of features to use.  Focus mainly on the 3x3 plots, as that's where the comparisons are being made.  These plots are primarily time vs. frequency, with amplitude represented by color.

I was thinking that maybe we could "collapse" each matrix down to a vector by somehow choosing some representative frequency/amplitude-related feature at each time slice (we're using Short Time Fourier Transforms to analyze the sounds) and then get a vector of some length, containing a bunch of scalars.  Although this could make accounting for different lengths of sounds difficult.  Would it be reasonable to just set the shorter sounds' vectors to be filled in with zeros if they have no useful data?  That would effectively make these sounds a projection onto some lower-dimensional space.  Then, maybe we could just use dot products to compare the vectors; if they're parallel, they'll have a large for product, but of they're nearly perpendicular, they'll have a dot product near zero.

Alternatively, I was thinking that something like a trace of our matrices, or finding their characteristic polynomial, might be useful direction to pursue.  I've read a bit about PCA, but I'm not quite understanding it enough to know if this may be what I'm looking for.

Can anyone think of any other ways of handling and reducing this data?  For what it's worth, we're currently planning on using Sci-Kit Learn (sorry that I can't use more than 2 links yet) to perform our machine learning.

Interesting ideas on finger printing, but that isnt going to work here, as you don't have a definitive sound you are working with.

To me, this seems a bit more like an image processing problem. So instead of thinking how you could reduce dimensionality, could could try and combine your data together into "images" that digitally represent the sounds you are looking at. Then using standard image processing techniques. I say this waving my hands around vaguely, as I don't know of specific ones, but have enough understanding of edge detection, feature detection and applying map/reduce type methodologies which might be able to give templates for what you are after.


Interpreting the new dimensions after PCA I have telecom data with large number of dimentions. Now if I apply dimentionality reduction like PCA then from resulting dimention say PC1, PC2 I would loose the meaning or would not understand what they represent.

Are their any techniques other than PCA which can provide any meaning or intution about the new dimentions. Also suggest if there are any research papers in this.

To give meaning to the Axis of the PCA, you can study the scalar product between the two new axes and all your original axes (you have to normalise the vectors before doing that).

Those axes with a high scalar product will be highly associated the with the new axes and viceversa. This may help you interpret them.


Shape Error in Tensorflow I am just trying to assign value to a placeholder but i m am getting error:Cannot feed value of shape () for Tensor 'image_input_22:0', which has shape '(1, 51, 51, 64)'

import numpy as np
import tensorflow as tf
res=np.random.random([1,51,51,64])
image=tf.placeholder(tf.float32,shape=[1,51,51,64],name="image_input")
reshaped_image=tf.reshape(image,[1,51,51,64])
init=tf.global_variables_initializer()
res=0
with tf.Session() as sess:
 sess.run(init)
 res=sess.run(reshaped_image,feed_dict={image:res})

You reinitialized res to 0, which is a scalar, hence the error.


Why DDPG's policy target is Q-value itself? Could someone explain why the target of the DDPG's policy is $Q(s,\mu(s))$?

My understanding of DDPG is like this. Since it is intractable to calculate the $argmax_a Q(s,a)$ in a continuous space, DDPG uses an universal function estimator (Neural Network) to learn and predict the best action that realize $maxQ(s,a)$ output.

So, my question is that what is the actual target when DDPG trains $\mu(s)$? I thought that it should be an actual action that gives the highest Q-value at given state $s$ ($argmax_aQ(s,a)$). However, in OpenAI spinning up, it says it can make approximation that $max_aQ(s,a)\approx Q(s,\mu(s))$, and says the loss of the policy is $E[Q(s, \mu(s))]$.

What does this mean? Are they targeting the Q-value, instead of action-value? How can this policy can learn the best action?

In DDPG, the actor network is used to calculate the action that maximises the expected reward in a given state, i.e. the action that maximises the $Q$ function. Critic network is used to calculate the action value in a given state. So the critic network is updated just like the way in DQN. While updating the actor however, we don't have a supervised target action (actual action that maximises the Q function) and our aim is to generate the action that maximises $Q(s,a)$, So we generate an action using actor, apply it to the critic function, then modify the actor params to maximise the $Q(s,a)$, i.e. the gradients flow all the way back from Q network to the actor and the update is done using gradient ascent. So, the policy network is not trained independently, it is done with the help of critic network using the $Q$ value as the target and I'd say the loss of the policy is $-E[Q(s, \mu(s))]$.