Is activation only used for nonlinearity ?
Is activation only used for nonlinearity or for both problems . I am still confused why do we need activation function and how can it help.
1 answer

Generally, such a question would be suited for Stats Stackexchange or the Data Science Stackexchange, since it is a purely theoretical question, and not directly related to programming (which is what Stackoverflow is for).
Anyways, I am assuming that you are referring to the classes of linearly separable and not linearly separable problems when you talk about "both problems. In fact, nonlinearity in a function is always used, no matter which kind of problem you are trying to solve with a neural network.The simple reason for nonlinearities as activation function is simply the following:
Every layer in the network consists of a sequence of linear operations, plus the nonlinearity.
Formally  and this is something you might have seen before  you can express the mathemtical operation of a single layer
F
and it's inputh
as:F(h) = Wh + b
where
W
represents a matrix of weights, plus a biasb
. This operation is purely sequential, and for a simple multilayer perceptron (withn
layers and without nonlinearities), we can write the calculations as follows:y = F_n(F_n1(F_n2(...(F_1(x))))
which is equivalent to
y = W_n W_n1 W_n2 ... W_1 x + b_1 + b_2 + ... + b_n
Specifically, we note that these are only multiplications and additions, which we can rearrange in any way we like; particularly, we could aggregate this into one ubermatrix W_p and bias b_p, to rewrite it in a single formula:
y = W_p x + b_p
This has the same expressive power as the above multilayer perceptron, but can inherently be modeled by a single layer! (While having much less parameters than before).
Introducing nonlinearities to this equation turns the simple "building blocks"
F(h)
into:F(h) = g(Wh + b)
Now, the reformulation of a sequence of layers is not possible anymore, and then nonlinearity additionally allows us to approximate any arbitrary function.
EDIT: To address another concern of yours ("how does it help?"), I should explicitly mention that not every function is linearly separable, and thus cannot be solved by a purely linear network (i.e. without nonlinearities). One classic simple example is the XOR operator.
See also questions close to this topic

Why I'm getting the same output even if I apply different machine learning algorithm
I'm working on a dataset which has all the categorical value. I converted the categorical value with a numerical one and feed to different machine learning algorithm like KNN, SVM, Random Forest. But each and every time I get the same result for all the three algorithms I mentioned. Why is that? Why I'm getting the same output again and again
PS: My data is imbalanced.

Numpy mmap and kubernetes RAM limits
I'm trying to load a large (~40GB) file with np.load(path, mmap_mode='r') on a kubernetes cluster which allows very high shortterm memory use but much lower indefinitely. I have 24GB "requested" memory with a much higher limit. np.load() detects that memory resources are available, so it automatically loads the entire file into RAM (my understanding is numpy automatically uses the memory mapping to the extent it needs to given available memory). Because the pod is dramatically exceeding requested memory, it is automatically killed when others request memory. If I set the memory limit to something low, the pod is 00M killed because mmap again doesn't detect the limitation and plows right past the ceiling. Is there any way around this without a lot of memory micromanaging?

Paraphrasing questions in different ways in NLP
I have situation where i have question like What is your name? how it is possible to ask same question programmatically(not manual efforts) in different ways:
Q Whats your name?
Q I would like to know your name?
Q tell me your name?
Q so on
Urgent Help is required

Torch / lua, how to increase the true positive rate with multilayer perceptron neural network applied to an imbalanced dataset?
I've implemented a multilayer perceptron neural network in Torch / lua and applied to an imbalanced dataset (33 features, around 300 elements, of which approximately 70% negative elements and 30% positive elements).
I tried several models and several settings. With the current model, I can get a quite high true negative rate (around 0.7) but a quite low true positive rate (around 0.3). I really want to improve my true positive rate, but how?
I'm splitting the dataset into training set, validation set, and test set, and using the validation set to find the top number of hidden layers and hidden units. I'm currently using learning rate = 0.1 and iterations = 1000. The activation function is a Sigmoid. I tried several neural network tricks (momentum, dropout, Xavier initialization) but they did not lead to any improvement.
I worked with other imbalanced datasets in the past, and I was always able to get good rates even on the smaller class. But not this time...
The surprising thing is that my neural network does quite well during training, because I see the mean square error that drops very quickly. So I would expect good results on the test set as well, but it does not happen.
What can I do to improve my true positive rate?
Here's my model:
perceptron = nn.Sequential() perceptron:add(nn.Linear(this_input_number, this_hidden_units)) perceptron:add(nn.Sigmoid()) for w=1,this_hidden_layers do perceptron:add(nn.Linear(this_hidden_units, this_hidden_units)) perceptron:add(nn.Sigmoid()) end perceptron:add(nn.Linear(this_hidden_units, this_output_number))
And, for example, here's the MSE error during the second model training:
$$$ hidden_units = 25 hidden_layers = 1 $$$ completion: 10% (epoch=100)(element=194) loss = 0.04 error progress = 3.56623% completion: 20% (epoch=200)(element=194) loss = 0.004 error progress = 2.2678% completion: 30% (epoch=300)(element=194) loss = 0 error progress = 1.56329% completion: 40% (epoch=400)(element=194) loss = 0.001 error progress = 1.2053% completion: 50% (epoch=500)(element=194) loss = 0.003 error progress = 0.98677% completion: 60% (epoch=600)(element=194) loss = 0.004 error progress = 0.84489% completion: 70% (epoch=700)(element=194) loss = 0 error progress = 0.74345% completion: 80% (epoch=800)(element=194) loss = 0 error progress = 0.67081% completion: 90% (epoch=900)(element=194) loss = 0.021 error progress = 0.69629% completion: 100% (epoch=1000)(element=194) loss = 0.001 error progress = 0.67999%
As you can see, the error quickly goes to zero.
You can find my complete working Torch code here, with the dataset included. You're more than welcome to test my code on your computer and try some additional variants of the method.
Do you have any suggestion on how to increase the true positive rate? Thanks!

Using a Custom Dual Input Keras Layer
I am attempting to create a simple multilayer perceptron in Keras. The general structure I would like to create is one where a matrix A of dimension [n_a1, n_a2] is sent through a number of layers of a multilayer perceptron, and at a certain point, the dot product of the morphed A matrix is taken with a randomly selected y vector [n_y, 1] from a set of y vectors, and the result then continues through a number more layers before reaching the end where it is compared with the input labels as normal.
Unfortunately, I am having issues with figuring how to implement this properly. I created a custom multiinput layer per the simple example offered at https://keras.io/layers/writingyourownkeraslayers/, but it seems that I still can't have multiple inputs in the network. I am getting an error for setting an array element as a sequence:
ValueError: setting an array element with a sequence.
Also, its unclear to me how the network would know what to do with the list formatted inputs for the other layers. Do I need to specify the list shape for each layer, and to somehow only use the A in the [A, y] list?
Custom Layer
class DualLayer(Layer): def __init__(self, output_dim, **kwargs): self.output_dim = output_dim super(DualLayer, self).__init__(**kwargs) def build(self, input_shape): #Trainable weight variable for layer self.kernel = self.add_weight(name='kernel', shape=(input_shape[1], self.output_dim), initializer='uniform', trainable=True) super(DualLayer, self).build(input_shape) def call(self, x): aOpt, y = x return [K.dot(aOpt, y)] def compute_outpute_shape(self, input_shape): assert isinstance(input_shape, list) shape_y, shape_aOpt = input_shape return [shape_y[0]]
Model
def modFunc(y1, A1, y2, A2, xSim): model = Sequential() model.add(Flatten(input_shape = np.shape(A1))) model.add(Dense(np.shape(A1)[0]*np.shape(A1)[1], activation = 'relu', kernel_regularizer = 'l1', activity_regularizer = 'l1')) model.add(DualLayer([y1, A1])) model.add(Dense(5, activation = 'relu')) clf = model.fit([y1, A1], xSim, epochs=5, batch_size=1, verbose=2) return clf.coef_

Image classifier neural network using Tensorflow In C++
I have 2D Trajectory data for various categories such as Line, Circle, Ellipse and Arc.
I want to construct a simple CNN network to train my images data and predict the right classification.
My approach is as follows:
1) Instead of providing the input as "Images", I want to provide the input as a Matrix (lets say, of size 200x200).Since,I have the (x,y) coordinates data, I would be scaling all my coordinates data so that they lie in the range of (0,200).Finally,I would populate the values of 0's (indices in the matrix,which do not consist of the coordinates data) and 1's (indices,which contain the coordinates data) to my initialized matrix (initially,all the indices contain 0's)
2) Train a simple CNN Model and predict its classification.
I would be really glad,if someone can help me achieve this task using C++.As the web help in case of CNN Implementation in C++ is very limited,I am finding it really difficult to achieve it in C++.
Thanks in Advance.

