How would you a apply a cnn to do age estimation on static images? After doing some reading on age estimation using the IMDB wiki dataset I wanted to try it out myself on a smaller scale but I dont quite understand the application of the CNN. Any clarification would be great.

With a CNN, one of the typical (and simplest) approaches is to perform classification or regression: you train a network with a set of labeled images (supervised learning) with the aim of, after the training, the network will be able to assign the correct label (among the available labels in the training set) to a new image (never seen before).

Basically, you need:

Decide which are your labels: they can be ages from 0 to 100, so you will have 101 labels. You can decide that you have 10 labels, 0-10, 10-20, and so on... For regression you can use real numbers within a range...
Training labeled data: set o pairs $(img,l)$ where $img$ refers to images and $l$ to labels, i.e. for each image there exists only one label among those you have decided to use
Testing labeled data: the same as above, but this images won't be used to train the network. They will be used to test its performance
CNN: design a Convolutional Neural Network (or get one done) to perform classification or regression. There are lots of examples out there
Train the CNN on your training data set, get the appropriate performance metric
Test the CNN on your test set after training, with the same performance metric
Compare training results with test results to evaluate the bias, variance and overfitting issues of the network

EDIT (following  Mark.F comment):

The network will be slightly different if you try to perform classification or regression: for classification, the network can only assign one of the values (typically integers in this scenario) that are available in the training data. For regression, the network will assign a certain value within a range for each image (for example 0-100). The cost functions for both types or network are different and usually also the last layers. What you do need for both approaches is labeled data.

The CNN are used with images because they use 2D filtering over them to extract the most important features. Basically, the CNN can learn the most important "aspects" of the images that best help to perform the desired task


How to extract characteristics from text using machine learning? I would like to develop some kind of model/algorithm that allows me to extract the characteristics of a given product name. (let's say the brand, model and color).

I am looking for a solution similar to the one offered by MonkeyLearn and its model Laptop Feature Extract.

For example:

Given the item "Apple iPhone 6s, 64GB Silver", It should compute:

{
  brand: "iPhone",
  model: "6s",
  capacity: "64Gb",
  color: "Silver"
}

Any suggestions will be appreciated.
Thank you.

What you need to look for is called "Named Entity recognition". From Wikipedia

  Named-entity recognition (NER) (also known as entity identification,
  entity chunking and entity extraction) is a subtask of information
  extraction that seeks to locate and classify named entity mentions in
  unstructured text into pre-defined categories such as the person
  names, organizations, locations, medical codes, time expressions,
  quantities, monetary values, percentages, etc.

There are already trained models for that, but most of them are for generic usage. For example in Python

import spacy

nlp = spacy.load('en_core_web_sm')
doc = nlp('European authorities fined Google a record $5.1 billion on Wednesday for abusing its power in the mobile phone market and ordered the company to alter its practices')
print([(X.text, X.label_) for X in doc.ents])

The output is

[('European', 'NORP'),
('Google', 'ORG'),
('$5.1 billion', 'MONEY'),
('Wednesday', 'DATE')]

Source of code: TowardsDataScience

In your case, you have to either train an NER yourself for phone specifications or find one that is available in public.


Apply LSTM to each matrix element with Keras I'm trying to apply a LSTM/GRU to each entry of a matrix $X$

note: Each matrix element is a time-series, so shape of X is (batch_size, rows, cols, time_steps, dims)

$
y_{i,j}=
    \begin{cases}
      0, & \text{if}\ x_{i,j}\small[0\small] = 0 \\
      LSTM(x_{i,j}), & \text{otherwise}
    \end{cases}
$

, where:

$x_{i,j} := \text{element (i,j)  of matrix X. This element is a time series}$
$x_{i,j}\small{[0]} := \text{ First tiem step of time series $x_{i,j}$ }$

$y_{i,j} := \text{LSTM output for $x_{i,j}$}$

PROBLEM

I've managed to evaluate the condition and to apply the LSTM to each entry, but I can not build a keras model with these outputs, beacuse of the typical error:

AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

CODE

import keras

def convert_to_layer(X):
    return X

def gather(r):
    def gather_(X):
        return X[:,r,:]
    return gather_

n_rows = 2
n_cols = 2
time_steps = 20
dims = 2
units = 32

M = keras.layers.Input(batch_shape=(1,n_rows,n_cols,time_steps,dims))

# Serialize inputs
M2 = keras.layers.Reshape((n_rows*n_cols,time_steps,dims))(M)

# Gather element 0 (only work on this element for simplicity...)
seq = keras.layers.Lambda(gather(0))(M2)

# Create condition
cond = keras.backend.equal(keras.backend.sum(keras.backend.abs(seq),axis=-1),0)

# Apply if statement
y = keras.backend.switch(cond,
                          lambda: keras.layers.GRU(units,return_sequences=True)(seq),
                          lambda: keras.backend.zeros(shape=(1,time_steps,units)))

y = keras.layers.Lambda(convert_to_layer)(y)
print(y)

model = keras.models.Model(M,y)

OUTPUT

Tensor("lambda_311/Identity:0", shape=(1, 20, 32), dtype=float32)

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-76-983e9545cca1> in <module>()
     29 print(y)
     30
---> 31 model = keras.models.Model(M,y)

5 frames

/usr/local/lib/python3.6/dist-packages/keras/engine/network.py in build_map(tensor, finished_nodes, nodes_in_progress, layer, node_index, tensor_index)
   1378             ValueError: if a cycle is detected.
   1379         """
-> 1380         node = layer._inbound_nodes[node_index]
   1381
   1382         # Prevent cycles.

AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

QUESTIONS

The problem is with the keras.backend.switch (if the switch is removed and the output of the model is the output of the gru_cell unconditionally, the model can be build)

Why isn't the model compiling?
Any other alternative way of computing the LSTM over the matrix $X$

SOLUTION

Replace:

# Apply if statement
y = keras.backend.switch(cond,
                          lambda: keras.layers.GRU(units,return_sequences=True)(seq),
                          lambda: keras.backend.zeros(shape=(1,time_steps,units)))

y = keras.layers.Lambda(convert_to_layer)(y)

with:

y = keras.layers.Lambda(lambda x: keras.backend.switch(cond,
                          lambda: keras.layers.GRU(units,return_sequences=True)(x),
                          lambda: keras.backend.zeros(shape=(1,time_steps,units))))(seq)


Scalling and unscalling of data for SVR prediction I'm trying to use SVR to predict a certain feature. I create the model with the following code:

from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler

X = data
# this is the outcome variable
y = data.iloc[:, 10].values

sc_X = StandardScaler()
sc_y = StandardScaler()

X2 = sc_X.fit_transform(X)
y = sc_y.fit_transform(y.reshape(-1, 1))

# my_custom_kernel looks at certain columns of X2 / scaled data
my_regressor = SVR(kernel=my_custom_kernel)
my_regressor = regressor.fit(X2, y)

After creating the model, I want to test it to see if the prediction is good. The first thing the code does is scale the row that I want to test, using the same scaler as above (sc_X). Then I try to reverse the scaling of the prediction result (using sc_y). During this process, I get datatype errors. Here is the code:

line1 = X.iloc[0].as_matrix().reshape(1, -1)
line1_scaled = sc_X.fit_transform(line1)
res = regressor.predict (line1_scaled)
pred_line1 = sc_y.inverse_transform (res) # The error appears to be here

Error:

ValueError: non-broadcastable output operand with shape (1,) doesn't match the broadcast shape (24,)

Thanks.

First , I am not sure why you want to scale the "Y" value at all? Inverse transformation is to scale back features of X mainly.
https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html#sklearn.preprocessing.MinMaxScaler.inverse_transform

Second, You are passing only 1 column of X now for prediction and your sc_y remembers the shape you passed it earlier. Your error is telling you the difference also. One has(24,) and another(1,).


How to predict value in every 120 minutes using LSTM in python I want to predict value in every 120 minutes continuous using LSTM model. Here I wrote the code for predction. But I'm not getting proper prediction values . Here from start time I need to predict values in every 120 minutes.

model = Sequential()
model.add(LSTM(4, return_sequences=True,input_shape=(x_train_n.shape[2])))
# returns a sequence of vectors of dimension 32
model.add(LSTM(8, return_sequences=True))  # returns a sequence of vectors
of dimension 32
model.add(LSTM(8))  # return a single vector of dimension 32
model.add(Dense(1))
batchsize = 1
model.compile(loss="mean_squared_error",optimizer="adam")
#model.compile(loss='mean_squared_error', optimizer='adadelta',metrics= ['accuracy'])
history = model.fit(x_train_n,y_train_n, batch_size = batchsize,
nb_epoch=30,validation_data=(x_test_n, y_test_n),shuffle =True)\
model.reset_states()
pred1=model.predict(x_test_n)
pred2= model.predict(x_train_n)

#end of the sequences
model.reset_states()

#data=pd.DataFrame(fit1.predict(x_test))

pred1 = scaler_y.inverse_transform(np.array(pred1).reshape ((len(pred1), 1)))
real_test = scaler_y.inverse_transform(np.array(y_test_n).reshape
((len(y_test_n), 1))).astype(int)
real = scaler_y.inverse_transform(np.array(y_train_n).reshape
((len(y_train_n), 1))).astype(int)
pred1 = pred1[:,0]
real_test = real_test[:,0]

#predition in every 120 minutes

sequence_timestep = 120
last_sequence_train = x_train_n[-1]
pred1 = []
def sequence_constructor():
   if len(pred1) >= sequence_timestep:
     new_sequence = pred1[-dimension_seq:]
   else:
      splitter = sequence_timestep - len(pred1)
      part_1 = last_sequence_train[-splitter:]
      new_sequence = np.append(part_1,pred1) #Concatenate 2 list
  new_sequence = np.array(new_sequence)
  return new_sequence
for i in range(1440):
new_sequence = sequence_constructor()
new_prediction = model.predict(new_sequence)
pred1.append(new_prediction)

when I wrote this code error is coming and it's not predicting the values properly.
Here I upload my csv file also, and I wrote what I'm trying to do to predict my value. In my csv file g and p in my two inputs. When the prediction values in every 120 minutes will be the next input of my LSTM .
According to my csv file
10/3/2018 start time = 6:00:00 a.m from that predict the g values in every 120 minutes.
Then again next day start time is again 6:00:00 a.m from that time again new prediction values in every 120 minutes.

if you want to predict exactly at a particular timestamp, & you do not want to train the model again and again then may be you can run a crontab on a python file to load the model and predict the value, with cron job starting at 6 AM & execute again at every 120 minutes

Even if you have a stream of data coming continuously you can re-train the model in between those 120 minutes.


Exogenous Variables in Time Series Models I found that in the forecast package in R that I can easily incorporate an exogenous variable Y in my ARIMA model meant to forecast X. While I have a general understanding of the kind of process I need to go through in order to determine the right ARIMA model on X, I'm not sure what I need to understand about Y in order to incorporate it as an exogenous variable.

I was wondering if anybody could help me understand the implications of having this variable in my model and whether there are any tests/checks I can perform to determine whether it's a good idea to have it in my model (other than my intuition and subject matter understanding). Any reading material on this would be helpful. Thanks!

If your error is already very low, you don't have to use xreg, otherwise you may increase the error, but it's worth to try. But if your error is high because your model can't capture the seasonality that you want, try to use an exogenous variable (xreg) for forcing an seasonality.

You can use weekends/holidays dates as an xreg, recurring 0/lowest values is an example of the very thing you want your model to learn. But in my experience, simply try to use the X histogram (binned X) as an xreg, before you try anything. I found that fact after my months of confusion.

The intuition is you can see the histogram/bin as a product of the seasonality itself.


Variational auto-encoders (VAE): why the random sample? Why do people train variational auto-encoders (VAE) to encode means and variances (regularised towards 0 and 1), and then sample a random Gaussian, rather that simply encode latent vectors and regularise them to follow a standard N(0,I), which would appear as a more natural choice?

To have a common mental image of AE and VAE please take a look at this answer first.

Lets go through this "why not?" thought process step by step:

Why not deterministic? lets directly encode the latent vector $z$ inside a layer of neural network. But this way, $z$ would be deterministic, meaning a fix input $x$ always produces a fix latent vector $z$, thus, $z$ would not have distribution $q(z|x)$. This is the ordinary auto-encoder. Sampling $z$ from  $q(z|x)$ means if we try the same input $x$ twice, we should get two different values for $z$. Deterministic computations (layers) cannot achieve this. Therefore, we need to inject a random element in the calculation of $z$, otherwise the same $x$ always gives the same $z$. Note that getting a different $z$ for a different training point $x$ implies the existence of distribution $p(z)$, which exists for ordinary auto-encoder too, not the existence of conditional distribution $q(z|x)$,
Why not only mean? OK, lets add these random elements to a layer to get our beloved probabilistic $z$, lets call this layer $\mu$. So, suppose $\mu$ is calculated from previous layer $y$ as $\mu=\mbox{tanh}(y)$, then we select the random elements $\epsilon_d \sim  N(0,1)$ per dimension of $\mu$, and then calculate $z= \mu + \epsilon$ which is the same as sampling $z_d \sim  N(\mu_d, 1)$, now our $z$ is probabilistic and we have got rid of standard deviation $\sigma$ and its regularization all together, we now only need to regularize mean $\mu$ to $0$,
Why not only random elements? Lets go one step further, lets throw $\mu$ away too and set $z_d = \epsilon_d \sim  N(0,1)$, and get rid of all the regularizations. But wait a minute! now $z$ is completely disconnected from previous layers, so no information is delivered from input $x$ to latent variable $z$.

Going from VAE with two parameters $(\mu, \sigma)$ to (2) with one parameter $\mu$ and then (3) with zero parameter is the same as saying instead of using parameter $w$ and regularizing it via $\parallel w \parallel^2$, lets set $w=0$ and get rid of the regularization. Even though we want parameters to be close to zero but still we want them not to be zero and carry information. This is the same as $(\mu, \sigma)$ in VAE, we want them to carry information, although being closer to $(0, 1)$ is favorable. $(\mu, \sigma)$ are two channels through which information is distilled from $x$ to $z$. We may even add a third variable (channel) to our distribution if complexity-performance trade-off is favorable. It is safe to say that researchers have experimented with only $\mu$ and observed that adding another channel for variance would be beneficial, so here we are with two channels $(\mu, \sigma)$ and a crucial random vector $\epsilon$.

Why would VAE work better than AE?

The main difference that VAE introduces compared to AE is that now $z$ is loosely (probabilistically) related to $x$ compared to AE, this creates an additional regularization effect that throws some information of $x$ out by not obeying the exact network computations for $z$, thus this extra regularization could improve the performance in practice. Of course, this is not a complete dominance. In some tasks this extra regularization may work against VAE.

In my opinion, the why's could be answered this far as they reach a similar level to "Full Bayesian vs regularized least squares regression".


Proper feature selection methods for classifying signal into two category I have a confusion to decide which feature selection method that I should employ in my research whose objective is to analyze which features that are significant in representing a certain condition of the human body,  lie on two categories: normal or not.
I used multiple sensors to determine some features and plan to characterize the signal through the feature respecting the patient condition.
I have explored so many articles and blog about the fittest method for doing feature selection in classifying data into two categories. and this one is pretty good to me:
https://machinelearningmastery.com/feature-selection-with-real-and-categorical-data/
It said that we can use ANOVA and kendall's rank,
but on another site, it is mentioned that RFE could be employed too in selecting features for classifying data,
another paper also said that we could employ mRmR, genetic algorithm, and Relief,
I am not sure about this, but is it mean I can employ all of those on feature selection without any further restriction according to my research objective?

Generally, feature selection is somewhat of a fuzzy process. Since you usually don't have a ground truth in predicting biology, you will always have to consider how realistic whatever you came up with is. I would recommend to start with the most simple method and see how your model performs. After establishing this as a baseline, you can then explore other feature selection methods to see if that improves your model.
For most models, there are general assumptions that should be met - check this before trying to optimise your features. So, for example, if one feature is just the sum of featureA and featureB, you don't need it in your data. Also, of course, always be cautious about over/under-fitting and do proper cross-validation. And if you come up with a set of features, it might make sense to think about their implications since ML is prone to find shortcuts that are only specific to your data.


List of feature engineering techniques Is there any resource with a list of feature engineering techniques? A mapping of type of data, model and feature engineering technique would be a gold mine.

There is no definite source on how to do feature engineering. It is often dependent on the problem you are trying to solve. Some say it is more of an art than it is science.

But I would go through some of the high scoring kaggle kernels / winning solutions if available. Just head over to kaggle and browse through the competitions. There is a lot of very useful material in there.

Also the journal of machine learning research has a lot of papers about feature engineering. Just search on their site http://www.jmlr.org/.

The following links are useful and to long to paraphrase:

Some information about some best practices of feature engineering can
be found on Quora, see this
link
At machine learning mastery there is this
page
about feature engineering.


How to scatter plot with each dimension having its own color I want to get insight for some values. I have two vectors of 300 dimensions, and want to compare them coordinate by coordinate. So I thought to plot each point with a different color, but the color for a dimension to be the same so that I would know which coordinates are lying where.

I have the below code, which I modeled on some results at stackoverflow.

colors = itertools.cycle(["r", "g", "b"])

for d_in in inp_samples:
    clr = next(colors)
    plt.scatter(len(inp_samples), d_in, c=clr)

colors = itertools.cycle(["r", "g", "b"])

for d_out in out_samples:
    clr = next(colors)
    plt.scatter(len(out_samples), d_out, c=clr)

But the plot I am getting is very weird. I was expecting it would be scattered but it is something like this:

I also tried this:

colors = cm.jet(np.linspace(0, 1, 300))

for d_in, d_out, c in zip(inp_samples, out_samples, colors):
    plt.scatter(len(inp_samples), d_in, c=c)
    plt.scatter(len(out_samples), d_out, c=c)

Can anyone help in understanding what I am doing wrong?

In scatter in Matplotlib, the first two arguments must be the $x$ and $y$ values that you want to combine. In your code, the first value ($x$) is len(inp_samples) (or len(out_samples)) which is the number 300, so all your points line on the $x=300$ vertical line.

In order to combine the inp_samples with the out_samples, I too would recommend using zip, as in your second sample of code. Just replace

plt.scatter(len(inp_samples), d_in, c=c)
plt.scatter(len(out_samples), d_out, c=c)

with

plt.scatter(d_in, d_out, c=c)

(and see if it works).

Edit

  So I thought to plot each point with a different color, but the color for a dimension to be same, so that i would know, which dimensional points are lying where.

In a scatter plot, there is no need for any special colouring -- by two values forming one pair of $(x_i,y_i)$, it is already given which point belongs to which.

You could also try to plot $(i,x_i)$ and $(i,y_i)$ in the same plot. In this case, points above each other belong together and you still don't need any special colouring.

colors = itertools.cycle(["r", "g", "b"])
for i in range(300):
    clr = next(colors)
    plt.scatter(i, d_in[i], c=clr)
    plt.scatter(i, d_out[i], c=clr)


Isn't the depth of a convolutional layer, the number of colors (or colorspace size)? I have been going through a CNN tutorial and noticed that depth of a convolutional layer is equal to the number of filters. But, shouldn't the depth be the number of colors in the image? I mean, if it's RGB then, depth is 3 right? Am I missing something here?

Yes, the depth of an image is equal to the color channels (1 for gray-scale images, 3 for RGB).

However, that is only the case for the input layer of the CNN. During the first convolution layer, the image can be passed through as many filters as we select. This number becomes the depth of the first layer. The subsequent layers can have any depth we want.

For example in the CNN depicted below:

The image input to the CNN initially has a depth of 1 (because it is gray-scale). The first convolutional layer passes it through 6 filters, so the depth of the first layer becomes 6. The second layer passes the output of the first through 16 filters, meaning that the second layer has a depth of 16, etc.


What if the training data and the prediction data are the same? It is a completely theoretical question. What if the training data and the prediction data are the same? Will it impact the results if I use the same data for model predictions that I have used for training?

Yes, it will impact the results.
In the best case your model will not overfit at all so you could expect the same performance between the training set and a previously unseen set (= normally the testing set). In case your model overfits even a little bit, then your results on the training set will be higher (better) than on unseen data.
The impact depends on the classifier/hyperparameters you are testing. For instance, if you are using a KNearestNeighbors classifer with k=1, then the impact will be extreme as you will have 100% accuracy on whatever training set.


Generating the right target for an LSTM model Trying to explain my question on a simplified data set.
Having the following dataset:
   day   f1    f2
0    0   10  1000
1    1   45  2000
2    2  120  3400
3    3   90  5000

I'm trying two approaches to generates a score based on the data observations:
Approach 1:
I've scaled the features so the max value is 1.0 by dividing each feature by it's max value to get:
   day        f1    f2
0    0  0.083333  0.20
1    1  0.375000  0.40
2    2  1.000000  0.68
3    3  0.750000  1.00

I created a score where score = (f1,f2) so now the data looks like so:
   day        f1    f2     score
0    0  0.083333  0.20  0.141667
1    1  0.375000  0.40  0.387500
2    2  1.000000  0.68  0.840000
3    3  0.750000  1.00  0.875000

Approach 2:
I did a similar score calculation, however, the normalization of the features were done with CDF like so:
from scipy import stats
df['f1'] = df.f1.apply(stats.norm.cdf, args=(df.f1.mean(),df.f1.std()))
df['f2'] = df.f2.apply(stats.norm.cdf, args=(df.f2.mean(),df.f2.std()))

   day        f1        f2
0    0  0.123267  0.143672
1    1  0.330776  0.312474
2    2  0.865919  0.624118
3    3  0.687676  0.891864

And the final score:
   day        f1        f2     score
0    0  0.123267  0.143672  0.133469
1    1  0.330776  0.312474  0.321625
2    2  0.865919  0.624118  0.745019
3    3  0.687676  0.891864  0.789770

Looking at the larger picture.
My actual data set is composed of 1280 sequences of 30 days each with 10 features (shape is (1280,30,10)) and I'm trying to predict day 30 score based on the first week of data.
Looking at the histogram for day 30 score of my first approach it looks like so:

while my other approach ac produce the following:

Question: When building an LSTM regression model, what scoring method would better reflect the overall score? Is that a product question, or is there a better statistical method to calculate the score?

The way you framed it is not a regression problem since regression predictions are unbounded. Your actual scores are strictly bounded between zero and one which makes it more similar to predicting a probability.
To get the best predictions, it would best to apply a sigmoid to the final output node activation to bound predictions between zero and one.
For a scoring, cross-entropy would work well as a loss function:
$$H(p,q) = - \sum_{i}p_i logq_i$$
The true label for a datapoint would be modeled as $p_i$ and the predicted value of the current model would be modeled as $q_i$.


Tensorflow dataset batching for complex data I tried to follow the example in this link:

https://www.tensorflow.org/programmers_guide/datasets

but I am totally lost about how to run the session. I understand the first argument is the operations to run, and feed_dict is the placeholders (my understanding is the batches of the training or test dataset),

So, here is my code:

batch_size = 100
handle_mix = tf.placeholder(tf.float64, shape=[])
handle_src0 = tf.placeholder(tf.float64, shape=[])
handle_src1 = tf.placeholder(tf.float64, shape=[])
handle_src2 = tf.placeholder(tf.float64, shape=[])
handle_src3 = tf.placeholder(tf.float64, shape=[])

I create the dataset from mp4 tracks and stems, reading mixture and sources magnitudes, and pad them to be suitable to batching

dataset = tf.data.Dataset.from_tensor_slices(
    {"x_mixed":padded_lbl, "y_src0": padded_src[0], "y_src1":   padded_src[1],"y_src2": padded_src[1], "y_src3": padded_src[1]})

dataset = dataset.shuffle(1000).repeat().batch(batch_size)

iterator = tf.data.Iterator.from_structure(dataset.output_types, dataset.output_shapes)

from the example I should do:

next_element = iterator.get_next()

training_init_op = iterator.make_initializer(dataset)
for _ in range(20):
    # Initialize an iterator over the training dataset.
    sess.run(training_init_op)
    for _ in range(100):
        sess.run(next_element)

However, I have a loss, summaries, and optimiser operations and need to feed the data as batches, following another example as:

l, _, summary = sess.run([loss_fn, optimizer, summary_op], feed_dict={handle_mix: batch_mix, handle_src0: batch_src0,
                             handle_src1: batch_src1, handle_src2: batch_src2, handle_src3: batch_src3})

So I thought something like:

batch_mix, batch_src0, batch_src1, batch_src2, batch_src3 = data.train.next_batch(batch_size)

or maybe a separate run to fetch the batches first, then run the optimisation as above, such as:

batch_mix, batch_src0, batch_src1, batch_src2, batch_src3 = sess.run(next_element)
l, _, summary = sess.run([loss_fn, optimizer, summary_op], feed_dict={handle_mix: batch_mix, handle_src0: batch_src0, handle_src1: batch_src1, handle_src2: batch_src2, handle_src3: batch_src3})

This last attempt, returned string names of the batches as created in the tf.data.Dataset.from_tensor_slices ("x_mixed", "y_src0", ... etc) and failed to cast to tf.float64 placeholders in the session.

Can you please let me know how to create this dataset, there might be an error in the structure from tensor slices in the first place, then how to batch them,

thank you very much,

Manal

From what I can understand from your code, it seems like you need to use the initializable iterator. Here is why:

Your are creating a dataset from a placeholder.

Here is my solution:

batch_size = 100
handle_mix = tf.placeholder(tf.float64, shape=[])
handle_src0 = tf.placeholder(tf.float64, shape=[])
handle_src1 = tf.placeholder(tf.float64, shape=[])
handle_src2 = tf.placeholder(tf.float64, shape=[])
handle_src3 = tf.placeholder(tf.float64, shape=[])

PASS A TUPLE TO  .from_tensor_slices method of the tf.data.Dataset class

dataset = tf.data.Dataset.from_tensor_slices((handle_mix, handle_src0,
                                               handle_src1, handle_src2,
                                               handle_src3))

dataset = dataset.shuffle(1000).repeat().batch(batch_size)

iter = dataset.make_initializable_iterator()

# unpack five values since dataset was created from five placeholders
a, b, c, d, e = iter.get_next()

create a session object and initialize the iter making sure to pass "your data" to the placeholders. 'your data' can be numpy arrays

sess = tf.Session()
sess.run(iter.initializer, feed_dict={handle_mix:'your data',
         handle_src0:'your data',handle_src1:'your data',
         handle_src2:'your data',handle_src3:'your data'})

run the following to print the contents from the iterator

print "%r, %r, %r, %r, %r" % sess.run([a,b,c,d,e])

relevant details or clarification are available from the official TensorFlow documentation on how to use the dataset API.


How many video streams can single GPU handle for object detection I need to detect objects from multiple video streams at realtime (or close to it, like 10 FPS).
How many GPUs do I need to detect objects using YOLOv3 or MobileNet for, say, 10 video streams?
Is it possible to use CPU or something else?
I don't need an exact number. I just need to understand scalability perspective and costs per single stream.

Since batch training is nothing else than computing multiple samples at once, you could test what the maximum batch size your GPU can handle for the model and that is how much you could calculate at once.
With regards to FPS it really depends on what GPU you have and how big the model is. Just try it out.


Machine learning algorithms for time series analysis I am wondering which ML algoirthms (supervised) are commonly used for TS analysis.
Which ones have used and found to be succesful for your projects?

It mainly depends on the task you are looking to accomplish with your time series data. Is it a regression or a classification task. In general, Random Forests are known to be easy to use and pretty useful. So I think it's a good way to start. Of course, dimensionality reduction techniques and feature extractors like PCA, kernel PCA, t-SNE and many others are very useful if you want to get better results. Neural networks are also useful, depending on the task you can try both recurrent (LSTM) or convolutional NN.


How many features can we input for a SVM to classify? I am new to SVM classifiers. I read on the internet that SVM are binary classifiers and also many SVMs, as described in research papers, only take 2 features as the input.

(e.g. https://scikit-learn.org/stable/auto_examples/svm/plot_iris.html)

My question is, does it have to be 2 input features? Can we use more than 2? If so, how do we write this code in python?

There is no restriction on the number of features.

The syntax is exactly the same. For your tutorial, rather than

X = iris.data[:, :2]

That takes only $2$ columns, just change it to a bigger number if those columsn area in your dataframe. It is a common practice that the last column is the $y$ values of which case just to remove that column, just use $-1$.

For some online tutorials, sometimes we use $2$ features for the convenience of visualization only.

However, note that we do not want to use too many features as we want to build a model can be generalized. A simple model that works is better than a complicated model.


Why use sum and not average for sentiment analysis? In Sentiment Analysis, I've seen the sentiment of an entire string of words (tweets, paragraphs, sentences, etc.) computed by summing the sentiment of all of the terms in the string.

I know there is a monotonic relationship between summing and averaging.  So, if sentences are only ever compared to other sentences, then of course it wouldn't make a difference.

So, why is it that the average is not used instead of the sum?  Especially if there are cases where strings could contain only one word, or many words, it seems to me that an average sentiment would be the way to go.  Is that not true?

Consider the following two sentences:

My awesome girlfriend bought me a delicious popsicle at the store.
0--1-------0----------0------0--0-1---------0--------0--0---0-----:2:11

My awesome girlfriend, Joyce, drove to the grocery store to buy me a delicious Dole popsicle.
0---1------0-----------0------0-----0--0---0-------0-----0--0---0--0-1---------0----0-------:2:16

See how you can sum the values and get 2 for both, but average the sentiments and get 2/11 vs. 2/16.  There are cases where people have seen better results measuring the total number of good sentiment words rather than letting zero value words cloud the picture.  In this case, I would argue that the sentences have the same sentiment and averaging artificially pulls down the sentiment of the second sentence.  In certain methods like TFIDF, the word vectors are normalized against are relevant corpus, so this may change things.  In other methods, the stop words may have already been removed, so this has less effect.

In twitter examples, users are limited to 140 characters, so one might consider this to be an inherent denominator which makes averaging really averages of averages.  Its very dependent on the specific application and methodology.

Without a more specific reference, it is difficult to be more specific in my answer.

Hope this helps!


Text generation using Tensor Factorization Text generation is well studied using Markov chains or NNs, but I am not aware of any works to word sequence prediction in terms of subspace learning.

Treating phrases or sentences as temporal data such as time series, it is possible to represent word sequences as a tensor $T = WS \times W \times K$, where $WS$ is the set of word sequences present in the corpus, $W$ represents the set of segmented words, and $K$ is the maximum length of observed sequences
 For instance, for a phrase, ws = word sequence prediction, then $T(ws, ``sequence", 2) = 1$

For an incomplete tensor, where entries s.t. prediction are missing, the reconstructed tensor after decomposition can then be used to generate texts, in terms of the observed word space.

My questions are as follows:

1) Is there any works using tensor factorization or factorization machines for word sequence generation?

2) How subspace learning models differ from those generative models, such as Recurrent Neural Networks or Belief Networks? What are the downsides of using subspace methods as compared to other established methods?

2) How to establish the threshold for the length of the predicted sequence? For example, can one look at the $WS_r \times K_r$ space, and use cross-validation to find the threshold for each word sequences?

Any pointers or answers to any of the above questions is highly appreciated.

Tensor Factorization would not work for text generation as a stand-alone technique. There is no way for the decomposition to model long-term dependencies in language. Without modeling long-term language dependencies, its results would be similar to low-order Markov chains.

Tensor Factorization could be used as another signal in a larger natural language generation system, for example improving word-embeddings.


What is the highest accuracy for classifying cats and dogs from CIFAR-10? Resnet, DenseNet, and other deep learning algorithms achieve average accuracies of 95% or higher on CIFAR-10 images. However, when it comes to similar images such as cats and dogs they don't do as well. I am curious to know which network has the highest cat vs dog accuracy and what it is.

I am aware of Cat vs Dog competition on Kaggle and such, but none that I know of are on CIFAR-10.

Google's AutoML for large scale image classification has come up with    NASnet which now by far produces the best results(96.2% accuracy    on the top5) on ImageNet image classification (Nasnet implementation).

Imagenet has both cats and dogs as pre-trained classes but they are further divided into many sub categories of cats and dogs.


"Change the features of a CNN into a grid to fed into RNN Encoder?" What is meant by that? So in the paper for OCR pr LaTex formula extraction from image What You Get Is What You See: A Visual Markup Decompiler, they pass the features of the CNN into RNN Encoder. But there is problem that rather than passing the features directly, they have proposed a solution to change it into the grid.
Extract the features from the CNN and then arrange those extracted features in a grid to pass into an RNN encoder. This is the exact language they have used.
What is meant by that? Theoratically speaking, if I have an CNN without any Dense/Fully Connected layer and produces an output of [batch,m*n*C], then how can I change it in the form of a grid??  Please see the picture below. So after getting the output from the CNN, they have chnged it somehow before passing it to RNN. What is the method that one can use to get this transformation?

So if I have to pass something to keras.layers.RNN()(that_desired_grid_format), what should be this grid format and how can I change it?

It seems they use a shared RNN which process each row sequentially on the sequence of concatenated channels of individual pixels. From the paper

Implementation with channels last
Let the output of the ConvNet be of size (batch_size, height, width, channels). The RNN expects an input of size (batch_size, sequence_length, input_size)`. So you have to reshape it with the following correspondence.
batch_size*height -> batch_size
channels -> input_size
width -> sequence_length

And process each row (along height dimension) with the same RNN and concatenate the result.
To do that, we simply reshape to merge the batch and height axis into one dimension so that our RNN will process columns independantly.
rnn_input = keras.layers.Reshape((batch_size*height, width, channels))(convnet_output)
rnn_output = keras.layers.RNN(hidden_dim, return_sequences=True)(rnn_input)

rnn_output will have shape (batch_size*height, width, hidden_dim). You can then combine this tensor into a context vector using a dense layer with tanh activation, as it is written in the paper.
The paper also use trainable initial state for the RNN, you might be interested in this library to implement it.
Implementation with channels first
If you set your Conv2D layer with "channels_first", the output convnet_output will be of size (batch_size, channels, height, width). Therefore you need first to permute the dimensions before reshaping.
convnet_output = keras.layers.Permute((0, 2, 3, 1))(convnet_output)

After this step, convnet_output has dimension (batch_size, height, width, channels). You can then proceed as previously, reshaping and feeding to the RNN.
rnn_input = keras.layers.Reshape((batch_size*height, width, channels))(convnet_output)
rnn_output = keras.layers.RNN(hidden_dim, return_sequences=True)(rnn_input)


Extracting tokens from a document: applying Deep Learning or Classification? I have a legal document from Law. That document is 4-pages of evidence from the plaintiff. I want to identify the Dates, Addresses and Financial transactions in that document.

Can I apply deep learning, the data with me is very small, on just one 4-page document, or should I apply Text Classification to solve my problem?

For your case, it might be easier to use text classification or regular expressions.


How to visualize convolutional network focus on autoencoder? I'm trying to create some cool (and at least somewhat meaningful) visualizations of my CNN that I am using to autoencode images, as I use the encoded middle layer to do similarity comparisons.
I.e. I want a view of the input image but with some sort of "heat map" or something overlayed that shows what the CNN is "focusing on", if that makes sense. Some sort of gradient magnitude perhaps, I saw this on a different CNN project, e.g. analogous to the examples on this site.
How could I go about doing this for my autoencoder? Network is coded in Python with Keras, and I've reproduced the code below:
img = Input(shape=(IMG_W, IMG_H, IMG_C), name='input_img_')
    network = Convolution2D(CONV_0, 3, 3, activation='elu', border_mode='same', name='conv_0_e')(img)
    network = MaxPooling2D((2, 2), border_mode='same')(network)
    network = Convolution2D(CONV_1, 3, 3, activation='elu', border_mode='same', name='conv_1_e')(network)
    network = MaxPooling2D((2, 2), border_mode='same')(network)
    network = Convolution2D(CONV_2, 3, 3, activation='elu', border_mode='same', name='conv_2_e')(network)
    network = MaxPooling2D((2, 2), border_mode='same')(network)
    network = Convolution2D(CONV_3, 3, 3, activation='elu', border_mode='same', name='conv_3_e')(network)
    encoded = MaxPooling2D((2, 2), border_mode='same', name='encoded')(network)

    network = Convolution2D(CONV_1, 3, 3, activation='elu', border_mode='same', name='conv_0_d')(encoded)
    network = UpSampling2D((2, 2))(network)
    network = Convolution2D(CONV_1, 3, 3, activation='elu', border_mode='same', name='conv_1_d')(network)
    network = UpSampling2D((2, 2))(network)
    network = Convolution2D(CONV_2, 3, 3, activation='elu', border_mode='same', name='conv_2_d')(network)
    network = UpSampling2D((2, 2))(network)
    network = Convolution2D(IMG_C, 3, 3, activation='elu', border_mode='same', name='conv_decoded')(network)
    decoded = UpSampling2D((2, 2))(network)

model_inputs = img
    model_outputs = decoded
    model = Model(input=model_inputs, output=model_outputs)

    model.compile(loss='binary_crossentropy', optimizer='adadelta')

This question is a little more open-ended, I don't think there's a "right answer" per se, but I'm very open to creative ideas of how I could visualize what the network is doing. Hate to put it this way, but I'm looking for something that I would show a layman and they'd be like "wow look at that AI go" lol. But seriously that's what I'm looking for here. Maybe a collaged image of feature maps? Let me know what you think, and I'd really appreciate any sample code!

You can visualise the activation maps of the various layers, to show how a deep CNN decomposes your input into more and more abstract building blocks. There is a good discussion on Cross Validated SE here. This explains the main idea and intuition as to how CNNs work and normally gets layman motivated to listen and learn more  :-)

There is another great idea names saliency maps - [Simonyan et al.]. As well as reading that paper, I suggest watching Lecture 12 of CS231n (slides here). The general idea of 'how to do this' is, I believe, can really be applied to most deep neural networks.

Here is a sample from the linked paper:

The steps are something like this:

take a trained network architecture
choose the layer that you are interested in (be it the final layer or an intermediate layer
set all weights in layers prior to your chosen layer to zero
perform backpropagation rom your selected layer back to your input

Now you will have a set of weights that match the dimensions of your input, but the values will be those of the weights at your selected layer, simply extrapolated backwards onto your input.

You can then use these weights with an input image, perhaps simply add these value to one or all of the colour channels, and you will have an image which has certain regions highlighted. The highlighted regions signify where the corresponding neurons of your selected layer are activated - i.e. what they focus on to make their contribution towards the final prediction.

Yet another approach is to systematically occlude (cover up) parts of your images, and track how the predictions fluctuate. The intuition is that, when you cover up a certain part of an image, and the prediction becomes terrible, you know your network really looks for that part, which you just covered up. The first time this was really published (as far as I can find) was in the 2013 paper from Zeiler and Fergus.

In the image below (from that linked paper), you can see how the grey square was shifted over and image to create a final heatmap, which highlights the critical portions of the image to getting the correct image classification.


Gradient descent with infinite gradient value Given a function $f(x)$ and $\frac{\partial f(x)}{\partial x_i}=\frac{f^2(x1,...,x_i+\pi/2,...,x_n)-f^2(x1,...,x_i-\pi/2,...,x_n)}{f(x)}$. When $f(x)\to0$, $\frac{\partial f(x)}{\partial x_i}$ could be infinitely large. ($f^2(x1,...,x_i+\pi/2,...,x_n)-f^2(x1,...,x_i-\pi/2,...,x_n)$ is always non-zero)

I have very little experience in deal with this situation in gradient descent process...In my code, $f(x)$ is in continuous domain but for purpose to simulate some real world process, $f(x)$ is sampled to be discrete and would return values uniformly distributed over $[0,1]$. Assume discrete $f(x)$ has $N$ identity values, at the beginning there is a training set of size $M$ ($M$ is very large), $\{x_i, f(x_i)=\frac{k_i}{N}\}_{i=1..M} (k_i \in 1, 2, ..., N)$.

I found that setting $1/f(x)$ to some value like $0.01$ when $f(x)=0$ would reach the optimizim easily but slightly slower than ideal process, while set to much smaller value like $0.00001$ would let $f(x)=0$ have a great impact on the process and failed to form a descent curve.

Is the method replacing infinitely large values to some large but finite values correct? Or there are any better ways to deal with the infinite gradient problem?

Thanks in advance!

Is the method replacing infinitely large values to some large but
  finite values correct?

Yes. For example, the same problem happens for the logarithm in cross-entropy loss function, i.e. $p_i \text{log}(p'_i)$ when $p'_i \rightarrow 0$. This is avoided by replacing $\text{log}(x)$ with $\hat{\text{log}}(x) = \text{log}(x+\epsilon)$ for some small $\epsilon$.

Similarly, you are changing $f(x)$ in the denominator to $\hat{f}(x) = max(\epsilon, f(x))$.

However, I would suggest $\hat{f}(x) = f(x) + \epsilon$ instead of a cut-off threshold. This way, the difference in $f(x_1) < f(x_2) < \epsilon$ would not be ignored unlike the max cut-off.


Does stronger regularization always improve performance on testing set? I am using the Sklearn logistic regression function to do a binary classification task on texts.
I did the task using three different inputs: Bag-Of-Words, TF-IDF, Doc2vec embeddings.
The question is that for the Bag-Of-Words, stronger regularization improves the performance on testing set.
However, for TF-IDF and Doc2vec embedding, stronger regularization doesn't improve the performance on testing set. Actually the performance downgrades with stronger regularization.
Why this is the case?

Title question
The answer to the title question is a pretty clear-cut "no." For any fixed dataset, taking sufficiently strong regularization will make the linear model essentially constant, presumably not the best model.  Like most other things, there's a bias-variance tradeoff: increasing regularization weakens the model's ability to learn the training data, which to a point will tend to prevent overfitting and improve test performance.
Your more specific context
Word embeddings are substantially different from the other two, so the optimal regularization strength is probably mostly unrelated.
Between bow and tfidf, if nothing else the scale of the features will be larger in bow, so if you're not scaling, the penalties will be larger and the optimal regularization strength is likely to be smaller.


How to implement LSTM with Spark? I would like to build an LSTM network for text classification with PySpark, but I don't find any library or function about it.

Is it possible to implement this kind of network in Spark?

Apache Spark currently has no Deep Learning libraries. However, you can run TensorFlow models on clusters. According to this article "The TensorFlow library can be installed on Spark clusters as a regular Python library".

Additionally, I just found the SparkFlow module, that should be meant to interface Spark and TensorFlow. The GitHub repo is here.


Minimum number of features for Naïve Bayes model I keep on reading that Naive Bayes needs fewer features than many other ML algorithms. But what's the minimum number of features you actually need to get good results (90% accuracy) with a Naive Bayes model? I know there is no objective answer to this -- it depends on your exact features and what in particular you are trying to learn -- but I'm looking for a numerical ballpark answer to this.
I'm asking because I have a dataset with around 280 features and want to understand if this is way too few features to use with Naive Bayes. (I tried running Naive Bayes on my dataset and although I got 86% accuracy, I cannot trust this number as my data is imbalanced and I believe this may be responsible for the high accuracy. I am currently trying to fix this problem.)
In case it's relevant: the exact problem I'm working on is generating time tags for Wikipedia articles. Many times the infobox of a Wikipedia article contains a date. However, many times this date appears in the text of the article but is missing from the infobox. I want to use Naive Bayes to identify which date from all the dates we find in the article's text we should place in the infobox. Every time I find a sentence with a date in it I turn it into a feature vector -- listing what number paragraph I found this in, how many times this particular date appears in the article, etc. I've limited myself to a small subset of Wikipedia articles -- just apple articles -- and as a result, I only have 280 or so features. Any idea if this is enough data?
Thanks!

The number of features to get a minimum accuracy is an empirical question, that will depend on the specific problem. There are problems where a single feature will result in "good enough" performance, there are other problems where no number of features is sufficient.
Naive Bayes does provide the most predictive features for each class. So the increase in performance by adding features can be calculated.
For your specific problem, more features (more text signals) and more observations (more of Wikipedia) would increase accuracy.


Understanding of Convolutional Neural Network (CNN) I am a beginner in ML and I would like to learn CNN with Math behind. If you suggest any good blog or documentation which help me get depth knowledge about CNN.

Following blog posts would be helpful for Visualization and intuitive understanding:

Understanding Convolutions - To understand the basic operation involved, Convolution in 1-D setting
Conv Nets: A Modular Perspective - To understand its usage in construction of CNN ( 2-D setting )


How can I tell whether my Random-Forest model is overfitting? I was trying to generate predictions for Iris species using the UCI Machine Learning Iris dataset. I used a RandomForestClassifier with GridSearchCV and calculated the mean absolute error. However, upon generating predictions with the testing set it gave me a suspicious MAE of 0.000000, and a score of 1.0. Is it likely that the model is overfit? If so, why did this happen, and how do I prevent this?

iris = pd.read_csv('/iris/Iris.csv')

le = LabelEncoder()
i2 = iris.copy()
labelled_iris_df = pd.DataFrame(le.fit_transform(i2.Species)).rename(columns={0:'Species_Encoded'})

i3 = i2.drop('Species', axis=1)
i3 = pd.concat([i3, labelled_iris_df], axis=1) #Encoded dataset

y = i3.Species_Encoded
X = i3.drop('Species_Encoded', axis=1)

X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.75, random_state=42)

params = {
    'n_estimators':[50,100,150,200],
    'max_depth':[3,4,5,6]
}

rfc = RandomForestClassifier(random_state=42)
gc = GridSearchCV(rfc, params, cv=3).fit(X,y)
print (gc.best_params_) #n_estimators: 50, max_depth:4

model = RandomForestClassifier(n_estimators=50, max_depth=4, random_state=42)
model.fit(X_train,y_train)
preds = model.predict(X_test)
mae = mean_absolute_error(y_test, preds)
sc = model.score(X_test, y_test)
print("mae: %f \t\t score: %f" % (mae, sc)) #Prints mae: 0.000000   score: 1.0

I'm a beginner to Machine Learning so please feel free to comment on bad sections of this code and how I can improve them.

I think you are probably overfitting.

The issue is that while you have performed a train/test split, you are selecting your hyperparameters based on the whole dataset! This way you are feeding information to the model, about the test set, through your hyperparameter selection. To be honest I haven't seen this in such small grid searches, but to be sure you aren't overfitting you need to change:

gc = GridSearchCV(rfc, params, cv=3).fit(X, y)

to

gc = GridSearchCV(rfc, params, cv=3).fit(X_train, y_train)

Another detail I'd like to point out is that MAE isn't the proper metric for classification; it is more suited for regression problems. For example is an example belongs to class "1", predicting the class "3" isn't twice as worse than predicting class "2". They are both simply misclassifications.

If after these you are still getting high performance, then I'd say its safe to assume you aren't overfitting. If you are I'd like to point you to this answer.


How to use ColumnTransformer and FunctionTransformer to apply the same function to many columns, but separately? I want to apply pd.cut as a transformer in a pipeline, like this:

numerical_preprocessing = Pipeline([
            ('cut_into_bins', FunctionTransformer(pd.cut, kw_args={'bins': [10, 100, 1000]})
             )]

However, I get an error: ValueError("Input array must be 1 dimensional")
I could just write the same function over and over again with each column separately, but that looks like a terrible coding practice. Any thoughs on this?

I solved the problem by creating a wrapper around pd.cut, which then applies pd.cut using the apply method of DataFrame:

if isinstance(x, pd.Series):
    return pd.cut(x, bins_final, labels=labels, **kwargs)
elif isinstance(x, pd.DataFrame):
    return x.apply(pd.cut, args=(bins_final,), axis=0, labels=labels, **kwargs)


Can Expectation Maximization estimate truth and confusion matrix from multiple noisy sources? Suppose we have $m$ sources, each of which noisily observe the same set of $n$ independent events from the outcome set $\{A,B,C\}$. Each source has a confusion matrix, for example for source $i$:

$$C_i = \begin{bmatrix} 0.98 & 0.01 & 0.07 \\ 0.01 & 0.97 & 0.00 \\0.01 & 0.02  & 0.93\end{bmatrix} $$

where each column relates to the truth, and each row relates to the observation. Eg. if the true event is $B$ then source $i$ will get it right 97% of the time, and observe $A$ 1% of the time and $C$ 2% of the time. We can assume the diagonal elements are > 95%

Given a sequence of $n$ events, where each event $j$ was observed by source $i$  as $O_{i,j}$, it is trivial to estimate the pmf of the truth $T_j$ by solving $P(T_j | O_{1,j},\dots,O_{m,j})$ using Bayesian formula (given some reasonable priors about the probabilities of the events themselves, say, uniform).

However suppose we didn't have the confusion matrices, nor the ground truth, and instead wanted to estimate them both. One algorithm is:

Start with some reasonable confusion matrix for each source $C_{i,0}$
Fixing the confusion matrices $C_{i,k}$, estimate the most likely truths $T_{j,k}$ using Bayes formula
Fixing truths $T_{j,k}$, estimate new confusion matrices $C_{i,k+1}$ based on how often each source got it "wrong" (allegedly)
Repeat the last two steps incrementing $k$ until convergence

This seems like the EM algorithm but I don't know how to show this formally. (No this is not homework.)

1) Is this EM, or some other standard algorithm in data fusion?

2) Does it have convergence guarantees?

3) Does it have any guarantees about the quality of the solution and how well the final confusion matrices will approximate the true confusion matrices?

4) Are there issues about the number of parameters being estimated vs. the number of samples? Eg. it seems there are $n + 6m$ parameters being estimated - the $n$ truths and the $6m$ elements across all confusion matrices (last cell of each column is determined by the others).

EDIT

These two papers describes exactly the problem and how to use EM to solve it:

Maximum Likelihood Estimation of Observer Error-Rates Using the EM Algorithm
http://crowdsourcing-class.org/readings/downloads/ml/EM.pdf

LEARNING FROM NOISY SINGLY-LABELED DATA
https://openreview.net/pdf?id=H1sUHgb0Z

So the answers are:

1) Properly interpreted yes this is EM

2) EM in general converges to local optimum

3) Not really. In this problem though since the sources are 97%+ accurate, I expect the estimates to be pretty good

4) I don't think this is an issue - this is a "non-parametric" EM algorithm as the confusion matrices are not parameterized in anyway way. The sample sizes I deal with are in the 100000's+ so shouldn't be an issue

I don't have a full answer to your question, but I wanted to help (and would love to know a complete answer). In a simpler case when you have one source $m=i=1$, it seems to me you are describing a scenario that resembles a hidden Markov model which uses a EM algorithm-based solution (Baum–Welch algorithm) – that is if you make the further hypothesis that a Markov property is satisfied. Because convergence properties of the stationary distribution of Markov chains are exponentially fast the algorithm convergence may be limited to the implemented algorithm. I hope that info helps point you in the right direction.


ANN Classifier for extracted discrete image features I have a features extraction algorithm that works well to extract features from images. I want to develop an ANN to classify those images based on those features. I have extracted features in a csv file as columns and rows.
I know we can use CNN to extract and classify images but my scerio is only the second half (features classification).
Thanks

If you have already extracted a feature vector $X$ from your images. Then indeed you can use an artificial neural network (ANN) to classify these given you have labelled instances.
You can do this in python using multiple different libraries such as scikit-learn, Keras, or tensorflow quite easily. Scikit-learn is the easiest to implement but the least flexible. Keras allows for more complex ANNs, and tensorflow allows for every kind of custom structure you can imagine.
Scikit-learn
This is the easiest way to implement a single-layer ANN.
from sklearn.neural_network import MLPClassifier
X = [[0., 0.], [1., 1.]]
y = [0, 1]
clf = MLPClassifier(solver='adam', alpha=1e-5,
                    hidden_layer_sizes=(32,), random_state=1)
clf.fit(X, y)
clf.score(X, y)

https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html#sklearn.neural_network.MLPClassifier
Keras
First we build a model we want to use. This is an example of a simple single-layered neural network. $k$ is the number of features per instance and $num_classes$ is the number of output classes you are expecting.
from __future__ import print_function
import keras
from keras.models import Sequential
from keras.layers import Dense, Flatten
from keras.models import model_from_json
from keras import backend as K

input_shape  = (k,)

model = Sequential()
model.add(Dense(32, activation='tanh',
             input_shape=input_shape))
model.add(Dense(num_classes, activation='linear'))

model.compile(loss=keras.losses.mean_squared_error,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

You can see the summary of the model by using
model.summary()

Then we can train this model using
batch_size = 128
epochs = 10
model.fit(x_train, y_train_binary,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test_binary))

You will need to tune all the hyper-parameters in this code sample to better suit your data.
https://keras.io/api/models/sequential/


Multi Class Classification on large dataset with over 600 classes I'm trying to train a text data for multi class classification which comprises of 1 Million rows. After cleaning the data, I'm using a sparse matrix of Word2Vec features (Feature size is 300)

The data which I have is 1. ID 2. Dictionary 3. Label

Dictionary size varies from 10 keys to 900 keys

Steps I followed on Dictionary columns are:

Converted Dictionary to String
Getting only good tokens from the string
Removing Stopwords
Stemming of words
Word2Vec Model training with feature size 300.
Word2Vec feaure generation
Label Encoding
Converting Feature Vectors to Numpy Array
Converting Numpy Array to Sparse Matrix of (1114220, 300)
Tried OneVsRest model for training
onevsrest = OneVsRestClassifier(SVC(probability=True) , n_jobs=-1)

onevsrest.fit(sparse_matrix , df.labels)

I was running this model for nearly two days and it got killed automatically

I also tried Logistic Regression

lr = LogisticRegression(penalty ='l1' , C=1 ,dual=False , solver='saga' , n_jobs=-1)

lr.fit(sparse_matrix , df.labels)

Still I faced the same issue ( Model keeps training for 2 days and gets killed)

Am I doing something wrong? Or is there any better way to do this type of problem?

Generally I have seen SVM perform well with Text Classification tasks. Why don't you first try with taking TF-IDF in place of Word2vec.
Let's say you have a sentence D1 with terms T1,T2 and T3. Represent it as TF-IDF first and classify. This link might help https://www.analyticsvidhya.com/blog/2018/04/a-comprehensive-guide-to-understand-and-implement-text-classification-in-python/
Then you will know if the problem is with memory issues or your word2vec approach.


RF regressor for probabilites I am using sklearn multioutput RF regressor to learn statistics in my data. So my target contains several probabilities for the different features, and the sum of all these probabilities is one as they are fractions of how often the feature occurs.
The RF actually learns this property even though I have not enforced anywhere that the outputs should sum to one. I have also added a constant to my targets and the RF then learn that the outputs should sum to one plus that constant, so it is not some normalization.
I'm pretty sure I know how an RF regressor works but I cant explain how it can learn such metafeatures of my data. I would have expected the sum of my output to be somewhere around 1, not always exactly one.
Any ideas?

This is indeed expected behavior, because of the way tree models handle multioutput problems.  The nodes contain some number of samples, and the score for each output is the average of those samples' corresponding output.  Since averaging commutes with sums, the property of summing to 1 is preserved.  I'm not sure if this will help, but in symbols:
$$ \sum_{\text{output }i} p_i = \sum_{\text{output }i} \left(\operatorname*{avg}_{\text{sample }j}(p_i^j)\right) = \operatorname*{avg}_j \left(\sum_{\text{output }i} p_i^j\right) = \operatorname*{avg}_j 1 = 1.$$
Then for the entire forest, you're just applying another averaging, and so the property is again maintained.


Shall I use ordinal encoding or One-Hot-Encoding when using DBSCAN for content clustering on websites? I want to cluster the preparation steps on cooking recipes websites in one cluster so I can distinguish them from the rest of the website.
To achieve this I extracted for each text node of the website the DOM path (e.g. body->div->div->table->tr ....) and did a One-Hot-Encoding before I executed the DBSCAN clustering algorithm.
My hope was, that the DBSCAN algorithm recognizes also not only 100% identical DOM-paths as 1 common cluster, because sometimes one preparation step is e.g. in a   tag and the others are not. But even though I tried a lot to vary epsilon and MinPoints paramters, it does not recognice then all as one cluster.
My question:
Is One-Hot-encoding maybe the wrong way, because a dom path is not really 100% categorical but maybe a kind of ordinal? Because the more common DOM path elements two DOM-paths have the more likely is, that they are building one common cluster.

With the information you provide, I suggest you can use both and test based upon a metric like Siluhette Score or Davis Bouldin index to measure the quality of your clusters when using either of both preprocessing techniques. In this way, you will have an objective metric to compare between the two preprocessing techniques and keep the one that maximizes the chosen metric since it means you are taking the preprocessing that generates better-formed clusters


Denoising Autoenoders with Variable Length Input I'm working on a problem with data from a continuous real-valued signal. The goal is to use ML to smooth the signal based off of past data. To accomplish this, the signal is windowed into a period that's meaningful within the domain. The problem is that this period is highly variable in length.

I've reviewed this question and this question and neither solve the problem, they are more about how to deal with missing values.

Seeing as denoising autoencoders are based off of matrix multiplication, this presents a serious problem. What is the standard approach in such a situation? Should I define an arbitrary (large) window size, and expand windows that are too small (and vice versa)? Or is there a better approach for variable length inputs?

Recurrent Neural Networks can deal with variable length data. You might want to have a look at:

Andrej Karpathy: The Unreasonable Effectiveness of Recurrent Neural Networks.
Christopher Olah: Understanding LSTM Networks.
Hochreiter, Schmidthuber: Long short-term memory.

Another idea (which I have not tested so far and  just came to my mind) is using a histogram approach: You could probably make fixed-size windows, get the data from those windows and make it discrete (e.g. vector quantization, k-means). After that, you can make a histogram of how often those vectors appeared.

You could also use HMMs for recognition of variable length data.

Transformations (e.g. Fourier transform from the time domain in the frequency domain) might also come in handy.


How can I turn user audit data into behavior clusters? I have logs of user activity on my system. This is a CMS system.

The logs consist of:

User ID
Action Performed (one of 110 possible actions - things like page edited, page read, login, etc.)
Site action was performed on (not relevant for all possible actions)
Object action was performed on (document name or document id - not relevant for all possible actions)
Date and time that the action was performed

I would like to use this data to cluster users into groups with similar behavior, so that I can then survey the groups to find out who they are and then target them with training interventions, newsletters etc.

I need help with converting the above list of things into clusters.

What techniques can I use to generate a matrix from the above data that I can feed into a K-Means cluster algorithm?

I tried counting action by user and using that to feed a cluster algorithm but the result was useless. I also tried treating the site and document ids as text, feature hashing that and feeding it into a K-Means cluster model, also got useless data out.

Scaling

Standardize you data by $(x - mean(x)) / std(x)$

Most K-mean implementation by default uses Euclidean distance which assumes the equal importance of all features. This requires proper scaling to prevent one action dominates the others.

Dimensionality

K-mean is not robust against the curse of dimensionality (see this post). So as always it good to reduce you dimensionality before feed it to the algorithm.

Do some feature engineering first. For example, login and page load can be group together as a measure of passive engagement, page edit and page created could be considered as a single activate engagement feature.

Also, you can try using some standard dimensionality reduction algorithm like PCA


memory error while converting images into an array I am working on a facial recognition use case. I have 57k jpg images and am converting them into an array. While executing the program, I am getting a  memory error.

The function I am using:

def image_array(l):
    features = []
    for pgm in l:
        pic = image.load_img(pgm, target_size=(224, 224))
        x = image.img_to_array(pic)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        features.append(x)
    npfeatures = np.array(features)
    img_dt = np.rollaxis(npfeatures, 1, 0)
    return img_dt[0]

The input for this function is a list which looks like:

l =['/home/user/image1.jpg','/home/user/image2.jpg','/home/user/image3.jpg'......]

The error that I am getting:

Traceback (most recent call last):
  File "FR.py", line 145, in <module>
    vec_image1 = image_array(final_df['image1'].values.tolist())
  File "FR.py", line 140, in image_array
    npfeatures = np.array(features)
MemoryError

The imports that I used for above function are:

> from keras.preprocessing import image
> from keras.applications.vgg16 import preprocess_input

You can actually compute how much memory it will take to hold 57,000 images in memory (it is a lot!). You are also holding them twice: once in the features list and then trying again in npfeatures. This second part will make a full copy of the entire features list. Hence why it runs out of memory there.

Here are some starting steps that should help you understand the limitations of your approach and perhaps get a working method:

1. You don't need this line: x = np.expand_dims(x, axis=0):

They do that in the Keras documentation, because you need a single image to have an additional dimension for the batch size. You do not need that because you append many images to a list (features), and the length of that list is the same thing, the batch size (the number of images).

2. Do your own scaling

In the case f VGG16 (and 19 I believe), the preprocess_input method simply scales pixel values between -1 and +1. You can probably do this a little more light-weight yourself. You can do the scaling on a numpy array like this:

x = (x / 127.5 - 1)

... and now remove the line with preprocess_input

3. Break it down into smaller pieces:

You can then either feed them directly into you models (if that is possible), or save the npfeatures to disk, one chunk at a time.

4. Try seeing how much memory your machine has while running this script:

Add a simple line after features.append(x), like this:

print("Loaded {} images"format(len(features)))

If you are running this on a Linux machine or Mac OSX, try using a tool like htop in terminal. There is also system monitor... same for Windows. You should be able to see the memory consumption grow until the point your script crashes. Now you know how many images you can do in one cycle - the last printed number before the crash.


What models do Create ML and Turi Create use I'm taking a course on Apple's machine learning technologies.  I just came across this paragraph:

  Turi Create and Create ML are task-specific, rather than
  model-specific. This means that you specify the type of problem you
  want to solve, rather than choosing the type of model you want to use.
  You select the task that matches the type of problem you want to
  solve, then Turi Create analyzes your data and chooses the right model
  for the job.

My question is when you select a task like binary image classification, and Create ML / Turi Create selects an appropriate model for you, what models does it have at its disposal.  Aren't there MANY models in the market that do this?  Did Apple simply pick the one they thought was best?

Thanks!

TuriCreate has several different models implemented. You can either use them specifically or you can use a create method which will analyze your data and pick one of them (not always the same one).

For instance in binary classification (not image) there is support for random forest, decision tree, boosted trees, logistic regression, SVMs and nearest neighbor.

I don't know exactly how it makes the selection, but I imagine it has a lot to do with the dimensions of your data.


How to use Cross Entropy loss in pytorch for binary prediction? In the pytorch docs, it says for cross entropy loss:

  input has to be a Tensor of size (minibatch, C)

Does this mean that for binary (0,1) prediction, the input must be converted into an (N,2) tensor where the second dimension is equal to (1-p)?

So for instance if I predict 0.75 for a class with target 1 (true), would I have to stack two values (0.75; 0.25) on top of each other as input?

In Pytorch you can use cross-entropy loss for a binary classification task. You need to make sure to have two neurons in the final layer of the model. Make sure that you do not add a softmax function.

Use the below for resources:

https://discuss.pytorch.org/t/do-i-need-to-use-softmax-before-nn-crossentropyloss/16739

https://discuss.pytorch.org/t/why-does-crossentropyloss-include-the-softmax-function/4420


What could be the reason of having a lower RMSE than MAE? I used some machine learning algorithms in my dataset and I found that my RMSE goes low than the MAE. What are the most common reasons for that type of typical scenario. Since from my understanding the RMSE is normally higher than the MAE. But if I am wrong is it actually possible to have a lower RMSE and higher MAE? (for example: RMSE: 26, and MAE : 36)

Yes, you are correct that in general $RMSE(x) \geq MAE(x)$ holds (see this answer for a good explanation of the different error measures and this paper for an interesting comparison of the two measures).

Therefore, your case must stem from other sources:

Randomness in models: the models you have applied are non-deterministic, e.g. your random forest randomly sub-samples features. This could either be solved by fixing all random parameters or measure RMSE and MAE on the same trained model:

model.fit(X_train)
y_pred = model.predict(X_test)
rmse = RMSE(y_test, y_pred)
mae = MAE(y_test, y_pred)

instead of

model.fit(X_train)
y_pred = model.predict(X_test)
rmse = RMSE(y_test, y_pred)

model.fit(X_train)
y_pred = model.predict(X_test)
mae = MAE(y_test, y_pred)

This answer shows a way to fix random seeds.

Randomness in data splits: Data splits are another source of randomness. So you either either make the split deterministic or measure both errors on the same split:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.7)
y_pred = model.predict(X_test)
rmse = RMSE(y_test, y_pred)
mae = MAE(y_test, y_pred)

instead of

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.7)
y_pred = model.predict(X_test)
rmse = RMSE(y_test, y_pred)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.7)
y_pred = model.predict(X_test)
mae = MAE(y_test, y_pred)


How to represent a document in test data with the Document-Term Matrix created from the training set? I build a classifier of documents using the vector representation of each document in the training set (i.e a row in the Document-Term Matrix). Now I need to test the model on the test data. But how can I represent a new document with the Document-Term Matrix since some terms might not be included in training data?

The easiest way is to treat all out-of-vocabulary terms as a specific term in your matrix (i.e. "OOV").

So for instance, if my training data contains 3 words: "I", "like", "cake", my document-term matrix would contain 4 items, "I", "like", "cake", and "OOV".


What is the difference between TDNN and CNN? I read about time delay neural network (TDNN) and I am not sure I understood it. From what I read it seems that tdnn works just like  one dimensional convolutional neural network (CNN).
What are the differences between the architectures, if they exist?

They are the same, as far as i can see. The name convolution usually applies to spatial not time dimensions, but that is only convention. Wikipedia also links the two.


Logistic regression vs Random Forest on imbalanced data set I have an imbalanced data set where positives are just 10% of the whole sample. I am using logistic regression and random forest for classification. While comparing the results of these models, I have found that the probability output of logistic regression ranges between [0,1] while that of random forest ranges between [0, 0.6].
I cannot share the data set but my doubt is around the working of these algorithms. How can random forest generate probability less than 0.6?

To have a probability of 1 in a RF, it means that your algorithm can construct a leaf containing only positive sample. Since it doesn't, this means that your features are not explaining the variance of the output or that your algorithm is under-fitted.
I suggest that you try optimize the hyper-parameters of your RF by using cross-validation and use some oversampling to reduce the bias in your dataset.


Is there any paper of object detection that detect not trained class or object? After reading some object detection papers (kinds of R-CNN, YOLO,...) I'm wondering if there is a detector that detects objects about not trained class.

For example my model is not trained to detect buildings, but is it possible to make my model detect building or some vehicles just with pixel data?

Yes thats possible, but you need some kind of adaption. Normally you would use transfer learning to teach your model, that there are more things to detect with the features it already can extract. For that you normally take your model, and just delete the last dense layer, which is for classification. Then you freeze all earlier layers of the CNN and setup a new Dense-layer on the end with the number of classes you want to detect. Giving the network now new examples with ground truth (not that many normally) to adapt onto the new domain. This can work, but also defenitely fail. For example i tried using ResNet-backbones (51,101) trained on ImageNet (a big dataset to find humans, cars and stuff) and tried to use it on microscopy cell segmentation and classification. Yeah it found some boundarys, but couldnt get a precise classification at all.

To sum it up: yes you can transfer learn, or use tasks of the field of "UDA" -> Unsupervised domain adaption, or normal Supervised / semi-supervised domain adaption to teach models new things. Or just to strengthen their knowledge. Detecting things without having seem them ever before is also an updcoming task and is called "zero-shot-learning", often in combination with Graph-Neural-Networks but its quite in the beginning. Bridging the gap between multiple domains is one of the sota questions in ML overall, trying to find object specific features. One step in this direction are cGAN, trying to transform one image into another, by extracting the most important information of both domains, to perfom a precise transformation. But also that, is quite in the beginning , but can work surprisingly well with some effort and loss-function tuning. Some examples:

Transform green to red to make it possible for another CNN to segment the image:

Additional interesting papers on that topic are:

Noisy Student

Unsupervised Pixel–Level Domain Adaptation with GenerativeAdversarial Networks

Transfer learning


How does testing a CNN work after training? i mean that how CNN know the output probability will lead to following class. i mean that if i train the CNN on the Dog and cat picture or data set so at the end of the know that this probability will lead to the Dog or cat...

The last layer of your neural network is a sigmoid function. During training, all the parameters are being learned. Hence, given a new picture, we can compute a value that is between $0$ and $1$ from the sigmoid function (which is used as a model of probability) to let us know the desired probability.

For multi-class classification problem. A softmax layer is used instead.


Model Validation accuracy stuck at 0.65671 Keras I am using conv1d to classify EEG signals, but my val_accuracy stuck at 0.65671. No matter what changes i do, it never go beyond 0.65671.
Here is the architecture

model=Sequential()
model.add(Conv1D(filters=4,kernel_size=5,strides=1,padding='valid',kernel_initializer='RandomUniform',input_shape=X_train.shape[1::]))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Conv1D(filters=6,kernel_size=3,strides=1,padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Conv1D(filters=8,kernel_size=3,strides=1,padding='valid',activation='relu'))
#model.add(Conv1D(filters=24,kernel_size=7,strides=1,padding='same',activation='relu'))

model.add(Flatten())
model.add(Dense(12,activation='relu'))
model.add(Dense(1,activation='sigmoid'))

Shape of training data is (5073,3072,7) and for test data it is (1908,3072,7).

I have tried reducing the number of neurons in each layer, changing activation function, and add more layers. But this upper limit has not changed mostly.

I have tried one hot encoding of binary class, using keras.utils.to_categorical(y_train,num_classes=2) but this issue does not resolve.

I have tried learning rate of 0.0001, but it does not work. I have tried some kernel_initializer and optimizers but nothing help

Results

   Train on 5073 samples, validate on 1908 samples
Epoch 1/8
 - 23s - loss: 0.6865 - acc: 0.5757 - val_loss: 0.6709 - val_acc: 0.6564

Epoch 00001: val_acc improved from -inf to 0.65645, saving model to weights.hdf5
Epoch 2/8
 - 22s - loss: 0.6760 - acc: 0.5837 - val_loss: 0.6569 - val_acc: 0.6567

Epoch 00002: val_acc improved from 0.65645 to 0.65671, saving model to weights.hdf5
Epoch 3/8
 - 21s - loss: 0.6661 - acc: 0.5843 - val_loss: 0.6669 - val_acc: 0.6111

Epoch 00003: val_acc did not improve from 0.65671
Epoch 4/8
 - 21s - loss: 0.6622 - acc: 0.5915 - val_loss: 0.6579 - val_acc: 0.6253

Epoch 00004: val_acc did not improve from 0.65671
Epoch 5/8
 - 22s - loss: 0.6575 - acc: 0.5939 - val_loss: 0.6540 - val_acc: 0.6255

Epoch 00005: val_acc did not improve from 0.65671
Epoch 6/8
 - 21s - loss: 0.6554 - acc: 0.5940 - val_loss: 0.6448 - val_acc: 0.6399

Epoch 00006: val_acc did not improve from 0.65671
Epoch 7/8
 - 21s - loss: 0.6511 - acc: 0.6042 - val_loss: 0.6584 - val_acc: 0.6195

Epoch 00007: val_acc did not improve from 0.65671
Epoch 8/8
 - 21s - loss: 0.6487 - acc: 0.6059 - val_loss: 0.6647 - val_acc: 0.6030

Epoch 00008: val_acc did not impr

ove from 0.65671

I would like to see you data set :)
I am also doing some signal classification.

Unless there is some simple bug in data preprocessing stage: (check what you didn't show here first!)

As correctly pointed to you by @stefanLopez your number of filters is way too low.
Next, filter length is too short to capture anything serious.
Remove batchnorm while testing.
Reduce dropout while testing.
Test with ELU (Exponential Linear Unit) activation.
Last, use more FC layers with more neurons.
Try using glorot (commonly known as Xavier) initializer.

Example model:

model=Sequential()

model.add(Conv1D(filters=24,kernel_size=16,strides=1,padding='valid',activation='elu',kernel_initializer='glorot_normal',input_shape=X_train.shape[1::]))

model.add(Conv1D(filters=16,kernel_size=9,strides=1,padding='same',activation='elu',kernel_initializer='glorot_normal'))
model.add(Dropout(0.1))

model.add(Conv1D(filters=12,kernel_size=9,strides=1,padding='valid',activation='elu',kernel_initializer='glorot_normal'))
model.add(Dropout(0.1))

model.add(Flatten())
model.add(Dense(128,activation='elu'))
model.add(Dropout(0.1))
model.add(Dense(16,activation='elu'))
model.add(Dropout(0.1))
model.add(Dense(1,activation='sigmoid'))

Tell if it helps.


Kernel Trick and Inner Product Preservation I understand that the point of using the kernel trick is to project the problem onto a higher dimensional space, where the problem is linearly separable. In this explanation, https://www.quora.com/What-is-the-kernel-trick, it states that the inner product $\langle x,y \rangle$ will be equal to $\langle \phi(x),\phi(y)\rangle$. Understanding this equality seems to be key to understanding how this trick works.

My question is, how do know that our function $\phi$ will preserve the inner product and what are the conditions for this to happen?

I have tried google searching this and despite many references to the kernel trick, I do not believe that this has been answered anywhere.

It doesn't mean that $\langle x, y \rangle = \langle \phi(x), \phi(y)\rangle$.

Kernel method in general means that for an algorithm that involve $\langle x, y\rangle$, we can replace it with a function $K(x,y)=\langle \phi(x), \phi(y)\rangle$ where computing $K(x,y)$ is easy. It is known that for $K$ that satisfies Mercer's theorem, there is a corresponding $\phi$ which is the map to the higher dimensional space.

We do not need to know $\phi$ explicitly and $\phi$ can be very complicated.


How to categorize unlabelled promotional email data I have unlabelled data of promotional emails. I want to categorize those emails based on the topics like fashion, health & wellness, sports, media, Entertainment, etc. Can anyone let me know any effective method or any pre-trained model which i can directly use to categorize those emails (any similar model like YOLO which we have for object detection).
Any kind of help regards to this would be much appreciable. Thanks!

So the question asks what model can you use to put texts into topic categories in a unsupervised way.
The main model that we use for this sort of task is Latent
Dirichelt Allocation (LDA) (https://programmerbackpack.com/latent-dirichlet-allocation-for-topic-modelling-explained-algorithm-and-python-scikit-learn-implementation/)
Also, there is another method you could try, which is to convert texts into document embedding using a Doc2Vec (https://medium.com/wisio/a-gentle-introduction-to-doc2vec-db3e8c0cce5e) model, which will convert the text into an n dimensional vector. From these document embedding you could use any form of clustering (KNN or Agglomerative Clustering) to cluster the texts into topics.


Deconvolutional Network in Semantic Segmentation I recently came across a paper about doing semantic segmentation using deconvolutional network: Learning Deconvolution Network for Semantic Segmentation.
The basic structure of the network is like this:

The goal is to generate a probability map in the end. I'm having trouble figuring out how to realize the deconvolution layer. In the paper, it says:

The output of an unpooling layer is an enlarged, yet sparse activation map. The deconvolution layers densify the sparse activations obtained by unpooling through convolution-like operations with multiple learned filters. However, contrary to convolutional layers, which connect multiple input activations within a filter window to a single activation, deconvolutional layers associate a single input activation with multiple outputs.
The output of the deconvolutional layer is an enlarged and dense activation map. We crop the boundary of the enlarged activation map to keep the size of the output map identical to the one from the preceding unpooling layer.
The learned filters in deconvolutional layers correspond to bases to reconstruct shape of an input object. Therefore, similar to the convolution network, a hierarchical structure of deconvolutional layers are used to capture different level of shape details. The filters in lower layers tend to capture overall shape of an object while the class-specific fine details are encoded in the filters in higher layers. In this way, the network directly takes class-specific shape information into account for semantic segmentation.

Can anyone explain how the deconvolution works? I'm guessing it's not a simple interpolation.

There are two main functions they undo.

The pooling layers in the convolutional neural network downsample the image by (usually) taking the maximum value within the receptive field.  Each rxr image region is downsampled to a single value.  What this implementation does is store which unit had the maximum activation in each of these pooling steps.  Then, at each "unpooling" layer in the deconvolutional network, they upsample back to a rxr image region, only propagating the activation to the location that produced the original max-pooled value.

Thus, "the output of an unpooling layer is an enlarged, yet sparse activation map."

Convolutional layers learn a filter for each image region that maps from a region of size r x r to a single value, where r is the receptive field size. The point of the deconvolutional layer is to learn the opposite filter.  This filter is a set of weights that projects an rxr input into a space of size sxs, where s is the size of the next convolutional layer.  These filters are learned in the same way that as regular convolutional layers are.

As the mirror image of a deep CNN, the low-level features of the network are really high-level, class-specific features.  Each layer in the network then localizes them, enhancing class-specific features while minimizing noise.


Reframing action recognition as a reinforcement learning problem Given the significant advancements in reinforcement learning, I wanted to know whether it is possible to recast problems such as action recogniton, object tracking, or image classification into reinforcement learning problems.

Given the significant advancements in reinforcement learning

Worth noting that many of the recent advancements are due to improvements in neural networks used as function approximators, and understanding how to integrate them with reinforcement learning (RL) to help solve RL challenges involving vision or other complex non-linear mapping from state to best action.

So at least some of current improvements in RL were due to researchers asking the opposite question "Given the significant advancements in neural networks . . ."

  I wanted to know whether it is possible to recast problems such as action recogniton, object tracking, or image classification into reinforcement learning problems.

Generally, at the top level, the answer is Yes, but it offers no benefit, and could perform a lot worse.

That is because in typical supervised learning scenarios there is nothing to match the concept of actions that modify state. In the example classifiers you can have the equivalent of a state (the input to be classified), an action (the choice of category) and the reward (whether of not the choice matches the label). But taking an action does not lead to another state, rewards are not sparse or cumulative across multiple actions within a specific environment or "episode". There are no time steps.

RL algorithms are generic MDP solvers - they can learn about relationships between state, action and likely next state, and optimise long-term goals over may time steps when given a current state. That also makes them less efficient learners when those relationships are not valid or important in the problem you want to solve. If you trained e.g. Q-learning on a typical image classification dataset, and added time steps, it would spend a lot of time/resources establishing that its choice of action had no influence on which images it was subsequently presented with, or how easy it was to get a reward from later images as opposed to earlier ones depending on how the state varied. If you really did allow the choice of action to determine the next image, then you would be training the RL to do something else other than classification.

You could frame the classifiers as contextual bandits, which is perhaps closer match. However, that still throws away knowledge that you have about the classification problem, replacing it with a generic reward system. For instance, a contextual bandit solver would deliberately guess wrong classes to check whether sometimes there was a small chance of a high reward for doing that.

If you were very careful about how you represented actions and rewards, and set other hyper-parameters, then you might be able to re-create a similar gradient setup to normal supervised learning, and only lose a little bit of efficiency through using RL or contextual bandit framing of your problem. However, you would still have added some non-necessary complexity.

If you search, you may find some ways to combine RL with supervised learning, for instance in this paper, the authors propose using RL to refine a generative RNN. However, these currently seem niche and are not aimed to improve upon or replace supervised learning.

Finally, in theory you could allow RL to control a video camera pan/zoom as part of activity recognition (or any other video or multi-image classification task). That would be a full RL problem, because the agent's actions really would be influencing the later states and hopefully improving the accuracy of recognition. For learning efficiency, you would likely want to combine this initially with a network that had already been trained to recognise actions on a supervised data set. You would need to experiment with how much the recognition part was trained compared to the RL part (as it will start to collect data outside of your normal datasets). And of course setup and training of the combined system could be a major project. You may be able to simulate it in a game engine perhaps in the early stages.


How can I tokenize a text file with BERT or something similar? I want to use the twitter datasets in a project and the tweet contents look something like this:

tweet_ID         tweet_text

12324124         some text here that has been twitted bla bla bla
35325323         some other text, trump, usa , merica ,etc.
56743563         bla bla text whatever tweet bla bla

Now I would like to end-up with a file that contains tweet_IDs and some vector encodings. I was reading about BERT, ROBERTA, etc. Is there a way to simply generate these encodings without writing a huge amount of boilerplate code?

With Huginface's Transformers, it should be doing with not much boilerplate.

A minimum example using PyTorch:

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased')
input_ids = torch.tensor(tokenizer.encode("Hello, my dog is cute")).unsqueeze(0)  # Batch size 1
outputs = model(input_ids)
last_hidden_states = outputs[0]  # The last hidden-state is the first element of the output tuple

Depending on how data you want to process, you might want to speed it up a little bit. In that case, you should do it batches which would require doing padding and passing the padding mask to the model.


Input changing row count matrices in an MLP I want to input a numpy 2d array into MLP but I have an array of 50395 rows that contains many 2d array of shape (x, 129). x because some matrices have different row numbers. Here is an example :
train['spec'].shape
>>(50395,)
train['spec'][0].shape
>>(41, 129)
train['spec'][5].shape
>>(71, 129)

Here an snippet of my code :
X_train = train['spec'].values;     X_valid = valid['spec'].values
y_train = train['label'].values;    y_valid = valid['label'].values
model.add(Dense(12, input_shape=(50395, ), activation='relu'));
model.fit(X_train, y_train, validation_data=(X_valid, y_valid), epochs=500, batch_size=1);

I get this error on last line (model.fit) :
ValueError: Error when checking input: expected dense_54_input to have shape (50395,) but got array with shape (1,)
How to fix this problem so that the network can take as input all 50395 matrices of shape (x, 129)?

First, I made the matrices (x, 129) to have the same shape by padding in with the rows filled with zeros.
Second, I transformed my large matrices into dimension 3 by the following process:
train = list(train_df['spec'])
np.array(train).shape
>>> (50395, 71, 129)

X_train = list(train['spec']);     X_valid = list(valid['spec'])
y_train = train['label'].values;    y_valid = valid['label'].values
model.add(Dense(12, input_shape=(50395, 71, 129), activation='relu'));
model.fit(np.array(X_train), y_train, validation_data=(np.array(X_valid), y_valid), epochs=500, batch_size=1);


Model should predict the same value every time for the same input I have used a random forest model for prediction of prices. Should the model be predictable in its behavior? By this, I mean that I'm not changing the model and the input, Will the predicted value always be the same?

Yes, the output is the same for the same input.

The "randomness" just concerns the approach how an ensemble is built: bagging (considering random subset of objects) and random selection of features for splitting (when we create each decision tree and choose which feature would be used as a splitting criteria).