2017-01-11 6 views
2

私はケラスを初めて使っています。自分のニューラルネットワークを構築しようとしています。ケラスで簡単なニューラルネットワークを構築する方法(画像認識ではない)

タスク:

私は1体のまたは複数の敵を満たして文字、ために意思決定を行うことができるシステムを記述する必要があります。システムを知ることができます。

  • パーセンテージヘルス文字
  • ピストルの存在;
  • 敵の数です。

    1. 攻撃
    2. 実行
    3. 隠す(のための奇襲攻撃)
    4. は何の関係も

答えは、次のいずれかの形式でなければなりません

私は「レッスン」のテーブルを作った:

ここで

# Create first network with Keras 
from keras.models import Sequential 
from keras.layers import Dense 
from keras.optimizers import SGD 
import numpy 
# fix random seed for reproducibility 
seed = 7 
numpy.random.seed(seed) 
# split into input (X) and output (Y) variables 
X = numpy.array([[0.5,1,1], [0.9,1,2], [0.8,0,1], [0.3,1,1], [0.6,1,2], [0.4,0,1], [0.9,1,7], [0.5,1,4], [0.1,0,1], [0.6,1,0], [1,0,0]]) 
Y = numpy.array([[1],[1],[1],[2],[2],[2],[3],[3],[3],[4],[4]]) 
# create model 
model = Sequential() 
model.add(Dense(3, input_dim=3, init='uniform', activation='relu')) 
model.add(Dense(1, init='uniform', activation='sigmoid')) 
# Compile model 
sgd = SGD(lr=0.001) 
model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy']) 

# Fit the model 
model.fit(X, Y, nb_epoch=150) 
# calculate predictions 
predictions = model.predict(X) 
# round predictions 
rounded = [round(x) for x in predictions] 
print(rounded) 

私が手予測:

https://i.stack.imgur.com/lD0WX.png

は、だからここに私のコードです。 [1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0]

各エポックの精度は0.2727であり、損失は減少します。 それは正しくありません。

私は、アクティベーションとオプティマイザを変えて、学習率を10倍にしようとしていました。データ入力も手動で入力します。 誰も私の簡単な問題を解決する方法を教えてもらえますか?どうも。

答えて

2

コードにはいくつかの問題があります。

  1. NNモデルに比べてデータエントリの数が非常に少ない。
  2. Yは、クラス番号として表され、クラスベクトルではありません。回帰モデルはこれで学ぶことができますが、その設計の選択肢は貧弱です。これはあなたのモデルのみ0-1間の値を吐き出すために知って使用されているよう
  3. 出力softmax機能のは、常に0-1 ...の間です。以下ここ

ビットより良く修正されたコードである:クラスは

相互に排他的であるように、私は softmax活性化を使用している

from keras.models import Sequential 
from keras.layers import Dense 
from keras.optimizers import SGD 
import numpy 
# fix random seed for reproducibility 
seed = 7 
numpy.random.seed(seed) 
# split into input (X) and output (Y) variables 
X = numpy.array([[0.5,1,1], [0.9,1,2], [0.8,0,1], [0.3,1,1], [0.6,1,2], [0.4,0,1], [0.9,1,7], [0.5,1,4], [0.1,0,1], [0.6,1,0], [1,0,0]]) 
y = numpy.array([[1],[1],[1],[2],[2],[2],[3],[3],[3],[0],[0]]) 

from keras.utils import np_utils 
Y = np_utils.to_categorical(y, 4) 
# print Y 

# create model 
model = Sequential() 
model.add(Dense(3, input_dim=3, activation='relu')) 
model.add(Dense(4, activation='softmax')) 
# Compile model 
# sgd = SGD(lr=0.1) 
# model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) 
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) 

# Fit the model 
model.fit(X, Y, nb_epoch=700) 

# calculate predictions 
predictions = model.predict(X) 

predictions_class = predictions.argmax(axis=-1) 
print(predictions_class) 

関連する問題