Keras LSTM培训数据格式

lop*_*oes 8 numpy neural-network lstm keras

我正在尝试使用LSTM神经网络(使用Keras)来预测对手在Rock-Paper-Scissor游戏中的下一步行动.

我将输入编码为Rock:[1 0 0],Paper:[0 1 0],Scissor:[0 0 1].现在我想训练神经网络,但我对训练数据的数据结构有点困惑.

我已将对手的游戏历史存储在.csv文件中,其结构如下:

1,0,0
0,1,0
0,1,0
0,0,1
1,0,0
0,1,0
0,1,0
0,0,1
1,0,0
0,0,1
Run Code Online (Sandbox Code Playgroud)

我试图将每5个数据用作我的训练标签,并将之前的4个数据用作训练输入.换句话说,在每个时间步,将具有维度3的向量发送到网络,并且我们有4个时间步长.

例如,以下是输入数据

1,0,0
0,1,0
0,1,0
0,0,1
Run Code Online (Sandbox Code Playgroud)

第五个是培训标签

1,0,0
Run Code Online (Sandbox Code Playgroud)

我的问题是Keras的LSTM网络接受什么类型的数据格式?为此目的重新安排数据的最佳方法是什么?如果有帮助,我的不完整代码附加如下:

#usr/bin/python
from __future__ import print_function

from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.optimizers import Adam

output_dim = 3
input_dim = 3
input_length = 4
batch_size = 20   #use all the data to train in one iteration


#each input has such strcture
#Rock: [1 0 0], Paper: [0 1 0], Scissor: [0 0 1]
#4 inputs (vectors) are sent to the LSTM net and output 1 vector as the prediction

#incomplete function
def read_data():
    raw_training = np.genfromtxt('training_data.csv',delimiter=',')




    print(raw_training)

def createNet(summary=False):
    print("Start Initialzing Neural Network!")
    model = Sequential()
    model.add(LSTM(4,input_dim=input_dim,input_length=input_length,
            return_sequences=True,activation='softmax'))
    model.add(Dropout(0.1))
    model.add(LSTM(4,
            return_sequences=True,activation='softmax'))
    model.add(Dropout(0.1))
    model.add(Dense(3,activation='softmax'))
    model.add(Dropout(0.1))
    model.add(Dense(3,activation='softmax'))
    model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])
    if summary:
        print(model.summary())
    return model

if __name__=='__main__':
    createNet(True)
Run Code Online (Sandbox Code Playgroud)

Nas*_*Ben 4

LSTM 的输入格式应具有形状 (sequence_length, input_dim)。所以在你的情况下,形状 (4,3) 的 numpy 数组应该可以做到。

然后,您将输入到模型中的内容将是形状的 numpy 数组(number_of_train_examples、sequence_length、input_dim)。换句话说,您将提供形状为 (4,3) 的 number_of_train_examples 个表。建立一个清单:

1,0,0
0,1,0
0,1,0
0,0,1
Run Code Online (Sandbox Code Playgroud)

然后执行 np.array(list_of_train_example)。

但是,我不明白为什么要返回第二个 LSTM 的整个序列?它会输出形状为 (4,4) 的东西,密集层可能会失败。返回序列意味着您将返回整个序列,即 LSTM 每一步的每个隐藏输出。我会将第二个 LSTM 设置为 False,以便仅获得 Dense 层可以读取的形状 (4,) 的“摘要”向量。无论如何,即使对于第一个 LSTM,这也意味着使用形状 (4,3) 的输入,您输出的东西具有形状 (4,4),因此您将拥有比该层的输入数据更多的参数......可以'真的不好。

关于激活,我也会使用 softmax,但仅在最后一层,softmax 用于获取概率作为该层的输出。在 LSTM 和 Dense 之前使用 softmax 并没有什么意义。寻求其他一些非线性,例如“sigmoid”或“tanh”。

这就是我要做的模型方面的事情

def createNet(summary=False):
    print("Start Initialzing Neural Network!")
    model = Sequential()
    model.add(LSTM(4,input_dim=input_dim,input_length=input_length,
            return_sequences=True,activation='tanh'))
    model.add(Dropout(0.1))
    # output shape : (4,4)
    model.add(LSTM(4,
            return_sequences=False,activation='tanh'))
    model.add(Dropout(0.1))
    # output shape : (4,)
    model.add(Dense(3,activation='tanh'))
    model.add(Dropout(0.1))
    # output shape : (3,)
    model.add(Dense(3,activation='softmax'))
    # output shape : (3,)
    model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])
    if summary:
        print(model.summary())
    return model
Run Code Online (Sandbox Code Playgroud)