0

I have the following code. Although, the problem is very simple but I cannot figure out the reason behind it.

The following is my main.py:

from model_MNIST import Model

def main():
    model = Model()

if __name__ == '__main__':
    main()

And the model_MNIST.py file is as follows:

# some import statements
class Model(object):
    def __init__(self, input_dimensions, output_dimensions):
        # some init statements

    def train_on_data(self, training_data, training_labels):
        N = training_labels.size

Whenever the class initialization is called I get an output as name 'training_labels' is not defined. This is preventing the execution of the program. Can someone point me out what I might be missing?

Edit1: Please refer to the shared link for the file. SharedFolder

11
  • 2
    Please edit your question to include your actual code, and the full error traceback output. Commented Jun 10, 2018 at 17:42
  • Which line is the error actually occurring? where is train_on_data() being invoked? Commented Jun 10, 2018 at 17:44
  • I have provided the shared links for the files. The error comes as soon as the class is instantiated. Commented Jun 10, 2018 at 17:57
  • Your code as posted in the above question does not match the code in the shared folder that you link to. Please update the question with the real code. Commented Jun 10, 2018 at 17:58
  • @quamrana the code from both of the files would take up a lot of space, that is why I gave a general scenario of my code. I do not think it would be problem to refer to the shared link. Commented Jun 10, 2018 at 18:00

3 Answers 3

1

This is a summary of the code from your link:

# some import statements
class Model(object):
    def __init__(self, input_dimensions, output_dimensions):
        # some init statements

    def train_on_data(self, training_data, training_labels):
        '''
        Multiline comment
        '''
    N = training_labels.size
    ...

In the above code the last line is part of the class, not the train_on_data method.

I think the last line (and the others elided) should be indented to be part of that method.

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

1 Comment

Bingo. Code in this method from this statement onward was not indented properly. Thank you once again.
0

From the shared code, what I can understand that you have confused with class and instance variables.

In the function train_on_data, you have some codes like self.training_labels = training_labels but your init method doesn't contain the self.training_labels variable.

3 Comments

Ah thank you for pointing out that. I'll correct the nonsense mistake and then check if it works. I'll update then.
@mb0850: In python is doesn't matter whether instance variables are initialised in init if they are initialised in another method. (As to whether this is good programming or not is another question).
@quamrana That is true, however, in the shared code self.training_data is being defined inside a method(train_on_data), so if there is an invocation of that variable in any other method without running train_on_data first, it would give error
0

You need to change this Model.train_on_data(X_train, y_train) to model.train_on_data(X_train, y_train) in main_MNIST.py, in simple .py file https://pastebin.com/JZArvWC3

1 Comment

Yeah already tried that. I think what @mb0850 posted as solution might be the real reason here. I'll update.

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.