Cartpolev1 using A2C has very low reward with no reason
I'm using a2c to work on the Cartpolev1. The final goal is to find the mean reward. However, my reward is less than 10. I was thinking whether my code erase part of the rewards or something during the training process? I don't know whats wrong with it. The setup of the code seems fine. The whole process is supposed to have reward around 300~500.
import tensorflow as tf import numpy as np import gym # Supresses compilation warnings import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' # Constants learning_rate = 0.001 #trials = 3 episodes = 1000 moves = 999 discount = 0.99 hidden_size = 32 critic_size = 128 updates = 50 avgRs = [] totRs = [] # Helper function to generate distribution def generate_disRs(hist): dist = [] last_reward = 0 for element in reversed(hist): reward = discount * last_reward + element dist.append(reward) last_reward = reward return list(reversed(dist)) class A2C: def __init__(self): self.game = gym.make('CartPolev1') self.game.reset() self.num_actions = self.game.action_space.n self.state_size = self.game.observation_space.shape[0] self.state_input = tf.placeholder(tf.float32, [None, self.state_size]) self.rewards = tf.placeholder(shape=[None], dtype=tf.float32) # Define any additional placeholders needed for training your agent here: self.state_value = self.critic() self.actProbs = self.actor() self.loss_val = self.loss() self.train_op = self.optimizer() self.session = tf.Session() self.session.run(tf.global_variables_initializer()) def optimizer(self): """ :return: Optimizer for your loss function """ optimizer = tf.train.AdamOptimizer(learning_rate) trainOp = optimizer.minimize(self.loss) return trainOp def critic(self): """ Calculates the estimated value for every state in self.state_input. The critic should not depend on any other tensors besides self.state_input. :return: A tensor of shape [num_states] representing the estimated value of each state in the trajectory. """ # Placeholders for critic loss V1 = tf.Variable(tf.random_normal([4, critic_size], dtype=tf.float32, stddev=.1)) v1Out = tf.nn.relu(tf.matmul(self.state_input, V1)) V2 = tf.Variable(tf.random_normal([critic_size, 1], dtype=tf.float32, stddev=.1)) self.v2Out = tf.matmul(v1Out, V2) return self.v2Out def actor(self): """ Calculates the action probabilities for every state in self.state_input. The actor should not depend on any other tensors besides self.state_input. :return: A tensor of shape [num_states, num_actions] representing the probability distribution over actions that is generated by your actor. """ # Layer 1 # self.state = tf.placeholder(shape=[None, 4], dtype=tf.float32) self.W = tf.Variable(tf.random_uniform([4, hidden_size], dtype=tf.float32)) self.bias = tf.Variable(tf.random_uniform([hidden_size], dtype=tf.float32)) self.hidden = tf.nn.relu(tf.matmul(self.state_input, self.W) + self.bias) # Layer 2 self.O = tf.Variable(tf.random_uniform([hidden_size, 2], dtype=tf.float32)) self.bias2 = tf.Variable(tf.random_uniform([2], dtype=tf.float32)) self.output = tf.nn.softmax(tf.matmul(self.hidden, self.O) + self.bias2) self.actions = tf.placeholder(shape=[None], dtype=tf.int32) self.indices = tf.range(0, tf.shape(self.output)[0]) * 2 + self.actions self.actProbs = tf.gather(tf.reshape(self.output, [1]), self.indices) #self.aloss = tf.reduce_mean(tf.log(self.actProbs) * self.advantage) return self.actProbs #return self.aloss def loss(self): """ :return: A scalar tensor representing the combined actor and critic loss. """ # Placeholders # self.rewards = tf.placeholder(shape=[None], dtype=tf.float32) # self.actions = tf.placeholder(shape=[None], dtype=tf.int32) # self.indices = tf.range(0, tf.shape(self.output)[0]) * 2 + self.actions # self.actProbs = tf.gather(tf.reshape(self.output, [1]), self.indices) self.aloss = tf.reduce_mean(tf.log(self.actProbs) * self.rewards) self.cLoss = tf.reduce_mean(tf.square(self.rewards  self.v2Out)) self.loss = self.aloss + self.cLoss return self.loss def train_episode(self): """ train_episode will be called 1000 times by the autograder to train your agent. In this method, run your agent for a single episode, then use that data to train your agent. Feel free to add any return values to this method. """ # reset st = self.game.reset() # List to store state, action and reward histories state_hist = [] action_hist = [] reward_hist = [] # List to store history of states and values state_value_hist = [] for move in range(moves): # Run actDict, stateVal = self.session.run([self.output, self.v2Out], feed_dict={self.state_input: [st]}) # Get the random action action = np.random.choice(np.array([0, 1]), p=actDict[0]) st1, reward, done, info = self.game.step(action) # Render the game # game.render() # Add to the history action_hist.append(action) reward_hist.append(reward) state_hist.append(st) state_value_hist.append(stateVal[0][0]) # Iterate st = st1 # Update if done or (move % updates == 0 and move != 0): # Get disRs disRs = generate_disRs(reward_hist) # Compute Difference difference = np.array(disRs)  np.array(state_value_hist) # Run feed_dict = {self.state_input: state_hist, self.actions: action_hist, self.rewards: difference} l, _ = self.session.run([self.loss_val, self.train_op], feed_dict=feed_dict) if done: totRs.append(move) # print move, disRs[0] break def check_actor(model): """ The autograder will use your actor() function to test your agent. This function checks that your actor returns a tensor of the right shape for the autograder. :return: True if the model's actor returns a tensor of the correct shape. """ dummy_state = np.ones((10, 4)) #actDict = model.session.run(model.output, feed_dict={model.state_input: [model.game.reset()]}) actor_probs = model.session.run(model.actProbs, feed_dict={ model.state_input: dummy_state) }) return actor_probs.shape == (10, 2) if __name__ == '__main__': # Change __main__ to train your agent for 1000 episodes and print the average reward over the last 100 episodes. # The code below is similar to what our autograder will be running. learner = A2C() for i in range(1000): learner.train_episode() print(str(np.average(totRs[900: 1000])))

