-4

I'm new to machine learning and Python. I have been using Keras for a project. The first argument of almost all attributes of Model is self.

For example:

fit(self, x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None)

Can someone please tell me what this self refers to?

DriedNuts
  • 93
  • 3
  • 9
  • it's not specific to Keras – Paolo Apr 09 '18 at 16:46
  • If you do not understand this sort of thing, **please** work through a Python language tutorial from start to finish and make sure you understand the fundamentals, before trying to work with advanced third-party libraries. – Karl Knechtel Sep 05 '22 at 09:20

1 Answers1

0

In a method of Model, self is the particular instance of class Model that you're working with. If a class were a species, an instance (or object) of that class is a particular individual of that species.

In a procedural language (like e.g. C), all data is passed to functions as parameters. In a language that supports object orientation (like e.g. Python), functions (methods) are regarded as belonging to certain classes of objects. Rather than passing such an object as an explicit parameter to the function, it is passed "anonymously" in self.

<object>.<function> (<explicit params>) leads to calling <function> (self = <object>, <explicit params>).

In some object oriented languages (like e.g. C++), this self (or, in that case this parameter doesn't even have to be mentioned if the call is made from another method of the same object, but it's there anyhow implicitly.

Jacques de Hooge
  • 6,750
  • 2
  • 28
  • 45
  • I'm confused about what exactly goes in place of self in model.fit. I know x is the training data, y is the target data, but I don't really understand what goes in the place of self. – DriedNuts Apr 09 '18 at 16:51
  • An object of class Model, holding data that represents the state of your model. So model.fit () in an OO language is like fit (model) in a procedural language. If you are going to make heavy use of Python, work through a good tutorial first, it'll pay back the effort. This has nothing to do with Keras and everything with OO programming. – Jacques de Hooge Apr 09 '18 at 16:54