Machine learning image training set data as whole object but finding part of object If the images to test against are only part of the object, does it help to have training set data of the whole object, or is it better for all the training data to be parts of the object? For instance, if test pictures are the legs of a chair or the top of a chair, does it help to have training set image data of the entire chair? Or should I focus on only getting training set data of parts of the chair? I'm planning on using TensorFlow.

I would be classifying the object as a chair, not part of a chair. But if it would give me a higher rate of recognizing chairs by having it recognize parts, it could recognize parts, and I could combine the information at the end. I only need to recognize at least one chair (or much more likely part of a chair) in the image.

Train with the same kinds of inputs as you wish the network to recognise. There is no easy way to add logical combinations that you think should help the network, because deep neural networks, despite the hype as the best thing to hit AI, are essentially just really dumb complex statistical analysers.

Provided you supply training images, the CNN will learn how to statistically combine components to decide if overall an image is a chair or not. For instance if the image is of a person wearing a padded jacket, perhaps that will look a little like the back of a chair. The NN might output a 0.75 confidence that a back of a chair is in the image. If you manually combine decisions from components, you will need to figure out from that whether to say there is a chair in the image. If you leave it to the network, the CNN itself will combine this data with the additional detection there is a 0.2 confidence that the person's legs are chair legs (and they are in the wrong relative place) and decide - hopefully - that there is no chair.

You don't need to explicitly label components of objects for the NN to learn that there are components and what they look like. The outputs of Deep Dream and other CNN visualisations demonstrate that internally the CNN builds up its own structures that represent parts of objects. They are not necessarily the same parts as a person would focus on, but by allowing the training process to choose, you should be able to  get a good recognition rate.

So, if you give the network a lot of chair parts to recognise separately, it will get better at labelling those chair parts accurately. But that will not really help the network recognise a chair. In fact it could be worse at the main task if the training images are too different from images it is expected to recognise. Plus, it will be your task to then decide how to re-combine the classes to detect the chair later.

There probably are architectures and image-processing pipelines that can be used to combine logical knowledge of parts and their poses into whole objects. It is something I very much expect to be an active area of research in computer vision systems. However, these approaches will not use a simple CNN and altering training data as the solution.


Setting "missing" distance values to zero when training a neural network Not sure if missing values is the right name to use here. I want to train a DNN on data given by a sensor. The sensor gives the (x,y) coordinates of the founded objects. The sensor can keep track of up to 32 objects at once. If the sensor can't find 32 objects (which is always the case) it sets the x and y coordinates of the objects not found to zero. Could there be a problem training a neural network on this kind of data? The sensor is set on a car, the objects are other cars and the networks job is to predict the next move.

Another problem is that when the sensor finds a new object, already existing/found object might change id. Any tips on that? I am thinking about making randomly permute the indices of the object since that should not make any difference?

Are there any standard solutions to these kind of problems? Especially Setting the distance of non existing objects to zero.

The short answer is "it depends"

Is zero better than one or infinity?  It depends on the range of x-y coordinates that you use.  It also depends on your output.  If you go from (x,y) to something like z = x^2, then you're placing your "null" at a local minimum. So, if it's a car, I'm assuming you're using a forward-facing camera.  Do you really want your algorithm to see a bunch of "cars" bunched up at the origin?
By the way, where is your origin?  Upper left (like most image indexing)? Lower left (like a cartesian plane)? Center (if so, what direction is positive x and y?)

As for re indexing, what you can do is look at the previous frame (or frames) where your 32 objects are indexed, along with their position and velocity.  Then, look at the current frame and compare each object's position to the positions in the previous frame.  Assign the objects in the current frame the same id as the object nearest it in the previous frame.  You can break ties with velocity.

following up on your comments:

a neural network can learn any arbitrary function to any arbitrary precision.  Eventually.  If you want certain behavior at (0,0) that is dramatically different from behavior at (0+ε,0+ε), then your network will take a long time to converge.

I suggest seeing what kind of results you get by converting nulls to zero and then comparing it to other techniques.  Maybe replace null with the average of all the other cars, or set it immediately behind your ego vehicle (since cars primarily move forward, your network will probably put less importance on cars behind the ego car).


How to set the Reconstruction error threshold for anomaly detection using autoencoders? Hi I am doing anomaly detection using auto encoders.I have trained the model using 'Non Anomalous' values.Now when I give anomalous points as test data.

What should be the Reconstruction error threshold I should give to classify it as whether it is anomalous or not?

I have currently set it to 0.1 , but it doesnt find any anomalies(actually test data have lot of anomalies)
What are the factors that I should consider before setting the value?

If one considers prediction of anomalous status as binary classification (i.e., if reconstruction error < threshold, classify as normal, else classify as anomalous), one can find the threshold that maximizes some appropriate metric of classification performance (e.g., F-beta) by optimizing the metric over a suitable validation set containing normal and anomalous data. See Malhotra et al., 2016 for an example of how to do this for time series.


what is the training phase in N-gram model? Following is my understanding of N gram model used in text prediction case :
Given a sentence say, " I love my " (say N = 1 /bigram),  using N gram and say 4 possible candidates ( country, family, wife, school) I can estimate the conditional probability on each of the candidates and take the one with highest probability as the next word.
Question :

I understand the probability part of the model but to even get to the probability, we need the possible candidates ( next words, in this case family, wife, school, country). How does the model choose the candidates

Most of the articles online talk about the probability part but doesn't mention anything about training phase. What exactly is happening in training phase of this model?

I will start with an advice -  just google "n gram language model" and you will find a lot of good detailed explanations.
With that being said I will give a short explanation about the "training phase" of n-gram language models (answer to question 2). The simplest way to build an N-gram language model strats with finding a big corpus - a set of many sentences. the words of the model will be the words that appear at least once in the corpus. The probability of the word xn given a past context of the words x1,x2,...,xn-1 will be the number of occurrences of the sequence x1,x2,...xn-1,xn in the corpus / the number of occurrences of the sequence x1,x2,...,xn-1 in the corpus.
This is the simplest way and it has problems, especially What happens if the sequence x1,x2,...,xn does not appear in the corpus? It will always get propability zero. Therefore there are smoothing techniques to handle this problem (read about it).
And now for question 1 - In the simplest case, without smoothing, the candidates are the words that appear in the corpus. In models with smoothing the candidates may be all of the words in the sense that every word might get a positive probability.


Monthly trend with fb prophet-Interpreting the graph I have monthly data with month/year in one column and price on another. I would like to get a yearly trend with fb prophet library in python (how to use monthly data with the library is explained at the end of this page ).

This is my code:

import fbprophet
import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('data.csv', sep=';')
data_prophet = fbprophet.Prophet(interval_width=0.95, changepoint_range=0.9, changepoint_prior_scale=0.15, seasonality_mode='multiplicative', n_changepoints=100)
data_prophet.fit(data)

# Make a future dataframe for 5 years
data_forecast = data_prophet.make_future_dataframe(periods=12 * 5, freq='M')
# Make predictions
data_forecast = data_prophet.predict(data_forecast)

#Plot
data_prophet.plot(data_forecast, xlabel = 'Date', ylabel = 'Price')
plt.show()

data_prophet.plot_components(data_forecast)
plt.show()

This is what I got:

At the first glance, seems that minimums/maximums at the yearly trend correspond to mid-month, but they are not at the same point each month. The picks are shifted a little bit.

How to draw a correct yearly plot? And does this make sense for monthly data?

As explained in the link you provided, you will encouter problems with monthly data when you try to see what happen at a timestep below. They do not exactly use the same graph as yours but they say that : "The seasonality has low uncertainty at the start of each month where there are data points". It seems to be what happen here : on your graph you can see that there is no seasonal effect on a monthly basis, as the multiplicative coefficient seems to be 1 at the start of each month :

Below the monthly time-step there seems to be oscilations. I am not 100% sure but it might be a practical exemple of Runge's phenomenon, where oscillations appears while doing interpolation. Below is a graphical exemple with the Runge function : some oscilations appears when you try to approximate it with polynomials, and quite counterintuitively, the size of those oscillations may grow as you try to improve the model by increasing the number of estimation knots.

This might be what happen here as you dont have a perfectly flat effect and the interpolation create oscilations between your monthly dates.
Generally speaking for the prophet framework the way to deal with this are mentionned in the link you provide :

use monthly regressor if you only want to get monthly effect. As said : "this approach would avoid the within-month unidentifiability seen above. Be sure to use yearly_seasonality=False if monthly extra regressors are being added."
only make monthly forecasts, which can be done by passing the frequency into make_future_dataframe (freq='MS')

Obviously the main alternative being to use another framework that might be more transparent on what it does exactly.


How to treat time based ticket prices for train/test split I have a dataset of airfare price tickets that were scraped throughout a 6 month period where each observation represents a particular price for a specific flight on a specific date that it was scraped. In other words, a specific unique flight may appear multiple times in the dataset if it's scraped multiple times on different days. For example,

Scrape Date: 11/13/19, Days To Trip: 42, Flight: DL1345 , Departure: 12/25, Time: 5:00PM, Price: 290
Scrape Date: 11/22/19, Days To Trip: 33, Flight: DL1345 , Departure: 12/25, Time: 5:00PM, Price: 330
Scrape Date: 12/01/19, Days To Trip: 24, Flight: DL1345 , Departure: 12/25, Time: 5:00PM, Price: 349

I know that with time-series data such as stock prices, you want to split your training/testing data so that the data in testing is in the future and comes after the data in training. However, I don't believe the dataset I have would warrant a split like this and I can instead randomly shuffle the data for train/test split but I am not 100% sure on the right call. Should I split the data based on time or can I randomly sample since the price of the tickets don't depend on each other?

Indeed airfare data is different than many other price data. In To Buy or Not to Buy: Mining Airfare Data to Minimize Ticket Purchase Price the authors put it as following:

  Computational finance is concerned with predicting prices
  and making buying decisions in markets for stock, options,
  and commodities. Prices in such markets are not determined
  by a hidden algorithm, as in the product pricing case, but
  rather by supply and demand as determined by the actions
  of a large number of buyers and sellers. Thus, for example,
  stock prices tend to move in small incremental steps rather
  than in the large, tiered jumps observed in the airline data.

(tho the reality is not that black and white as airfares also depend on seat availability and stock prices are also indirectly driven by algorithms but generally they have a point)

These algorithm-driven price changes do not so much rely on historical price development which is why I would give your approach a try. In line with that there are multiple papers in which airfare prices have not been treated as timeseries (besides the one already linked also see Predicting Airfare Prices - tho in this paper they did consider an attribute related to the most recent previous price of the ticket!).

Nevertheless, there are many date-related aspects your might want to consider. "Days to trip" is one which is in your data already. Others, easily available from your data, could be

"weekday of departure",
"departure during holiday season or not" and
"weekday of price request".

And then of course there are tons of other variables which might be important, e.g. "number of stopovers", "overnight flight or not", "number of free baggage" (and obviously the cabin class or maybe even booking class) etc. (also see Airfare Prices Prediction Using Machine Learning Techniques)


How to give CIFAR-10 as an input to MLP I am a noob in machine learning and trying to build a classifier using keras by following this tutorial machine learning mastery tutorial

I want to build a classifier based on MLP like in classification of MNIST using MLP for CIFAR-10 data set.Like in MNIST data set the 28*28 images to a 784 vector is given as a input to the neural network.
Like wise how should I frame the data in CIFAR-10 should it be changed in the 36*36 pixels rgb values to an array? or what type of array should I make to give as a input to the MLP network?

MNIST has 28x28 pixel greyscale images, so there are $28\times28 = 784$ features per image.

CIFAR10 contains colour images that are 32x32 pixels. At first glance it may seem like there are only $32\times32 = 1024$ features per image, but there are red, blue and green channels in the image, which means each image is actually $3\times32\times32 = 3072$ features.

Usually, these channels are arranged as three separate 32x32 pixel images, rather than a single 32x32 image with RGB pixels. In keras, you can load the CIFAR10 dataset like so:

from keras.datasets import cifar10

(x_train, y_train), (x_test, y_test) = cifar10.load_data()

However, this will load the train and test dataset in the shape (num_samples, 3, 32, 32). In order to input these into an MLP, we need to flatten the channels and pixel arrays to form an array of shape (num_samples, 3072), just like with MNIST. We can do this in python like so:

x_train = x_train.reshape(-1, 3072)
x_test = x_test.reshape(-1, 3072)


What do mean and variance mean for high dimensional data? I'm using Scikit Learn to guess the tag of Stack Overflow posts given the title and body. I represent the title and body as two 300-dimensional vectors of floats. The documentation for Scikit learn's SGDClassifier states:

  For best results using the default learning rate schedule, the data should have zero mean and unit variance.

With one-dimensional data, I understand what mean and variance are but when we go into higher dimensions how would one calculate the mean and variance for a dataset?

It's just the expansion of one dimensional mean and standard deviation. Suppose that you are trying to estimate the weight of a person and you have two inputs, salary and height. For finding the weight, you have two inputs which are of different scales, so you try to find the mean and variance of each feature, salary and height, separately using the data samples. Suppose you have two data samples in a tuple like (salary, height). They are as follows:

  (10000, 180)
  (5000, 175)

Although the first guy may seem so rich, the real point about the first and second data samples is that their scale is not the same. The approach is like this:

Find the mean of first feature among data samples, salary here. (10000 + 5000) / 2 = 7500
Find the mean of second feature among data samples, height here. (180 + 175) / 2 = 177.5
Find the standard deviation of first feature, among data samples, which is 2500 for salary.
Find the standard deviation of second feature, among data samples, which is 2.5 for height.
Reduce the amount of mean from corresponding feature for each data sample and divide each with the standard deviation of corresponding feature.
(10000 - 7500) / 2500 = 1
(5000 - 7500) / 2500 = -1

(180 - 177.5) / 2.5 = 1
(175 - 177.5) / 2.5 = -1

Data samples which have been normalized now are like as follows:

  (1, 1)
  (-1, -1)

Whenever you normalize your data, your cost function would be so easier to learn. The weights don't have to struggle to reach to high values in situations where your features are not in same range.


Question about train example code for TensorFlow I am trying to learn TensorFlow, and I could understand how it uses the batch in this example:

cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
sess.run(tf.initialize_all_variables())
for i in range(20000):
  batch = mnist.train.next_batch(50)
  if i%100 == 0:
    train_accuracy = accuracy.eval(feed_dict={
        x:batch[0], y_: batch[1], keep_prob: 1.0})
    print("step %d, training accuracy %g"%(i, train_accuracy))
  train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

print("test accuracy %g"%accuracy.eval(feed_dict={
    x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

My question is, why it get a batch of 50 training data, but only use the first one for training. Maybe I did not understand the code correctly.

If I understood you correctly, you are asking about this line of code:

train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

Here you only specify which part of batch is used for features and which for your predicted class.


Why is my PCA boomerang-shaped when normalizing? Running an unsupervised plot of my data, I noticed a hyperbolic ('boomerang') shape:

dim=2

vectorizer = TfidfVectorizer(min_df=5, max_df = 0.4, stop_words = 'english')
train_tf_idf = vectorizer.fit_transform(bunch_train.data)
svd = TruncatedSVD(n_components=dim,random_state=42)
svd_train = svd.fit_transform(train_tf_idf)
svd_train = Normalizer().fit_transform(svd_train)

labels = {
        0:'alt.atheism',
        1:'comp.graphics',
        2:'sci.med'}
y = np.vectorize(labels.get)(bunch_train.target)

with plt.style.context('seaborn-whitegrid'):
    plt.figure(figsize=(12, 8))
    for lab, col in zip(('alt.atheism', 'comp.graphics', 'sci.med'),
                        ('blue', 'red', 'green')):
        plt.scatter(svd_train[y==lab, 0],
                    svd_train[y==lab, 1],
                    label=lab,
                    c=col)

    plt.title('2D SVD on TF-IDF - 3-NewsGroups',size=28)
    plt.xlabel('PC1')
    plt.ylabel('PC2')
    plt.legend(loc='upper left',prop={'size': 20})
    plt.tight_layout()
    plt.show()

I suspect it has something to do with the Normalizer - when deleting the following line:

svd_train = Normalizer().fit_transform(svd_train)

the data plots like this:

The problem here is that you are normalizing the output of SVD. It might be clear that this output does not need to be normalized if you consider the output of each step:

Vectorizer: [n_items x n_words] Matrix. Unnormalized.
TFIDF: [n_items x n_words] Matrix with rows normalized to unit Euclidean norm.
SVD: [n_items x dim] Matrix. Columns correspond to each row's loading onto two orthonormal unit vectors.

In short, the output of the third step (SVD) is meaningful as-is and normalization obscures that meaning.

As to why the normalized output appears curved? The math to show this seems a bit involved but my intuition is that normalization is mapping the row weightings onto the ellipsoid manifold used in SVD.


Error: keras merge LSTM layers in sum mode I want to merge two sequential models in sum mode into one model using keras as:

left = Sequential()
left.add(LSTM(64,activation='sigmoid',stateful=True,batch_input_shape=(10,look_back,dim)))
right = Sequential()
right.add(LSTM(64,activation='sigmoid',stateful=True,batch_input_shape=(10,look_back,dim)))
model = Sequential()
model.add(Add()([left, right]))

But the statement model.add(Add()[left,right]) gives error: Layer add was called with an input that isn't a symbolic tensor. Received type: . Full input: [, ]. All inputs to the layer should be tensors.

The error says what's the problem: the method expects a Tensors, but you are giving a Sequential model object.

Use functional model (from keras.models import Model), not Sequential.

Then, merge the models with:

merged_models = Model(inputs=[first_model_input, second_model_input], outputs=[first_model_output, second_model_output])

or whatever your input looks like.


Speed of training decrease by adding more GPUs I am using the distributed Tensorflow with Mirror Strategy. I am training the VGG16 based on custom Estimator. However, by increasing the number of GPUs time of training is increased. As I check, the GPUs Utilization is about 100% and it seems the input function can feed data to GPUs. As all GPUs are in a single machine, Is there any clue to found out the problem. This is the computation graph and I am wondreing the Groups_Deps cause the problem.

Using GPUs can accelerate training but as you increase the number of GPUs your training as to be distributed which means your data has to be moved to several GPUs which can cost in term of bandwidth. I would profile the training and see the time spent to move the data to GPUs and et results back. Plus it's harder to syncronize training like this.

If you are using tensorflow 1.14+ try to change the distribution method to "MirroredStrategy" . This tend to work better with multiple GPUs I found.


shapes recognition for shapes formed by dotted points In the image, below right corner there is circle shape and triangle shape made with set of co-ordninates.I have searched extensively in the net for algorithm or approach to classify shape correctly .And yet came up with nothing.I need approach or directions to solve this problem.

Use MINST dataset trained model, make predictions.

Look here. find the one with biggest accuracy, and the circle should be class that
represents class of number 0.

EDIT: check this notebook where you can download your own images from google, annotate them andperform transfer learning relatively fast. Here
So you find all geometrical shape that you want and update some bigge model, or train your own from scratch


Adding bias in deconvolution (transposed convolution) layer My question is regarding the transposed convolution operation (also commonly called deconvolution or upconvolution). In TensorFlow, for instance, I refer to this layer.

My question is, how / when do we add the bias (intercept) term when applying this layer?

When working with 'regular' convolution, we do this:

conv_output = tf.nn.conv2d(input, kernel, strides, padding='VALID')
conv_output = tf.nn.bias_add(conv_output, bias)

How do we do this when applying the deconvolution layer? My confusion arises because my advisor told me to visualise upconvolution as a pseudo-inverse convolutional layer (inverse in the sense that convolution downsamples the input, while transposed convolution upsamples it. I know they are not mathematically inverse.)

According to him:

Regular convolution: conv = x.w + b

Transposed convolution: x = (conv - b).W (where w and W are not the same).

Is the above equation even right? Something about it makes me feel uneasy.

In this scenario, since we are "going backwards", should we do something like this:

deconv_output = tf.nn.bias_add(input, -1 * bias)
deconv_output = tf.nn.conv2d_transpose(deconv_output, kernel, strides, padding='VALID')

Or should we add the bias after applying the transpose convolution, as we do in 'regular' convolution?

We are going backwards in the sense that we are upsampling and so doing the opposite to a standard conv layer, like you say, but we are more generally still moving forward in the neural network. For that reason I would add the bias after the convolution operations. This is standard practice: apply a matrix dot-product (a.k.a affine transformation) first, then add a bias before finally applying a non-linearity.

With a transpose convolution, we are not exactly reversing a forward (downsampling) convolution - such an operation would be referred to as the inverse convolution, or a deconvolution,  within mathematics. We are performing a (transpose) convolution operation that returns the same input dimensions that produced the activation map in question, with no guarantee that the actual values are identical to the original input.

You can see from the animations of various convolutional operations here, that the transpose convolution is basically a normal convolution, but with added dilation/padding to obtain the desired output dimensions. The trick is to retain the mappings of localisation between the pixels.

In the paper from which those animations are taken, they explain how a transpose convolution is essentially the convolution steps performed in reverse:

  ..., although the kernel w defines a convolution whose forward and
  backward passes are computed by multiplying with $\textbf{C}$ and $\textbf{C}^T$ respectively, it also defines a transposed convolution whose forward and backward passes are computed by multiplying with $\textbf{C}^T$ and $(\textbf{C}^T)^T = \textbf{C}$ respectively.

One other source to back up my opinion: in the PyTorch implementation it seems the bias is added the output of the convolution's result.


Visualize graph network with more than 30k edges I have a dataset with users connection and I want to create a directed network graph.

Nodes: ~20.000
Edges: ~33.000

I used to work before on NodeXL and Gephi, but now I am on Mac and I don't have access on NodeXL.

I tried Gephi and another tool called Cytoscape. Both of them were lagging and I could barely do some statistics. Visualizing the graph was a dream.

Is there any software (or maybe Python library) that can handle such a big network Graph?

Yes There are!

Networkx

I think 20k-30k node-edge would be OK on Networkx, IF YOU HAVE A GOOD MACHINE! Networkx is a great library in Python particularly for Graph Analysis so you have access to great analysis tools beside visualizing but visualizing 20k vertices needs much RAM and takes long.

Igraph

Igraph is another great tool for Graph Analysis with APIs in Python, R and C. You can do a lot with Igraph library including nice plotting facilities.

SNAP

SNAP was developed by Jure Leskovec and his colleagues at Stanford. A large scale graph processing library natively written in C with API in Python. This library can handle huge graphs on a single personal machine.

PS.1 Both of these guys have interfaces with different graph softwares like Gephi, etc.
PS.2 For large graphs I strongly recommend to work in C++ if you can. Speaking as a Network/Graph guy, C++ solves many problems out there!


Decentralized machine learning Currently, to train a model, you need to collect a huge blob of data.

Are there feasible concepts of decentralized machine learning? Like, feed the model somehow from isolated data sources, or merge smaller models?

Using federated learning the model training does not require the whole data to be present at a centralized server instead the model training is decentralized such that the model gets trained collectively on individual nodes and then summarizes the changes as a small focused update. Only this update to the model is sent to the cloud, using encrypted communication, where it is immediately averaged with other user updates to improve the shared model. All the training data remains on your device, and no individual updates are stored in the cloud.

References-
https://ai.googleblog.com/2017/04/federated-learning-collaborative.html

PySyft is a library hooked into Pytorch that provides federated learning capabilities.

PySyft github repository link-
https://github.com/OpenMined/PySyft

Demo code for federated learning using PySyft library-
https://colab.research.google.com/drive/1F3ALlA3ogfeeVXuwQwVoX4PimzTDJhPy#scrollTo=PTCvX6H9JDCt


Understanding the concept vanishing gradient and exploding gradient problem in terms of training data I'm trying to figure out the essence of the concepts "vanishing gradient and exploding gradient problem" in terms of real-world input-output training examples instead of in terms of the properties of the choice of activation function.

Can anybody direct to a good tutorial that include such examples?

I always refer "Andrew NG" documents if available for any ML understanding. I believe that Youtube link Vanishing and Exploding Gradient, will help you in understanding the concept in a better way.

Still a brief from my side:

  DNN of K layers without an activation function, will be like
  multiplying K coefficients(weights) together. Somehow if you received
  initial coefficient value < 0, in such a case during back
  propagation till you reach to the starting layers (from Input layer
  side), it may possible you completely lost the value of gradient,
  as gradually you are multiplying smaller value i.e. vanishing a value. Similarly if initial
  coefficient value > 0, multiplying together will form a very big
  number i.e. exploding a value.


Coding MLP: good practices? I recently finished coding my own MLP neural network in Python. To make my code easier to read, I separated the MLP, into classes; the network class, the layers class and the neuron class, where the neuron class would do all the calculations such as the activation and calculating the error signal. Each neuron also contained an array of the weights.

To verify the results of my MLP, I tried the sklearn-MLPClassifier with the same dataset, and found that sklearn was actually much faster to train than with my code.

Would my code be faster if I rewrite it with matrices? Whats the right way to code a NN and how can I get my performance to be comaparable to that of sklearn ?

Would my code be faster if I rewrite it with matrices?

Without seeing the code it's impossible to know, but very likely. Also, I would never model single neurons. Too much overhead without any use. Model layers instead.

  how can I get my performance to be comaparable to that of sklearn

Sklearn is open source. Read the code: https://github.com/scikit-learn/scikit-learn/blob/7389dba/sklearn/neural_network/multilayer_perceptron.py#L682


This looks like a bug in the 0.80 version.

It works when upgraded to xgboost 0.82.


"Relearning" parameters If this is a duplicate, I apologize. I'm not really sure what to even search for to try and find a duplicate/answer!

We are working on a system for providing musical feedback to change the 'mood' of a subject. There are a number of parameters of the music that we can manipulate.

At the same time, we measure the physiology of the subject. I'm using a dynamic Bayesian network to reasonably accurately determine the level of frustration of the subject.

I'm looking for a way to, based on the level of frustration, tell the music generation mechanism "this set of parameters is effective", or not. The music parameters would then adjust automatically and iteratively, as we continue to make judgements of the subject's level of frustration.

I'd appreciate any pointers I can get! Please let me know if I can provide any clarification!

This can be thought of as an active learning problem. You want feedback from the learning process to influence what kind of input examples you next train on.  Active learning is still a difficult problem in most settings.

As @cohoz points out, hill climbing is an intuitive option in this case, but only if you always want to increase or decrease your measure of frustration. If instead the goal is to explore frustration as a function of as much of the music parameter feature space as possible, you will want to read about active learning and uncertainty.


Neural network obfuscation Neural networks are trained to minimize some error function over the weights of the neural connections. In some applications, these weights could be considered intellectual property. Is there a way to encrypt these weights and still have an operational neural network?
Some context: I'm trying to scale a neural network algorithm, but right now we're doing all the computations on a centralized server and it's getting bogged down. We can shift the computation to the client side, but we don't want someone to unpack the executable and obtain the weights of the network. Is there some way to distribute an "encrypted neural network" such that our IP is protected?
To clarify: I am not talking about "oblivious" neural networks that operate on encrypted data, I am talking about the weights of the neural network themselves.
I'm fine with alternative obfuscation techniques.
Edit: I found this paper, but it states

Great attention
is paid to avoid any unnecessary disclosure of information,
so that at the end of the protocol the user only knows
the final NN output, whereas all internal computations are
kept secret. In this way, the possibility for a malevolent user
to provide a set of fake inputs properly selected to disclose
the network secrets is prevented. A solution is also sketched
that permits to obfuscate the network topology, however, a
deeper investigation in this direction is left for future research.

indicating that this paper is on a related concept, but I'm looking for a resource where the topology is obfuscated.

No.  What you want is probably not achievable in practice, because the approach you are considering obfuscates only the weights but does not obfuscate the inputs and outputs to the network.  In any reasonable ischeme I can imagine, the input $x$ to the obfuscated network will be known and under the attacker's control, and the output $y$ from the obfuscated network will be known to the attacker.  In particular, the attacker can pick any $x$ of his choice, and observe the output $y$.

If that is true, your goose is cooked.  The attacker can use the ability to invoke your neural network to learn his own neural network that is just as good as yours.  In particular, the attacker can assemble an arbitrarily large training set by picking many potential inputs $x$ and for each one computing the corresponding $y$ by running your obfuscated network on $x$, then putting the pair $(x,y)$ in the training set.  This doesn't require labelled examples; it only requires the adversary to be able to put together a large set of unlabelled instances (and then use your obfuscated network to label those instances).  Typically, it's not too hard for someone to create a large set of unlabelled instances.

Finally, once the adversary has assembled this training set, the adversary can train their own neural network using standard techniques.  It's likely that the resulting neural network will be approximately as good as yours -- i.e., have approximately the same accuracy.  (This is what seems to happen in practice.)

As a result, no obfuscation scheme for obfuscating the weights is likely to be terribly effective, because that doesn't hide the inputs and outputs to the network.  The most you can hope for is a scheme that acts as a "speed bump" that slightly increases the cost of de-obfuscation or that raises the bar a little bit, but nothing you do will provide strong security against a knowledgeable adversary.  So, don't spend too much time or energy or money on trying to make this work.  Instead, you might be better off looking for other ways to deal with this issue.

P.S. Even if you could hide the outputs and reveal only the ultimate classification (i.e., hide the continuous probability values from the softmax and just reveal the highest-probability class), that's probably still not enough.  Revealing the class is still enough for an adversary to label a bunch of instances, create a training set, and then train their own network.


parse pdf into Json or Xml I want to create a neural net that can obtain some specific words from a pdf document into JSON or XML. For example let's assume that I have a pdf containing some information about countries and i want to recuperate the countries name and population to obtain  something like this :

<countries>
  <country>
    <name>
      France
    </name
    <population>
      70m
    </population
  </country>
.
.
.
</countries>

Should I build a neural net and train it myself? If so can you give a good  tutorial to follow please, or is there an already trained one that I can use?

Well, Unless your goal is to build a neural net to solve the problem. This can be done in a much simpler way, Like in case of country name you can just check against a list of country names, and so on.
At best some NLP could give you what you want. A neural net solution might be a little overkill.

If a neural net is compulsory, Then I think You could get a better answer if some details were specified. Are you looking for a fixed set of fields, what kind of text content do the pdfs contain etc.

Also just in case, if you were thinking a neural net will give you a json as output (just in case if you were thinking that). That will not be the case. you would have to convert it to json from the neural nets output, but that conversion stuff is very trivial, so i should not even be talking about that.

I know i have not answered your question. But i hope you got some direction.


Multidimensional K-Means wiith sklearn, centroids problem when plotting I am working with a dataset (X) to predict 12 clusters with K-Means using python SKLEARN library:
numClusters= 12
kmeans = KMeans(n_clusters=numClusters).fit(X)
centroids = kmeans.cluster_centers_

# Predicting the clusters
labels = kmeans.predict(X)
# Getting the cluster centers
C = kmeans.cluster_centers_

#transform n variiables to 2 principal components to plot
pca = PCA(n_components=2)
principalComponents = pca.fit_transform(X)
principalDf = pd.DataFrame(data = principalComponents
         , columns = ['principal component 1', 'principal component 2'])

colors =['red','green','blue','cyan','yellow', 'lime','orange','coral','brown','peru','khaki','tan']
centroidColor= []
for item in range(numClusters):
  centroidColor.append(colors[item])

dataPointColor=[]
for row in labels:
  dataPointColor.append(colors[row])

fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(1,1,1)
ax.set_xlabel('Principal Component 1', fontsize = 15)
ax.set_ylabel('Principal Component 2', fontsize = 15)
ax.set_title('2 component PCA', fontsize = 20)
plt.scatter(principalDf['principal component 1'], principalDf['principal component 2'],
c=dataPointColor, s=50, alpha=0.5)

plt.scatter(C[:, 0], C[:, 1], c=centroidColor, s=200, marker=('x'))
plt.show()

My problem comes when trying to plot the n dimensions of my dataset into 2 PCA. The clustering seems fine, but the centroids (marked with a star icon) are not correct:

The centroids should not always be in the same near spot.
I know that I did not standardize the dataset (X) because all values are in a range [1:5]:
[[4 5 4 ... 4 4 4]
[4 4 1 ... 4 3 5]
[3 4 3 ... 1 1 4]
...
[2 5 1 ... 2 1 4]
[5 5 1 ... 5 5 1]
[2 5 1 ... 1 1 5]]

Could the problem come from that?
Thanks for your help.

In your plot you used PCA to reduce the dimensionality of your data, but you plotted the first 2 dimensions of your centroids. You should also transform the centroids using the PCA transform you fitted on your data.
This code should work for you
numClusters= 12
kmeans = KMeans(n_clusters=numClusters).fit(X)
centroids = kmeans.cluster_centers_

# Predicting the clusters
labels = kmeans.predict(X)
# Getting the cluster centers
C = kmeans.cluster_centers_

#transform n variiables to 2 principal components to plot
pca = PCA(n_components=2)
pca_fit = pca.fit(X)
principalComponents = pca_fit.transform(X)
principalDf = pd.DataFrame(data = principalComponents
         , columns = ['principal component 1', 'principal component 2'])

colors =['red','green','blue','cyan','yellow', 'lime','orange','coral','brown','peru','khaki','tan']
centroidColor= []
for item in range(numClusters):
  centroidColor.append(colors[item])

dataPointColor=[]
for row in labels:
  dataPointColor.append(colors[row])

fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(1,1,1)
ax.set_xlabel('Principal Component 1', fontsize = 15)
ax.set_ylabel('Principal Component 2', fontsize = 15)
ax.set_title('2 component PCA', fontsize = 20)
plt.scatter(principalDf['principal component 1'], principalDf['principal component 2'],
c=dataPointColor, s=50, alpha=0.5)

C_transformed = pca_fit.transform(C)
plt.scatter(C_transformed[:, 0], C_transformed[:, 1], c=centroidColor, s=200, marker=('x'))
plt.show()


Validity of PU learning while using character-level encoding using CNNs for classifying text data I'm trying to classify a large set of documents (~100M) as valid or invalid, based upon a small given set of labeled valid documents (~3k). I'd like to know if the PU learning approach described in this paper combined with the character-level string encoding described in this paper is likely to give good results or not, and why.

Some preliminary info about the dataset (ignore if you want to skip ahead to the model):

Each document has <= 50 words.
Each word has <= 50 characters.
Each character has <= 50 words.
Any randomly selected document pair is likely to have very few common words.
There are a significant number of proper nouns in the dataset that have to be captured.
Original set for training the network contains ~10k unlabeled documents and ~3k labeled ones. The assumption is that the 10k unlabeled documents selected will be mostly different from the 3k labeled ones.
Train-test-validation split is 80%-10%-10%.

The first goal is to write a classifier that distinguishes labeled and unlabeled documents. For completeness' sake, the code for this classifier (written using Keras) is as follows:

def cnn_model(input_batch_shape):
  input_layer = layers.Input(batch_shape=input_batch_shape)
  fixed_length_letter = layers.Dense(16, activation="relu")(input_layer)
  reshaped_fixed_length_letter = layers.Reshape((keras.backend.int_shape(fixed_length_letter)[1], keras.backend.int_shape(fixed_length_letter)[2], keras.backend.int_shape(fixed_length_letter)[3], 1))(fixed_length_letter)
  conv_word_1 = layers.Conv3D(16, (1, int(keras.backend.int_shape(reshaped_fixed_length_letter)[2]/5), keras.backend.int_shape(reshaped_fixed_length_letter)[3]), activation="relu")(reshaped_fixed_length_letter)
  conv_word_1 = layers.MaxPooling3D((1,5,1))(conv_word_1)
  conv_word_2 = layers.Conv3D(16, (1, int(keras.backend.int_shape(conv_word_1)[2]/4), keras.backend.int_shape(conv_word_1)[3]), activation="relu")(conv_word_1)
  conv_word_2 = layers.MaxPooling3D((1,4,1))(conv_word_2)
  conv_sentence_1 = layers.Conv3D(8, (int(keras.backend.int_shape(conv_word_2)[1]/5), 1, 1))(conv_word_2)
  conv_sentence_1 = layers.MaxPooling3D((5, 1, 1))(conv_sentence_1)
  conv_sentence_2 = layers.Conv3D(8, (int(keras.backend.int_shape(conv_sentence_1)[1]/4), 1, 1))(conv_sentence_1)
  conv_sentence_2 = layers.MaxPooling3D((4, 1, 1))(conv_sentence_2)
  flattened_sentence = layers.Flatten()(conv_sentence_2)
  output_layer = layers.Dense(1, activation="softmax")(flattened_sentence)
  model = models.Model(inputs=input_layer, outputs=output_layer)
  return model

model_check_labeled = cnn_model((16, 50, 50, 39))
model_check_labeled.compile(optimizers.Adam(), losses.binary_crossentropy, metrics=['accuracy'])
model_check_labeled.summary()

As the code shows, I've dropped the part of Dos Santos and Gatti de Bayser's proposed architecture where they encode the words into vectors as well, since I don't think my vocabulary of words is large enough to generate a good encoding with the number of valid samples I have.

This led to the following accuracy and loss curves:

As the graph shows, my classifier gave me ~25% accuracy - meaning that there have been no meaningful features captured at all. I would like to know if this is expected, given the architecture of my classifier and the dataset I have described. I would also like to know if there are other approaches I could use that would help at least classify new documents as labeled or unlabeled.

Please tell me if the description of the dataset sounds too vague. I'll try and present some examples that have similar features.

Papers cited:

Elkan, Charles & Noto, Keith. (2008). Learning classifiers from only positive and unlabeled data. Proceedings of the ACM SIGKDD International Conference on Knowledge Discovery and Data Mining. 213-220. 10.1145/1401890.1401920.
Dos Santos, Cicero & Gatti de Bayser, Maira. (2014). Deep Convolutional Neural Networks for Sentiment Analysis of Short Texts.

Well, I went over the code a couple more times, and I figured it out. There is a glaring error in the following line:

output_layer = layers.Dense(1, activation="softmax")(flattened_sentence)

Softmax needs a one-hot vector for output. I'd used softmax because I wanted the probabilities for the two to be separate, but forgotten at some point when I was writing the generator for my input. Converting it to sigmoid leads to the classifier getting to about 90% accuracy on the test set.

The important lesson there is that softmax requires both classes to be present in the output for binary classification, while sigmoid requires only one class, which varies from 1 to 0.


Can PyLearn do everything that Theano can? Since PyLearn2 is build upon Theano, is it possible to do anything I can do in Theano in Pylearn2? For example, if I have some snippets of Theano code, can I run them as-is in Pylearn2, or would this not work? If it wouldn't, why not?

For your info, Pylearn2 is now deprecated. You can check it's alternatives i.e. platforms that are build on top of theano, which are: lasagne, blocks and keras.


Different training method for encoder-decoder model Trying to learn the encoder-decoder model for some NLP problems.
I am referring to this Keras tutorial.
During the model training phase, this tutorial just uses the following:
model.fit([encoder_input_data, decoder_input_data], decoder_target_data,
          batch_size=batch_size,
          epochs=epochs)

I understand this logic. But the confusion is in some other tutorials for EXACTLY THE SAME PROBLEM. For example, in Tensorflow's documentation for NMT with Attention the training_phase is very different where they use custom training loops with a custom train step and calling the step for every batch manually.
The question is are these 2 different training methods which should be used in particular cases OR its the same training method with 2 different forms of implementation?

One is a straight forward use of API while the other one gives you much better control on training. If you are experimenting, it is good to go with a custom training loop so you can control the various things that happen to your model's training.
But, if you want to just train a model and not experiment with it much, save time and go for the straight forward method of training.
Both are same but one gives you much more control.


More features hurts when underfitting? I was training a binary classifier using XGBClassifier (basically boosted decision trees if I understand it correctly). I have 10K training examples. I have two distinct set of features (but they could be dependent), one contains 26 features (call the set A) and the other contains 96 features (call the set B). I tried to train 3 classifiers, each w/ a different combination of the feature sets, namely A, B and A+B. The result is that using only A is clearly better than using both A and B. At this point I thought it might be overfitting, so that using less number of features actually avoids the overfitting.

The # of trees used in the above trainings was 100. So I used 10-fold cross validation to find the optimal # of trees for each feature set combination, and they are all beyond 100 (like 300-500). So it seems to me the models were learning w/ less-than-desired degree of freedom and they were underfitting if you will.

So this confuses me: why providing additional features make things worse (i.e. using only A is better than using both A and B) when the models are on the under-fitting side (as opposed to overfitting)?

Or maybe a more general question: how do I find out the real problem in this situation?

Note: I have read this question about overfitting/underfitting and I still think my models should be underfitting, because apparently increasing model complexity helps (e.g. from 100 trees to 300-500 trees).

Theoretically speaking, more data leads to better model. However, in practice, more features often leads to the difficulty of model training.

Assuming there are 30 "main" features of your dataset. Feature set A contains 20 "main" features, so it might be easy (20 out of 26) for one "main" feature to be "chosen" and "trained", under certain hyperparameters (in your case, 100 trees). When it comes to feature set B, which contains all "main" features, it's hard (30 out of 96) for one "main" feature to be chosen, and it's harder when there's only 100 trees (cause there are 66 "minor" features which should not be trained, relatively). That's what we called "under-fitting".

Back to your question, when the model is under-fitting, if we're lucky that model trained on feature set B (namely model B) contains all these 30 "main" features, the model will be good, and we might not able to find out it's under-fitting. But in most cases, we're not that lucky, model can be ruined with all 66 "minor" features, relatively.

In my ML practice, I'll try more training iterations when more samples come, and try a more complex model when more features come, with the control of over-fitting/under-fitting.


TypeError: 'tuple' object cannot be interpreted as an integer I have an image (480,640,3) and I am trying to resize it to (150,150,1). Since my Keras model has been trained on images of shape (150,150,1), unfortunately I cannot seem to resize it using the PIL library. Any suggestions on how to fix this?

This is the code I am using right now:

The error is caused on the line image5.resize()

    stream.seek(0)
    data = numpy.fromstring(stream.getvalue() , dtype = numpy.uint8)
    image5 = cv.imdecode(data , 1)
    print(image5.shape)
    #cv.imwrite('uhhu.png',image5)
    img = image5.resize((150,150,1 ), Image.ANTIALIAS)
    cv.imwrite('hhh.png',img)
    x = img_to_array(img)
    x = x.reshape((1,) + x.shape)
    x = x/255
    x = numpy.array(x)
    print(x.shape)
    #score = loaded_model.predict(img)
    #print(score)

The solution to the problem is to resize using openCV instead of using PIL and specifying the appropriate pixel width and height.

img = cv.resize(image5 , (150,150))


How to move forward on Regression problem I'm an undergrad interested in machine learning, and I'm playing around with some data in order to get a better understanding of the field.

DATA

I'm working with the following data:

To give you an idea of what you're looking at (sorry for the unlabeled axes) -- each data point corresponds to a basketball player's points per game (y-values) over a one-week period (x-values).

END GOAL

I want to predict progress throughout the year, so in order to test/train, I start with the first two points, fit a regression model, and then predict the third point. I then add the actual value of the third point to the set, re-train, and predict the fourth point, etc.

ISSUES

As you can see, the data is all over the place, and the predictions are just as messy (sometimes getting up to ~200 points per game, which is totally impossible).

I've tested different degrees of linear regression (quadratic, cubic, etc.) and degree=1 is always the best predictor because of how wonky the data is.

IDEAS

I have thought of the following ways to get more accurate predictions:

Smoothing the data, maybe using a moving average or some variant
Set an upper limit for predictions
Non-linear regression

But outside of smoothing the data, I'm not sure if the rest are even possible in a regression model (upper limit) or applicable to this situation (non-linear).

QUESTION

Are any of the ideas I had above worth pursuing? If not, is there anything I should look into that might help me solve this problem?

Thanks!

You have little features (as in none hehe). Your data reminds me a lot of stock market predictions, where you know nothing about what you're studying except for price. Furthermore, price behaves chaotically, because if it was easily predictable, then everybody would be predicting it, thus removing its predictability.

Therefore, if you are serious about studying that data, you may want to look up literature on stock market prediction.

I suggested in a comment that one approach could be to see this as a classification problem rather than a regression problem. This is in fact what many people do in the stock market. Instead of predicting actual price, people study whether the price will go up or down (and therefore whether they should buy or sell).

Here is an example of what I mean, implemented in sklearn:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cross_validation import cross_val_score
from sklearn.linear_model import LogisticRegression

# random signal
n = 100
t = np.arange(n)
x = np.random.rand(n)
plt.plot(t, x)
plt.show()

# model ups and downs: convert to 0s (down) and 1s (up)
t = t[1:]
y = ((np.sign(x[1:] - x[:-1])+1)/2).astype(int)
plt.plot(t, y)
plt.show()

# one look-ahead model using the four previous observations
X = np.c_[y[:-4], y[1:-3], y[2:-2], y[3:-1]]
y = y[4:]
scores = cross_val_score(LogisticRegression(), X, y, cv=10, n_jobs=-1)
print('Acc: %.2f (%.2f)' % (np.mean(scores), np.std(scores)))

My results:

Acc: 0.75 (0.17)

One advantage of this approach is that it gives you probabilities of going up or down. Remember, a logistic regression is a probabilistic model $P(y|x)$, and here we are taking $P(y|x)<0.5$ to mean down and $P(y|x)>0.5$ to mean up. But if the probability is close to 0.5, you could have your model say "I don't know". This way you only take action when the model reports down or up with high confidence (close to 0 (down) or 1 (up)). A colleague of mine wrote his thesis on this kind of thing. Are you trying to model sports in order to place bets? If so, this is a pretty simple but effective model of choosing when it is worthy to place bets.

But if the idea is to learn about data mining, then I would try another dataset.

sklearn comes with a bunch of nice datasets
kaggle has a lot of 101 training competitions rich in tutorials


Improve performance of SVM I have written some code for an SVM and the final output isn’t brilliant.  The data is linearly separable so I don’t understand why the data isn’t perfectly separated. Here is the code:

from matplotlib import pyplot as plt, numpy as np

x = np.array([[-2, 4, -1], [4, 1, -1], [1, 6, -1], [2, 4, -1], [6, 2, -1]])

y = np.array([-1, -1, 1, 1, 1])

def svm_sgd(X, Y):
    w = np.zeros(len(X[0]))    # Parameters
    eta = 1
    epochs = 100000

    # Update rules

    for epoch in range(1, epochs):
        for i, x in enumerate(X):
            if (Y[i] * np.dot(X[i], w)) < 1:
                w = w + eta * ((X[i] * Y[i]) + (-2 * (1/epoch) * w))
            else:
                w = w + eta * (-2 * (1/epoch) * w)
    return w

w = svm_sgd(x, y)
print(w)

# Print Hyperplane —————————————

for d, sample in enumerate(x):
    if d < 2:
        plt.scatter(sample[0], sample[1], s=120, marker='_', linewidths=2)
    else:
        plt.scatter(sample[0], sample[1], s=120, marker='+', linewidths=2)

plt.scatter(2,2, s=120, marker='_', linewidths=2, color='yellow')
plt.scatter(4,3, s=120, marker='+', linewidths=2, color='blue')

x2=[w[0],w[1],-w[1],w[0]]
x3=[w[0],w[1],w[1],-w[0]]

x2x3 = np.array([x2,x3])
X,Y,U,V = zip(*x2x3)
ax = plt.gca()
ax.quiver(X,Y,U,V,scale=1, color='blue')
plt.show()

The bottom portion just uses matplotlib to print the data, so it’s not part of the SVM algorithm, and can therefore be ignored.

  The problem ~ can anyone explain to me, based on my code, why the line doesn’t separate the data more perfectly?

This is the output when I run your code. It seems to be doing fine.

[  1.58876117   3.17458055  11.11863105]

Try to run it again? Perhaps you changed some parameters accidentally before posting it?


XGBoost skews towards minority class I have a dataset with 85k positive labels and 53k negative labels. For this use-case, I am trying to maximize my efforts to the negative class (accurately identify true negatives, and minimize false negatives).
Currently, I am able to train a xgboost classifier to 71% accuracy and my confusion matrix looks like this when benched against a test set
[ 3890 | 8887 ]
[  844 | 20044]

Again for this task, I'd really like to improve my recall and minimize false negatives. However, even in the models' current state, if I try to submit 43k new records for prediction (results are unknown), my model predicts that all 43k records are non-compliant.
Given this information, my two questions are as follows:

How irregular is this occurrence. I thought that skewing to the majority class was possible, but not the minority.
Are there any 'best practices' that can be applied to reduce the aggressiveness of my model? not only does it predict that all claims are negative (0), but it does so with very strong confidence (minimum 'probability of occurrence' is above 90%). Again, less than 40% of the records in my trainset are of the minority class, so I don't understand why it's skewing this hard.

Thanks

First of all, your confusion matrix looks normal to me. As shown below, the majority is positive class, and the model prefers to predict most of the negative cases as positive, which is common in imbalanced classes. Your model predicted 8887 negative cases as positive.
True   0 [ 3890 | 8887 ]
Labels 1 [  844 | 20044]
            0       1
         predicted labels

Your classes are imbalance, so the model does the fit/prediction in favor of majority class.
To overcome this issue,  you must perform over/under-sampling to eliminate possible issues caused by imbalance classes. You can use imbalanced-learn library in this regard.
So to answer your questions:
A1: You read the confusion matrix incorrectly. The model is skewing to the majority class.
A2: You must balance your data. Don't forget to perform over/under-sampling on your training set only. In addition, you may need to use precision or recall but not accuracy only, based on your need; for example if one class is important and having less false positive or less false negative is your goal.


Dataset map function error : TypeError: Expected list for 'input' argument to 'EagerPyFunc' Op, not Tensor I am currently trying to write a script to create a TFRecord file.
Therefore, I am following the instruction on the offical tensorflow website: https://www.tensorflow.org/tutorials/load_data/tfrecord#writing_a_tfrecord_file
However, when applying the map function to each element of the Dataset I get an error that I do not understand.
This is my code (should be copy and pasteable):
import numpy as np
import tensorflow as tf
from tensorflow.data import Dataset

def generate_random_img_data(n_count=10, patch_size=5):
    return np.random.randint(low=0, high=256, size=(n_count, patch_size, patch_size, 3))

def as_int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

def serialize_one_image(img):
    features = {}
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            for k in range(img.shape[2]):
                features.update({str(i) + "_" + str(j) + "_" + str(k) : as_int64_feature(img[i,j,k]) })
    example_proto = tf.train.Example(features=tf.train.Features(feature=features))
    return example_proto.SerializeToString()

def tf_serialize_one_image(img):
    tf_string = tf.py_function(serialize_one_image, img, tf.string)
    return tf.reshape(tf_string,())

ds = Dataset.from_tensor_slices(generate_random_img_data())
ds_serialized = ds.map(tf_serialize_one_image) # <--- not working

And when running this code I get the error:
TypeError: in user code:

    <ipython-input-116-ec81a7077c70>:25 tf_serialize_one_image  *
        tf_string = tf.py_function(serialize_one_image, img, tf.string)
    /Users/Tom/ML-Projects/vdst/lib/python3.7/site-packages/tensorflow/python/ops/script_ops.py:455 eager_py_func  **
        func=func, inp=inp, Tout=Tout, eager=True, name=name)
    /Users/Tom/ML-Projects/vdst/lib/python3.7/site-packages/tensorflow/python/ops/script_ops.py:341 _internal_py_func
        name=name)
    /Users/Tom/ML-Projects/vdst/lib/python3.7/site-packages/tensorflow/python/ops/gen_script_ops.py:69 eager_py_func
        name=name)
    /Users/Tom/ML-Projects/vdst/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:385 _apply_op_helper
        (input_name, op_type_name, values))

    TypeError: Expected list for 'input' argument to 'EagerPyFunc' Op, not Tensor("args_0:0", shape=(5, 5, 3), dtype=int64).

What exactly am I doing wrong here?

Okay, it was a very stupid mistake.
As the error message states, an input list is required.
So replacing this part of the code:
tf_string = tf.py_function(serialize_one_image, img, tf.string)

to this
tf_string = tf.py_function(serialize_one_image, [img], tf.string)

i.e. wrapping the imgobject into a list solved the problem.
Now it works as expected.
Thanks for reading anyways.


How does Q-Learning deal with mixed strategies? I'm trying to understand how Q-learning deals with games where the optimal policy is a mixed strategy. The Bellman equation says that you should choose $max_a(Q(s,a))$ but this implies a single unique action for each $s$. Is Q-learning just not appropriate if you believe that the problem has a mixed strategy?

One possibility is to use softmax and choose each action a randomly with probabiliy $p = \frac{\exp(Q(s,a))}{\sum_a \exp(Q(s,a))}$. I don't thinks it is still Q-learning though.


Is there a closed formula/function for decision trees? i've been studying gradient boosting so realize the pure algorithm requires a function F/model to get boosted.What is the explicit F on gradient boosting trees?

It can be expressed as a linear combination of indicators, where the weights are the training averages on each partition of the space, and the indicator is the indicator for that partition.
Having said that, I haven't come across any software that automatically does it for you. I also think the analytic expressions might become very large when dealing with moderately wide trees.


How to find out bad images? I have a task to find out bad visuals from given video.
Let's define bad images in the video as per the human brain -:

Images which don't contain subject in detail,
are too noisy or too many ads and subscription
letter or underexposed
or overexposed could be called as bad images.
The possibilities of bad images could be greater than this, but this
is just getting started.

Dataset is [collection of videos]
labels is [good:0,bad:1]

Do you have some sort of a dataset where you have labels of images with good visuals and bad visuals?
Without it, the definition of finding bad visuals seems under-defined to me.

Tracing ads/subscriptions could probably be done by using Hough transform (I think that all of them have rectangular bounding boxes).
Overexposed images can found by comparing the image brightness histogram with some threshold value.
"Images which don't contain subject in detail" - you need to use some detection model for this.


Formulate a MDP for a problem given below Can someone help me to formulate an MDP for the below problem ?.

Problem Definition

Bunny wakes up in a strange room with 2 doors; one on the left, and one on the
right.  In front of him is a map of the building, laying out clearly what lies behind each door.  Behind one of the doors is a room with the way outside, and behind the other one a room with a hungry tiger.  Bunny does not like tigers.  Especially hungry ones.

Consider  the  control  problem  where  the  current  state  is  specified  by  the current  room  Bunny  is  in,  and  the  actions  Bunny  can  take  are  to  move through  a  door  to  another  room.   Assume  that  there  are  2  doors  in  the starting room,  each leading to its own room,  and that one of these other rooms (specifically the one on the left) has a single door that leads to the exit, while the other one (specifically the one on the right) has a tiger in it.
Moreover, assume that Bunny has full knowledge of these details but that
there is a 10% chance that when trying to move through one of the doors,
he gets confused and goes through the other door instead.  Lastly, assume
that the episode does not end when Bunny enters the room with the tiger.

