TFLearn DNN以相同的方式对每个输入进行分类

时间:2018-10-19 06:03:26

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

我正在使用TFLearn创建四个网络:用于对图像进行分类的基本ANN,用于对声音文件进行分类的基本ANN,用于对图像进行分类的CNN和用于对声音文件进行分类的CNN。无论我训练网络多少,使用什么学习率或激活功能,或训练它们的数据集如何,我总能得到相同的结果。网络总是将所有输入都归为一类(例如,经过训练以区分蜜蜂图片和没有蜜蜂的图片的网络总是将每张图片都标识为包含蜜蜂)。

我知道我并没有过拟合,因为即使训练数据集也会产生这种结果。

什么可能导致此问题?

以下是创建和测试网络的代码。他们使用.fit()方法进行培训。

import tflearn
import tensorflow as tf
import numpy as np
import pickle as cPickle
import random
import os
from math import inf

def create_image_ann():
    image_ann_input = tflearn.layers.core.input_data(shape=[None, 32, 32], 
    name='input_layer')
    image_ann_layer_1 = 
    tflearn.layers.core.fully_connected(incoming=image_ann_input, 
    activation='linear', n_units=100, name='fc_layer_1')
    image_ann_layer_2 = 
    tflearn.layers.core.fully_connected(incoming=image_ann_layer_1, 
    activation='linear', n_units=100, name='fc_layer_2')
    image_ann_layer_3 = 
    tflearn.layers.core.fully_connected(incoming=image_ann_layer_2, 
    activation='linear', n_units=100, name='fc_layer_3')
    image_ann_layer_4 = 
    tflearn.layers.core.fully_connected(incoming=image_ann_layer_3, 
    activation='linear', n_units=100, name='fc_layer_4')

    image_ann_output = 
    tflearn.layers.core.fully_connected(incoming=image_ann_layer_4, 
    activation='linear', n_units=2, name='output_layer')

    image_ann_regression = 
    tflearn.layers.estimator.regression(image_ann_output, optimizer='sgd', 
    learning_rate=0.1)

    model = tflearn.DNN(image_ann_regression)

    return model

def create_image_cnn():
    input_layer = tflearn.layers.core.input_data(shape=[None, 32, 32])
    input_layer = tf.expand_dims(input_layer, 2)

    conv_layer_1 = tflearn.layers.conv.conv_2d(input_layer, nb_filter=20, 
    filter_size=5, activation='linear', name='conv_layer_1')
    conv_layer_2 = tflearn.layers.conv.conv_2d(input_layer, nb_filter=20, 
    filter_size=5, activation='linear', name='conv_layer_2')

    conv_layer_3 = tflearn.layers.conv.conv_2d(input_layer, nb_filter=20, 
    filter_size=10, activation='linear', name='conv_layer_3')
    conv_layer_4 = tflearn.layers.conv.conv_2d(input_layer, nb_filter=20, 
    filter_size=10, activation='linear', name='conv_layer_4')

    conv_layer_5 = tflearn.layers.conv.conv_2d(input_layer, nb_filter=20, 
    filter_size=15, activation='linear', name='conv_layer_5')
    conv_layer_6 = tflearn.layers.conv.conv_2d(input_layer, nb_filter=20, 
    filter_size=15, activation='linear', name='conv_layer_6')

    pool_layer_1 = tflearn.layers.conv.max_pool_2d(conv_layer_1, 2, 
    name='pool_layer_1')
    pool_layer_2 = tflearn.layers.conv.max_pool_2d(conv_layer_2, 2, 
    name='pool_layer_2')
    pool_layer_3 = tflearn.layers.conv.max_pool_2d(conv_layer_3, 2, 
    name='pool_layer_3')
    pool_layer_4 = tflearn.layers.conv.max_pool_2d(conv_layer_4, 2, 
    name='pool_layer_4')
    pool_layer_5 = tflearn.layers.conv.max_pool_2d(conv_layer_5, 2, 
    name='pool_layer_5')
    pool_layer_6 = tflearn.layers.conv.max_pool_2d(conv_layer_6, 2, 
    name='pool_layer_6')

    merged_pool_layer = tflearn.layers.merge_ops.merge([pool_layer_1, 
    pool_layer_2, pool_layer_3, pool_layer_4, pool_layer_5, pool_layer_6], 
    'concat', name='merge_layer')

    fc_layer_1 = tflearn.layers.core.fully_connected(merged_pool_layer, 
    n_units=100, activation='linear', name='fc_layer_1')

    output_layer = tflearn.layers.core.fully_connected(fc_layer_1, 
    n_units=2, activation='linear', name='output_layer')

    regression = tflearn.layers.estimator.regression(output_layer, 
    optimizer='sgd', learning_rate=0.1)

    model = tflearn.DNN(regression)

    return model

