python报错:float object is not callable
程序员文章站
2022-07-14 23:43:35
...
如图报错
出错代码
# -*- 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
引用自:‘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.00
第01轮 x=2.40 y=1.60 gra_f2x=4.80 gra_f2y=3.20 delta_x=-0.48 delta_y=-0.32 z=8.32
第02轮 x=1.92 y=1.28 gra_f2x=3.84 gra_f2y=2.56 delta_x=-0.38 delta_y=-0.26 z=5.32
第03轮 x=1.54 y=1.02 gra_f2x=3.07 gra_f2y=2.05 delta_x=-0.31 delta_y=-0.20 z=3.41
第04轮 x=1.23 y=0.82 gra_f2x=2.46 gra_f2y=1.64 delta_x=-0.25 delta_y=-0.16 z=2.18
第05轮 x=0.98 y=0.66 gra_f2x=1.97 gra_f2y=1.31 delta_x=-0.20 delta_y=-0.13 z=1.40
第06轮 x=0.79 y=0.52 gra_f2x=1.57 gra_f2y=1.05 delta_x=-0.16 delta_y=-0.10 z=0.89
第07轮 x=0.63 y=0.42 gra_f2x=1.26 gra_f2y=0.84 delta_x=-0.13 delta_y=-0.08 z=0.57
第08轮 x=0.50 y=0.34 gra_f2x=1.01 gra_f2y=0.67 delta_x=-0.10 delta_y=-0.07 z=0.37
第09轮 x=0.40 y=0.27 gra_f2x=0.81 gra_f2y=0.54 delta_x=-0.08 delta_y=-0.05 z=0.23
第10轮 x=0.32 y=0.21 gra_f2x=0.64 gra_f2y=0.43 delta_x=-0.06 delta_y=-0.04 z=0.15
第11轮 x=0.26 y=0.17 gra_f2x=0.52 gra_f2y=0.34 delta_x=-0.05 delta_y=-0.03 z=0.10
第12轮 x=0.21 y=0.14 gra_f2x=0.41 gra_f2y=0.27 delta_x=-0.04 delta_y=-0.03 z=0.06
第13轮 x=0.16 y=0.11 gra_f2x=0.33 gra_f2y=0.22 delta_x=-0.03 delta_y=-0.02 z=0.04
第14轮 x=0.13 y=0.09 gra_f2x=0.26 gra_f2y=0.18 delta_x=-0.03 delta_y=-0.02 z=0.03
第15轮 x=0.11 y=0.07 gra_f2x=0.21 gra_f2y=0.14 delta_x=-0.02 delta_y=-0.01 z=0.02
第16轮 x=0.08 y=0.06 gra_f2x=0.17 gra_f2y=0.11 delta_x=-0.02 delta_y=-0.01 z=0.01
第17轮 x=0.07 y=0.05 gra_f2x=0.14 gra_f2y=0.09 delta_x=-0.01 delta_y=-0.01 z=0.01
第18轮 x=0.05 y=0.04 gra_f2x=0.11 gra_f2y=0.07 delta_x=-0.01 delta_y=-0.01 z=0.00
第19轮 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报错:float object is not callable
-
Python报错:TypeError: the JSON object must be str, bytes or bytearray, not ‘dict‘
-
python报错:TypeError: ‘NoneType‘ object is not subscriptable
-
Python网络编程报错TypeError: a bytes-like object is required, not 'str' 的解决办法
-
报错:Expected object of scalar type Float but got scalar type Long for argument #2 'target'
-
python报错:AttributeError: 'module' object has no attribute 'xfeatures2d'
-
python: tuple object is not callable
-
python-pip升级报错- AttributeError: 'NoneType' object has no attribute 'bytes'
-
树莓派,运行python+opencv+pyqt5编写的程序报错[g_object_new_with_properties: assertion ‘G_TYPE_IS_OBJECT (object_]
-
python2.7 报错 Input 'split_dim' of 'Split' Op has type float32 that does not match expected type of