tf.train.shuffle_batch returning nan's after random number of iterations
While training a pretty standard convolutional net, I discovered a weird bug. Everything starts out fine with a nice loss curve, but suddenly the loss goes to nan. I was able to trace the nans back all the way to the input pipeline.
As you can see, I am printing the errors before and after they are batched using tf.train.shuffle_batch(). The second print comes up as nan, and this is propagated all the way through the question.
What might be causing this? I have played around with different values of capacity, threads, etc.
The code and context is below. Nans are appearing in the batched before/after images, but not the before/after image.
I should note that the tfrecord files have an arbitrary number of examples in them, but I believe that this shouldn't matter for the enqueue/dequeue operations.
def input_pipeline(self, filenames, batch_size, num_epochs=None): """Function that creates a highly abstracted input pipeline consisting of a bunch of threads and queues given a few simple parameters. See https://www.tensorflow.org/versions/r0.10/how_tos/reading_data/index.html#multipleinputpipelines for more information and indepth explanations. Args:  filenames: a list of filenames of tfrecords files random.shuffle(filenames) train_filenames = filenames train_filename_queue = ( tf.train.string_input_producer(train_filenames, num_epochs=num_epochs, shuffle=True, seed=1)) before_image, after_image, mask_image = ( self._read_and_decode_reach_tfrecords(train_filename_queue)) # min_after_dequeue defines how big a buffer we will randomly sample # from  bigger means better shuffling but slower start up and more # memory used. # capacity must be larger than min_after_dequeue and the amount larger # determines the maximum we will prefetch. Recommendation: # min_after_dequeue + (num_threads + a safety margin) * batch_size min_after_dequeue = 1000 saftey_margin = 3 capacity = min_after_dequeue + (3 + saftey_margin) * batch_size capacity = 2000 before_image = tf.Print(before_image,[tf.reduce_mean(before_image + after_image)], "pre_shuffle: ") mask_image = tf.Print(mask_image, [tf.reduce_mean(mask_image)], "pre_shuffle_mask: ") before_images, after_images, mask_images = ( tf.train.shuffle_batch( [before_image, after_image, mask_image], batch_size=batch_size, capacity=capacity, min_after_dequeue=min_after_dequeue, num_threads=5, seed=1)) before_images = tf.Print(before_images,[tf.reduce_mean(before_images + after_images)], "post_shuffle: ")

libavcodec 56 not found even though it is installed
I keep getting this error message when I try to compile an Intel OpenVino project in Ubuntu 18.04:
warning: libavcodecffmpeg.so.56, needed by /opt/intel/computer_vision_sdk_2018.3.343/opencv/lib/libopencv_videoio.so.3.4.3, not found
I also get similar error messages for:
libavformatffmpeg.so.56
,libavutilffmpeg.so.54
, andlibswscaleffmpeg.so.3
.However, when I type
ffmpeg
in the terminal, I get:ffmpeg version 2.7.7 Copyright (c) 20002016 the FFmpeg developers built with gcc 7 (Ubuntu 7.3.027ubuntu1~18.04) configuration: libavutil 54. 27.100 / 54. 27.100 libavcodec 56. 41.100 / 56. 41.100 libavformat 56. 36.100 / 56. 36.100 libavdevice 56. 4.100 / 56. 4.100 libavfilter 5. 16.101 / 5. 16.101 libswscale 3. 1.101 / 3. 1.101 libswresample 1. 2.100 / 1. 2.100 Hyper fast Audio and Video encoder
I have all the correct versions. The ffmpeg is in /usr/local/bin. Why can't OpenVino see it? Any advice will be appreciated, thanks in advance!
Here's the project I'm trying to compile: https://github.com/inteliotdevkit/intruderdetector
For people who downvote, please explain why. I'm new to OpenVino and stack overflow. I'd like to improve my posts if possible.

How to generate cross section from the boundary of a 3D point cloud?
I had a number of raw 3d points and a boundary for it, as well as the indices for those points on the boundary surface. As shown in the figure:
Left: Raw data points and its boundary; Right: points on the boundary surface
I want to make cross sections out of the boundary surface, at a different angle. How should I do this?
I looked at varies ways to make cross sections, including 2D interpolation and 3d contour plot for meshgrids, but none of them work since the point cloud is not a meshgrid.

estimation of the ground plane in pinhole camera model
I am trying to understand the pinhole camera model and the geometry behind some computer vision and camera calibration stuff that I am looking at.
So, if I understand correctly, the pinhole camera model maps the pixel coordinates to 3D real world coordinates. So, the model looks as:
y = K [RT]x
Here
y
is pixel coordinates in homogeneous coordinates,RT
represent the extrinsic transformation matrix andx
is the 3D world coordinates also in homogeneous coordinates.Now, I am looking at a presentation which says
project the center of the focus region onto the ground plane using [RT]
Now the center of the focus region is just taken to be the center of the image. I am not sure how I can estimate the ground plane? Assuming, the point to be projected is in input space, the projection should that be computed by inverting the [RT] matrix and multiplying that point by the inverted matrix?
 What is Ordered max pooling and Orderless pooling?

Is it possible to infer more than one parameter from Convolution neural network
I have a question and I am not sure if it's a smart one. But I've been reading quite a lot about convolution neural networks. And so far I understand that the output layer could for example be a softmax layer for a classification problem or you could do regression in order to get a quantitative value. But I was wondering if it is possible to infer more than one parameter. For example, if I have a data and my output label is both price of the house and size of the house. I know it is not a smart example. But I just want to know if it's possible to predict two different output values in the same output layer in the convolution neural network. Or do I need to have two different convolution neural network where one predicts the size of the house and the one predicts price of the house. And how can we combine these two predictions then. And if we can do it in one convolution neural network, then how can we do that?