# We first define the observations as a list and then also as a table for the experienced worker's performance.
Observation1 = [2.0, 6.0, 2.0]
Observation2 = [1.0, 5.0, 7.0]
Observation3 = [5.0, 2.0, 1.0]
Observation4 = [2.0, 3.0, 8.0]
Observation5 = [4.0, 4.0, 0.0]
ObservationTable = [
Observation1,
Observation2,
Observation3,
Observation4,
Observation5
]
# Then we define our learning rate, number of observations, and the epoch counters we will be utilizing (10, 100, and 1000).
LearningRate = 0.01
ObservationCounter = 5
EpochVersion1 = 10
EpochVersion2 = 100
EpochVersion3 = 1000
# Thus, we are now ready to define the Stochastic Gradient Descent Algorithm:
def StochasticGradientDescent(EpochCounter):
Theta0 = 10.0
Theta1 = 0.0
Theta2 = -1.0
while (EpochCounter != 0):
ObservationCounter = 5
while (ObservationCounter >= 0):
Theta0_Old = float(Theta0)
Theta1_Old = float(Theta1)
Theta2_Old = float(Theta2)
n = 5 - ObservationCounter
x = ObservationTable [n]
x0 = float(x[0])
x1 = float(x[1])
x2 = float(x[2])
Theta0_New = Theta0_Old - LearningRate*[(Theta0_Old+Theta1_Old*float(x0)+Theta2_Old*float(x1))-float(x2)]
Theta1_New = Theta1_Old - LearningRate*[(Theta0_Old+Theta1_Old*float(x0)+Theta2_Old*float(x1))-float(x2)]*float(x0)
Theta2_New = Theta2_Old - LearningRate*[(Theta0_Old+Theta1_Old*float(x0)+Theta2_Old*float(x1))-float(x2)]*float(x1)
print(Theta0_New, Theta1_New, Theta2_New)
ObservationCounter -= 1
else:
EpochCounter -= 1
if (EpochCounter == 0):
print(Theta0_New, Theta1_New, Theta2_New)
StochasticGradientDescent(int(EpochVersion1))
代码输出 TypeError: can't multiply sequence by non-int of type 'float'。我已经在每个可能的步骤将值转换为浮点数,但错误仍然存在。关键线主要是与 SGD 的定义功能有关的那些。