2017-02-09 333 views
5

我正尝试使用LSTM神经网络(使用Keras)来预测对手在游戏Rock-Paper-Scissor中的下一步棋。Keras LSTM训练数据格式

我把输入编码为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 

,我试图用每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) 

回答

2

用于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 

,然后做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