Python函数返回错误值

时间:2018-11-30 02:26:17

标签: python function return return-value gradient-descent

我在python中有一个函数(梯度下降),可以返回一些值:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


def read_data(file):
    df = pd.read_excel(file)
    x_data= np.array(df['X_axis']) 
    y_data = np.array(df['Y_axis']) 

    return x_data,y_data 


x_data ,y_data = read_data('path\file')

alpha= 10**-8

#Auxiliaries Functions
...

这是函数:

def gradient(x_axis,y_axis):

    current_iteration = 0
    iterations_number= 100

    #Initial values
    #I guess that the problem is here. When I print those initial 
    #values,inside the function, they are different from what they should be   


    A1_inicial = y_axis[0] 
    A2_inicial = y_axis[-1] 
    x0_inicial = np.mean(x_axis) 
    dx_inicial = (np.std(x_axis))   


    while current_iteration < iterations_number:
        sum_A1 = 0
        sum_A2 = 0
        sum_dx = 0
        sum_x0 = 0


       for x_values,y_values  in zip(x_axis, y_axis):
            sum_A1 += derivada_A1(A1_inicial,A2_inicial,x0_inicial,dx_inicial,x_values,y_values  )
            sum_A2 += derivada_A2(A1_inicial,A2_inicial,x0_inicial,dx_inicial,x_values,y_values  )
            sum_dx += derivada_dx(A1_inicial,A2_inicial,x0_inicial,dx_inicial,x_values,y_values  )
            sum_x0 += derivada_x0(A1_inicial,A2_inicial,x0_inicial,dx_inicial,x_values,y_values  )



            A1_inicial = A1_inicial - (alpha* sum_A1 )
            A2_inicial = A2_inicial - (alpha* sum_A2 )
            x0_inicial = x0_inicial - (alpha* sum_x0 )
            dx_inicial = dx_inicial - (alpha* sum_dx )



        return A1_inicial,A2_inicial,x0_inicial,dx_inicial
        current_iteration+=1

因此,问题在于,每当我使用此函数时,返回的值都是错误的。我知道它们是错误的,因为我有可以比较的真实价值。

这是我使用该功能的方式:

new_y = []

A1_inicial,A2_inicial,x0_inicial,dx_inicial = gradient(x_axis,y_axis)

for x in x_axis:
    new_y.append(A2_inicial + ((A1_inicial - A2_inicial) /(1+np.exp((x- x0_inicial)/dx_inicial))))

print("A1: {}".format(A1_inicial))
print("A2: {}".format(A2_inicial))
print("X0: {}".format(x0_inicial))
print("DX: {}".format(dx_inicial))

我知道,如果我撤消此功能,就像这样:

current_iteration = 0
iterations_number= 100


A1_inicial = y_axis[0] 
A2_inicial = y_axis[-1] 
x0_inicial = np.mean(x_axis) 
dx_inicial = (np.std(x_axis))   


while current_iteration < iterations_number:
    sum_A1 = 0
    sum_A2 = 0
    sum_dx = 0
    sum_x0 = 0


    for x_values,y_values in zip(x_axis, y_axis):
        sum_A1 += derivada_A1(A1_inicial,A2_inicial,x0_inicial,dx_inicial,x_values,y_values  )
        sum_A2 += derivada_A2(A1_inicial,A2_inicial,x0_inicial,dx_inicial,x_values,y_values  )
        sum_dx += derivada_dx(A1_inicial,A2_inicial,x0_inicial,dx_inicial,x_values,y_values  )
        sum_x0 += derivada_x0(A1_inicial,A2_inicial,x0_inicial,dx_inicial,x_values,y_values  )

    A1_inicial = A1_inicial - (alpha* sum_A1 )
    A2_inicial = A2_inicial - (alpha* sum_A2 )
    x0_inicial = x0_inicial - (alpha* sum_x0 )
    dx_inicial = dx_inicial - (alpha* sum_dx )

然后:

new_y = []


for x in x_axis:
    new_y.append(A2_inicial + ((A1_inicial - A2_inicial) /(1+np.exp((x- x0_inicial)/dx_inicial))))

print("A1: {}".format(A1_inicial))
print("A2: {}".format(A2_inicial))
print("X0: {}".format(x0_inicial))
print("DX: {}".format(dx_inicial))

它起作用了,给了我正确的值,但是我不知道该函数出了什么问题,为什么它不起作用。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

在渐变函数中,在增加current_iteration变量之前,您具有return语句。因此,它永远不会增加。该函数在返回点终止。

在return语句之前添加current_iteration增量器,并在while循环结束后添加return语句。