Keras LSTM的训练数据格式

8

我正在尝试使用LSTM神经网络(使用Keras)来预测游戏石头剪刀布中对手的下一步动作。

我已经将输入编码为石头:[1 0 0],剪刀:[0 0 1]和布:[0 1 0]。现在我想训练神经网络,但是我的训练数据结构有些困惑。

我已经将对手的游戏历史存储在一个.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

我尝试使用每5个数据作为我的训练标签,前4个数据作为训练输入。换句话说,在每个时间步,会将一个3维向量发送到网络,并且我们有4个时间步。

例如,以下是输入数据:

1,0,0
0,1,0
0,1,0
0,0,1

第五个是训练标签。
1,0,0

我的问题是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)
1个回答

4
LSTM的输入格式应该具有形状(sequence_length, input_dim)。因此,在您的情况下,形状为(4,3)的numpy数组就可以了。
您将喂给模型的是一个形状为(number_of_train_examples, sequence_length, input_dim)的numpy数组。 换句话说,您将喂入number_of_train_examples个形状为(4,3)的表格。 请建立一个列表:
1,0,0
0,1,0
0,1,0
0,0,1

然后执行np.array(list_of_train_example)。但是,我不明白为什么你要为第二个LSTM返回整个序列?这将输出一个形状为(4,4)的结果,Dense层可能无法处理。返回序列意味着您将返回LSTM每个步骤中的每个隐藏输出的整个序列。对于第二个LSTM,我会将其设置为False,只获取形状为(4,)的“摘要”向量,供Dense层读取。无论如何,即使对于第一个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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接