Questions

Formulate a Markov Decision Process (MDP) for the problem of con-
trolling Bunny’s actions in order to avoid the tiger and exit the building.
(Give the transition and reward functions in tabular format, or give the
transition graph with rewards).

I would like to know, is there any procedures or rules, that needs to be considered before formulating an MDP for a problem

  What i have done so far

State A - Starting room
State B - Room with an exit door
State C - Room that has a tiger in it

I don't know how to proceed further.

I would like to know, is there any procedures or rules, that needs to be considered before formulating an MDP for a problem

For a simple problem like this, I would do the following:

List the states.

You have missed a state - the terminal state of leaving the building

Mark terminal states

In this case, only exiting the building is a terminal state. The hungry tiger doesn't actually do anything to Bunny, the problem states that the MDP should just continue.

List the actions possible in each state.

In your starting diagram, you do not show actions, and this is already limiting your ability to express the MDP.

List the possible transitions and probabilities of them

For most action choices in the example, there are two possible outcomes from each action. The problem doesn't actually describe what happens when there is only one door, for the room with the tiger, but it seems reasonably safe to say that it is not possible for Bunny to get confused when there is only one door, so exiting the room with the tiger is one exception.

Assign rewards. Rewards should relate to the goals stated in the problem definition. Negative rewards for things to avoid happening (or in general to avoid time wasting). Positive rewards for reaching desired targets.

In your case you have two goals - avoid tiger and exit building. There does not appear to be any time constraint.

Typically you would show action choices as labelled arrows going from states to an action node (usually just a small solid circle) where the state transitions then take effect, with further arrows labelled with probabilities going to states. So State A should have two arrows going out "choose left door" and "choose right door", then these choices will branch again - "choose left door" will go to state B with p=0.9 and state C with p=0.1.

You have some free choices over where to put reward labels, and there are a few different standard approaches. In this case, I would put the non-zero rewards on the state transitions that lead to goal-affecting states. E.g. maybe a +1 reward on the state transition from B taking the exit door and ending up outside. Note that for both the exit and the room with the tiger, that there are two possible ways to get to them due to the random transitions (thanks to Bunny getting confused).

Note there are no actions, and no possibility of different transitions, where Bunny starts in state C and ends up in state C on the next timestep. I think you are confusing state C as being a terminal state - although this is understandable as some encounters with hungry tigers in the real world could well be terminal, the problem clearly states that everything just carries on for Bunny if he encounters the tiger. It is just something he wants to avoid. If he ends up in state C, then he still has an action he can take to go through the door. When he takes this action, he cannot "get confused" and pick the wrong door, so should always end up in state B with p=1.0.

If you decide to draw a transition diagram, you have free choice of layout. There is no fixed approach for this, and for many MDPs it may not be possible to draw a nice 2D diagram. However, in your case, it is definitely possible to layout the states in a simple 2D arrangement. A good place to start would be to place the states in locations that correspond to the imaginary room map. Leave plenty of space initally, as the states do not link directly (as you have started), but link via action nodes which themselves branch to the next state.

I hope this enough guidance for you to complete the problem by yourself. Solving the remainder on your own will be much better for your learning than turning it into a worked example here.


Grouping numeric data into efficient group/pool I have devices assigned with different data plan. But based on device behaviour amount of data used by device changes during the month. I need to put the device into appropriate data plan based on predicted data usages before end of the month to avoid data overage.

There are 5MB,25MB,50MB,150MB,250MB data pool plans per device. If I put 2 devices into 25MB plan then both device can collectively use 50MB of total data among each, if there are 5 device in 25MB plan then all 5 devices collectively can use 125MB data.

My first simple approach was to based on total data usages of the device, put it into higher data plan. It was not effective because it's shared pool, and pool size varies based on how many devices are in the pool, I need to optimised the grouping of the devices into any particular data plan to optimised pool data usages. I can have any number of group/pool (Max 5 because there are 5 different data plans).

another way I tried was to cluster the device based on data usages and add them into particular group/pool, but with this cluster gets created based on how the usages data is distributed, which is not optimally close to data plan.

Anyone know how to approach this problem to put the devices in a pool to optimally use the pools data limit and not go beyond it.

Sort all devices.

Take the first x elements until their average would be over 5 MB.

Then take the next x2 devices until their average would hit 25 MB.

Straightforward with sorted data. It's not the global optimum but will get you reasonably close.


Could GANs be used to augment data? I want to use GAN for data augmentation but I am confuse what are the pros. and cons. of data augmentation using GAN or why we use data augmentation using GAN compared to other data augmentation technique like standard data augmentation?

Your answer will be a great help to me.
Thanks.

GAN's and traditional augmentation techniques are fundamentally different in a way: A GAN produces (and combines) patterns previously seen in a dataset, data augmentation adds patterns to the data.

Well thought out data augmentation tries to add variations that could exist in the data. For instance: In arial photography rotation around the z-axis is very trivial and it could be smart to add that generously, on the other hand of the spectrum: In computer generated cartoons for instance, you'd reasonably do not expect much gaussian noise, and you might want to use that sparsely.

GAN's don't add information, they mostly add the same localised patterns that your (I'm assuming) ConvNet to train also has. The benefit a GAN offers is that you can use unlabelled data to train your convolutional layers with concepts you'd might expect in the domain.

Other techniques that go in that same direction, and you might want to check out, are weakly supervised learning and auto-encoding (which is imho very close to your original GAN idea).


Optimisation strategy webstores with shipping costs I am scraping the prices of several products on different websites for the past couple of weeks. These prices are stored, plotted and e-mailed to me every day with an update about the price changes. Now I want to take it to the next level, and make a store selection based on several products.

What I want, is pick a number of products, and calculate the most optimal shops where to buy these products, including shipping costs.

For example, I want to buy products A, B and C, at stores X and Y, with the following prices (so product A costs 2 at store X, and 3 at store Y):

   X Y
A  2 3
B  3 4
C  3 2

Not taking into account shipping costs, I would buy A and B at store X, and C at store Y. However, if the shipping costs are too large for store Y, it might be better to buy everything at store X. Making things more complicated, if stores X and Y have shipping costs of 5, but Y is free when buying more then a specific amount, it might be more profitable to buy everything at Y.

Can anyone point me in the right direction how to solve this? The idea for now is to include ~10 products at three stores. I though about linear optimisation, but I do not know how to include the shipping costs.

I have done the opposite of your problem - I have written code to implement shipping costs for e-commerce sites which runs on their sites.

Shipping cost rules can be almost completely arbitrary logic. In the general case, you have no good choice but to implement them as logic that is assessed per order. That means your optimiser will have to run that logic for every combination of purchases it considers. Which makes this a planning/combinatorics problem.

This may not be so bad - e.g. for purchasing 10 items across 5 stores you have $5^{10}$ combinations which is 9765625. That is possible by brute force.

For larger orders, or more choices of store, you may want to look at dynamic solutions to optimise cost, such as simulated annealing, genetic algorithms. Reinforcement learning run per order may also work.


Difference between a log scale and linear scale : np.random.rand() How and why will a linear np.random.rand()  (to generate a linear scale between 0.0001 and 1) not result in better distributed result but np.power(10,-4 * np.random.rand()) (log scale) will.
I am just accepting the below explanation as it is, But I can't understand the reason behind it.(simple math, i believe, which i am missing. Kindly help with some example)

Reference to my query:
Using an Appropriate Scale to Pick Hyperparameters

  To understand this, consider the number of hidden units
  hyperparameter. The range we are interested in is from 50 to 100. We
  can use a grid which contains values between 50 and 100 and use that
  to find the best value:

  Now consider the learning rate with a range between 0.0001 and 1. If
  we draw a number line with these extreme values and sample the values
  uniformly at random, around 90% of the values will fall between 0.1 to
  1. In other words, we are using 90% resources to search between 0.1 to 1, and only 10% to search between 0.0001 to 0.1. This does not look
  correct! Instead, we can use a log scale to choose the values:

This query is from Andrew Ng's Specialization Course on Deep Learning - Coursera
Course 2 Week 3

Let us assume you want to do hyperparameteroptimization with a hyperparameter $h\in[0,1]$. Let us additionally assume that we want to test hundred possible values for the parameter.

If you choose a linear scale your parameter values will be uniformly selected form $[0,10^4]$. It will be very unlikely that you will small parameter values smaller than $10^{-4}$, because it is very unlikely to choose a number from this interval. The probability for randomly choosing a number from this interval is given by the following fraction

$$\dfrac{10^{-4}}{10^4}=10^{-8}.$$

Now if we choose our random numbers from the intervall $[-5,5]$ and use this as the power of the number $10$ we will stretch the interval onto an exponential scale which will make it more likely that we have numbers in $[0,10^{-4}]$ because the chance of choosing  a negative number for the exponent is much more likely. The probability will be $1/10$, because the wanted interval from $[-5,-4]$ has a width of $1$ and the whole interval from $[-5,5]$ has the width $10$.


is a 4 output regression equivalent to 4 single output regression superposition? I would like to generate a regression based on 6 different inputs and 4 output.

I use python and I compare sklearn and scipy. But in both cases, regression models are essentially focused on 1 output parameter.

My question is : can I considerer that all of my outputs are unlinked, and if yes, is it true to imagine that I can perform 4 regressions in parallel (one for each output) to make an equivalent 4 ouput model ?

Tank you for your tips !

"My question is : can I consider that all of my outputs are unlinked"

No, in general you can't, unless you have prior knowledge indicating so. There is a high chance that your outputs are correlated. To explore linear correlations, perform a correlation analysis (calculate correlation matrix). To investigate nonlinear relations, calculate the distance correlation.

  and if yes, is it true to imagine that I can perform 4 regressions in
  parallel (one for each output) to make an equivalent 4 ouput model ?

If your analysis proves that there is low correlation between the outputs, only then you can implement 4 independent regression models in parallel with meaningful results.

My suggestion would be to implement a MIMO machine learning model (multiple inputs / multiple outputs), such as a multivariate Recurrent Neural Network.

Hope it helps :)


Classification Based Collaborative Filtering Model I was going through algorithms for collaborative filtering-based prediction. Most of the places, I read about using matrix factorization based on ratings of the likeness of the user. But for my use case, I have to predict on the bases of classified data. For example, I have a list of user who orders from place1,place2, etc and they mostly get their items delivered to address1,address2 etc. Initially, I thought to give binary ratings to the places, but I am having multiple criteria i.e addresses and places. How can I use collaborative filtering in this case?

User
Place
Delivery Address

One
X
A

One
X
A

One
Y
C

Two
Y
C

Two
Z
B

Three
Y
??

Actually, your objective here is not clear.
Following your example, I belive you want to obtain likeness/relationship between users and places.
So basically what you would want to do is to create a domain of users and places. Now for different addresses (eg. A and C), you can treat the same user (eg. One) as different users i.e. "One-A" and "One-C". You can now assign a binary value for each "user-address" v/s "place". Then you can use any collaborative filtering method to get the affinity of the users corresponding to a particular address, for a particular ordering place.


Maximize Precision Deep Learning For some binary image classification problems having close to 100% precision is super important and recall is much less important.

What are best practices for maximizing precision? Setting the probability threshold for model.predict() greater than 0.5 seems like a reasonable approach.

Are there any other recommended methods?

If you are exploring the best hyper-parameter combination that maximizes precision you could try GridsearchCV along with KerasClassifier. You create a wrapper of your model and use it as the estimator of the GridsearchCV method. Also, since you want to maximize precision, you would have to set the scorer parameter to precision. After that you are going to have to set the hyper-parameter space that you would like to explore and finally run the experiment.

Please bear in mind that this approach is computationally expensive. If your dataset is very large, a space of n > 2 (i.e 2-d), would need a cluster so as to produce results in a timely manner.


Why this calculation of weight vector in linear regression is only for small dataset? Slides from my university says, that the following way of calculating the weight vector is suitable only for small datasets. Can you please explain, why it may be suitable for small datasets?

Here, X is observed variable and Y is dependent variable{usually 0 or 1}

$X$ is your design matrix (data), so can get large, both in number of data points and number of predictors. The issues are indeed:

Forming $XX^T$ may be computationally expensive
Computing it explicitly may be inaccurate due to floating-point roundoff, as you're adding lots of products
Inverting a matrix can be expensive here if the number of predictors is large as the Gramian is large
With lots of predictors and a big Gramian, it's more likely to be ill-conditioned and suffer from numerical instability


What does scaling a gradient do? In the MuZero paper pseudocode, they have the following line of code:

hidden_state = tf.scale_gradient(hidden_state, 0.5)

What does this do? Why is it there?

I've searched for tf.scale_gradient and it doesn't exist in tensorflow. And, unlike scalar_loss, they don't seem to have defined it in their own code.

For context, here's the entire function:

def update_weights(optimizer: tf.train.Optimizer, network: Network, batch,
                   weight_decay: float):
  loss = 0
  for image, actions, targets in batch:
    # Initial step, from the real observation.
    value, reward, policy_logits, hidden_state = network.initial_inference(
        image)
    predictions = [(1.0, value, reward, policy_logits)]

    # Recurrent steps, from action and previous hidden state.
    for action in actions:
      value, reward, policy_logits, hidden_state = network.recurrent_inference(
          hidden_state, action)
      predictions.append((1.0 / len(actions), value, reward, policy_logits))

      # THIS LINE HERE
      hidden_state = tf.scale_gradient(hidden_state, 0.5)

    for prediction, target in zip(predictions, targets):
      gradient_scale, value, reward, policy_logits = prediction
      target_value, target_reward, target_policy = target

      l = (
          scalar_loss(value, target_value) +
          scalar_loss(reward, target_reward) +
          tf.nn.softmax_cross_entropy_with_logits(
              logits=policy_logits, labels=target_policy))

      # AND AGAIN HERE
      loss += tf.scale_gradient(l, gradient_scale)

  for weights in network.get_weights():
    loss += weight_decay * tf.nn.l2_loss(weights)

  optimizer.minimize(loss)

What does scaling the gradient do, and why are they doing it there?

Author of the paper here - I missed that this is apparently not a TensorFlow function, it's equivalent to Sonnet's scale_gradient, or the following function:

def scale_gradient(tensor, scale):
  """Scales the gradient for the backward pass."""
  return tensor * scale + tf.stop_gradient(tensor) * (1 - scale)


What is "posterior collapse" phenomenon? I was going through this paper on Towards Text Generation with Adversarially Learned
Neural Outlines and it states why the VAEs are hard to train for text generation due to this problem. The paper states

  the model ends up
  relying solely on the auto-regressive properties of the decoder while ignoring the latent variables,
  which become uninformative.

please simplify and explain the problem in a lucid way.

With the help of better explanations provided in Z-Forcing: Training Stochastic Recurrent Networks:

When posterior is not collapsed, $z_d$ (d-th dimension of latent variable $z$) is sampled from $q_{\phi}(z_d|x)=\mathcal{N}(\mu_d, \sigma^2_d)$, where  $\mu_d$ and $\sigma_d$ are stable functions of input $x$. In other words, encoder distills useful information from $x$ into $\mu_d$ and $\sigma_d$.

We say a posterior is collapsing, when signal from input $x$ to posterior parameters is either too weak or too noisy, and as a result, decoder starts ignoring $z$ samples drawn from the posterior $q_{\phi}(z|x)$.

