loss-function

Do the operations defined in array ops in Tensorflow have gradient defined?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 15:46:24
问题 I want to know whether the tensorflow operations in this link, have a gradient defined. I am asking because I am implementing a custom loss function and when I run it I always have this error : ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval. This is my custom Loss function: def calculate_additional_loss(y_true,y_pred): #additional loss x_decoded

Errors when Building up a Custom Loss Function

百般思念 提交于 2019-12-11 12:50:11
问题 I try to build up my own loss function as follows import numpy as np from keras import backend as K def MyLoss(self, x_input, x_reconstruct): a = np.copy(x_reconstruct) a = np.asarray(a, dtype='float16') a = np.floor(4*a)/4 return K.mean(K.square(a - x_input), axis=-1)` In compilation, it says ValueError: setting an array element with a sequence Both x_input and x_reconstruct are [m, n, 1] np arrays. The last line of code is actually copied directly from Keras' built-in MSE loss function.

Compute external loss function but compute gradients in tensorflow?

大城市里の小女人 提交于 2019-12-11 06:08:56
问题 I want to train a model for which the loss function can only be computed externally. So, I take the output of my last layer, compute some value externally and want to use this to update my network. Can I implement such a setup in tensorflow? 回答1: Yes you can, you have to define your minimised loss out of the graph, for example: loss = Network.loss(input_tensor) loss_2 = out_function(loss) And then you just have to specify : train_step = optimizer.minimize(loss_2) It should work 来源: https:/

Keras Custom Binary Cross Entropy Loss Function. Get NaN as output for loss

帅比萌擦擦* 提交于 2019-12-11 04:35:19
问题 I try writing a custom binary cross-entropy loss function. This is my script: def my_custom_loss(y_true,y_pred): t_loss = (-1)*(y_true * K.log(y_pred) + (1 - y_true) * K.log(1 - y_pred)) return K.mean(t_loss) When I run my script using this loss function, after few iterations, I get NaN as output for loss function. Then I looked at TensorFlow documentation, I modified the loss function into the following: t_loss = K.max(y_pred,0)-y_pred * y_true + K.log(1+K.exp((-1)*K.abs(y_pred))) The code

Keras training with batches: Is the training loss computed before or after each optimization step?

不问归期 提交于 2019-12-11 02:54:26
问题 this is probably a very basic question, however I wasn't able to find an answer to it: When I train a network with Keras using batches, the console output shows and keeps updating a display of the current loss value of the training set during each training epoch. As I understand it, this loss value is computed over the current batch (as a proxy for the overall loss) and probably averaged with the loss values that were calculated for the previous batches. But there are two possibilities to get

keras custom loss pure python (without keras backend)

被刻印的时光 ゝ 提交于 2019-12-11 02:53:17
问题 I am currently programming an autoencoder for image compression. I would like to use a custom loss function written in pure python, i.e. without making use of keras backend functions. Is this at all possible and if so how? If it is possible I'd be very grateful for a minimum working example (MWE). Please look at this MWE, in particular the mse_keras function: # -*- coding: utf-8 -*- import matplotlib.pyplot as plt import numpy as np import keras.backend as K from keras.datasets import mnist

Keras custom loss function not printing value of tensor

自古美人都是妖i 提交于 2019-12-11 02:28:47
问题 I am writing just a simple loss function in which I have to convert the tensor to numpy array(it's essential). I am just trying to print value of the tensor but I am getting this error:- Tensor("loss/activation_4_loss/Print:0", shape=(?, 224, 224, 2), dtype=float32) def Lc(y_true, y_pred): x=K.print_tensor(y_pred) print(x) return K.mean(y_pred) Kindly tell me that how can I get the value(numerics) from the tensor? I also tried "eval" but it also threw a big fat error about no session is there

How do I use categorical_hinge in Keras?

吃可爱长大的小学妹 提交于 2019-12-10 14:54:52
问题 Maybe a very dumb question but I can't find an example how to use categorical_hinge in Keras. I do classification and my target is shape(,1) with values [-1,0,1] so I have 3 categories. Using the functional API I have set up my output layer like this: output = Dense(1, name='output', activation='tanh', kernel_initializer='lecun_normal')(output1) Then I apply: model.compile(optimizer=adam, loss={'output': 'categorical_hinge'}, metrics=['accuracy']) The result is that the model is converging

Use a keras model in a custom keras loss

自作多情 提交于 2019-12-10 10:36:16
问题 I have a regular keras model called e and I would like to compare its output for both y_pred and y_true in my custom loss function. from keras import backend as K def custom_loss(y_true, y_pred): return K.mean(K.square(e.predict(y_pred)-e.predict(y_true)), axis=-1) I am getting the error: AttributeError: 'Tensor' object has no attribute 'ndim' This is because y_true and y_pred are both tensor object and keras.model.predict expects to be passed a numpy.array . Any idea how I may succeed in

How can I sort the values in a custom Keras / Tensorflow Loss Function?

半腔热情 提交于 2019-12-10 07:19:24
问题 Introduction I would like to implement a custom loss function to Keras. I want to do this, because I am not happy with the current result for my dataset. I think the reason for this is because currently the built-in loss functions focuses on the whole dataset. And I just want to focus on the top values in my dataset. That is why I came up with the following idea for a custom loss function: Custom Loss Function Idea The custom loss function should take the top 4 predictions with the highest