# Path: gradients/subproblem.py
# def attention(Q, K, V):


from .subproblem import sigmoid, attention,s
import numpy as np
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

#taken from Pegasos algorithm by avaitla
def SGD(a, lab, Q, lr):
    for i in range(20):
        iterations = 1
        for tau in range(len(a)):
            if a[tau] > 0:
                wx = a @ Q[:,tau]
                a[tau] *= (1 - 1/iterations)
                if(lab[tau]*wx < 1):
                    a[tau] += lab[tau]/(lr * iterations)
                iterations += 1
    return a

def attention_sgd(x,y,a=None):
    if a == None:
        a = np.zeros(x.shape[1])
    else:
        a = np.resize(a,x.shape[1])        
    for i in range(5):
        for tau in range(x.shape[1]):
            gh = abs(1-np.min(attention(np.mat(x),np.mat(y),a[tau]))) * np.gradient(x.T*a[tau],axis=0)[tau]

# Path: gradients/subproblem.py
# def attention(Q, K, V):


# Path: gradients/descent.py
from .subproblem import sigmoid, attention,s
import numpy as np

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

#taken from Pegasos algorithm by avaitla
def SGD(a, lab, Q, lr):
    for i in range(20):
        iterations = 1
        for tau in range(len(a)):
            if a[tau] > 0:
                wx = a @ Q[:,tau]
                a[tau] *= (1 - 1/iterations)
                if(lab[tau]*wx < 1):
                    a[tau] += lab[tau]/(lr * iterations)
                iterations += 1
    return a

def attention_sgd(x,y,a=None):
    if a == None:
        a = np.zeros(x.shape[1])
    else:
        a = np.resize(a,x.shape[1])        
    for i in range(5):
        for tau in range(x.shape[1]):
            gh = abs(1-np.min(attention(np.mat(x),np.mat(y),a[tau]))) * np.gradient(x.T*a[tau],axis=0)[tau]

            et = sigmoid(np.array(gh))