The too noisy signal means $\mu_d$ and $\sigma_d$ are unstable and thus sampled $z$'s are also unstable, which forces the decoder to ignore them. By "ignore" I mean: output of decoder $\hat{x}$ becomes almost independent of $z$, which in practice translates to producing some generic outputs $\hat{x}$ that are crude representatives of all seen $x$'s.

The too weak signal translates to
$$q_{\phi}(z|x)\simeq q_{\phi}(z)=\mathcal{N}(a,b)$$
which means $\mu$ and $\sigma$ of posterior become almost disconnected from input $x$. In other words, $\mu$ and $\sigma$ collapse to constant values $a$, and $b$ channeling a weak (constant) signal from different inputs to decoder. As a result, decoder tries to reconstruct $x$ by ignoring useless $z$'s which are sampled from $\mathcal{N}(a,b)$.

Here are some explanations from Z-Forcing: Training Stochastic Recurrent Networks:

  In these cases, the posterior approximation tends to provide a too
  weak or noisy signal, due to the variance induced by the stochastic
  gradient approximation. As a result, the decoder may learn to ignore z
  and instead to rely solely on the autoregressive properties of x,
  causing x and z to be independent, i.e. the KL term in Eq. 2 vanishes.

and

  In various domains, such as text and images, it has been empirically
  observed that it is difficult to make use of latent variables when
  coupled with a strong autoregressive decoder.

where the simplest form of KL term, for the sake of clarity, is
$$D_{KL}(q_{\phi}(z|x) \parallel p(z|x)) = D_{KL}(q_{\phi}(z|x) \parallel \mathcal{N}(0,1))$$
The paper uses a more complicated Gaussian prior for $p(z|x)$.


Analytics term for turning row values into column names and count its assigned values Do we have a data mining/analysis term for turning row values into column names and count its assigned values?


using movielens dataset build recommendation engine Where I can get the complete guide (step by step )on building a recommender system for example using movielens datsets building content based, collaborative or may be hybrid system.

There are plenty of ways to realize this. Originally everybodies approach to the Netflix Prize (quite similar to the movielense dataset) were nearest neighbor methods. Their intuition should be fairly clear. Matrix Factorization became the next big thing and was nicely described by Simon Funk in his journal: http://sifter.org/~simon/journal/20061211.html . If you are more interested in these two topics and don't mind a bit of bad english you may consider this thesis: http://patrick-ott.de/dl/diploma_ott.pdf .

The crowd then went on to Restricted Boltzmann Machines for the task, e.g. (cs.utoronto.ca/~hinton/absps/netflixICML.pdf) . I think it is beyond this answer to explain these for this particular task -- maybe worth another question,

All three approaches though (Nearest Neighbor, Matrix Factorization and RBMs) never we are able to reach to goal set by Netflix, which let to the development of more or less clever methods to combine their predictions, which finally was enough to get to the point Netflix set out.

These are all somewhat old approaches to the problem, their might be newer ones. However, the most recent machine learning hype, i.e. Deep Learning has so far not given many possible solutions although dominating in other fieds.


what's this approach to spatiotemporal data named as? I have some sequential data (e.g. audio, video, text etc.) and I am using this approach to classify sequences. I am sure there's a name for it, but I can't think of it:
vectors =
    t1,[v1_0....v1_n]
    t2,[v2_0....v2_n]
        :
        :
    tm,[vm_0....vm_n]

where t1..tm are the time offsets and the VM are the feature vectors.
Out of this data, I create oversampling by using different window_size, and step_size
[t0,win_size,step_size, np.median(vectors[frame_id:frame_id+win_size],axis=0)]

win_size = the size of the window to mean/median ahead
step_size = how many columns to move with each step
np.median(vectors[frame_id:frame_id+win_size],axis=0) = column0wise mean or median across the array as a resultant of the vectors

and use the above to train a classifier and should be to generate predictions at multiple scales (e.g. at a large window size).
Edit: for predictions, from the query clip extract a similar set of vectors and then predictions can be aggregated somehow to generate the "consensus".

I would call that a multi-headed convolutional model.


Deep Learning for Time series Deep Learning is an excellent model for classification problem such as image recognition or object detection. Can we use deep learning for regression problems - Time Series prediction ? So if it can, how can we build structure of deep learning. I mean how to building layer to extract features from time series.

Recurrent neural networks (RNNs) can work with series as input or output or both.

Even a simple one-layer RNN is effectively "deep" because it has to solve similar problems as multi-layer non-recursive networks. That is because backpropagation logic in a RNN has to account for delay between input and target, which is solved by backpropagation through time - essentially adding a layer to the network for every time step of delay between first input and last output.

RNN architecture has become more sophisticated in recent years by using "gating" techniques such as Gated Recurrent Units (GRU) or Long Short Term Memory (LSTM). These have multiple trainable params - 3 or 4 - per neuron, and the schematics are more complicated than feed-forward networks. They have been demonstrated as very effective in practice, so this extra complexity does seem to pay off.

Although you can research and implement RNNs yourself in a library like Theano or Tensor Flow, several neural network libraries already implement RNN architectures (e.g. Keras, torch-rnn)


Inspect false classified Recently, I was able to train a simple classification algorithm (my first ML-Project) and I even got a pretty satisfying precision score.

Now I am looking for a way to inspect, which datapoints in my train_data have been falsely classified.
My basic idea was something like:

If y_train != y_pred Then:
     (get indices of y_train)
     (look up the data in my csv and try to find a pattern)

My main problem is, that the train_test_split function provides me with a y_test subset like this:

print(y_test):

    28886    0
    23319    0
    8913     1
    25770    0

and y_pred is a list like this:

print(y_pred):

    [0 0 1 ... 0 1 0]

Since there already is an existing index in y_test, I can't just compare y_test[2] with y_pred[2]. It seems to me that, y_test[2] does not provide the third element of y_test. Rather it provides the third element of my original dataset.

I am looking for a way to compare position n of my y_test subset with position n of y_pred, so I can get the index of all false classified.

The Python code I used to get this result:

x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(X,Y, test_size=0.2)

clf = KNeighborsClassifier(n_neighbors=13)
clf.fit(x_train,y_train)

y_pred = clf.predict(x_test)
acc = metrics.accuracy_score(y_test,y_pred)
print(acc)

You can convert series to the list and do the comparison, the order must have remained same any way.

or you if you want to keep the reference you can convert list to series using

pd.Series(list, index= y_test.index)


How to extract titles from documents? How can one automate the extraction of a relevant title from a given document (docx, pdf, etc..)?

Some thoughts:

Intuitively likely to be the first line in the text
Cannot be something like a date, "all rights reserved", "Harvard University - Faculty of Law" etc.
Might be in the footer
The human brain solves this immediately (by filtering out common names and numbers)

I'm using textract in python to extract the whole text, which limits the solution to be based on text only. Let me know if you are familiar with any other packages.

The automate the extraction of a title is an example of extractive summarization. It is typically solved with a combination of supervised and sem-supervised learning. More recently Deep Learning models with attention have been giving state-of-the-art results.

You'll need labeled to train your own model or used established packages such as symy.


Can Random Forest regressor or Decision trees handle missing values and outliers? I have below assumptions about RF & Decision trees in general, please correct me if the assumptions are incorrect.

It takes care of missing values
It handles outliers
It handles skewness in the data so the transformation is not required.
Feature scaling is not required
Feature selection is not required.

Please correct if my observations are incorrect as you can see there is a nan value error in the dataset. Also how to rectify it?
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

importing dataset
share3.info()
share2.isnull().sum()

XRD = share2.drop(['Close'], axis = 1)
YRD = share2['Close']

training & test set
from sklearn.model_selection import train_test_split
X_trainRD, X_testRD, Y_trainRD, Y_testRD = train_test_split(XRD,YRD,test_size = 0.2, random_state = 0)

fitting random forest regresson to the dataset
from sklearn.ensemble import RandomForestRegressor
regressor = RandomForestRegressor(n_estimators= 350,random_state = 0)
regressor.fit(X_trainRD, Y_trainRD)

***ValueError: Input contains NaN, infinity or a value too large for dtype('float32')***
```

No, not all of your assumptions are true

1. Missing values

The sklearn implementation of RandomForest does not handle missing values internally without clear instructions/added code. So while remedies (e.g. missing value imputation, etc.) are readily available within sklearn you DO have to deal with missing values before training the model.

This involves understanding how and why values are missing (MCAR VS. MAR VS. ...) and determining the best way to deal with those values (median/mean imputation, MICE, removing values, etc.).

2. Outliers and Skewness

Again this isn't a done deal! You need some EDA to understand your data here and handle it accordingly. Strong biases in the distribution of the dependend variable (so-called class imbalances) can heavily influence the result. Look into ways to handle those like SMOTE.

Outliers are less of a problem, granted they really ARE outliers. Check via EDA and confirm this isn't a problem to be dealt with.

3. Feature selection and transformation

It is true that many ML models favor a more-is-more approach to feature selection. The main benefit of using RandomForest, XGB over classical statistical approaches is that they cope much better with irrelevant predictors.

Still feature selection also means feature engineering which is still helpful and necessary. Additionally for practical purposes you should still aim to reduce predictor amount if you want to use your model again. Because no matter how irrelevant every predictor used in the training model NEEDS TO BE PRESENT in the unseen data as well.

Lastly you will need to transform data! Simply example, RandomForest needs one-hot-encoding (OHE) for factor variables and string variables. You do not need the same scale for all your numerical variables but they all need to be numerical!


What is preferred upsampling or zero padding? When training a CNN one option is either to zero pad an image to make it bigger or upsample it. When should I choose each one?
What criteria is leveraged for choosing a method?

It depends on application.
For upsampling, Keras documentation says:

Repeats the rows and columns of the data by size[0] and size[1]
respectively.

For zero padding, Keras documentation says:

This layer can add rows and columns of zeros at the top, bottom, left
and right side of an image tensor.

So, imagine You have picture of sharp edge shapes, and You want to build model that could learn to distinguish them. In that case, I would likely go with UpSampling, because You would have extra data, and Your shape structure wouldn't be ruined. While, with smooth curved edges, smoothness could be ruined. In that case, I would go with ZeroPadding, because it won't touch inner data, it will only add zeros side the borders.
This is my intuition about those two functions, don't take it for granted. The best option You could do is to test both on Your sample and see the output.