简体   繁体   中英

How to input an array of n items and output an array of size k in a neural network using keras?

I am new to machine learning and using neural networks with keras. I am trying to use reinforcement learning along with the help of a neural network, which may eventually predict the correct actions for a robot to take in a monopoly game, if it were to play against humans.

For this I am trying to use a neural network which receives an array of 23 float numbers (defining the players state), and outputs an array of 7 float numbers (the maximum number of possible actions that can be taken at a given time). My current NN is the following:

model = Sequential()
model.add(Dense(150, input_dim=23, activation='relu'))
model.add(Dense(7, activation='sigmoid'))
model.compile(loss='mse', optimizer=Adam(lr=0.2))

My intention is to have a 3 layer nn, with a 150 (neurons) hidden layer, and 7 neurons in the last layer.

#An input example would be:
state = [0.35,0.65,0.35,3.53...] # array of 23 items, float numbers.
output = model.predict(state)

#I expect output to be:
[0.21,0.12,0.98,0.32,0.44,0.12,0.41] #array size of 7

#Then I could simply just use the index with the highest number as the action to take. 
action = output.index(max(output))

I am not sure why, but I get this error instead: ValueError: Error when checking input: expected dense_23_input to have shape (23,) but got array with shape (1,)

I'm sure it would be better if I could just have a single last layer neuron predicting integer numbers in a range, for instance numbers 1 to 7. However, I do not know of any activation function which can do this. Please feel free to suggest better nn models for this purpose, I would highly appreciate. I am aware that this might not be the best possible model for this purpose.

But essentially, the main question here is, how do I input a single array size 23, and output an array of size 7?

Thank you!!

I quite not so familiar with keras, but in pytorch everything is expected to work in batches, and that's why you're getting more dimensions than you want.

The input for your first linear layer should has dimensions (batch_size,23) . If you want to see how a single example runs throgh the network first reshape it like input.reshape(1,-1) . The output will have dims (1,7) . You should change the last layer activation to softmax

Thank you for your contribution. I managed to solve the issue: I finally ended up using a 3 layer nn with a single neuron output and a sigmoid activation function which looked like this:

model = Sequential()
model.add(Dense(150, input_shape=(23,), activation='relu'))
model.add(Dense(1, input_shape=(7,), activation='sigmoid'))
model.compile(loss='mse', optimizer=Adam(lr=0.2))

#Required input would look something like this:
input =np.array([0.2,0.1,0.5,0.5,0.8,0.3,0.2,0.2,0.2,0.9,0.2,0.8,0.6,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.1,0.5,0.4])
input = np.reshape(input,(1,-1))

#The output would look like something like this:
print(saved_model.predict(input))
#[[0.00249215 0.15893634 0.50805619 0.86176279 0.34417642 0.29258215
  0.131994  ]]

From here, I would simply just get the index with the highest probability to determine the class of my input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM