为什么网络为测试中的每个输入提供相同的结果?

时间:2018-06-10 10:49:44

标签: python-3.x tensorflow neural-network conv-neural-network

我的网络将尺寸为62 * 71的图像转换为124个输出的矢量。在测试中,我为每个输入获得了相同的输出。我检查了4000例。

我似乎无法表示问题,因为学习似乎很好,错误有所改善并且错误相对较低。

有人可能知道这是什么问题?

#load data
data_in= np.transpose(np.loadtxt("images_in_10000.csv", delimiter=',',dtype=np.float32))
data_out= np.transpose(np.loadtxt("out_to_image_10000.csv", delimiter=',',dtype=np.float32))

x_train = data_in[0:6000, :]
x_test = data_in[6000:10001,:]
y_train = data_out[0:6000, :]
y_test = data_out[6000:10001, :]

#parametersa
batch=100
epochs=7
learning_rate=0.01

n = x_test.shape[1] #4392
m = x_train.shape[0] #6000
d = y_test.shape[1]  #124
l = y_test.shape[0]     #4000

trainX = tf.placeholder(tf.float32, [batch, n])
trainY = tf.placeholder(tf.float32, [batch, d])
testX = tf.placeholder(tf.float32, [l, n])
testY = tf.placeholder(tf.float32, [l, d])

W_c1= tf.Variable(tf.random_normal([5, 5, 1, 32]))
W_c2= tf.Variable(tf.random_normal([5, 5, 32, 64]))
W_fc= tf.Variable(tf.random_normal([18 * 16 * 64, 128]))
W_out= tf.Variable(tf.random_normal([128, d]))

b_c1= tf.Variable(tf.random_normal([32]))
b_c2=tf.Variable(tf.random_normal([64]))
b_fc=tf.Variable(tf.random_normal([128]))
b_out=tf.Variable(tf.random_normal([d]))

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def maxpool2d(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

def convolutional_neural_network(x):

    x = tf.reshape(x, shape=[-1,61,72, 1])

    conv1 = tf.nn.relu(conv2d(x, W_c1) + b_c1)
    conv1 = maxpool2d(conv1)

    conv2 = tf.nn.relu(conv2d(conv1, W_c2) + b_c2)
    conv2 = maxpool2d(conv2)

    fc = tf.reshape(conv2, [-1, 18 * 16 * 64])
    fc = tf.nn.relu(tf.matmul(fc, W_fc) + b_fc)

    output = tf.matmul(fc, W_out) + b_out

    return output

   prediction = convolutional_neural_network(trainX)
   cost =tf.reduce_mean(tf.pow(prediction-trainY,2))
   optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)

prediction_t = convolutional_neural_network(testX)
losstest = tf.reduce_mean(tf.pow(prediction_t - testY, 2))

k=0
a = np.linspace(0, m - batch, m / batch, dtype=np.int32)
costshow = [0] * (len(a) * epochs)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(epochs):
        epoch_loss = 0
        for i in (np.linspace(0,m - batch, m / batch, dtype=np.int32)):
            x = x_train[i:i + batch, :]
            y = y_train[i:i + batch, :]
            sess.run(optimizer, feed_dict={trainX: x, trainY: y})
            cost_val = sess.run(cost, feed_dict={trainX: x, trainY: y})
            costshow[k]=cost_val
            print("Epoch=", '%04d' % (epoch + 1), "loss=", " {:.9f}".format(cost_val))
            k = k + 1

    print("finsh train-small ")
    result = sess.run(prediction_t, feed_dict={testX: x_test})
    test_loss = sess.run(losstest, feed_dict={testX: np.asarray(x_test), testY: np.asarray(y_test)})
    print("Testing loss=", test_loss)

1 个答案:

答案 0 :(得分:0)

图片背后的指标已明确定义。图像的值通常在0-1或0-255之间。对于CNN,您应该将输入值标准化(0-1)。

因此你必须小心你的体重初始化。例如,如果您的偏差为0.6且值为0.6,则您获得1.2作为图像值,并且您的绘图程序认为您处于0-255范围内且一切都是黑色。

因此,尝试使用glorot-initializer作为偏差初始化器的权重和零初始化器:

重量:

tf.get_variable("weight", shape=[5, 5, 1, 32], initializer=tf.glorot_uniform_initializer())

偏压:

tf.get_variable("bias", shape=[32], initializer=tf.zeros_initializer())

此外,不推荐使用tf.Variabel。最好使用tf.get_variable