Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

softmax function python

def softmax(x):
    return np.exp(x) / np.sum(np.exp(x), axis=0)
Comment

a softmax function

import numpy as np
def softmax(x):
    """Calculates the softmax for each row of the input x.

    Your code should work for a row vector and also for matrices of shape (m,n).

    Argument:
    x -- A numpy matrix of shape (m,n)

    Returns:
    s -- A numpy matrix equal to the softmax of x, of shape (m,n)
    """
    
    #(≈ 3 lines of code)
    # Apply exp() element-wise to x. Use np.exp(...).
    # x_exp = ...

    # Create a vector x_sum that sums each row of x_exp. Use np.sum(..., axis = 1, keepdims = True).
    # x_sum = ...
    
    # Compute softmax(x) by dividing x_exp by x_sum. It should automatically use numpy broadcasting.
    # s = ...
    
    # YOUR CODE STARTS HERE
    x_exp = np.exp(x)
    x_sum = np.sum(x_exp, axis=1, keepdims=True)
    s=x_exp/x_sum
    
    # YOUR CODE ENDS HERE
    
    return s
Comment

PREVIOUS NEXT
Code Example
Python :: difference between __str__ and __repr__ 
Python :: how to make a distance function in python 
Python :: how to set background image in python tkinter 
Python :: call a function onclick tkinter 
Python :: python logging into two different files 
Python :: installation of uvicorn with only pure python dependencies 
Python :: async sleep python 
Python :: label encoding in python 
Python :: save image from jupyter notebook 
Python :: read emails from gmail python 
Python :: pandas index between time 
Python :: remove last line of text file python 
Python :: python access global variable 
Python :: python web parsing 
Python :: ssl django nginx 
Python :: mailchimp send email python 
Python :: models. type for phone number in django 
Python :: random library python 
Python :: python find directory of file 
Python :: python string: iterate string 
Python :: python find duplicates in string 
Python :: how to find a word in list python 
Python :: ravel python 
Python :: pandas map using two columns 
Python :: python acf and pacf code 
Python :: install fasttext python 
Python :: python cast list to float 
Python :: datetime object to string 
Python :: how yo import python lib 
Python :: python string indexing 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =