为什么这个神经网络模型工作不佳?

时间:2019-07-07 17:48:44

标签: python tensorflow neural-network conv-neural-network

我正在尝试创建一个卷积神经网络,但是出于某种原因,在训练后它绝对没有用。

她总是给出这样的结果。 未知(得分= 1.00000) 测试(分数= 0.00000)

也许我建立的模型不对。

我想知道。我做错了什么?谢谢。

def create_conv_model(fingerprint_input, model_settings, is_training):

  if is_training:
    dropout_prob = tf.placeholder(tf.float32, name='dropout_prob')

  weights = {
    'wc1': tf.Variable(tf.random_normal([3, 3, 1, 8])),
    'wc2': tf.Variable(tf.random_normal([3, 3, 8, 16])),
    'wc3': tf.Variable(tf.random_normal([1, 1, 16, 16])),
    'wc4': tf.Variable(tf.random_normal([3, 3, 16, 32])),
    'wc5': tf.Variable(tf.random_normal([1, 1, 32, 32])),
    'wc6': tf.Variable(tf.random_normal([3, 3, 32, 32])),
    'wc7': tf.Variable(tf.random_normal([1, 1, 32, 32]))
  }

  biases = {
    'bc1': tf.Variable(tf.random_normal([8])),
    'bc2': tf.Variable(tf.random_normal([16])),
    'bc3': tf.Variable(tf.random_normal([16])),
    'bc4': tf.Variable(tf.random_normal([32])),
    'bc5': tf.Variable(tf.random_normal([32])),
    'bc6': tf.Variable(tf.random_normal([32])),
    'bc7': tf.Variable(tf.random_normal([32])),
    'bc8': tf.Variable(tf.random_normal([2]))
  }

  fingerprint_input = tf.reshape(fingerprint_input, shape=[-1, 98, 40, 1], name="fingerprint_input")

  conv1 = depthwise_conv2d(fingerprint_input, weights['wc1'], biases['bc1'])

  pool1 = maxpool2d(conv1, 3)

  conv2 = conv2d(pool1, weights['wc2'], biases['bc2'])

  conv3 = conv2d(conv2, weights['wc3'], biases['bc3'])

  pool2 = maxpool2d(conv3, 3)

  conv4 = conv2d(pool2, weights['wc4'], biases['bc4'])

  conv5 = conv2d(conv4, weights['wc5'], biases['bc5'])

  conv6 = conv2d(conv5, weights['wc6'], biases['bc6'])

  conv7 = conv2d(conv6, weights['wc7'], biases['bc7'])

  fc1 = tf.contrib.layers.flatten(conv7)

  fc1 = tf.layers.dense(fc1, 2)

  sft = tf.nn.softmax(fc1, name='labels_softmax')

  if is_training:
    return sft, dropout_prob
  else:
    return sft

1 个答案:

答案 0 :(得分:0)

这似乎是过度拟合的情况。你可以

    通过使用Shuffle中的Data
  1. shuffle=True cnn_model.fit。代码如下所示:

    history = cnn_model.fit(x = X_train_reshaped, y = y_train, batch_size = 512, epochs = epochs, callbacks=[callback], verbose = 1, validation_data = (X_test_reshaped, y_test), validation_steps = 10, steps_per_epoch=steps_per_epoch, shuffle = True)

  2. 使用Early Stopping。代码如下所示

    callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=15)

  3. 使用正则化。正则化代码如下所示(您也可以尝试l1正则化或l1_l2正则化):

from tensorflow.keras.regularizers import l2

Regularizer = l2(0.001)

cnn_model.add(Conv2D(64,3, 3, input_shape = (28,28,1), activation='relu', data_format='channels_last', activity_regularizer=Regularizer, kernel_regularizer=Regularizer))

cnn_model.add(Dense(units = 10, activation = 'sigmoid', activity_regularizer=Regularizer, kernel_regularizer=Regularizer))

  1. 您可以尝试使用BatchNormalization

  2. 使用ImageDataGenerator执行图像数据增强。有关更多信息,请参见this link

  3. 如果像素不是Normalized,则用255除以像素值也有帮助。

  4. 最后,如果仍然没有更改,则可以尝试使用Pre-Trained ModelsResNet之类的VGG Net,等等。

有关更多信息,请参阅此link