Iterating a trained network within TensorFlow - python

Suppose I have a fully-trained TensorFlow network that converts an input A into an output B, without any additional inputs. I want to iterate the network to generate a series of states A,B,C,D,... where each output is fed as an input to the network in order to generate the next state (like a Markov chain).
Is there a way to do this iteration entirely within TensorFlow (ie, include iteration within the graph) while recording a snapshot of the output at each timestep? This seems to me like it might involve RNN, but I am unsure how to build an RNN when I already have a working network for the forward pass.

Related

Removing 'initializer' layers in Tensorflow

I am trying to save my trained model to a file to ship to my inference server, and then benchmark it. The only issue is that my model (using keras) has init layers (such as conv1/weights/RandomUniform, for example) still in it, which can not be run on the device I am benchmarking.
So, how do I go about removing all of these layers from my graph?
I've tried using tfgraph.finalize(), convert_variables_to_constants, and remove_training_nodes, and none seem to remove these nodes.
The exact layer it breaks on is:
'res_net50/conv1/kernel/Initializer/random_uniform/RandomUniform'
The strip_unused_nodes transformation allows you to provide a set of input and output nodes. The graph transformation tool will remove all branches of the graph that do not feed into the output nodes.
Having said that, my gut feeling is that removing the initialisation nodes will not have a significant impact on inference time because the operations will not be evaluated. They do however consume some memory if they are not pruned.

Machine Learning: Predict set of numbers based on previous number

I have a set of 5 numbers as input and it produces output of 4 numbers. Need to find a model which can learn from the existing input/output and able to predict the numbers.
Go for recurrent neural networks or LSTM (Long-Short term memories). In simple terms, a feed-forward neural network simply produces output for given input based on the way it has figured out the relation between Input and Output while training, but it has no idea what the previous state (output) was, the basic idea is to recursively feed the output back to the input, i.e we make use of previous state of output along with current input as input to the neural network in a feedback loop fashion, so the output of neural network not only depends on the input but also on previous output.
LSTMs are being widely used for poetry generation, code-generation etc, your problem falls under the same domain.
Refer this link for more details: LSTM

Accessing Input Layer data in Tensorflow/Keras

I am trying to replicate a neural network for depth estimation. The original authors have taken a pre-trained network and added between the fully connected layer and the convolutional layer a 'Superpixel Pooling Layer'. In this layer, the convolutional feature maps are upsampled and the features per superpixel are averaged.
My problem is that in order to successfully achieve this, I need to calculate the superpixels per image. How can I access the data being used by keras/tensorflow during batch processing to perform SLIC oversegmentation?
I considered splitting the tasks and working by pieces i.e. feed the images into the convolutional network. Process the outputs separately and then feed them into a fully connected layer. However, this makes further training of the network impossible.
At the time it seems to be impossible to actually access the data within the symbolic tensor. It also seems unlikely that such functionality will be added in the future since in the Tensorflow page it says:
A Tensor object is a symbolic handle to the result of an operation, but
does not actually hold the values of the operation's output.
Keras allows for the creation of personalized layers. However, these are limited by the available backend operations. As such, it is simply not possible to access the batch data.

Tensorflow: Use same dequeued batch at different locations in graph

I have a working input pipeline that uses multiple threads to read data from tfrecords files, decodes and preprocesses the data, and serves them to the graph. I used this example as a starting point. My problem is that I need the input batch again in the loss layer, to compute a reconstruction error of the input. If I now just add the dequeuing operation multiple times to my graph, i.e. once at the input layer and once at the loss layer, I obviously get two times a different batch since tensorflow evaluates the operation twice. What is the easiest way to overcome this?
Probably you can try adding identity operation after dequeuing and connect other nodes to that identity operation.

How to use TF model for inference while training

I'm adapting the Tensorflow tutorial for sequence to sequence modeling for my project. Specifically, I am basing my code off of translate.py.
The tutorial computes the perplexity on the dev set every n training steps. I'd instead like to calculate BLEU score on the dev set.
The problem I'm facing is that when creating a model, you specify whether it is forward only or not. Looking through the code, it seems that if it is (which happens when training), at each step the network will not calculate the final output for the input sequence, but will calculate gradients. When it's not forward only (as in the decoding function later in the tutorial), it applies the loop function which feeds the output back into the input for the RNN which allows for the output sequence to be generated. However, it doesn't compute the gradients. So as far as I understand it, you can construct a model for either training (i.e. gradients) or testing (i.e. performing full inference on it).
Since I want to compute the BLEU score, I need some sequence produced by the model which corresponds to an input sequence in the dev set. Because of how the models are constructed, I would need both types of models (forward-only and not forward-only). However, trying to do this (even with a new session and a new variable scope), I can't seem to load the model for inference while I also have a model created for training. Without a new session/variable scope, I get errors about duplicated variables. It would be nice if there were a way to switch the model from not forward-only to forward-only.
In this case, is there any way to perform inference (run the full RNN) while I am also in the scope of training it?

Categories

Resources