爲了深入瞭解Keras和TensorFlow後端的進一步研究項目,我試着實現一個簡單分類問題的神經網絡。我只想區分randomly distributed points in 2D into two categories depending on their coordinates(顏色表示類別)。 相關的代碼以產生所述數據爲:Keras中的簡單神經網絡 - 錯誤的分類
import numpy as np
np.random.seed(40)
def createData(N=120, M=75):
train_x1 = np.random.random(size=N)
train_x2 = np.random.random(size=N)
test_x1 = np.random.random(size=M)
test_x2 = np.random.random(size=M)
train_x = np.zeros((N, 2))
train_y = np.zeros((N, 1))
test_x = np.zeros((M, 2))
test_y = np.zeros((M, 1))
for i in range(N):
train_x[i][0] = train_x1[i]
train_x[i][1] = train_x2[i]
if train_x1[i] < 0.5:
if train_x2[i] < 0.5:
train_y[i][0] = 1
else:
train_y[i][0] = 2
else:
if train_x2[i] < 0.5:
train_y[i][0] = 2
else:
train_y[i][0] = 1
for j in range(M):
test_x[j][0] = test_x1[j]
test_x[j][1] = test_x2[j]
if test_x1[j] < 0.5:
if test_x2[j] < 0.5:
test_y[j][0] = 1
else:
test_y[j][0] = 2
else:
if test_x2[j] < 0.5:
test_y[j][0] = 2
else:
test_y[j][0] = 1
return train_x, train_y, test_x, test_y
我給神經網絡代碼如下:
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
import matplotlib.pyplot as plt
X, Y, x, y = createData()
# Model
model = Sequential()
model.add(Dense(4, input_dim=2, activation='relu'))
model.add(Dense(10, activation='sigmoid'))
model.add(Dense(1, activation='sigmoid'))
# Compile
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])
# Fit
model.fit(X, Y, epochs=500, batch_size=25)
# Evaluation
scores = model.evaluate(X, Y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
# Predictions
predictions = model.predict(x)
rounded = [round(z[0]) for z in predictions]
print(rounded)
我的問題是,該神經網絡達到47.5%與上述的精度提到種子和配置。它分配all test coordinates to only one category。然而,其他配置(更多層,更多/更少的神經元,其他激活功能,其他損失函數等)導致類似的結果。
外殼上的「監控時代」的最後幾行看起來是這樣的:
Epoch 490/500
120/120 [==============================] - 0s - loss: -8.3351 - acc: 0.4750
Epoch 491/500
120/120 [==============================] - 0s - loss: -8.1866 - acc: 0.4750
Epoch 492/500
120/120 [==============================] - 0s - loss: -8.3524 - acc: 0.4750
Epoch 493/500
120/120 [==============================] - 0s - loss: -8.2608 - acc: 0.4750
Epoch 494/500
120/120 [==============================] - 0s - loss: -8.3269 - acc: 0.4750
Epoch 495/500
120/120 [==============================] - 0s - loss: -8.2039 - acc: 0.4750
Epoch 496/500
120/120 [==============================] - 0s - loss: -8.1786 - acc: 0.4750
Epoch 497/500
120/120 [==============================] - 0s - loss: -8.2488 - acc: 0.4750
Epoch 498/500
120/120 [==============================] - 0s - loss: -8.3090 - acc: 0.4750
Epoch 499/500
120/120 [==============================] - 0s - loss: -8.3457 - acc: 0.4750
Epoch 500/500
120/120 [==============================] - 0s - loss: -8.1235 - acc: 0.4750
如何提高神經網絡從這個愚蠢的行爲脫身?非常感謝您的任何意見和建議!
學習率的附加參數已經導致了顯着的改進。用下面的代碼,我得到了一個至少體面的結果: 'model.add(Dense(2,input_dim = 2,activation ='relu')) model.add(Dense(4,activation ='relu')) model.add(降(0.5)) model.add(密集(1,活化= '乙狀結腸')) model.compile(損耗= 'binary_crossentropy',優化= keras.optimizers.Adam(LR = 0.0001),度量= ['精度'])' – Inco83