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