def create_audio_ann():
    input_layer = tflearn.layers.core.input_data(shape=[None, 500])
    input_layer = tf.expand_dims(input_layer, 2)

    fc_layer_1 = tflearn.layers.core.fully_connected(input_layer, 
    n_units=100, activation='linear', name='fc_layer_1')
    fc_layer_2 = tflearn.layers.core.fully_connected(fc_layer_1, 
    n_units=100, activation='linear', name='fc_layer_1')
    fc_layer_3 = tflearn.layers.core.fully_connected(fc_layer_2, 
    n_units=100, activation='linear', name='fc_layer_1')
    fc_layer_4 = tflearn.layers.core.fully_connected(fc_layer_3, 
    n_units=100, activation='linear', name='fc_layer_1')
    fc_layer_5 = tflearn.layers.core.fully_connected(fc_layer_4, 
    n_units=100, activation='linear', name='fc_layer_1')

    output_layer = tflearn.layers.core.fully_connected(fc_layer_5, 
    n_units=3, activation='linear', name='output_layer')

    regression = tflearn.layers.estimator.regression(output_layer, 
    optimizer='sgd', learning_rate=0.7)

    model = tflearn.DNN(regression)

    return model

def create_audio_cnn():
    input_layer = tflearn.layers.core.input_data(shape=[None, 500])
    input_layer = tf.expand_dims(input_layer, 2)

    conv_layer_1 = tflearn.layers.conv.conv_1d(input_layer, nb_filter=20, 
    filter_size=50, activation='linear', name='conv_layer_1')
    conv_layer_2 = tflearn.layers.conv.conv_1d(input_layer, nb_filter=20, 
    filter_size=75, activation='linear', name='conv_layer_1')
    conv_layer_3 = tflearn.layers.conv.conv_1d(input_layer, nb_filter=20, 
    filter_size=100, activation='linear', name='conv_layer_1')

    pool_layer_1 = tflearn.layers.conv.max_pool_1d(conv_layer_1, 
    kernel_size=5, name="pool_layer_1")
    pool_layer_2 = tflearn.layers.conv.max_pool_1d(conv_layer_2, 
    kernel_size=5, name="pool_layer_2")
    pool_layer_3 = tflearn.layers.conv.max_pool_1d(conv_layer_3, 
    kernel_size=5, name="pool_layer_3")

    merged_pool_layer = tflearn.layers.merge_ops.merge(
    [pool_layer_1, pool_layer_2, pool_layer_3], 'concat',
    name='merge_layer')

    fc_layer_1 = tflearn.layers.core.fully_connected(merged_pool_layer, 
    n_units=100, activation='linear', name='fc_layer_1')

    output_layer = tflearn.layers.core.fully_connected(fc_layer_1, 
    n_units=3, activation='linear', name='output_layer')

    regression = tflearn.layers.estimator.regression(output_layer, 
    optimizer='sgd', learning_rate=0.1)

    model = tflearn.DNN(regression)

    return model

def test_network(net, test_data):
    test_inputs = test_data[0]
    targets = test_data[1]

    correct_answers = 0

    outputs = net.predict(test_inputs)
    for i in range(len(outputs)):
        output = outputs[i]

        max_output = -inf
        max_output_index = -1
        for j in range(len(output)):
            if output[j] > max_output:
                max_output = output[j]
                max_output_index = j

        input(str(output) + ', ' + str(targets[i]) + ': ' + str(targets[i] 
        [max_output_index] == 1))
        if targets[i][max_output_index] == 1:
            correct_answers += 1

    print("Amount Correct: " + str(correct_answers) + " / " + 
    str(len(test_inputs)))
    print(str(100.0 * correct_answers / len(test_inputs)) + "%")

def save(obj, file_path):
    with open(file_path, 'wb') as fp:
        cPickle.dump(obj, fp)

def load(file_path):
    with open(file_path, 'rb') as fp:
        return cPickle.load(fp)

0 个答案:

没有答案