欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python报错:float object is not callable

程序员文章站 2022-07-14 23:43:35
...

如图报错

python报错:float object is not callablepython报错:float object is not callable

出错代码

# -*- coding: utf-8 -*-
"""
@File    : 梯度下降法求函数最小值.py
@Time    : 2020/5/17 21:30
@Author  : Dontla
@Email   : aaa@qq.com
@Software: PyCharm
"""


# 函数
def f(a, b):
    return a ** 2 + b ** 2


# 梯度
def gra_f2x(a, b):
    return 2 * a


def gra_f2y(a, b):
    return 2 * b


# 位移向量
def delta_x(gra_x, ita):
    return gra_x * ita


def delta_y(gra_y, ita):
    return gra_y * ita


if __name__ == '__main__':
    # 学习率
    eta = 0.1

    # 初始参数
    x, y = 3.00, 2.00
    gra_f2x, gra_f2y = gra_f2x(x, y), gra_f2y(x, y)
    delta_x, delta_y = delta_x(gra_f2x, eta), delta_y(gra_f2y, eta)
    z = f(x, y)

    # 执行梯度下降法
    for i in range(0, 100):
        print(
            '第{:0>2d}轮:\tx={:.2f}\ty={:.2f}\tgra_f2x={:.2f}\tgra_f2y={:.2f}\tdelta_x={:.2f}\tdelta_y={:.2f}\tz={:.2f}'.format(
                i, x, y, gra_f2x, gra_f2y, delta_x, delta_y, z))
        # 更新变量
        x = x + delta_x
        y = y + delta_y
        gra_f2x = gra_f2x(x, y)
        gra_f2y = gra_f2y(x, y)
        delta_x = delta_x(gra_f2x, eta)
        delta_y = delta_y(gra_f2y, eta)
        z = f(x, y)

解决办法

说是变量不能跟函数重名??

不太懂g

python报错:float object is not callable
引用自:‘float’ object is not callable报错问题

然后我把函数全部重命名后,就好了。。。

# -*- coding: utf-8 -*-
"""
@File    : 梯度下降法求函数最小值.py
@Time    : 2020/5/17 21:30
@Author  : Dontla
@Email   : aaa@qq.com
@Software: PyCharm
"""


# 函数
def f(a, b):
    return a ** 2 + b ** 2


# 梯度
def calcu_gra_f2x(a, b):
    return 2 * a


def calcu_gra_f2y(a, b):
    return 2 * b


# 位移向量
def calcu_delta_x(gra_x, ita):
    return gra_x * -ita


def calcu_delta_y(gra_y, ita):
    return gra_y * -ita


if __name__ == '__main__':
    # 学习率
    eta = 0.1

    # 初始参数
    x, y = 3.00, 2.00
    gra_f2x, gra_f2y = calcu_gra_f2x(x, y), calcu_gra_f2y(x, y)
    delta_x, delta_y = calcu_delta_x(gra_f2x, eta), calcu_delta_y(gra_f2y, eta)
    z = f(x, y)

    # 执行梯度下降法
    for i in range(0, 20):
        print(
            '第{:0>2d}轮\tx={:.2f}\ty={:.2f}\tgra_f2x={:.2f}\tgra_f2y={:.2f}\tdelta_x={:.2f}\tdelta_y={:.2f}\tz={:.2f}'.format(
                i, x, y, gra_f2x, gra_f2y, delta_x, delta_y, z))
        # 更新变量
        x = x + delta_x
        y = y + delta_y
        gra_f2x = calcu_gra_f2x(x, y)
        gra_f2y = calcu_gra_f2y(x, y)
        delta_x = calcu_delta_x(gra_f2x, eta)
        delta_y = calcu_delta_y(gra_f2y, eta)
        z = f(x, y)

结果:

D:\20200228_play_with_pytorch\python\python.exe C:/Users/SIQI/Desktop/20200511_用Excel体验梯度下降法(改成用python体验)/梯度下降法求函数最小值/梯度下降法求函数最小值.py
第00轮	x=3.00	y=2.00	gra_f2x=6.00	gra_f2y=4.00	delta_x=-0.60	delta_y=-0.40	z=13.0001轮	x=2.40	y=1.60	gra_f2x=4.80	gra_f2y=3.20	delta_x=-0.48	delta_y=-0.32	z=8.3202轮	x=1.92	y=1.28	gra_f2x=3.84	gra_f2y=2.56	delta_x=-0.38	delta_y=-0.26	z=5.3203轮	x=1.54	y=1.02	gra_f2x=3.07	gra_f2y=2.05	delta_x=-0.31	delta_y=-0.20	z=3.4104轮	x=1.23	y=0.82	gra_f2x=2.46	gra_f2y=1.64	delta_x=-0.25	delta_y=-0.16	z=2.1805轮	x=0.98	y=0.66	gra_f2x=1.97	gra_f2y=1.31	delta_x=-0.20	delta_y=-0.13	z=1.4006轮	x=0.79	y=0.52	gra_f2x=1.57	gra_f2y=1.05	delta_x=-0.16	delta_y=-0.10	z=0.8907轮	x=0.63	y=0.42	gra_f2x=1.26	gra_f2y=0.84	delta_x=-0.13	delta_y=-0.08	z=0.5708轮	x=0.50	y=0.34	gra_f2x=1.01	gra_f2y=0.67	delta_x=-0.10	delta_y=-0.07	z=0.3709轮	x=0.40	y=0.27	gra_f2x=0.81	gra_f2y=0.54	delta_x=-0.08	delta_y=-0.05	z=0.2310轮	x=0.32	y=0.21	gra_f2x=0.64	gra_f2y=0.43	delta_x=-0.06	delta_y=-0.04	z=0.1511轮	x=0.26	y=0.17	gra_f2x=0.52	gra_f2y=0.34	delta_x=-0.05	delta_y=-0.03	z=0.1012轮	x=0.21	y=0.14	gra_f2x=0.41	gra_f2y=0.27	delta_x=-0.04	delta_y=-0.03	z=0.0613轮	x=0.16	y=0.11	gra_f2x=0.33	gra_f2y=0.22	delta_x=-0.03	delta_y=-0.02	z=0.0414轮	x=0.13	y=0.09	gra_f2x=0.26	gra_f2y=0.18	delta_x=-0.03	delta_y=-0.02	z=0.0315轮	x=0.11	y=0.07	gra_f2x=0.21	gra_f2y=0.14	delta_x=-0.02	delta_y=-0.01	z=0.0216轮	x=0.08	y=0.06	gra_f2x=0.17	gra_f2y=0.11	delta_x=-0.02	delta_y=-0.01	z=0.0117轮	x=0.07	y=0.05	gra_f2x=0.14	gra_f2y=0.09	delta_x=-0.01	delta_y=-0.01	z=0.0118轮	x=0.05	y=0.04	gra_f2x=0.11	gra_f2y=0.07	delta_x=-0.01	delta_y=-0.01	z=0.0019轮	x=0.04	y=0.03	gra_f2x=0.09	gra_f2y=0.06	delta_x=-0.01	delta_y=-0.01	z=0.00

Process finished with exit code 0

相关标签: Python