简体   繁体   中英

Is this Keras Sequential model learning?

I am trying to build a Keras Sequential model that fits my data. However, I have trouble choosing the layers and setting the input shape. My model accuracy starts with 0.4943 and doesn't change between epochs. It seems that my model is not learning.

The data looks like this:

X = [[[0.00000000e+00 0.00000000e+00]
  [1.82562794e-01 6.81775296e-01]
  [1.13191538e+00 1.37766573e+00]
  ...
  [5.31509230e+01 4.88222520e+01]
  [5.38463488e+01 4.92077884e+01]
  [5.43891348e+01 4.98190918e+01]]

 [[0.00000000e+00 0.00000000e+00]
  [4.81657107e-01 4.62969773e-01]
  [1.33733394e+00 8.20860280e-01]
  ...
  [5.00154741e+01 4.49145568e+01]
  [5.06145436e+01 4.58551323e+01]
  [5.14753045e+01 4.66484598e+01]]

 [[0.00000000e+00 0.00000000e+00]
  [1.24209617e-01 3.41455813e-01]
  [6.62306377e-01 9.70226310e-01]
  ...
  [4.59534909e+01 5.14811676e+01]
  [4.65830639e+01 5.15458682e+01]
  [4.69169909e+01 5.18978055e+01]]

 ...

 [[0.00000000e+00 0.00000000e+00]
  [8.37513698e-01 2.36545136e-01]
  [2.09606414e+00 2.18579855e+00]
  ...
  [9.33516241e+01 9.02639438e+01]
  [9.48198248e+01 9.09696034e+01]
  [9.56924057e+01 9.11994364e+01]]

 [[0.00000000e+00 0.00000000e+00]
  [1.16628793e+00 3.07939104e-01]
  [2.90856042e+00 1.93300849e+00]
  ...
  [9.50615310e+01 9.54437621e+01]
  [9.64466547e+01 9.62387560e+01]
  [9.84132452e+01 9.68517902e+01]]

 [[0.00000000e+00 0.00000000e+00]
  [7.07518408e-02 1.63762559e+00]
  [1.47380576e+00 3.01519861e+00]
  ...
  [9.56341427e+01 8.22719298e+01]
  [9.75264435e+01 8.41242858e+01]
  [9.85001877e+01 8.44169342e+01]]]

X.shape = (2000, 100, 2)

y = [0. 0. 0. ... 1. 1. 1.]

y.shape = (2000,)

and here is the model code:

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(100,2)),
    keras.layers.Dense(16, activation=tf.nn.relu),
    keras.layers.Dense(16, activation=tf.nn.relu),
    keras.layers.Dense(1, activation=tf.nn.sigmoid),
])

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

h = model.fit(X_train, y_train, epochs=50, batch_size=3, shuffle=True)

test_loss, test_acc = model.evaluate(X_test, y_test)
print('Test accuracy:', test_acc)

I am trying to do a binary classification. Is there something wrong with my model? Any help is appreciated.

I noticed your batch_size=3 which is not a good choice. Try experimenting with different architectures/parameters. Here's a simple one to go with:

model = Sequential()
# model.add(Dense(256, input_dim=2, activation='relu'))
model.add(Dense(256, input_shape=X.shape, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu')
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='adam',
              loss='binary_crossentropy',
               metrics=['accuracy'])

model.fit(X_train, y_train, epochs=10, batch_size=64, shuffle=True)

Working example:

from keras import Sequential
from keras.layers import Dense, Dropout

# sample data, 100 samples
X_train = np.random.random((100,2)) 
y_train = np.random.randint(2, size=(100, 1)) 

model = Sequential()
model.add(Dense(256, input_dim=2, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='adam',
              loss='binary_crossentropy',
               metrics=['accuracy'])

model.fit(X_train, y_train, epochs=10, batch_size=2)
model.predict(X_train).shape # (100, 1)

After struggling with this question, I realized that changing the input shape to (200, ) fixes the problem. Things get complicated when you add dimensions to your data. Thank you @YOLO for helping me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM