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 :: label with list comprehension python 
Python :: assignment 6.5 python for everybody 
Python :: python indent print 
Python :: python get dir from path 
Python :: python hello world jenkins 
Python :: normalized histogram pandas 
Python :: convert timestamp to period pandas 
Python :: if statement python 
Python :: panda loc conditional 
Python :: create folders in python overwright existing 
Python :: delete first element of dictionary python 
Python :: python string formatting - padding 
Python :: accumulator programming python 
Python :: python selenium chrome save session 
Python :: get all commands discord.py 
Python :: list addition within a list comprehension 
Python :: Setting spacing (minor) between ticks in matplotlib 
Python :: import pyx file 
Python :: add element to array list python 
Python :: PySimpleGUI all elements 
Python :: decorators in python 
Python :: python binary tree search 
Python :: convert all sizes to terabytes pandas 
Python :: How to take n space separated Integer in a list in python? 
Python :: how to define number in python 
Python :: .unique() python 
Python :: python cron job virtualenv 
Python :: python convert float to whole part of number 
Python :: Python Generators with a Loop 
Python :: python ide 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =