2017-03-26 23 views
1

我有由標籤,文件名和數據(即像素)的下列二進制文件:Python/Tensorflow - 在這種情況下,所有精度值爲「1」是否正常?

[array([2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 
     0, 2, 1, 0, 2, 1, 0]), array(['10_c.jpg', '10_m.jpg', '10_n.jpg', '1_c.jpg', 
     '1_m.jpg', '1_n.jpg', '2_c.jpg', '2_m.jpg', 
     '2_n.jpg', '3_c.jpg', '3_m.jpg', '3_n.jpg', 
     '4_c.jpg', '4_m.jpg', '4_n.jpg', '5_c.jpg', 
     '5_m.jpg', '5_n.jpg', '6_c.jpg', '6_m.jpg', 
     '6_n.jpg', '7_c.jpg', '7_m.jpg', '7_n.jpg', 
     '8_c.jpg', '8_m.jpg', '8_n.jpg', '9_c.jpg', 
     '9_m.jpg', '9_n.jpg'], 
     dtype='<U15'), array([[255, 252, 255, ..., 255, 255, 255], 
     [136, 137, 138, ..., 114, 110, 111], 
     [200, 200, 199, ..., 179, 178, 177], 
     ..., 
     [146, 157, 165, ..., 202, 202, 201], 
     [228, 225, 222, ..., 219, 221, 223], 
     [128, 127, 127, ..., 133, 129, 127]])] 

我所提供的標籤0到圖像用後綴_n.jpg1爲那些與後綴_m.jpg,和2對於那些有後綴_c.jpg

我有一個卷積神經網絡(CNN)的下面的代碼部分:

import numpy as np 
import matplotlib.pyplot as plt 
import cifar_tools 
import tensorflow as tf 

data, labels = cifar_tools.read_data('C:\\Users\\abc\\Desktop\\Testing') 

x = tf.placeholder(tf.float32, [None, 150 * 150]) 
y = tf.placeholder(tf.float32, [None, 2]) 

w1 = tf.Variable(tf.random_normal([5, 5, 1, 64])) 
b1 = tf.Variable(tf.random_normal([64])) 

w2 = tf.Variable(tf.random_normal([5, 5, 64, 64])) 
b2 = tf.Variable(tf.random_normal([64])) 

w3 = tf.Variable(tf.random_normal([38*38*64, 1024])) 
b3 = tf.Variable(tf.random_normal([1024])) 

w_out = tf.Variable(tf.random_normal([1024, 2])) 
b_out = tf.Variable(tf.random_normal([2])) 

def conv_layer(x,w,b): 
    conv = tf.nn.conv2d(x,w,strides=[1,1,1,1], padding = 'SAME') 
    conv_with_b = tf.nn.bias_add(conv,b) 
    conv_out = tf.nn.relu(conv_with_b) 
    return conv_out 

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

def model(): 
    x_reshaped = tf.reshape(x, shape=[-1, 150, 150, 1]) 

    conv_out1 = conv_layer(x_reshaped, w1, b1) 
    maxpool_out1 = maxpool_layer(conv_out1) 
    norm1 = tf.nn.lrn(maxpool_out1, 4, bias=1.0, alpha=0.001/9.0, beta=0.75) 
    conv_out2 = conv_layer(norm1, w2, b2) 
    norm2 = tf.nn.lrn(conv_out2, 4, bias=1.0, alpha=0.001/9.0, beta=0.75) 
    maxpool_out2 = maxpool_layer(norm2) 

    maxpool_reshaped = tf.reshape(maxpool_out2, [-1, w3.get_shape().as_list()[0]]) 
    local = tf.add(tf.matmul(maxpool_reshaped, w3), b3) 
    local_out = tf.nn.relu(local) 

    out = tf.add(tf.matmul(local_out, w_out), b_out) 
    return out 

model_op = model() 

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(model_op, y)) 
train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost) 

correct_pred = tf.equal(tf.argmax(model_op, 1), tf.argmax(y,1)) 
accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32)) 

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    onehot_labels = tf.one_hot(labels, 2, on_value=1.,off_value=0.,axis=-1) 
    onehot_vals = sess.run(onehot_labels) 
    batch_size = 1 
    for j in range(0, 5): 
     print('EPOCH', j) 
     for i in range(0, len(data), batch_size): 
      batch_data = data[i:i+batch_size, :] 
      batch_onehot_vals = onehot_vals[i:i+batch_size, :] 
      _, accuracy_val = sess.run([train_op, accuracy], feed_dict={x: batch_data, y: batch_onehot_vals}) 
      print(i, accuracy_val) 

     print('DONE WITH EPOCH') 

當我運行程序時,我得到了5個曆元以下:

EPOCH 0 
0 1.0 
1 1.0 
2 0.0 
3 0.0 
4 0.0 
5 1.0 
6 0.0 
7 0.0 
8 0.0 
9 0.0 
10 0.0 
11 1.0 
12 1.0 
13 0.0 
14 0.0 
15 0.0 
16 1.0 
17 0.0 
18 0.0 
19 0.0 
20 1.0 
21 1.0 
22 0.0 
23 1.0 
24 0.0 
25 1.0 
26 0.0 
27 0.0 
28 0.0 
29 1.0 
DONE WITH EPOCH 
EPOCH 1 
0 0.0 
1 1.0 
2 1.0 
3 1.0 
4 0.0 
5 0.0 
6 1.0 
7 1.0 
8 0.0 
9 0.0 
10 1.0 
11 1.0 
12 1.0 
13 0.0 
14 1.0 
15 1.0 
16 1.0 
17 0.0 
18 0.0 
19 1.0 
20 1.0 
21 0.0 
22 0.0 
23 1.0 
24 1.0 
25 1.0 
26 0.0 
27 0.0 
28 0.0 
29 1.0 
DONE WITH EPOCH 
EPOCH 2 
0 0.0 
1 0.0 
2 0.0 
3 1.0 
4 1.0 
5 0.0 
6 1.0 
7 0.0 
8 1.0 
9 0.0 
10 1.0 
11 0.0 
12 0.0 
13 0.0 
14 1.0 
15 1.0 
16 0.0 
17 0.0 
18 1.0 
19 1.0 
20 0.0 
21 0.0 
22 1.0 
23 1.0 
24 1.0 
25 0.0 
26 1.0 
27 1.0 
28 1.0 
29 0.0 
DONE WITH EPOCH 
EPOCH 3 
0 0.0 
1 1.0 
2 1.0 
3 0.0 
4 0.0 
5 1.0 
6 0.0 
7 0.0 
8 1.0 
9 1.0 
10 1.0 
11 0.0 
12 0.0 
13 1.0 
14 0.0 
15 0.0 
16 0.0 
17 1.0 
18 1.0 
19 0.0 
20 1.0 
21 1.0 
22 1.0 
23 0.0 
24 0.0 
25 1.0 
26 0.0 
27 1.0 
28 0.0 
29 1.0 
DONE WITH EPOCH 
EPOCH 4 
0 1.0 
1 1.0 
2 0.0 
3 1.0 
4 1.0 
5 0.0 
6 1.0 
7 1.0 
8 1.0 
9 1.0 
10 1.0 
11 1.0 
12 1.0 
13 1.0 
14 0.0 
15 0.0 
16 1.0 
17 1.0 
18 0.0 
19 0.0 
20 1.0 
21 1.0 
22 0.0 
23 1.0 
24 0.0 
25 1.0 
26 0.0 
27 0.0 
28 1.0 
29 1.0 
DONE WITH EPOCH 

是那些價值(精確度)正常基於上述數據?我詢問的原因是,對於其他數據(但有10個類的數據量很大),我注意到原始程序(我做了一些調整以使其與我的數據一起工作)返回結果(精度),其結果如下:

EPOCH 0 
1 0.104 
2 0.1 
3 0.136 
4 0.14 
5 0.124 
6 0.156 
7 0.16 
8 0.172 
9 0.16 
10 0.164 
11 0.148 
... 
... 
... 

你覺得我得到的結果是正常的(即只01)?

EDIT-1

我已經改變了batch_size到:

batch_size = len(data) // 2 

我現在遇到的輸出如下:

EPOCH 0 
0 0.466667 
15 0.666667 
DONE WITH EPOCH 
EPOCH 1 
0 0.666667 
15 0.6 
DONE WITH EPOCH 
EPOCH 2 
0 0.333333 
15 0.333333 
DONE WITH EPOCH 
EPOCH 3 
0 0.333333 
15 0.333333 
DONE WITH EPOCH 
EPOCH 4 
0 0.533333 
15 0.666667 
DONE WITH EPOCH 

這是正確?所以,我有30張圖片(我知道他們很少,但僅僅用於演示目的)。將它們分批放入的最佳方法是什麼?如果現在是正確的,是否有更好的方式來表示上述輸出?或者,也許對於這樣一個小數據集,我們可以將它們全部放在一個批次中(即batch_size = len(data))?

舉例來說,當我的batch_size設置= LEN(數據),我得到如下:

EPOCH 0 
0 0.333333 
DONE WITH EPOCH 
EPOCH 1 
0 0.666667 
DONE WITH EPOCH 
EPOCH 2 
0 0.666667 
DONE WITH EPOCH 
EPOCH 3 
0 0.333333 
DONE WITH EPOCH 
EPOCH 4 
0 0.366667 
DONE WITH EPOCH 

感謝。

回答

1

batch_size在你的代碼的值是1,所以每次運行

sess.run([train_op, accuracy], feed_dict={x: batch_data, y: batch_onehot_vals}) 

您檢查只有一張圖片。然後你有以下兩行:

correct_pred = tf.equal(tf.argmax(model_op, 1), tf.argmax(y,1)) 
accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32)) 

所以besically,correct_pred只是1號(0或1),因爲它是基於一個圖片(。所以,如果tf.argmax(model_op, 1)=tf.argmax(y,1)然後correct_pred=1否則,它等於0)。

然後accuracy就等於這個數字。因此它的值始終爲0或1.

的迴應編輯1:在數值上,這些值是有意義的。你的batch_size=15,所以準確度應該是1/15 = 0.0667的某個整數倍,這在你表中的所有值中確實是這種情況。 ,它應該是1/15的整數倍的理由是,因爲這兩條線:

correct_pred = tf.equal(tf.argmax(model_op, 1), tf.argmax(y,1)) 
accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32)) 

陣列correct_pred僅僅是一個0-1矢量(因爲它是tf.equal的結果)。 accuracy只是correct_pred中的值的總和,除以15的批量大小。

關於最佳批量大小,它取決於許多因素。例如,您可以在討論here中閱讀更多內容。

+0

感謝您的回覆。在原始代碼中,「batch_size」如下所示:batch_size = len(data)// 200.數據長度爲50000,batch_size爲250.那麼這是否意味着我們每次都檢查250張圖片? – Simplicity

+1

是的,這意味着他們同時計算所有模型的輸出。這就是爲什麼你在x_reshaped = tf.reshape(x,shape = [ - 1,150,150,1])中有-1。 -1表示圖片的數量,這些圖片不是已知的頭部,但是隻要您在feed_dict中將其傳遞,它就是已知的。正如你所看到的,所有的卷積等應用於整個batch_data(因爲你的feed_dict中有x_reshaped = batch_data)。 –

+0

謝謝。你能在我的問題中看到** EDIT-1 **嗎? – Simplicity