【问题标题】:Why does this neural network model work poorly?为什么这个神经网络模型效果不佳?
【发布时间】:2019-11-17 08:43:19
【问题描述】:

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

她总是给出这样的结果。 未知(分数 = 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

【问题讨论】:

  • 你能确认一下'tf'是什么吗? (张量流?)
  • @PaulaThomas 将 tensorflow 导入为 tf
  • 你有一个新生儿模型。它可以做很多事情,但还没有经过训练。出生时你会哭,只会拉屎。它需要输入数据、训练和一个错误函数来让 goo goo 或 mama 摆脱它。
  • @EngrStudent 这里的问题很可能是它没有辍学。
  • 告诉我有关错误度量和培训的信息。

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


【解决方案1】:

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

  1. ShuffleData,通过在cnn_model.fit 中使用shuffle=True。代码如下:

    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 Models,比如ResNet或者VGG Net等。

更多信息请参考link。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 2012-11-03
    相关资源
    最近更新 更多