1

I am working on an RL problem and I created a class to initialize the model and other parameters. The code is as follows:

class Agent:
    def __init__(self, state_size, is_eval=False, model_name=""):
        self.state_size = state_size
        self.action_size = 20 # measurement, CNOT, bit-flip
        self.memory = deque(maxlen=1000)
        self.inventory = []
        self.model_name = model_name
        self.is_eval = is_eval
        self.done = False

        self.gamma = 0.95
        self.epsilon = 1.0
        self.epsilon_min = 0.01
        self.epsilon_decay = 0.995


    def model(self):
        model = Sequential()
        model.add(Dense(units=16, input_dim=self.state_size, activation="relu"))
        model.add(Dense(units=32, activation="relu"))
        model.add(Dense(units=8, activation="relu"))
        model.add(Dense(self.action_size, activation="softmax"))
        model.compile(loss="categorical_crossentropy", optimizer=Adam(lr=0.003))
        return model

    def act(self, state):
        options = self.model.predict(state)
        return np.argmax(options[0]), options

I want to run it for only one iteration, hence I create an object and I pass a vector of length 16 like this:

agent = Agent(density.flatten().shape)
state = density.flatten()
action, probs = agent.act(state)

However, I get the following error:

AttributeError                       Traceback (most recent call last) <ipython-input-14-4f0ff0c40f49> in <module>
----> 1 action, probs = agent.act(state)

<ipython-input-10-562aaf040521> in act(self, state)
     39 #             return random.randrange(self.action_size)
     40 #         model = self.model()
---> 41         options = self.model.predict(state)
     42         return np.argmax(options[0]), options
     43 

AttributeError: 'function' object has no attribute 'predict'

What's the issue? I checked some other people's codes as well, like this and I think mine is also very similar.

Let me know.

EDIT:

I changed the argument in Dense from input_dim to input_shape and self.model.predict(state) to self.model().predict(state).

Now when I run the NN for one input data of shape (16,1), I get the following error:

ValueError: Error when checking input: expected dense_1_input to have 3 dimensions, but got array with shape (16, 1)

And when I run it with shape (1,16), I get the following error:

ValueError: Error when checking input: expected dense_1_input to have 3 dimensions, but got array with shape (1, 16)

What should I do in this case?

6
  • 1
    You have a function and a variable with the same name (model), that's a bad idea Commented Nov 4, 2019 at 13:11
  • In the link, the person has also done the same thing. A lot of coders write their this way. Even I find it very strange. But apparently, that's how people are writing their code. Commented Nov 4, 2019 at 13:14
  • 2
    No, the code in the link is different, there is no function called model(), which is the core of your problem. Commented Nov 4, 2019 at 13:15
  • Okay. So now, I have changed the name of the function to model_rl. Even changed the name in line to options = self.model_rl.predict(state). I'm still getting the error Commented Nov 4, 2019 at 13:21
  • 1
    I think you have a big conceptual misunderstanding, that's not what I told you to do. You can change the name of the function, but then you have to call it and assign the return value of the function call to a variable (with a different name), from where you can call predict. Commented Nov 4, 2019 at 13:22

2 Answers 2

2

in last code block,

def act(self, state):
        options = self.model.predict(state)
        return np.argmax(options[0]), options

self.model is a function which is returning a model, it should be self.model().predict(state)

Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting this error: TypeError: Error converting shape to a TensorShape: int() argument must be a string, a bytes-like object or a number, not 'tuple'.
Can you post full stack trace, where you are getting this error?
40 # return random.randrange(self.action_size) 41 # model = self.model() ---> 42 options = self.model().predict(state) 43 return np.argmax(options[0]), options 44 <ipython-input-18-417404b4f01d> in model(self) 28 def model(self): 29 model = Sequential() ---> 30 model.add(Dense(units=16, input_dim=self.state_size, activation="relu")) 31 model.add(Dense(units=32, activation="relu")) 32 model.add(Dense(units=8, activation="relu"))
density.flatten().shape is a tuple which you are passing as state_size in Agent class which is being set as input_dim in Dense layer which doesn't take tuple as input. input_dim is number of dimensions of the features.
0

I used np.reshape. So in this case, I did

density_test = np.reshape(density.flatten(), (1,1,16))

and the network gave the output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.