确定成本的迭代方法

时间:2018-09-29 01:04:16

标签: python machine-learning iteration

因此,我正在尝试使用机器学习课程中介绍的一种迭代方法来确定三个不同项目的成本。我遇到的问题不是获得输出,而是解决方案似乎并未收敛,我认为这是应该的。我想我想说的是,我认为迭代方法不会跳来跳去,但会从上一次迭代中学到东西,但事实并非如此。代码如下:

import numpy as np
import matplotlib.pyplot as plt
import sys


# Fill in the codes between "%PLACEHOLDER#start" and "PLACEHOLDER#end"

# Ground-truth Cashier
groundUnitPrice = np.array([20, 25, 8])  # for fish, chip, and ketchup, respectively

# step 1: initialize your guess on the unit prices of fish, chip and ketchup.
estimatedUnitPrice = np.array([10, 10, 10])  # initial unit prices.
# MAX_POSSIBLE_UNIT_PRICE = 50
# estimatedUnitPrice = np.random.randint(MAX_POSSIBLE_UNIT_PRICE, size=3) # choose random initial guesses

# PLACEHOLDER_1#start: set your own stopping conditions and learning rate
# condition 1: maximal iterations, stop.
MAX_ITERATION = 100000
# condition 2: if the difference between your prediction and the cashier's price is smaller than a threshold, stop.
MIN_DELTA = 0.01
# learning rate
ALPHA = .001  # 1e-3
# PLACEHOLDER_1#end

# Y coordinates for plotting
deltaHistory = []


# step 2: iterative method
for i in range(0, MAX_ITERATION):
    # order a meal (simulating training data)
    randomMealPortions = np.random.randint(10, size=3)

    # calculate the estimated price     
    expectedTotalPrice = np.sum(estimatedUnitPrice * randomMealPortions)

    # calculate cashier/true price;     
    cashierPrice = np.sum(groundUnitPrice * randomMealPortions)

    # %%%PLACEHOLDER_2#start

    # calculate current error - difference between estimated price and true price
    iterError = expectedTotalPrice - cashierPrice

    # append iterError to the history array
    deltaHistory.append(abs(iterError))
    if len(deltaHistory) == 1:
        delta = abs(deltaHistory[-1])
        print("The delta value after iteration ", i, " is: ", delta)
    else:
        delta = abs(deltaHistory[-1]-deltaHistory[-2])
        print("The delta value after iteration ", i, " is: ", delta)

    # update unit prices
    estimatedUnitPrice = estimatedUnitPrice - ALPHA*(estimatedUnitPrice - cashierPrice)

    # %%%%PLACEHOLDER_2#end

    # check stop conditions
    if abs(delta) < MIN_DELTA:
        break

    print('iteration:{}, delta:{}'.format(i, abs(delta)))


# step 3: evaluation
error = np.mean(abs(estimatedUnitPrice - groundUnitPrice))
print('estimation error:{}'.format(error))

# visualize convergence curve: error v.s. iterations

plt.plot(range(0, len(deltaHistory)), deltaHistory)
plt.xlabel('iteration (cnt:{})'.format(len(deltaHistory)))
plt.ylabel('abs(delta)')
plt.title('Final:{}  est err:{}  actl Δ:{}'.format(['%.4f' % elem for elem in estimatedUnitPrice], round(error, 4), round(delta, 4)))
plt.show()

因此,我认为随着算法从自身中学到的值,delta值会开始变大,然后变得越来越小,但是似乎只是在跳跃。我编写的唯一代码在#%%% PLACEHOLDER_2#start和#%%% PLACEHOLDER_2#end之间,因为是的,这是一个分配。我是在做错什么,还是有人可以提供一些见解或解释?请谢谢!

0 个答案:

没有答案