6
$\begingroup$

I was wondering if it's possible to get the inverse of a neural network. If we view a NN as a function, can we obtain its inverse?

I tried to build a simple MNIST architecture, with the input of (784,) and output of (10,), train it to reach good accuracy, and then inverse the predicted value to try and get back the input - but the results were nowhere near what I started with. (I used the pseudo-inverse for the W matrix.)

My NN is basically the following function:

$$ f(x) = \theta(xW + b), \;\;\;\;\; \theta(z) = \frac{1}{1+e^{-z}} $$

I.e.

def rev_sigmoid(y):
    return np.log(y/(1-y))

def rev_linear(z, W, b):
    return (z - b) @ np.linalg.pinv(W)

y = model.predict(x_train[0:1])
z = rev_sigmoid(y)
x = rev_linear(z, W, b)
x = x.reshape(28, 28)
plt.imshow(x)

enter image description here

^ This should have been a 5:

t

Is there a reason why it failed? And is it ever possible to get inverse of NN's?

EDIT: it is also worth noting that doing the opposite does yield good results. I.e. starting with the y's (a 1-hot encoding of the digits) and using it to predict the image (an array of 784 bytes) using the same architecture: input (10,) and output (784,) with a sigmoid. This is not exactly equivalent, to an inverse as here you first do the linear transformation and then the non-linear. While in an inverse you would first do (well, undo) the non-linear, and then do (undo) the linear. I.e. the claim that the 784x10 matrix is collapsing too much information seems a bit odd to me, as there does exist a 10x784 matrix that can reproduce enough of that information.

enter image description here

$\endgroup$
  • 2
    $\begingroup$ The basic condition for a function to be invertible is that the function should be one to one (basically monotonic). In your structure you are clearly adding multiple inputs which destroys one to one relation. I think you can prove this formally, Maths.SE might help you. $\endgroup$ – DuttaA Jan 18 at 16:47
  • 1
    $\begingroup$ Interesting. I think Autoencoders come pretty close to what you're looking for but they aren't strictly "mathematical" inverses. On a theoretical note, it seems that the "resolution" of the pseudo inverse would be really low thus any "reconstruction" will be of low quality because of the low rank. Could you post the original picture of the 5? I might just be biased but it seems like there is a very low resolution 5 hiding in the noise. I'll write a more thorough answer in a bit. $\endgroup$ – respectful Jan 18 at 21:11
  • 2
    $\begingroup$ AFAIU, this is a major scientific problem of neural networks. We don't understand why they work, and how to inverse them. If you solve that problem, please public several academic papers (as open science please!) $\endgroup$ – Basile Starynkevitch Jan 18 at 23:33
  • 2
    $\begingroup$ @respectful go ahead. Just share your findings when you're done :-P $\endgroup$ – David Refaeli Jan 19 at 13:18
  • 1
    $\begingroup$ @respectful yes - the y-to-x mapping give similar results to all 10 chars/classes. $\endgroup$ – David Refaeli Jan 24 at 13:22
2
$\begingroup$

Mathematical Exploration

let $\Theta^+$ be the pseudo-inverse of $\Theta$.

Recall, that if a vector $\boldsymbol v \in R(\Theta)$ (ie in the row space) then $\boldsymbol v = \Theta^+\Theta\boldsymbol v$. That is, so long as we select a vector that is in the rowspace of $\Theta$ then we can reconstruct it with full fidelity using the pseudo inverse. Thus, if any of the images happen to be linear combinations of the rows of $\Theta$ then we can reconstruct it.

To be more specific. let $f(\boldsymbol x)$ have a pseudo-inverse $f^+(\boldsymbol x)$ defined as you have. If we restrict our domain such that $\boldsymbol x \in C(\Theta^T)$ (column space of the transpose) then $f^+= f^{-1}_{res}$.

That is, under our domain restriction the pseudo inverse becomes a true inverse.

An Extrapolation

It would then seem that so long as we are under such domain restrictions then we could define a pseudo inverse for a general NN. Though, it might be possible that some NNs don't have any restriction that admits an inverse. Maybe, there is some way to regularize the parameters such that this is possible. NNs with ReLU wouldn't admit such an inverse since ReLU loses information on negative values. Leaky Relu might work.

Further Investigation

Finally, this presents a zone for further study. Some questions to answer might be:

  • Is it possible for optimized parameters to contain non-trivial examples in their row-space?
  • If so, under what conditions is this possible?
  • Are the examples in any way represented in the row space?
  • Is there some way to regularize a NN such that it admits an inverse over some desired restriction?
  • Under what conditions is invertibility useful?
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ When researches change slightly the input of a NN to observe its impact on the output, hoping to find explainability/adversary examples, isn't that a poor-man's approach to inveritibility? If you agree that's the case, then these are the first two examples of an answer for your last question. The inveritibility would allow us to create clusters of domain-space rows that map to a specific output in classification problems, or map to variations in regression problems. Such a thing would be great! $\endgroup$ – Alpha Jan 23 at 0:39
  • 1
    $\begingroup$ @Alpha You are absolutely right. I'm actually writing a paper on this right now. This sort of thing will be covered. It's really fascinating. I'm hoping to have the results shortly. $\endgroup$ – respectful Jan 23 at 0:46
  • $\begingroup$ @respectful check my edit to the question. Maybe it can help... $\endgroup$ – David Refaeli Jan 23 at 10:34
  • $\begingroup$ @respectful also check my new answer $\endgroup$ – David Refaeli Jan 23 at 11:48
1
$\begingroup$

So, if I go the opposite way, start with my y and predict an x, and then ask for the inverse of that - I get really good results (actually - 100% accuracy).

i.e.

model = Sequential([
    Dense(784, input_shape=(10,), activation='sigmoid'),
])
model.compile(loss=keras.losses.binary_crossentropy,
              optimizer=keras.optimizers.Adam(0.01),
              metrics=['binary_crossentropy'])
model.fit(y_train, x_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(y_test, x_test))
# train until accuracy > 0.9, then:
W, b = model.get_weights()
y = y_train
x = reverse.predict(y)
z = rev_sigmoid(x)
y_hat = rev_linear(z, W, b)
(y_hat.argmax(axis=1) == y.argmax(axis=1)).mean()  # 1.0

After playing a bit with some toy examples, I think the other way is probably not possible, as the matrices don't have an inverse. Putting these (toy) matrices in WolframAlpha for example tells you the determinant is 0, but in numpy the determinant is just slightly bigger than 0, so you manage to calculate an "inverse" which is not really an inverse and get the bad results.

It's also makes sense. In the reversed scenario, we start with 10 dimension, expand to 784, and then collapse back to 10. But in the "regular" scenario, we start at 784, collapse to 10, and then expand to 784 again - and (I guess) too much information is lost then.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Nice edit! You are right, in general the neural network does not have an inverse. It does however have what I'm calling its pseudo inverse (extending the idea of a matrix pseudo inverse). I'm currently working on developing the general mathematical formula for the pre-image which is very useful in regards to adversarial examples. $\endgroup$ – respectful Jan 23 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.