Issue
I'm creating a simple tensorflow network like below:
from tensorflow.keras import Model
from tensorflow.keras.layers import Conv3D, Dense, Flatten
class myNetwork(Model):
def __int__(self, dim1: int, dim2, dim3):
super(myNetwork, self).__int__()
self.conv3 = Conv3D(filters=128, kernel_size=[dim3, 5, 5], strides=1, input_shape=dim2, padding='same')
self.dense = Dense(dim1, activation="linear")
self.flatten = Flatten()
@tf.function
def call(self, x):
h1 = self.conv3(x)
h2 = self.flatten(h1)
out = self.dense(h2)
return out
However, when running the code using:
out = self.PredNet(myInput)
where,
self.PredNet = myNetwork(dim1, dim2, dim3)
I get the error:
AttributeError: 'myNetwork' object has no attribute 'conv3'.
This is quite confusing, cause conv3 is right there!
Solution
The constructor method always needs to be called __init__
, not __int__
or something else. In Python, the __init__
method is a special method that is automatically called when you create an instance of a class. It is used to initialize the object's attributes and perform any necessary setup. If you name it something else, it will not be called when you create an instance of the myNetwork
class. As a result, the attributes self.conv3
and self.dense
will not be initialized, so you'll get the "AttributeError" when you try to access them.
Answered By - Ada
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.