Neural Network Memory - google-colaboratory

Using Google Colab, I made a neural network using tensorflow that generates text based on examples. I ran 60 epochs. How can I get my neural network to maintain what it has learned. Whenever I re-run it, it starts over.

Try saving your model at the end of the training like this:
import tensorflow as tf
tf.keras.models.save_model(model, 'model/my_model')
Then you can load the model like this:
tf.keras.models.load_model('model/my_model')

Related

Saving the Learned Weights of a Network to Train on another Dataset

I would like to train a MLP(Multi Layer Perceptron) with MNIST dataset. I use a validation set so I can save the weights of the best model. Then I want to load these weights back into the same architecture and use them to initialize and train with another dataset. I would like to know if this is possible with Tensorflow 1.x or 2.x. Right now I am trying to write a custom function to do it but it is getting complicated. I am using tf 1.x.
I suggest you take a look at tensorflow's documentation, here a link of a tutorial to save your weights and load them afterwards:
https://www.tensorflow.org/tutorials/keras/save_and_load

Visualize Tensorflow Graph from Checkpoint

I am importing a pretrained mobilenet's model mobilenet_v1_0.25_128_frozen.pb into my tensorflow environment. Once imported, I want to be able to save a snapshot of the model architecture in the form of .png. I know that there is a way to do this in keras with tf.keras.utils.plot_model(model, to_file="model.png"). Is there a way to do this in tensorflow session without using Tensorboard. In case, you recommend using tensorboard, I don't want to separately run tensorboard. I want a way to save the model architecture inside the tensorflow session without starting tensorboard.

Can we run training and validation on separate GPUs using tensorflow object detection API running on tensorflow 1.12?

I have two Nvidia Titan X cards on my machine and want to finetune COCO pretrained Inception V2 model on a single specific class. I have created the train/val tfrecords and changed the config to run the tensorflow object detection training pipeline.
I am able to start the training but it hangs (without any OOM) whenever it tries to evaluate a checkpoint. Currently it is using only GPU 0 with other resource parameters (like RAM, CPU, IO etc) in normal range. So I am guessing that GPU is the bottleneck. I wanted to try splitting training and validation on separate GPUs and see if it works.
I tried to look for a place where I could do something like setting "CUDA_VISIBLE_DEVICES" differently for both the processes but unfortunately the latest tensorflow object detection API code (using tensorflow 1.12) makes it very difficult to do so. I am also unable to verify my assumption about training and validation running in same process as my machine hangs. Could someone please suggest where to look for to solve it?

Cannot Reload saved Keras model using tensorflow

I am working in a single jupyter notebook. I create and train a very simple CNN with keras. It compiles, fits, and predicts fine. I save it with:
model.save("mymodel.hd5")
Model is a keras.models.Sequential.
I then read that back in with:
reload_keras_model = keras.models.load_model("mymodel.hd5")
That also works fine. However if I try to read the model in using tensorflow via:
from tensorflow.keras.models import load_model
reload_tf_mmodel = load_model("mymodel.hd5")
That fails with:
ValueError: Unknown layer:layers
Most of the threads I've read on github say "update your model" or comments about custom objects (I'm not using any). My target platform is the rpi zero and I've been able to install tf but unable to install keras, and that's why I want to load via tf. Why would keras and tf.keras handle this model differently and what do I need to update/change to read it in with tf.keras?
While keras (can) use TF as Backend, it does not guarantee that the saved model is readable in TF as well.
Note that you can use keras with both theano and tf, thus reload_keras_model = keras.models.load_model("mymodel.hd5") will work good with both backends, as the saving/loading is done in the "keras" part, and not using the backend.
You can use this tool: keras_to_tensorflow
Or something similar.

Tensorflow Retrain the retrained model

I am very new to Neural network and tensorflow, just starting on the retrain image tutorial. I have successfully completed the flower_photos training and i have 2 questions.
1.) Is it a good/bad idea to keep building upon a retrained model many times over and over? Or would it be a lot better to train a model fresh everytime? That leads to my second question
2.) If it is ok to retrain a model over and over, for the retrain model tutorial in Tensorflow (Image_retraining), in the retrain.py would i simply replace the classify_image_graph_def.pb and imagenet_synset_to_human_label_map.txt with the one outputted from my retraining? But i see that there is also a imagenet_2012_challenge_label_map_proto.pbtxt, would i have to replace that one with something else?
Thanks for your time