I want to implement Direct Feedback Alignemnt in Tensorflow.
Reference paper: https://arxiv.org/pdf/1609.01596v5.pdf, Nøkland (2016)
I implemented a simple network that does DFA in pure Python, having explicitly the backprop, I just switched the backward pass of BP with the one of DFA.
where B1 and B2 are two random matrices with appropriate dimensions.
Tensorflow training obviously does backprop by default. Where and what should I modify in Tensorflow code in order to get this training behavior?
Related
I am desperately trying to use cohen kappa's metric as either a loss function or an evaluation metric in my keras neural network. I have tried many different implementations of it online and none of them seem to be maintained. In particular due to the fact that tf.contrib no longer exists in tensorflow 2.0. Any help in pointing in me in the right direction of a working implementation will be much appreciated!
When using the tf add-on class found here https://www.tensorflow.org/addons/api_docs/python/tfa/metrics/cohens_kappa.
I keep getting the following error and have no idea how I would go about debugging this.
ValueError: Number of samples in y_true and y_pred are different
R's package 'forecast' has a function nnetar, which uses feed-forward neural networks with a single hidden layer to predict in time series.
Now I am using Python to do the similar analysis. I want to use neural network which does not need to be as complex as deep learning. Maybe 2 layers and a couple of nodes are good enough for my case.
So, does Python have a model of simple neural networks which can be used in time series lik nnetar? If not, how to deal with this problem?
Any NN model that uses 1 or more hidden layers is a multi-layer perceptron model, and for that case it is trivial to make it extendable to N layers. So any library that you pick will support it. My guess for you not picking a complex library like pytorch/Tensorflow is its size.
Tensorflow does have TF-Lite which can work for smaller IOT devices.
Sklearn does have MLPRegressor that can train NNs if that is more to your liking.
You can always write your model. There are plenty of examples for this that use numpy and are plenty fast for cpu computation.( Single Hidden layer NN I am guessing will be more memory bound than computation bound)
Use another ML algorithm. Single Hidden layer NNs will not perform nearly as well as other other simpler algorithms.
If there are other reasons for not using a standard library like tensorflow/pytorch then you should mention them.
In the case of multi-input or multi-output models according to https://keras.io/models/model/, one can use
model = Model(inputs=a1, outputs=[b1, b2])
What if b1 and b2 are actually identical target values? I.e. After few initial layers, model has two independent "branches" and each should give the same value. Below is very simplified example
a = Input(shape=(32,))
b1 = Dense(32)(a)
b2 = Dense(32)(a)
model = Model(inputs=a, outputs=[b1,b2])
Is there a nicer/better way of doing fit than duplicating target values?
model.fit(x_train, [y_train, y_train])
Additionaly, if true labels (y_train) are needed during fit (only), one can use them like this
model.fit([x_train,y_train], [y_train, y_train])
Is there any better solution? Also, what to do with the prediction?
model.predict([x_test, y_test_fake_labels])
First of all for the predict function: model.predict(X) will return a list of numpy arrays in your case. I think you are Kinda confusing tensorflow's session.run() with keras. And for single input and multi output use model.fit(X,[y1,y2]).
I am assuming you are using the tensorflow backend of Keras. In my opinion Keras arguably has the best API and syntax. It is straightforward and easy to learn compared to tf.learn, slim etc. Even though it runs tensorflow in the background, it is awfully slow compared to running the graph using pure tensorflow. Thus, a small hack which I use sometimes to squeeze performance out of my model is to define the model architecture using keras and then get the pure tensorflow graph from keras using keras.backend.get_session().graph and use slim or tf.learn to train
/infer your model. Thus you are using the best of two worlds. Syntactically, this opens up a lot of ways to train/infer your model.
I've been given a fully trained model by another researcher that has inputs as placeholders. Regarding it as a function f(x), I would like to find x to minimize my distance metric (loss function) dist(x, f(x)). This could be something like the euclidean distance between the two points.
I tried to use TensorFlow's built-in optimizer functions. The issue is that tf.train.AdamOptimizer(1e-4).minimize(loss, var_list[input_placeholder]) fails, complaining that input_placeholder isn't of a supported type. Thus, I cannot get gradients for my input.
How can I optimize a function in TensorFlow when the inputs have to be specified in this way? Unfortunately, these placeholders are not passed through a Variable first, and I have to treat that model as a black box.
Using the Keras functional API detailed in this question, I created a dense layer with no bias to sit right before the model I was given. Holding its input as a constant all 1's vector, I optimized the joined model using only the Variable in the dense layer, giving me the optimal vector as the output of that layer.
All TensorFlow Optimizer subclasses allow you to minimize while only modifying a particular set of Variables, which I got out of Keras fairly simply.
I'm trying to use a tensorflow op inside a Keras model. I previously tried to wrap it with a Lambda layer but I believe this disables that layers' backpropagation.
More specifically, I'm trying to use the layers from here in a Keras model, without porting it to Keras layers (I hope to deploy to tensorflow later on). I can compile these layers in a shared library form and load these into python. This gives me tensorflow ops and I don't know how to combine this in a Keras model.
A simple example of a Keras MNIST model, where for example one Conv2D layer is replaced by a tf.nn.conv2d op, would be exactly what I'm looking for.
I've seen this tutorial but it appears to do the opposite of what I am looking for. It seems to insert Keras layers into a tensorflow graph. I'm looking to do the exact opposite.
Best regards,
Hans
Roughly two weeks have passed and it seems I am able to answer my own question now.
It seems like tensorflow can look up gradients if you register them using this decorator. As of writing, this functionality is not (yet) available in C++, which is what I was looking for. A workaround would be to define a normal op in C++ and wrap it in a python method using the mentioned decorator. If these functions with corresponding gradients are registered with tensorflow, backpropagation will happen 'automagically'.