简体   繁体   中英

Write down piecewise functions using NumPy?

I'm new to NumPy and trying to figure out how can I write down piecewise-defined function. I'm having a derivative of MSE_Loss function using ReLU as activation function: 在此处输入图像描述

I have all parameters I need. My question is: how to use NumPy to handle piecewise functions (preferable without using loops)?

EDITED : For example, I can write down the following function using NumPy following way: 在此处输入图像描述

def loss_derivative(X, y, w, b):
    n = len(y)
    sigma = sigmoid(X @ w + b)
    return (X.T @ ((sigma - y) * sigma * (1 - sigma))) / n

How can I implement function with the same input for the piecewise formula from above?

I think the easiest way for this example is using condition multiplier as follows (not all elements in your function is defined in your question, so I am guessing them. However, you can fix it easily if it is not the case):

def loss_derivative(X, y, w):
    n = len(y)
    return X.T @ (((X @ w - y) / n) * ((X @ w) > 0))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM