Home > database >  How can I use the parameter that is stored in class in array
How can I use the parameter that is stored in class in array

Time:10-06

I have a question about my code. It is about the layer in MLP. I set the layers in list with class, such that

class Linear:
    def __init__(self, n, m, i = Flase):
        context
class ReLU:
    def __init__(self)
        context

And in other class I append the class in List

class MLP:
    def __init__(self, features):
        for i, (n, m) in context):
            layers  = [Linear(n, m, i==0), ReLU()]
        return layers

Then I try to use the layer in "layers" list in other class, so how can I use the parameter I stored before. (Exactly in "n" and "m" in Linear class) I write the code that

for i in range(len(layers)):
    if isinstance(layers[i], Linear):

And Next line I want to use the parameter in Liner class which is "n" and "m". Is their any solution with it?

CodePudding user response:

From what I was able to understand of your question, you want to assign some attributes to a class instance in it's constructor and then you want to be able to access those attributes afterwards.

Here is an example of how you might do that.

class Linear:
    def __init__(self, n, m):
        self.n = n   # make instance attributes using self
        self.m = m


layers = []

for (n, m) in context:  
    layers  = [Linear(n, m)]


for i in range(len(layers)):
    if isinstance(layers[i], Linear):
        var = layers[i]
        print(var.n)  
        print(var.m) # <--- use the obj.attr syntax to get the attributes
  • Related