TypeError: predict() got an unexpected keyword argument 'k' - Error on knn using CIFAR-10, Python 2.7.12

Asked 2 years ago, Updated 2 years ago, 15 views

I would like to read CIFAR-10 (data set of 80 million color images labeled size 32x32), class it by knn, and output its accuracy as %, but I got the following error.The problem seems to be in line 86 k, but I can't find a solution, so I would appreciate it if you could give me some advice.

Code:

import pickle
import numpy as np
importos

default(file):
    fo=open(file, 'rb')
    dict=pickle.load(fo)
    fo.close()
    return dict

def conv_data2 image(data):
    return np.rollaxis(data.reshape((3,32,32)), 0,3)

default_cifar10(folder):
    tr_data=np.empty(0,32*32*3))
    tr_labels = np.empty(1)
    '''
    32x32x3
    '''
    for i in range (1, 6):
        fname=os.path.join(folder, "%s%d"%("data_batch_",i))
        data_dict=unpickle(fname)
        if i == 1:
            tr_data=data_dict ['data']
            tr_labels=data_dict ['labels']
        else:
            tr_data=np.vstack(tr_data,data_dict['data']))
            tr_labels = np.hstack ((tr_labels, data_dict ['labels']))

    data_dict=unpickle(os.path.join(folder, 'test_batch')))
    te_data=data_dict ['data']
    te_labels=np.array(data_dict['labels'])

    bm=unpickle(os.path.join(folder, 'batches.meta')))
    label_names=bm ['label_names']
    return tr_data, tr_labels, te_data, te_labels, label_names


class NearestNeighbor (object):
  def__init__(self):
    pass

  deftrain(self,X,y):
    """ X is N x D where each row is an example. Y is 1-dimension of size N""
    # The neighbor classifier simple members all the training data
    self.Xtr=X
    self.ytr=y

  def predict (self, X):
    """ X is N x D where each row is an example we wish to predict label for """""
    num_test = X.shape [0]
    # lets make sure that the output type matches the input type
    Ypred=np.zero(num_test,dtype=self.ytr.dtype)

    # loop over all test rows
    for i in xrange(num_test):
      # find the nearest training image to the i'th test image
      # using the L1 distance (sum of absolute value differences)
      distances=np.sqrt(np.sum(np.square(self.Xtr-X[i,:],axis=1))
      min_index=np.argmin(distances)# get the index with small distance
      Ypred[i]=self.ytr[min_index]#predict the label of the neighbor example

    return Ypred

if__name__=='__main__':
    datapath="./data/cifar-10-batches-py"

    Xtr, Ytr, Xte, Yte, label_names10 = get_cifar10(datapath)
    Xtr_rows = Xtr.reshape (Xtr.shape[0], 32*32*3)
    Xte_rows = Xte.reshape (Xte.shape[0], 32*32*3)
    # asset we have Xtr_rows, Ytr, Xte_rows, Yte as before
    # recall Xtr_rows is 50,000 x 3072 matrix
    Xval_rows = Xtr_rows [:1000,:] #take first1000 for validation
    Yval = Ytr [:1000]
    Xtr_rows = Xtr_rows [1000:,:] # keep last 49,000 for train
    Ytr = Ytr [1000:]

    # find hyperparameters that work best on the validation set
    validation_accuracies = [ ]

    forkin [1, 3, 5, 10, 20, 50, 100]:
        # use a partial value of k and evaluation on validation data
        nn = NearestNeighbor()
        nn.train(Xtr_rows, Ytr)
        # here we assign a modified NearestNeighbor class that can take a kas input
        Yval_predict=nn.predict(Xval_rows,k=k)
        acc=np.mean(Yval_predict==Yval)
        print'accuracy: %f'%(acc,)

        # keep track of what works on the validation set
        validation_accuracies.append(k,acc))

Error below

Traceback (most recent call last):File
"C://PycharmProjects/Convolutionary Neural Networks for Visual
Recognition /knn.py", line 86, in
Yval_predict=nn.predict(Xval_rows,k=k)TypeError:predict()got an unexpected keyword argument'k'

python

2022-09-30 15:50

1 Answers

First of all, the content of the error is that the definition of the predict method does not have the argument k, so the error appears.Also, if you look at the source, the predict method is 1-nn, which is identified only in the nearest neighborhood, not k-nn."Also, regarding the details, the comment says that the distance should be L1 distance, but it seems that the distance is kept in a mysterious way."Is the predict method that I added and modified the argument k similar to the following?

import collections

def predict (self, X, k):
  """X is N x D where each row is an example we wish to predict label for."""
  num_test = X.shape [0]
  # lets make sure that the output type matches the input type                                                                                                                                              
  Ypred=np.zero(num_test,dtype=self.ytr.dtype)

  # loop over all test rows                                                                                                                                                                                 
  for i in xrange(num_test):
    # find the nearest training image to the i'th test image                                                                                                                                                
    # using the L1 distance (sum of absolute value differences)                                                                                                                                             
    distances=np.sum (np.absolute(self.Xtr-X[i,:]), axis=1)
    k_min_index=np.argsort(distances)[k]#get the index with k-smallest distance                                                                                                                         
    k_label=self.ytr [k_min_index]#predict the labels of the k-nearest example                                                                                                                           
    label,_=collections.Counter(k_label).most_common(1)[0]#count and extract most common element                                                                                                       
    Ypred[i] = label
  return Ypred

k We use collection.Counter to count the highest number of labels.


2022-09-30 15:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.