2017-02-09 11 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 

そして5つ目

をである私の質問Keras' LSTMネットワークはデータフォーマットの種類を受け入れないさ

1,0,0 

トレーニングラベルのですか?この目的のためにデータを並べ替える最適な方法は何でしょうか?

#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)がそれを行うべきです。

モデルに入力する内容は、形状の数値配列(number_of_train_examples、sequence_length、input_dim)になります。 つまり、number_of_train_examplesの表(4,3)をフィードします。 np.array(list_of_train_example)を行い、その後

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

と: はのリストを作成します。

しかし、2番目のLSTMのシーケンス全体を返す理由はわかりません。それはあなたに形状(4,4)で何かを出力します、密な層はおそらくそれで失敗します。リターンシーケンスとは、シーケンス全体を返すことを意味し、LSTMの各ステップですべての隠れた出力を返します。私はこれをFalseに設定して、2番目のLSTMがDenseレイヤーが読むことができるshape(4、)の "サマリー"ベクトルを取得するだけです。 とにかく、最初のLSTMの場合でも、shape(4,3)の入力ではshape(4,4)のものを出力するので、このレイヤの入力データよりも多くのパラメータがあります。Can本当によかったです。

私は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 
を何をするのかであります