ValueError:操作数不能与形状(13,1)(16,1)一起广播

时间:2019-11-24 17:08:36

标签: python-3.x machine-learning artificial-intelligence

def  cal_cost(theta,X,y):
    m = len(y)    
    λ = 0.01
    predictions = X.dot(theta)
    cost = (1/2*m) * (np.sum(np.abs(predictions-y)) + np.sum(λ * np.square(theta))) #regularized absolute cost error
    return cost

def minibatch_gradient_descent(X,y,batch_size):
    m = len(y)
    iterations = 450
    cost_history = np.zeros(iterations)
    time_history = np.zeros(iterations)
    learning_rate =0.3 
    theta = np.zeros((X_train.shape[1], 1))
    gd_update = np.zeros(iterations)
    t2_start = time.time()

    for it in range(iterations):
        cost =0.0
        indices = np.random.permutation(m)
        X = X[indices]
        y = y[indices]

        for i in range(0,m,batch_size):
            n_batches = int(m/batch_size)
            X_i = X[i:i+batch_size]
            y_i = y[i:i+batch_size]         
            prediction = np.dot(X_i,theta)
            theta = theta -(1/m)*learning_rate* ((prediction - y_i)/abs(prediction - y_i))
            cost += cal_cost(theta,X_i,y_i)

        cost_history[it]  = cost

    return theta, cost_history

batch_size = [1,16,32,64,128,256,404]
minibatch_gradient_descent(X_train, y_train,batch_size[i])

当我运行上述代码时,出现上述错误。 X_train.shape为(404,13)和y_train.shape为(404,1) 请帮助我纠正我错的代码。

0 个答案:

没有答案