Ftrl理论基础与代码实现

一、理论基础

 

二、伪代码:

Ftrl理论基础与代码实现_第1张图片

 

 

三、Python Code:

alpha = 0.9
beta = 1  # smoothing parameter for adaptive learning rate
lambda1 = 0.3
lambda2 = 0.3
z = []
n = []
omiga = []
for i in range(0, 18):
    omiga.append(1.0)
    z.append(0.0)
    n.append(0.0)

train_data_raw_ftrl = train_data_raw.drop(['Ticket', 'Cabin', 'Age', 'Fare', 'Name', 'PassengerId'], axis=1)

all_data_ftrl = pd.get_dummies(train_data_raw_ftrl)
all_data_ftrl = np.array(all_data_ftrl)
# print(all_data_ftrl[1:100, 0])

for i in range(0, len(all_data_ftrl)):
    all_data_ftrl_x = all_data_ftrl[i, 1:19]
    all_data_ftrl_y = all_data_ftrl[i, 0]

    for j in range(0, 18):
        sgn_z = 1 if z[j] >= 0 else -1

        if all_data_ftrl_x[j] == 0:
            continue
        if abs(z[j]) <= lambda1:
            omiga[j] = 0
        else:
            omiga[j] = (lambda1 * sgn_z - z[j]) / ((beta + np.sqrt(n[j])) / alpha + lambda2)
    prob = 1.0 / (1 + math.exp(-1 * np.dot(omiga, all_data_ftrl_x)))

    for j in range(0, 18):
        if all_data_ftrl_x[j] == 0:
            continue
        g = (prob - all_data_ftrl_y) * all_data_ftrl_x[j]
        # print("gradient of loss: ", g)
        deta = (math.sqrt(n[j] + g * g) - math.sqrt(n[j])) / alpha
        z[j] = z[j] + g - deta * omiga[j]
        n[j] = n[j] + g * g
    # print(omiga)

 

你可能感兴趣的:(Ftrl理论基础与代码实现)