how to implement custom metric in keras?

Here I’m answering to OP’s topic question rather than his exact problem. I’m doing this as the question shows up in the top when I google the topic problem. You can implement a custom metric in two ways. As mentioned in Keras docu. import keras.backend as K def mean_pred(y_true, y_pred): return K.mean(y_pred) model.compile(optimizer=”sgd”, loss=”binary_crossentropy”, metrics=[‘accuracy’, … Read more

Update TensorFlow

(tensorflow)$ pip install –upgrade pip # for Python 2.7 (tensorflow)$ pip3 install –upgrade pip # for Python 3.n (tensorflow)$ pip install –upgrade tensorflow # for Python 2.7 (tensorflow)$ pip3 install –upgrade tensorflow # for Python 3.n (tensorflow)$ pip install –upgrade tensorflow-gpu # for Python 2.7 and GPU (tensorflow)$ pip3 install –upgrade tensorflow-gpu # for Python … Read more

How does Keras define “accuracy” and “loss”?

Have a look at metrics.py, there you can find definition of all available metrics including different types of accuracy. Accuracy is not printed unless you add it to the list of desired metrics when you compile your model. Regularizers are by definition added to the loss. For example, see add_loss method of the Layerclass. Update … Read more

How to get the dimensions of a tensor (in TensorFlow) at graph construction time?

I see most people confused about tf.shape(tensor) and tensor.get_shape() Let’s make it clear: tf.shape tf.shape is used for dynamic shape. If your tensor’s shape is changable, use it. An example: a input is an image with changable width and height, we want resize it to half of its size, then we can write something like: … Read more

Pytorch – RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed

The problem is from my training loop: it doesn’t detach or repackage the hidden state in between batches? If so, then loss.backward() is trying to back-propagate all the way through to the start of time, which works for the first batch but not for the second because the graph for the first batch has been … Read more

How big should batch size and number of epochs be when fitting a model?

Since you have a pretty small dataset (~ 1000 samples), you would probably be safe using a batch size of 32, which is pretty standard. It won’t make a huge difference for your problem unless you’re training on hundreds of thousands or millions of observations. To answer your questions on Batch Size and Epochs: In … Read more

How to calculate prediction uncertainty using Keras?

If you want to implement dropout approach to measure uncertainty you should do the following: Implement function which applies dropout also during the test time: import keras.backend as K f = K.function([model.layers[0].input, K.learning_phase()], [model.layers[-1].output]) Use this function as uncertainty predictor e.g. in a following manner: def predict_with_uncertainty(f, x, n_iter=10): result = numpy.zeros((n_iter,) + x.shape) for … Read more