卷积神经网络为所有标签输出相等的概率

时间:2018-01-12 21:59:56

标签: tensorflow machine-learning neural-network conv-neural-network mnist

我目前在MNIST上训练CNN,随着训练的进行,输出概率(softmax)给出[0.1,0.1,...,0.1]。初始值不均匀,所以我无法弄清楚我是否在这里做了些蠢事?

我只训练了15个步骤,只是为了看看训练是如何进行的;虽然这个数字较低,但我认为这不应该导致统一的预测?

import numpy as np
import tensorflow as tf
import imageio

from sklearn.datasets import fetch_mldata
mnist = fetch_mldata('MNIST original')

# Getting data

from sklearn.model_selection import train_test_split
def one_hot_encode(data):
    new_ = []
    for i in range(len(data)):
        _ = np.zeros([10],dtype=np.float32)
        _[int(data[i])] = 1.0
        new_.append(np.asarray(_))
    return new_

data = np.asarray(mnist["data"],dtype=np.float32)
labels = np.asarray(mnist["target"],dtype=np.float32)
labels = one_hot_encode(labels)
tr_data,test_data,tr_labels,test_labels = train_test_split(data,labels,test_size = 0.1)
tr_data = np.asarray(tr_data)
tr_data = np.reshape(tr_data,[len(tr_data),28,28,1])
test_data = np.asarray(test_data)
test_data = np.reshape(test_data,[len(test_data),28,28,1])
tr_labels = np.asarray(tr_labels)
test_labels = np.asarray(test_labels)

def get_conv(x,shape):
    weights = tf.Variable(tf.random_normal(shape,stddev=0.05))
    biases = tf.Variable(tf.random_normal([shape[-1]],stddev=0.05))
    conv = tf.nn.conv2d(x,weights,[1,1,1,1],padding="SAME")
    return tf.nn.relu(tf.nn.bias_add(conv,biases))

def get_pool(x,shape):
    return tf.nn.max_pool(x,ksize=shape,strides=shape,padding="SAME")

def get_fc(x,shape):
    sh = x.get_shape().as_list()
    dim = 1
    for i in sh[1:]:
        dim *= i
    x = tf.reshape(x,[-1,dim])
    weights = tf.Variable(tf.random_normal(shape,stddev=0.05))
    return tf.nn.relu(tf.matmul(x,weights) + tf.Variable(tf.random_normal([shape[1]],stddev=0.05)))

#Creating model

x = tf.placeholder(tf.float32,shape=[None,28,28,1])
y = tf.placeholder(tf.float32,shape=[None,10])

conv1_1 = get_conv(x,[3,3,1,128])
conv1_2 = get_conv(conv1_1,[3,3,128,128])
pool1 = get_pool(conv1_2,[1,2,2,1])

conv2_1 = get_conv(pool1,[3,3,128,512])
conv2_2 = get_conv(conv2_1,[3,3,512,512])
pool2 = get_pool(conv2_2,[1,2,2,1])

conv3_1 = get_conv(pool2,[3,3,512,1024])
conv3_2 = get_conv(conv3_1,[3,3,1024,1024])
conv3_3 = get_conv(conv3_2,[3,3,1024,1024])
conv3_4 = get_conv(conv3_3,[3,3,1024,1024])
pool3 = get_pool(conv3_4,[1,3,3,1])

fc1 = get_fc(pool3,[9216,1024])
fc2 = get_fc(fc1,[1024,10])

softmax = tf.nn.softmax(fc2)
loss = tf.losses.softmax_cross_entropy(logits=fc2,onehot_labels=y)
train_step = tf.train.AdamOptimizer().minimize(loss)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for i in range(15):
    print(i)
    indices = np.random.randint(len(tr_data),size=[200])
    batch_data = tr_data[indices]
    batch_labels = tr_labels[indices]
    sess.run(train_step,feed_dict={x:batch_data,y:batch_labels})

非常感谢你。

1 个答案:

答案 0 :(得分:3)

您的代码存在一些问题,包括基本问题。我强烈建议您先阅读MNIST的Tensorflow分步教程,MNIST For ML BeginnersDeep MNIST for Experts

简而言之,关于你的代码:

首先,您的最后一层fc2 会激活ReLU。

其次,你建立批次的方式,即

indices = np.random.randint(len(tr_data),size=[200])

只是在每次迭代中抓取随机样本,这远非正确的方式...

第三,您输入网络的数据未在[0,1]中标准化,因为它们应该是:

np.max(tr_data[0]) # get the max value of your first training sample
# 255.0

第三点最初也让我感到困惑,因为在前面提到的Tensorflow教程中,他们似乎也没有对数据进行标准化。但仔细检查发现了原因:如果你通过Tensorflow提供的实用程序函数导入MNIST数据(而不是scikit-learn那些,就像你在这里一样),它们已经在[0,1]中已经标准化,这是无处可去的暗示:

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
np.max(mnist.train.images[0])
# 0.99607849

这是一个公认的奇怪的设计决策 - 据我所知,在所有其他类似的案例/教程中,规范化输入数据是管道的明确部分(参见例如Keras example),以及有充分的理由(当你使用自己的数据时,你肯定会在以后做这件事)。