Keras sequential network: Attempt to convert a value (75) with an unsupported type (<class 'numpy.int32'>) to a Tensor

那年仲夏 提交于 2021-01-29 16:33:58

问题


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.

来源:https://stackoverflow.com/questions/62088268/keras-sequential-network-attempt-to-convert-a-value-75-with-an-unsupported-ty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!