Tags down


Loss function in Keras does not match analogical function

By : Jeff Chiu
Date : September 15 2020, 06:00 AM
I wish this help you It's because the model.predict output shape is not same with Y_valid. If you get the transpose of the predictions it will give you almost same loss.
code :
>>> Y_valid.shape                                                          
>>> preds.shape                                                            
(2000, 1)
>>> np.abs(Y_valid - np.transpose(preds)).mean()

Share : facebook icon twitter icon

Custom loss function's results does not match with the inbuilt loss function's result

By : user2888981
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I have implemented a custom binary cross entropy loss function in tensorflow. To test this I had compared it with the inbuilt binary cross entropy loss function in Tensorflow. But, I got very different results in both cases. I am unable to understand this behaviour. , You have a slight error in your implementation.
You have:
code :
ans = -1*(w1*y_true*tf.log(y_pred+eps) + w2*(1-y_true)*tf.log(y_pred + eps))
ans = -1*(w1*y_true*tf.log(y_pred+eps) + w2*(1-y_true)*tf.log(1 - y_pred + eps))
def custom_loss(eps,w1,w2):
    def loss(y_true, y_pred):
        ans = -1*(w1*y_true*tf.log(y_pred+eps) + w2*(1-y_true)*tf.log(1-y_pred+eps))
        return tf.reduce_mean(ans)
    return loss
y_true = tf.constant([0.1, 0.2])
y_pred = tf.constant([0.11, 0.19])

custom_loss(y_true, y_pred)                         # == 0.41316
tf.keras.losses.binary_crossentropy(y_true, y_pred) # == 0.41317

keras sparse_categorical_crossentropy loss function output shape didn't match

By : Thato Sello
Date : March 29 2020, 07:55 AM
hope this fix your issue loss='sparse_categorical_crossentropy' is not meant for one-hot encodings but for integer targets. You probably need a "Dense(..." as the output layer and use y_train directly.

keras "unknown loss function" error after defining custom loss function

By : J. Ouellet
Date : March 29 2020, 07:55 AM
Hope that helps In Keras we have to pass the custom functions in the load_model function:

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

By : Oscar Lastera Sanche
Date : March 29 2020, 07:55 AM
I hope this helps . A naive implementation of Binary Cross Entropy will suffer numerical problem on 0 output or larger than one output, eg log(0) -> NaN. The formula you posted is reformulated to ensure stability and avoid underflow. The following deduction is from tf.nn.sigmoid_cross_entropy_with_logits.
code :
z * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))
= z * -log(1 / (1 + exp(-x))) + (1 - z) * -log(exp(-x) / (1 + exp(-x)))
= z * log(1 + exp(-x)) + (1 - z) * (-log(exp(-x)) + log(1 + exp(-x)))
= z * log(1 + exp(-x)) + (1 - z) * (x + log(1 + exp(-x))
= (1 - z) * x + log(1 + exp(-x))
= x - x * z + log(1 + exp(-x))
x - x * z + log(1 + exp(-x))
= log(exp(x)) - x * z + log(1 + exp(-x))
= - x * z + log(1 + exp(x))
max(x, 0) - x * z + log(1 + exp(-abs(x)))

Loading model with custom loss function: ValueError: 'Unknown loss function' in keras

By : Ash
Date : March 29 2020, 07:55 AM
this will help Compiling the model then saving. Then while loading model that time getting error. , Use custom_objects when loading your model:
code :
def def triplet_loss(y_true, y_pred, alpha = 0.3):
    anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2]
    pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), axis=-1)
    neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), axis=-1)
    basic_loss = tf.add(tf.subtract(pos_dist, neg_dist), alpha)
    loss = tf.reduce_sum(tf.maximum(basic_loss, 0.0))

    return loss

FRmodel = load_model('model.h5',custom_objects={'triplet_loss':triplet_loss})
Related Posts Related Posts :
  • The total maximum value of the value chosen
  • Nested while Loops is not working in python
  • Why do my python sklearn logistic regression results differ from this example?
  • Python Regex: How do I use regular expression to read in a file with multiple lines, and extract words from each line to
  • What is the use of drop_first in pandas?
  • Is it possible to pass a Flask session to another .py-File which is not in the routing?
  • tensorflow_hub to pull BERT embedding on windows machine - extending to albert
  • Python Pandas slicing with various datatypes
  • Pandas: Checking and changing all items in a column
  • Why does __call__ returned values get garbage collected when calling a class twice: SomeClass()()
  • Insert element at every nth location in list of lists
  • PD Read in Jupyter Notebook 3.7
  • Visualize Results of each iteration of While Loop into a Time Series Chart
  • Run a function for each row and create a new Column Pandas Dataframe
  • How can I create a small IDLE-like Python Shell in Tkinter?
  • extract variable and data from a string in python
  • CUDA implementation of Softmax
  • The function to_excel of pandas generate an unexpected TypeError
  • string is contain with newline symbol (\n), how to use regex to replace \n to \n?
  • How can I use %s to replace text within a file in python?
  • How to Reference a Pandas Column that has a dot in the name
  • How to use tuple as a key of a dictionary
  • How to extract two integer values from a column of a dataframe
  • How properly build a class in the __new__ with type(3 args) and 2 ancestors?
  • How to declare the return of a function as the default parameter to another function without calling the first function?
  • Elegant way to check arguments across multiple functions
  • How can I replace elemts of a list with other elements
  • i want to use variable globally in veiws.py
  • Pandas data not being plotted
  • Python Generator: How do I generate pairs from two different lists based on user input (of how many pairs to print)
  • Python: How to use a dictionary to call methods (values in dictionary) to run based on user input (key in dictionary) in
  • Read lines between two keywords Python
  • How do you insert data from the user into the file with the most optimal using Python?
  • How do you create a loop that will work in Snowflake?
  • Why can't I change the __class__ attribute of an instance of object?
  • Concatenating pandas dataframes from pickle vs. from in-memory dictionary - why does in-memory fail?
  • How to Calculate time difference between two date columns
  • In '<string>' requires string as left operand, not list
  • Django clean() change field requirement
  • Python - TypeError: write() argument must be str, not bytes
  • Commutative Count in a groupby dataframe on other columns condition
  • Undo np.fft.fft2 to get the original image
  • What is the proper way to share a program without sharing personal information?
  • Pandas DataFrame - summing rows by multiple column values
  • Python - best approach to mapping codes in data to description
  • I need to know how to do this, but it may be impossible
  • pandas dataframe columns with list values
  • Wrong value of standard deviation
  • Django POST error: tuple has no attribute get, despite similar code working previously
  • Convert a 3d-array into 2d-array
  • how to find the shortest distance between two regex
  • Python unit testing on class methods with no input arguments
  • Bokeh: Repeated plotting in jupyter lab increases (browser) memory usage
  • Cannot convert string into float in a for loop
  • constraint satisfaction problem missing one constraint
  • Is there a more accurate way to record time in C++?
  • SQL query in python
  • How do you create a python dictionary using lists of different size?
  • Running python in the terminal, syntax error
  • Combining List of DataFrames
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk