简体   繁体   English

如何在使用 keras 的神经网络中输入包含 n 个项目的数组和 output 大小为 k 的数组?

[英]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.我是机器学习和使用神经网络 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).为此,我尝试使用一个神经网络,它接收一个包含 23 个浮点数的数组(定义玩家状态),并输出一个包含 7 个浮点数的数组(在给定时间可以采取的最大可能动作数)。 My current NN is the following:我目前的NN如下:

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.我的意图是有一个 3 层 nn,有 150 个(神经元)隐藏层,最后一层有 7 个神经元。

#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,)我不知道为什么,但是我得到了这个错误: 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.我敢肯定,如果我可以有一个最后一层神经元预测 integer 数字在一个范围内,例如数字 1 到 7,那会更好。但是,我不知道有任何激活 function 可以做到这一点。 Please feel free to suggest better nn models for this purpose, I would highly appreciate.请随时为此目的建议更好的 nn 模型,我将不胜感激。 I am aware that this might not be the best possible model for this purpose.我知道这可能不是最好的 model 用于此目的。

But essentially, the main question here is, how do I input a single array size 23, and output an array of size 7?但本质上,这里的主要问题是,我如何输入一个大小为 23 的单个数组,以及 output 一个大小为 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.我对 keras 不太熟悉,但在 pytorch 中,一切都有望分批工作,这就是为什么你得到的尺寸比你想要的要多。

The input for your first linear layer should has dimensions (batch_size,23) .第一个线性层的输入应该有尺寸(batch_size,23) If you want to see how a single example runs throgh the network first reshape it like input.reshape(1,-1) .如果您想查看单个示例如何通过网络运行,请首先像input.reshape(1,-1)一样对其进行整形。 The output will have dims (1,7) . output 将有暗淡(1,7) You should change the last layer activation to softmax您应该将最后一层激活更改为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:我设法解决了这个问题:我最终使用了一个带有单个神经元 output 和一个 sigmoid 激活 function 的 3 层 nn,它看起来像这样:

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.从这里,我只需获取概率最高的索引来确定我输入的 class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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