оценщик должен быть оценщиком, реализующим метод «подгонки» - PullRequest
0 голосов
/ 02 июня 2018

когда я пытаюсь запустить код на python, я сталкиваюсь с этой проблемой, как я могу решить ее?) ') 96 #if name == " main ": main () ## с if 97

<ipython-input-23-95022ce9a680> in validation(classifier, data, y_data, y_target, class_names, title)
     52         print ("No accuracy to be computed")
     53     else:
---> 54         accuracy = model_selection.cross_val_score(classifier,x, y_target, scoring='accuracy')
     55         print("Accuracy: "+ str(accuracy))
     56     precision = model_selection.cross_val_score(self.classifier, x, target, scoring='precision')

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in cross_val_score(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch)
    130     cv = check_cv(cv, y, classifier=is_classifier(estimator))
    131     cv_iter = list(cv.split(X, y, groups))
--> 132     scorer = check_scoring(estimator, scoring=scoring)
    133     # We clone the estimator to make sure that all the folds are
    134     # independent, and that it is pickle-able.

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\metrics\scorer.py in check_scoring(estimator, scoring, allow_none)
    248     if not hasattr(estimator, 'fit'):
    249         raise TypeError("estimator should be an estimator implementing "
--> 250                         "'fit' method, %r was passed" % estimator)
    251     if isinstance(scoring, six.string_types):
    252         return get_scorer(scoring)

TypeError: estimator should be an estimator implementing 'fit' method, <__main__.Softmax object at 0x00000000048D1F98> was passed

import numpy as np import math import randomиз оператора import itemgetter

class Softmax: # из IPython.core.debugger import Tracer;Tracer () ()

def __init__(self, batch_size=50, epochs=1000, learning_rate=1e-2, reg_strength=1e-5, weight_update='adam'):
    self.W = None
    self.batch_size = batch_size
    self.epochs = epochs
    self.learning_rate = learning_rate
    self.reg_strength = reg_strength
    self.weight_update = weight_update

def train(self, X, y):
    n_features = X.shape[1]
    n_classes = y.max() + 1
    self.W = np.random.randn(n_features, n_classes) / np.sqrt(n_features/2)
    config = {'reg_strength': self.reg_strength, 'batch_size': self.batch_size,
            'learning_rate': self.learning_rate, 'eps': 1e-8, 'decay_rate': 0.99,
            'momentum': 0.9, 'cache': None, 'beta_1': 0.9, 'beta_2':0.999,
            'velocity': np.zeros(self.W.shape)}
    c = globals()['Softmax']
    for epoch in range(self.epochs):
        loss, config = getattr(c, self.weight_update)(self, X, y, config)
        print ("Epoch:" +str(epoch)+", Loss: "+str(loss))

def predict(self, X):
    return np.argmax(X.dot(self.W), 1)

def loss(self, X, y, W, b, reg_strength):
    sample_size = X.shape[0]
    predictions = X.dot(W) + b

    # Fix numerical instability
    predictions -= predictions.max(axis=1).reshape([-1, 1])

    # Run predictions through softmax
    softmax = math.e**predictions
    softmax /= softmax.sum(axis=1).reshape([-1, 1])

    # Cross entropy loss
    loss = -np.log(softmax[np.arange(len(softmax)), y]).sum() 
    loss /= sample_size
    loss += 0.5 * reg_strength * (W**2).sum()

    softmax[np.arange(len(softmax)), y] -= 1
    dW = (X.T.dot(softmax) / sample_size) + (reg_strength * W)
    return loss, dW

def sgd(self, X, y, config):
    items = itemgetter('learning_rate', 'batch_size', 'reg_strength')(config)
    learning_rate, batch_size, reg_strength = items

    loss, dW = self.sample_and_calculate_gradient(X, y, batch_size, self.W, 0, reg_strength)

    self.W -= learning_rate * dW
    return loss, config

def sgd_with_momentum(self, X, y, config):
    items = itemgetter('learning_rate', 'batch_size', 'reg_strength', 'momentum')(config)
    learning_rate, batch_size, reg_strength, momentum = items

    loss, dW = self.sample_and_calculate_gradient(X, y, batch_size, self.W, 0, reg_strength)

    config['velocity'] = momentum*config['velocity'] - learning_rate*dW
    self.W += config['velocity']
    return loss, config

def rms_prop(self, X, y, config):
    items = itemgetter('learning_rate', 'batch_size', 'reg_strength', 'decay_rate', 'eps', 'cache')(config)
    learning_rate, batch_size, reg_strength, decay_rate, eps, cache = items

    loss, dW = self.sample_and_calculate_gradient(X, y, batch_size, self.W, 0, reg_strength)

    cache = np.zeros(dW.shape) if cache == None else cache
    cache = decay_rate * cache + (1-decay_rate) * dW**2
    config['cache'] = cache

    self.W -= learning_rate * dW / (np.sqrt(cache) + eps)
    return loss, config

def adam(self, X, y, config):
    items = itemgetter('learning_rate', 'batch_size', 'reg_strength', 'eps', 'beta_1', 'beta_2')(config)
    learning_rate, batch_size, reg_strength, eps, beta_1, beta_2 = items
    config.setdefault('t', 0)
    config.setdefault('m', np.zeros(self.W.shape))
    config.setdefault('v', np.zeros(self.W.shape))

    loss, dW = self.sample_and_calculate_gradient(X, y, batch_size, self.W, 0, reg_strength)

    config['t'] += 1
    config['m'] = config['m']*beta_1 + (1-beta_1)*dW
    config['v'] = config['v']*beta_2 + (1-beta_2)*dW**2
    m = config['m']/(1-beta_1**config['t'])
    v = config['v']/(1-beta_2**config['t'])
    self.W -= learning_rate*m/(np.sqrt(v)+eps)
    return loss, config

def sample_and_calculate_gradient(self, X, y, batch_size, w, b, reg_strength):
    random_indices = random.sample(range(X.shape[0]), batch_size)
    X_batch = X[random_indices]
    y_batch = y[random_indices]
    return self.loss(X_batch, y_batch, w, b, reg_strength)

1 Ответ

0 голосов
/ 02 июня 2018

Должно работать (или, по крайней мере, исправляет текущую ошибку), если вы измените

def train(self, X, y):

на

def fit(self, X, y):

Для действительной оценки sklearn нужны fit и predictметоды.


Проверьте это Как создать пользовательский класс Sklearn Estimator

Попробуйте заменить:

class Softmax:

На:

from sklearn.base import BaseEstimator, ClassifierMixin
class Softmax(BaseEstimator, ClassifierMixin):  
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...