5

isinstance(object, classinfo) doesn't recognize an instance of a class.

I printed type(object) and the class itself to verify that they were the same types. They both printed the exact same thing however when I tried type(object) == class it returned False.

here is the code for the class:

class Convolute(object):
    def __init__(self, channels, filter_shape, pool_shape, pool_type, stride_length=1):
        self.channels = channels
        self.filter_shape = filter_shape
        self.filters = [np.random.randn(filter_shape[0], filter_shape[1]) for i in range(channels)]
        self.biases = [random.random() for i in range (channels)]
        self.stride_length = stride_length
        self.pool_shape = pool_shape
        self.pool_type = pool_type

here is the instance of the class:

con1 = n.Convolute(3, (4, 4), (4, 4), 0)

here is the output in the python shell when I tried to verify they were the same type:

>>> import network as n
>>> con1 = n.Convolute(3, (4, 4), (4, 4), 0)
>>> type(con1)
<class 'network.Convolute'>
>>> n.Convolute
<class 'network.Convolute'>
>>> type(con1) == n.Convolute
False
>>> isinstance(con1, n.Convolute)
False

Since the output of type(con1) and n.Convolute seem to be identical I expected that isinstance() and type(con1) == n.Convolute would return True but they return `False'. I am honestly beyond confused please help.

--EDIT--

type(con1).__name__ == n.Convolute.__name__ returns True but I still do not know why nothing else works

The problem is also inside of the file that I import from, I just also ran into the same problem in the file itself not just when I imported it. Here is the code inside the program:

class Network(object):

    #params for class are layers described by class e.g. ConvolutionalNetwork([Input([...]), Convolute([...]), Flatten(), Dense([...]), (Dense[...]]) 
    #__init__ and setflattensize functions initilize network structures
    def __init__(self, layers):
        self.layers = layers
        self.channels = [layers[0].channels]
        self.shapes = [layers[0].shape]
        for layer, index in zip(layers, range(len(layers))):
            if isinstance(layer, Flatten):
                self.setflattensize(layer, index)
            if isinstance(layer, Dense):
                layer.weights = np.random.randn(self.layers[index-1].size, layer.size) 
            #get list of channels and shapes/sizes that correspond with each layer
            if index>0:
                if self.channels[-1]*layer.channels == 0:
                    self.channels.append(1)
                else:
                    self.channels.append(self.channels[-1]*layer.channels)
                if isinstance(layer, Convolute):
                    self.shapes.append(((self.shapes[-1][0]-layer.filter_shape[0]+1)/layer.pool_shape[0], (self.shapes[-1][1]-layer.filter_shape[1]+1)/layer.pool_shape[1]))
                else:
                    self.shapes.append(layer.size)

the if isinstance(layer, Convolute): returns False instead of True. This is the problem that is explained more in depth earlier.

runable code that demonstrates the problem: https://github.com/Ecart33/MachineLearning/blob/master/neural_net/network_debug.py

ecart33
  • 78
  • 8
  • 1
    That's odd. Compare with: https://stackoverflow.com/a/1549814 – quamrana May 20 '19 at 13:43
  • I'm not able to replicate this issue using relatively simple classes, both `isinstance` and `type(inst)==cls` return `True`, whether imported or constructed in `__main__` – C.Nivs May 20 '19 at 13:52
  • 3
    Which version of python are you using? – C.Nivs May 20 '19 at 13:56
  • 4
    Are you doing something funny with your imports (e.g. mangling `sys.path` somewhere)? If so, you could end up with *two independent types* with the same name, imported from different copies of the same module. – Daniel Pryden May 20 '19 at 13:57
  • @DanielPryden I think you might be on to something, if I import two separate instances of a module containing that class, `import something as n; import something_else as s` the instantiate that class using `c = n.Convolute`, it fails the check for `isinstance(c, s.Convolute)` – C.Nivs May 20 '19 at 14:01
  • Just what Daniel Pryden says - that's actually the very typical symptom of duplicate imports. Check your `sys.path` and `sys.modules`. The whole thing is explained here: http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html#the-double-import-trap – bruno desthuilliers May 20 '19 at 14:02
  • 2
    @C.Nivs : what Daniel mentions is actually not related to "import XXX as YYY" which only binds two names to the same module instance (and doesn't cause any identity issue). Check the link I posted for a more in-depth explanation of the double-import gotcha (which creates two distinct modules _instances_ from the same source file) – bruno desthuilliers May 20 '19 at 14:05
  • @brunodesthuilliers checking it out now, cheers – C.Nivs May 20 '19 at 14:06
  • @brunodesthuilliers the problem itself is also in the python file I import from not only after I import it in the shell. I'll edit the question to add this as well. – ecart33 May 20 '19 at 14:08
  • 5
    At this point, all we can do is *guess* until you provide a [mcve] which we can actually *run* to demonstrate the problem. – Daniel Pryden May 20 '19 at 14:18
  • @DanielPryden this code is runable and demonstrates the problem: https://github.com/Ecart33/MachineLearning/blob/master/neural_net/network_debug.py – ecart33 May 20 '19 at 14:33
  • 1
    @ecart33 the code snippet you posted is NOT a proper MCVE - it's not minimal (useless deps on 3rd part packages and obviously useless code), and it's not complete (there are only class definitions, when obviously some code is needed to use those classes). – bruno desthuilliers May 20 '19 at 14:40
  • @brunodesthuilliers and others sorry about that I added code in to make it run and it does seem to be a problem with the imports. I don't know why there is a problem since I have been doing similar things with isinstance() that have worked perfectly fine but I will look into it. Thanks for the help. – ecart33 May 20 '19 at 14:53
  • Please read the link I posted and _check your sys.path and sys.modules_. – bruno desthuilliers May 20 '19 at 14:57

0 Answers0