这个 Keras Sequential 模型在学习吗?

huangapple go评论80阅读模式
英文:

Is this Keras Sequential model learning?

问题

我正在尝试构建一个适合我的数据的Keras Sequential模型。然而,我在选择层和设置输入形状方面遇到了困难。我的模型准确度从0.4943开始,在不同的时期之间没有变化。似乎我的模型没有学习。

数据看起来像这样:

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,)

以下是模型代码:

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 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.

答案1

得分: 2

我注意到你的batch_size=3这个选择不太好。尝试尝试不同的架构/参数进行实验。这里有一个简单的示例:

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)

工作示例:

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

# 样本数据,100个样本
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)
英文:

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)

答案2

得分: 0

我明白了。以下是您要翻译的内容:

在与这个问题挣扎后,我意识到将输入形状更改为(200,)可以解决问题。当您向数据添加维度时,情况会变得复杂。感谢@YOLO帮助我。

英文:

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.

huangapple
  • 本文由 发表于 2020年1月4日 00:20:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定