фигуры (401,1) и (401,1) не выровнены: 1 (тусклый 1)! = 401 (тусклый 0) - PullRequest
0 голосов
/ 25 апреля 2018

Я реализую классификатор «один против всех», однако я получил ошибку «формы (401,1) и (401,1) не выровнены: 1 (dim 1)! = 401 (dim 0)», иНиже приведена трассировка:

Traceback (most recent call last):

File "<ipython-input-1-682bb50c2435>", line 1, in <module>
    runfile('/Users/alvin/Documents/GitDemo/ML_Basic_Imple/Coursera_ML_Python/ex3/Multi_classify_oneVSall.py', wdir='/Users/alvin/Documents/GitDemo/ML_Basic_Imple/Coursera_ML_Python/ex3')

      File "/Users/alvin/Documents/tools/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 710, in runfile
        execfile(filename, namespace)

  File "/Users/alvin/Documents/tools/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/Users/alvin/Documents/GitDemo/ML_Basic_Imple/Coursera_ML_Python/ex3/Multi_classify_oneVSall.py", line 124, in <module>
    trained_theta = training_OnevsAll_theta(X,y,10,0.1)

  File "/Users/alvin/Documents/GitDemo/ML_Basic_Imple/Coursera_ML_Python/ex3/Multi_classify_oneVSall.py", line 119, in training_OnevsAll_theta
    theta,cost = opt_Cost(initial_theta,X,y,lamada)

File "/Users/alvin/Documents/GitDemo/ML_Basic_Imple/Coursera_ML_Python/ex3/Multi_classify_oneVSall.py", line 96, in opt_Cost
    res = optimize.fmin_bfgs(LR_Costfunction, theta, fprime=Gradient, args=(X,y,lamada) )

  File "/Users/alvin/Documents/tools/anaconda3/lib/python3.6/site-packages/scipy/optimize/optimize.py", line 859, in fmin_bfgs
    res = _minimize_bfgs(f, x0, args, fprime, callback=callback, **opts)

  File "/Users/alvin/Documents/tools/anaconda3/lib/python3.6/site-packages/scipy/optimize/optimize.py", line 934, in _minimize_bfgs
    old_fval, old_old_fval, amin=1e-100, amax=1e100)

  File "/Users/alvin/Documents/tools/anaconda3/lib/python3.6/site-packages/scipy/optimize/optimize.py", line 765, in _line_search_wolfe12
    **kwargs)

  File "/Users/alvin/Documents/tools/anaconda3/lib/python3.6/site-packages/scipy/optimize/linesearch.py", line 97, in line_search_wolfe1
    derphi0 = np.dot(gfk, pk)

ValueError: shapes (401,1) and (401,1) not aligned: 1 (dim 1) != 401 (dim 0)e

Не могли бы вы найти какую-либо проблему в моем нижеследующем коде?

Спасибо за вашего пациента!

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy.io 
import scipy.misc 
import matplotlib.cm as cm # Used to display images in a specific colormap
import random
from scipy.special import expit


datapath = 'data/ex3data1.mat'
data = scipy.io.loadmat(datapath)
X = data['X']
y = data['y']
print(X.shape)
print(y.shape)

def _display_data():

    all_fig = np.zeros((10*20,10*20)) 
    index_of_samples = random.sample(range(X.shape[0]),100) 

    row, col = 0, 0
    for i in index_of_samples:
        if col == 10:
            row += 1
            col = 0 
        fig = X[i].reshape(20,20).T       
        all_fig[row * 20:(row+1)*20,col * 20:(col+1)*20] = fig
        col += 1

    plt.figure(figsize=(8,8))
    img = scipy.misc.toimage(all_fig)
    plt.imshow(img, cmap = plt.cm.gray_r)

_display_data()

# ============ Part 2a: Vectorize Logistic Regression ============

def hpy_sigmod_fucntion(X_inter,theta_inter):

    return expit(np.dot(X_inter,theta_inter))

def LR_Costfunction(theta_inter,X_inter,y,lamada=0.):

    m = X_inter.shape[0] 
    hyp = hpy_sigmod_fucntion(X_inter,theta_inter)   
    reg = np.dot(theta_inter.T,theta_inter) * (lamada / (2 * m))
    J = np.dot(y.T,np.log(hyp))+np.dot((1 - y.T),np.log(1 - hyp)) 
    return J + reg


def Gradient(theta_inter,X_inter,y,lamada=0.):

    m = X_inter.shape[0] 
    hyp = hpy_sigmod_fucntion(X_inter,theta_inter) 
    hyp = np.asarray(hyp).reshape(hyp.shape[0],1)
    h_y = hyp - y # 5000 * 1
    reg = theta_inter[1:] * (lamada / m) 
    reg = np.asarray(reg).reshape(reg.shape[0],1)
    grad = (1 / m) * np.dot(X_inter.T,h_y) # 401 * 1 
    grad[1:] = grad[1:] + reg
    return grad # 401 * 1

def opt_Cost(theta,X,y,lamada=0.):

    from scipy import optimize
    res = optimize.fmin_bfgs(LR_Costfunction, theta, fprime=Gradient, args=(X,y,lamada) )
    return result[0], result[1]

Эта функция может бытьпроблема.

Существуют ли какие-либо ограничения при использовании функций fmin?

def training_OnevsAll_theta(X,y,num_labels,lamada=0.):

    m = X.shape[0]
    n = X.shape[1]
    all_theta = np.zeros((num_labels,n+1))

    X = np.hstack((np.ones((m,1)),X)) 

    for c in range(num_labels):

        print("Training theta for class %d" %c)
        initial_theta = np.zeros((n+1,1))
        theta,cost = opt_Cost(initial_theta,X,y,lamada)
        all_theta[c] = theta

    print("Finished!")

trained_theta = training_OnevsAll_theta(X,y,10,0.1)

Спасибо!

1 Ответ

0 голосов
/ 27 апреля 2018

Ага, я нашел ответ на матрицы не выровнены Ошибка: Python SciPy fmin_bfgs

На самом деле, некорректный градиент ввода приводит к возникновению проблемы, поэтому я проследил за ответом и добавил код ниже, прежде чем 'return grad'

grad = np.ndarray.flatten(grad)

И это работает!

...