Keras MSE definition

2024/10/5 20:33:39

I stumbled across the definition of mse in Keras and I can't seem to find an explanation.

def mean_squared_error(y_true, y_pred):return K.mean(K.square(y_pred - y_true), axis=-1)

I was expecting the mean to be taken across the batches, which is axis=0, but instead, it is axis=-1.

I also played around with it a little to see if K.mean actually behaves like the numpy.mean. I must have misunderstood something. Can somebody please clarify?

I can't actually take a look inside the cost function at run time right? As far as I know the function is called at compile time, which prevents me from evaluating concrete values.

I mean... imagine doing regression and having a single output neuron and training with a batch size of ten.

>>> import numpy as np
>>> a = np.ones((10, 1))
>>> a
array([[ 1.],[ 1.],[ 1.],[ 1.],[ 1.],[ 1.],[ 1.],[ 1.],[ 1.],[ 1.]])
>>> np.mean(a, axis=-1)
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])

All it does is flatten the array instead of taking the mean of all the predictions.

Answer

K.mean(a, axis=-1) and also np.mean(a, axis=-1) is just taking the mean across the final dimension. Here a is an array with shape (10, 1) and in this case, taking the mean across the final dimension happens to be the same as flattening it to a 1d array of shape (10,). Implementing it like so supports the more general case of e.g. multiple linear regression.

Also, you can inspect the value of nodes in the computation graph at run-time using keras.backend.print_tensor. See answer: Is there any way to debug a value inside a tensor while training on Keras?

Edit: You question appears to be about why the loss doesn't return a single scalar value but instead returns a scalar value for each data-point in the batch. To support sample weighting, Keras losses are expected to return a scalar for each data-point in the batch. See losses documentation and the sample_weight argument of fit for more information. Note specifically: "The actual optimized objective is the [weighted] mean of the output array across all data points."

https://en.xdnf.cn/q/73216.html

Related Q&A

How do I adjust the size and aspect ratio of matplotlib radio buttons?

Ive been trying for hours to get the size and aspect ratio of a simple list of radio buttons correct with no success. Initially, import the modules:import matplotlib.pyplot as plt from matplotlib.widge…

App engine NDB: how to access verbose_name of a property

suppose I have this code:class A(ndb.Model):prop = ndb.StringProperty(verbose_name="Something")m = A() m.prop = "a string value"Now of course if I print m.prop, it will output "…

How do I load specific rows from a .txt file in Python?

Say I have a .txt file with many rows and columns of data and a list containing integer values. How would I load the row numbers in the text file which match the integers in the list?To illustrate, sa…

How to check in python if Im in certain range of times of the day?

I want to check in python if the current time is between two endpoints (say, 8:30 a.m. and 3:00 p.m.), irrespective of the actual date. As in, I dont care what the full date is; just the hour. When I c…

Python __class__()

In Pythons documentation __class__ is described as an attribute. In the object type (the metaclass), __class__ appears to be a method.If we do:>>> class Foo:pass>>> a = Foo() >>…

how to handle ever-changing password in sqlalchemy+psycopg2?

I inherited some code that uses sqlalchemy with psycopg2, which needs to run on AWS. RDS Postgres supports iam-based authentication, but the way it does it is rather kludgy: you request a temporary pas…

How to determine which compiler was requested

My project uses SCons to manage the build process. I want to support multiple compilers, so I decided to use AddOption so the user can specify which compiler to use on the command line (with the defaul…

Does pytest have anything like google tests non-fatal EXPECT_* behavior?

Im more familiar with the google test framework and know about the primary behavior pair they support about ASSERT_* vs EXPECT_* which are the fatal and non-fatal assert modes.From the documentation:Th…

Radon transformation in python

Here is a dummy code:def radon(img):theta = np.linspace(-90., 90., 180, endpoint=False)sinogram = skimage.transform.radon(img, theta=theta, circle=True)return sinogram # end defI need to get the sinogr…

Librosa raised OSError(sndfile library not found) in Docker

Im trying to write the Dockerfile for a small python web project and there is something wrong with the dependencies. Ive been doing some search on the internet and it said that Librosa library requires…