Answers.Body: <blockquote>
  <p>Is this a valid implementation of second-order regression?</p>
  </blockquote>
  <p>No, but it is not far off.</p>
  <p>To perform a full second-order regression, you will need all terms for <span class="math-container">$x_{i,j}x_{i,k}$</span> where the first index is the example and the second index the feature. This includes every combination of two input variables. In your element-wise squaring you only produce terms <span class="math-container">$x_{i,j}x_{i,j}$</span>, only using each feature squared, so are missing a lot of potential second-order terms.</p>
  <blockquote>
  <p>Is this layer used in any deep learning architectures?</p>
  </blockquote>
  <p>In general no. One of the goals of deep learning is to automate the discovery of useful non-linearities - including feature interaction terms - in the hidden layers. These won't be exactly the same as your idea, but can work well enough to get good performance from the neural network on a given dataset.</p>
  <p>Pre-calculating a specific set of interactions <em>might</em> help with some data sets, but comes at a high cost of squaring the number of features represented in any given layer, and thus multiplying the number of weights required between two layers by the size of the input layer again. Your original idea would only double the size of the input layer, but would be less general.</p>
  <blockquote>
  <p>What is it useful for?</p>
  </blockquote>
  <p>It is useful in linear regression as a way to find interactions between features. Or you can think of it as a way to automatically search through a subset of possible nonlinear interactions to see if any of those have a strong correlation to the target variable.</p>
  <p>There is a danger though that by adding a large number of terms (and associated parameters), that you may find spurious correlations and thus overfit the training data. This is more likely, the larger the number of interactions that you add to the input. Usually this kind of feature generation is paired with regularisation, to reduce the chance of overfitting.</p>
  <blockquote>
  <p>Is this layer (exactly) equivalent to some MLP?</p>
  </blockquote>
  <p>No, although because MLPs can approximate any function, it should be possible to train an MLP such that it performs very similarly.</p>
  <p>It may not hurt to insert a layer like the one you describe into some relatively small layer of an MLP, and use your idea in pracice. I expect in some circumstances it may help, but it will be a little bit of an edge case, mostly not making much difference or real advantage. However, no harm in trying it on a few sample datasets. This applies to both your idea as written, using just the squared feature terms, and to the full second order expansion - both may have their niche where they could be useful.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-16T17:40:19.483Z
Answers.LastActivityDate: 2022-01-16T19:29:51.903Z
Answers.LastEditDate: 2022-01-16T19:29:51.903Z
Answers.UserDisplayName: Neil Slater
Body: <p>I was thinking about implementing second-order regression via a fully-connected layer, and I came up with this:</p>
  <ul>
  <li><span class="math-container">$X$</span> is the input data, shaped <span class="math-container">$(features, batch\_number)$</span>.</li>
  <li><span class="math-container">$w0$</span> is the bias, shaped <span class="math-container">$(output\_dims,)$</span>.</li>
  <li><span class="math-container">$w1$</span> and <span class="math-container">$w2$</span> are the weights, each shaped <span class="math-container">$(output\_dims, features)$</span>.</li>
  <li><span class="math-container">$Y$</span> is the target data, shaped <span class="math-container">$(output\_dims, batch\_number)$</span>.</li>
  <li><span class="math-container">$\hat{Y}$</span> is the output of the layer: <span class="math-container">$\hat{Y} = w2 \cdot X^2 + w1 \cdot X + w0$</span>
  <ul>
  <li><span class="math-container">$X^2$</span> is the element-wise square of <span class="math-container">$X$</span> taken over its first axis. (I.e., what you would expect intuitively.)</li>
  </ul>
  </li>
  </ul>
  <p>We will then minimize the MSE between <span class="math-container">$Y$</span> and <span class="math-container">$\hat{Y}$</span> using gradient descent.</p>
  <p>Is this a valid implementation of second-order regression?</p>
  <p>Is this layer used in any deep learning architectures? What is it useful for?</p>
  <p>Is this layer (exactly) equivalent to some MLP?</p>
  
Tags: <machine-learning><regression><multilayer-perceptrons><dense-layers><non-linear-regression>
Title: Does a second-order fully-connected layer have any uses?
UserDisplayName: HappyFace
ThreadText: # Does a second-order fully-connected layer have any uses?
  
  (Asked by: username_0 on 2022-01-16)
  
  I was thinking about implementing second-order regression via a fully-connected layer, and I came up with this:
  
  * $X$ is the input data, shaped $(features, batch\\_number)$.
  * $w0$ is the bias, shaped $(output\\_dims,)$.
  * $w1$ and $w2$ are the weights, each shaped $(output\\_dims, features)$.
  * $Y$ is the target data, shaped $(output\\_dims, batch\\_number)$.
  * $\hat{Y}$ is the output of the layer: $\hat{Y} = w2 \cdot X^2 + w1 \cdot X + w0$
    + $X^2$ is the element-wise square of $X$ taken over its first axis. (I.e., what you would expect intuitively.)
  
  We will then minimize the MSE between $Y$ and $\hat{Y}$ using gradient descent.
  
  Is this a valid implementation of second-order regression?
  
  Is this layer used in any deep learning architectures? What is it useful for?
  
  Is this layer (exactly) equivalent to some MLP?
  
  **Tags:** machine-learning, regression, multilayer-perceptrons, dense-layers, non-linear-regression
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-01-16. Score: 1 | Accepted answer
  
  > Is this a valid implementation of second-order regression?
  
  No, but it is not far off.
  
  To perform a full second-order regression, you will need all terms for $x\_{i,j}x\_{i,k}$ where the first index is the example and the second index the feature. This includes every combination of two input variables. In your element-wise squaring you only produce terms $x\_{i,j}x\_{i,j}$, only using each feature squared, so are missing a lot of potential second-order terms.
  
  > Is this layer used in any deep learning architectures?
  
  In general no. One of the goals of deep learning is to automate the discovery of useful non-linearities - including feature interaction terms - in the hidden layers. These won't be exactly the same as your idea, but can work well enough to get good performance from the neural network on a given dataset.
  
  Pre-calculating a specific set of interactions *might* help with some data sets, but comes at a high cost of squaring the number of features represented in any given layer, and thus multiplying the number of weights required between two layers by the size of the input layer again. Your original idea would only double the size of the input layer, but would be less general.
  
  > What is it useful for?
  
  It is useful in linear regression as a way to find interactions between features. Or you can think of it as a way to automatically search through a subset of possible nonlinear interactions to see if any of those have a strong correlation to the target variable.
  
  There is a danger though that by adding a large number of terms (and associated parameters), that you may find spurious correlations and thus overfit the training data. This is more likely, the larger the number of interactions that you add to the input. Usually this kind of feature generation is paired with regularisation, to reduce the chance of overfitting.
  
  > Is this layer (exactly) equivalent to some MLP?
  
  No, although because MLPs can approximate any function, it should be possible to train an MLP such that it performs very similarly.
  
  It may not hurt to insert a layer like the one you describe into some relatively small layer of an MLP, and use your idea in pracice. I expect in some circumstances it may help, but it will be a little bit of an edge case, mostly not making much difference or real advantage. However, no harm in trying it on a few sample datasets. This applies to both your idea as written, using just the squared feature terms, and to the full second order expansion - both may have their niche where they could be useful.
  
  
  ---
  
  