# 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))
The code outputs the TypeError: can't multiply sequence by non-int of type 'float'. I have already converted the values into floats at each possible step, but the error still remains. The key lines are mostly those relating to the definition function for the SGD.
I do not know very much about stochastic gradient descent but there are two improvements that I spot in your code.
Firstly, the error is caused because you try to multiply a float with a list and add that to a float. This is fixed by using round brackets instead of square:
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)
Secondly your while loop should end one iteration early, otherwise you will get an error when you try to access ObservationTable[5]
.
Therefore change your while loop to:
while (ObservationCounter >= 1):
output:
9.98 -0.04 -1.12
10.02 0.02 -0.9
9.93 -0.35000000000000003 -1.1400000000000001
10.01 0.02 -0.97
9.94 -0.24 -1.24
9.98 -0.04 -1.12
10.02 0.02 -0.9
9.93 -0.35000000000000003 -1.1400000000000001
10.01 0.02 -0.97
9.94 -0.24 -1.24
9.98 -0.04 -1.12
10.02 0.02 -0.9
9.93 -0.35000000000000003 -1.1400000000000001
10.01 0.02 -0.97
9.94 -0.24 -1.24
9.98 -0.04 -1.12
10.02 0.02 -0.9
9.93 -0.35000000000000003 -1.1400000000000001
10.01 0.02 -0.97
9.94 -0.24 -1.24
9.98 -0.04 -1.12
10.02 0.02 -0.9
9.93 -0.35000000000000003 -1.1400000000000001
10.01 0.02 -0.97
9.94 -0.24 -1.24
9.98 -0.04 -1.12
10.02 0.02 -0.9
9.93 -0.35000000000000003 -1.1400000000000001
10.01 0.02 -0.97
9.94 -0.24 -1.24
9.98 -0.04 -1.12
10.02 0.02 -0.9
9.93 -0.35000000000000003 -1.1400000000000001
10.01 0.02 -0.97
9.94 -0.24 -1.24
9.98 -0.04 -1.12
10.02 0.02 -0.9
9.93 -0.35000000000000003 -1.1400000000000001
10.01 0.02 -0.97
9.94 -0.24 -1.24
9.98 -0.04 -1.12
10.02 0.02 -0.9
9.93 -0.35000000000000003 -1.1400000000000001
10.01 0.02 -0.97
9.94 -0.24 -1.24
9.98 -0.04 -1.12
10.02 0.02 -0.9
9.93 -0.35000000000000003 -1.1400000000000001
10.01 0.02 -0.97
9.94 -0.24 -1.24
9.94 -0.24 -1.24
Final remark:
You do not need to convert EpochVersion1 to an integer: int(EpochVersion1)
.
It is already an integer when you declare it as EpochVersion1 = 10
.