Tuesday, August 9, 2022

Back propagation in simple terms

Back propagation is an algorithm used in machine learning that works by calculating the gradient of the loss function, which points us in the direction of the value that minimizes the loss function. It relies on the chain rule of calculus to calculate the gradient backward through the layers of a neural network. Using gradient descent, we can iteratively move closer to the minimum value by taking small steps in the direction given by the gradient.


During forward propagation, we use weights, biases, and nonlinear activation functions to calculate a prediction y hat from the input x that should match the expected output y as closely as possible (which is given together with the input data x). We use a cost function to quantify the difference between the expected output y and the calculated output y hat.

The goal of backpropagation is to adjust the weights and biases throughout the neural network based on the calculated cost so that the cost will be lower in the n
ext iteration. Ultimately, we want to find a minimum value for the cost function.





With calculus, we can calculate how much the value of one variable changes depending on the change in another variable. If we want to find out how a change in a variable x by the fraction dx affects a related variable y, we can use calculus to do that. The change dx in x would change y by dy.

In Calculus notation, we express this relationship as follows.


The first derivative of a function gives you the slope of that function at the evaluated coordinate. If you have functions with several variables, you can take partial derivatives with respect to every variable and stack them in a vector. This gives you a vector that contains the slopes with respect to every variable. Collectively the slopes point in the direction of the steepest ascent along the function. This vector is also known as the gradient of a function. Going in the direction of the negative gradient gives us the direction of the steepest descent. Going down the route of the steepest descent, we will eventually end up at a minimum value of the function.




Machine Learning from Google

Machine learning resources from Google good ones


https://developers.google.com/machine-learning/crash-course/reducing-loss/gradient-descent

https://developers.google.com/machine-learning/crash-course/ml-intro


Monday, August 8, 2022

How weights are updated in gradient descent?

 The basic equation that describes the update rule of gradient descent is. This update is performed during every iteration. Here, w is the weights vector, which lies in the x-y plane. From this vector, we subtract the gradient of the loss function with respect to the weights multiplied by alpha, the learning rate.


null

https://towardsdatascience.com/understanding-backpropagation-algorithm-7bb3aa2f95fd#:~:text=The%20algorithm%20is%20used%20to,parameters%20(weights%20and%20biases).


Forward and Backward pass in Neural network

 The "forward pass" refers to calculation process, values of the output layers from the inputs data. It's traversing through all neurons from first to last layer.

A loss function is calculated from the output values.

And then "backward pass" refers to process of counting changes in weights (de facto learning), using gradient descent algorithm (or similar). Computation is made from last layer, backward to the first layer.

Backward and forward pass makes together one "iteration".

During one iteration, you usually pass a subset of the data set, which is called "mini-batch" or "batch" (however, "batch" can also mean an entire set, hence the prefix "mini")

"Epoch" means passing the entire data set in batches.

One epoch contains (number_of_items / batch_size) iterations

The Backpropagation. The aim of backpropagation (backward pass) is to distribute the total error back to the network so as to update the weights in order to minimize the cost function (loss)

In simple terms, after each forward pass through a network, backpropagation performs a backward pass while adjusting the model's parameters (weights and biases).


How does the Gradient function work in Backpropagation?

 A gradient descent function is used in back-propagation to find the best value to adjust the weights by. There are two common types of gradient descent: Gradient Descent, and Stochastic Gradient Descent.


Gradient descent is a function that determines the best adjustment value to change the weights by. Over each iteration, it determines the volume/amount the weights should be adjusted by, the further away from the best determined weight, the bigger the adjustment value will be. You can think of it as a ball rolling down a hill; the ball's velocity being the adjustment value, and the hill being the possible adjustment values. Essentially, you want the ball (adjustment value) to be closest to the bottom of the world (possible adjustment) as possible. The ball's velocity will increase until it reaches the bottom of the hill - the bottom of the hill is the best possible value.


Stochastic gradient descent is a more complicated version of the gradient descent function and it is used in a neural network that may have a false-best adjustment value, where regular gradient descent won't find the best value, but a value it think's is the best. This can be analogised as the ball rolling down two hills, the hills are different in height. It rolls down the first hill and reaches the bottom of the first hill, thinking that it's reached the best possible answer, but with stochastic gradient descent, it would know that the position it was in now was not the best position, but in reality, the bottom of the second hill.


in back-propagation you calculate the furthest right weight-matrix's gradient and then adjust the weights accordingly, then you move one layer to the left, L-1, (on the next weight-matrix) and repeat the step, so in other words you determine the gradient, adjust accordingly and then move the the left.

The gradient of L wrt layer l−1 is calculated using the gradient wrt layer l




references:
https://stackoverflow.com/questions/66035281/how-does-the-gradient-function-work-in-backpropagation

Sunday, July 17, 2022

Android 12 Notification Permission

Notification permissions for apps targeting Android 12L (API level 32) or lower

Android automatically asks the user for permission the first time your app creates a notification channel, as long as the app is in the foreground. However, there are important caveats regarding the timing of channel creation and permission requests:


If your app creates its first notification channel when it is running in the background (which the FCM SDK does when receiving an FCM notification), Android will not allow the notification to be displayed and will not prompt the user for the notification permission until the next time your app is opened. This means that any notifications received before your app is opened and the user accepts the permission will be lost.

We strongly recommend that you update your app to target Android 13+ to take advantage of the platform’s APIs to request permission. If that is not possible, your app should create notification channels before you send any notifications to the app in order to trigger the notification permission dialog and ensure no notifications are lost. See notification permission best practices for more information. 

references:

https://firebase.google.com/docs/cloud-messaging/android/client

Android 13 - Notification Permissions

// Declare the launcher at the top of your Activity/Fragment:
private final ActivityResultLauncher<String> requestPermissionLauncher =
        registerForActivityResult
(new ActivityResultContracts.RequestPermission(), isGranted -> {
           
if (isGranted) {
               
// FCM SDK (and your app) can post notifications.
           
} else {
               
// TODO: Inform user that that your app will not show notifications.
           
}
       
});

// ...
private void askNotificationPermission() {
   
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
           
PackageManager.PERMISSION_GRANTED) {
       
// FCM SDK (and your app) can post notifications.
   
} else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
       
// TODO: display an educational UI explaining to the user the features that will be enabled
       
//       by them granting the POST_NOTIFICATION permission. This UI should provide the user
       
//       "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission.
       
//       If the user selects "No thanks," allow the user to continue without notifications.
   
} else {
       
// Directly ask for the permission
        requestPermissionLauncher
.launch(Manifest.permission.POST_NOTIFICATIONS);
   
}
}

 https://firebase.google.com/docs/cloud-messaging/android/client