how to convert numpy to tfrecords and then generate batches?

The whole process is simplied using the Dataset API. Here are both the parts: (1): Convert numpy array to tfrecords and (2): read the tfrecords to generate batches. 1. Creation of tfrecords from a numpy array: Example arrays: inputs = np.random.normal(size=(5, 32, 32, 3)) labels = np.random.randint(0,2,size=(5,)) def npy_to_tfrecords(inputs, labels, filename): with tf.io.TFRecordWriter(filename) as writer: … Read more

How to suppress verbose Tensorflow logging? [duplicate]

2.0 Update (10/8/19) Setting TF_CPP_MIN_LOG_LEVEL should still work (see below in v0.12+ update), but there is currently an issue open (see issue #31870). If setting TF_CPP_MIN_LOG_LEVEL does not work for you (again, see below), try doing the following to set the log level: import tensorflow as tf tf.get_logger().setLevel(‘INFO’) In addition, please see the documentation on … Read more

How to install Tensorflow on Python 2.7 on Windows?

If you only need TensorFlow because of Keras and your are on Python 2.7.x, you can avoid installing Tensorflow(Google) and replace it by CNTK(Microsoft). According to Jeong-Yoon Lee CNTK is a lot (about 2 to 4 times) faster than TensorFlow for LSTM (Bidirectional LSTM on IMDb Data and Text Generation via LSTM), while speeds for … Read more

How to prefetch data using a custom python function in tensorflow

This is a common use case, and most implementations use TensorFlow’s queues to decouple the preprocessing code from the training code. There is a tutorial on how to use queues, but the main steps are as follows: Define a queue, q, that will buffer the preprocessed data. TensorFlow supports the simple tf.FIFOQueue that produces elements … Read more

Multiple outputs in Keras

from keras.models import Model from keras.layers import * #inp is a “tensor”, that can be passed when calling other layers to produce an output inp = Input((10,)) #supposing you have ten numeric values as input #here, SomeLayer() is defining a layer, #and calling it with (inp) produces the output tensor x x = SomeLayer(blablabla)(inp) x … Read more

How do I get the weights of a layer in Keras?

If you want to get weights and biases of all layers, you can simply use: for layer in model.layers: print(layer.get_config(), layer.get_weights()) This will print all information that’s relevant. If you want the weights directly returned as numpy arrays, you can use: first_layer_weights = model.layers[0].get_weights()[0] first_layer_biases = model.layers[0].get_weights()[1] second_layer_weights = model.layers[1].get_weights()[0] second_layer_biases = model.layers[1].get_weights()[1] etc.

Reset weights in Keras layer

Save the initial weights right after compiling the model but before training it: model.save_weights(‘model.h5’) and then after training, “reset” the model by reloading the initial weights: model.load_weights(‘model.h5’) This gives you an apples to apples model to compare different data sets and should be quicker than recompiling the entire model.