8000 GitHub - woshidaerduotu99/sotfmax
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

woshidaerduotu99/sotfmax

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

sotfmax

import numpy as np

class Solfmax:

def __init__(self):
    self.softmax = None
    self.grad = None
    self.dnx = None

def __call__(self, nx):
    shifted_x = nx - np.max(nx)
    ex = np.exp(shifted_x)
    sum_ex = np.sum(ex)
    self.solfmax = ex / sum_ex
    return self.solfmax
    
def cross_entropy(a, y):
    return np.sum(np.nan_to_num(-y*np.log(a)-(1-y)*np.log(1-a)))

def get_grad(self):
    self.grad = self.solfmax[:, np.newaxis] * self.solfmax[np.newaxis, :]
    for i in range(len(self.grad)):
        self.grad[i, i] -= self.solfmax[i]
    self.grad = - self.grad
    return self.grad

def backward(self, dl):
    self.get_grad()
    self.dnx = np.sum(self.grad * dl, axis=1)
    return self.dnx

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0