0

I am attempting to predict the ideal move in a game using a keras sequential network. The network is fairly simple with (what I think to be) an input shape of (3216,). The code for defining the network is attached bellow.

    self.model = keras.Sequential()
    self.model.add(Dense(2000, activation='relu', input_dim=(2 * 7 + 2 + Cfg.tiles_x * Cfg.tiles_y)))
    self.model.add(Dense(1000, activation='relu'))
    self.model.add(Dense(100, activation='relu'))
    self.model.add(Dense(9)) self.model.compile(loss=keras.losses.categorical_crossentropy,
                                   optimizer='adam',
                                   metrics=['accuracy'])

The input data is the current game state (game board and player inventory). This is stored as a list until passed to the predict function at which point it converted to a numpy array using np.asarray(list).

Calling the function:

a1_action = agent1.get_move(np.asarray(a1_input))

Where a1_input is the game state list of length 3216. Agent1 is an object of the class storing the network and get_move function.

Function that returns the prediction:

def get_move(self, info):
    # This is where I print from in the next section
    prediction = self.model.predict(info)
    if max(prediction) > AI_Cfg.actionThreshold:
        return Agent.moves[prediction.index(max(prediction))]
    else:
        return ""

Now the error I get when passing info directly is at the model.predict() line is:

ValueError: Error when checking input: expected dense_input to have shape (3216,) but got array with shape (1,)

I have also tried suggestions from here but that converts my (3216,) input shape to (1, 3216) and gives the error:

ValueError: Attempt to convert a value (75) with an unsupported type (class 'numpy.int32') to a Tensor.

I have also tried reinstalling numpy.

If there is any more information I can provide I would be happy to do so. Thanks!

Printing that may be useful above the prediction line:

info.shape -> (3216,)

info -> [75 35 0 ... 0 0 0]

EDIT

After changing the code to try to fix the issue I am only getting the error below.

Attempt to convert a value (75) with an unsupported type (class 'numpy.int32') to a Tensor.

Interestingly this is the same error I get when (skipping over the first set of predictions and) try to train the model on data.

  • Try calling predict as : `self.model.predict([info])` – hacker315 May 29 '20 at 14:46
  • @hacker315 that gives error "ValueError: Attempt to convert a value (75) with an unsupported type () to a Tensor." – Reuben Walker May 29 '20 at 15:07
  • Try: `self.model.predict(info.reshape(1, info.shape[0]))` – hacker315 May 29 '20 at 15:18
  • @hacker315 that gives the same error as self.model.predict([info]) :(. I think that's just two different ways of doing the same thing. Right? – Reuben Walker May 29 '20 at 16:23
  • Can you show how did you trained the model? What shapes of _X_ and _y_ were there? – Yoskutik May 29 '20 at 16:28
  • @Yoshkutik the model has not been trained before the first prediction. The ai is supposed to learn through self-play which means going through cycles of playing and then learning from the winning player. Now that you mention it is there something I have to do to randomize the network weights if the model has not yet been fitted to anything? – Reuben Walker May 29 '20 at 17:05

0 Answers0