0

I'm studiyng Adaline SGD method and I have some questions about my code (I will not put the whole algorithm for how extensive it is):

 def __init__(self, eta=0.01, n_iter=50, shuffle=True, random_state =None) :
    self.eta = eta
    self.n_iter=n_iter
    self.w_initialized=False
    self.shuffle=True
    self.random_state = random_state 

def partial_fit(self,X,y):
    """Fit  training data without reinitializing the weights"""
    if not self.w_initialized:
        self._initialize_weights(X.shape[1])
    if y.ravel().shape[0]>1:
        for xi, target in zip(X,y):
            self._update_weights(xi, target)


def _shuffle(self,X,y):
    """Suffle training data"""
    r=self.rgen.permutation(len(y))
    return X[r], y[r]

def _initialize_weights(self,m):
    """Initialize weights to small random numbers"""
    self.rgen= np.random.RandomState(self.random_state)
    self.w_=self.rgen.normal(loc= 0.0, scale= 0.01, size = 1+m)
    self.w_initialized=True
  1. Why is self.w_initialized set to False in the begginning in def __init__(self, eta=0.01, n_iter=50, shuffle=True, random_state =None)and later to True in def _initialize_weights(self,m) at the end? I don't understand how it works.

  2. Why in r=self.rgen.permutation(len(y)) it is not written ...len(y)+1 to cover the whole list?. (Note: On the next line, in X[r], y[r] I assume that returns the same vectors alternately randomized, if I'm wrong let me know)

  3. What does m variable in self.w_=self.rgen.normal(loc= 0.0, scale= 0.01, size = 1+m) do?. I know the meaning of size,but ,what is the result after adding m to 1?

Let me know if more context is needed. Sorry if my english is not perfect.

1 Answer 1

1

Re 1.:

self.w_initialized seems to indicate if weights have been initialised, so when the object is created with __init__() that's not the case, but after _initialize_weights() has been called and completed, that is the case, so self.w_initialized is set to True to reflect this. It doesn't actually do anything, other than making it clear for anything using the object that action has now completed.

Re 2.:

It's unclear why you'd think permutation() would need the length +1? It seems to make sense to have a permutation that's exactly the length of y, give that it's used on the next line to obtain y[r]?

As for the second part of your question, the line does quite literally what it says: it returns the tuple X[r], y[r], so a vector with the elements from X indexed by r and the same for y. Which are random permutations of X and y, it would seem, without knowing more about the code or imports which you didn't share.

Re 3.:

Again, your question is surprising: m is a parameter passed to _initialize_weights(), 1 is added to it and the resulting value is passed to the size parameter of self.rgen.normal - what exactly is unclear about that?

General:

From your questions, it would appear that you're very much new to Python in general, I would recommend reading up on the basics of variables, functions and objects in Python, and then revisiting the code. Your questions actually have no bearing on the meaning of the code, but seem to be questions about how Python basically works. I'm assuming what you call "your code" is in fact code you obtained and are trying to understand or modify.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! , and yes I'm new in Pyhon and I'm trying to learn on my own with a machine learning book that explains it's codes, but very loosely. I know that m is a parameter passed to _initialize_weights (), and I had read about size and therefore I thought It could only take the None or an integeroption, not a variable, so I'm still a bit confused on that part
If m is an integer, then 1+m is also an integer and thus an integer is passed to size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.