2016-03-21 17 views
2

私はTensorFlow exampleを自分のデータで実行しようとしていますが、何らかの理由で分類器は常にすべてのテストの例で同じクラスを選択します。入力データは常に先にシャッフルされます。トレーニングセットとして約4000枚、テストセットとして約500枚の画像があります。右側には、すべての500枚の画像[1. 0.]のために残っているTensorflowは常に同じ結果を予測します

Result: [[ 1. 0.]] Actually: [ 1. 0.] 
Result: [[ 1. 0.]] Actually: [ 0. 1.] 
Result: [[ 1. 0.]] Actually: [ 1. 0.] 
Result: [[ 1. 0.]] Actually: [ 1. 0.] 
Result: [[ 1. 0.]] Actually: [ 0. 1.] 
Result: [[ 1. 0.]] Actually: [ 0. 1.] 
... 

結果私のようなルックスを取得します。分類はバイナリですので、ラベルは2つだけです。ここで

は、私のソースコードです:

import tensorflow as tf 
import input_data as id 

test_images, test_labels = id.read_images_from_csv(
    "/home/johnny/Desktop/tensorflow-examples/46-model.csv") 

train_images = test_images[:4000] 
train_labels = test_labels[:4000] 
test_images = test_images[4000:] 
test_labels = test_labels[4000:] 

print len(train_images) 
print len(test_images) 

pixels = 200 * 200 
labels = 2 

sess = tf.InteractiveSession() 

# Create the model 
x = tf.placeholder(tf.float32, [None, pixels]) 
W = tf.Variable(tf.zeros([pixels, labels])) 
b = tf.Variable(tf.zeros([labels])) 
y_prime = tf.matmul(x, W) + b 
y = tf.nn.softmax(y_prime) 

# Define loss and optimizer 
y_ = tf.placeholder(tf.float32, [None, labels]) 
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(y_prime, y_) 
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) 

# Train 
tf.initialize_all_variables().run() 
for i in range(10): 
    res = train_step.run({x: train_images, y_: train_labels}) 
# Test trained model 
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
print(accuracy.eval({x: test_images, y_: test_labels})) 

for i in range(0, len(test_images)): 
    res = sess.run(y, {x: [test_images[i]]}) 
    print("Result: " + str(res) + " Actually: " + str(test_labels[i])) 

私はポイント足りませんか?

答えて

10

あなたのコード内の3つの潜在的な問題があります。

  1. 重み、Wは、ゼロに初期化されています。 This question from stats.stackexchange.comは、なぜこれがトレーニングの成果に悪影響を及ぼしているのか(地域の最低限に固執するなど)についてよく議論しています。

    W = tf.Variable(tf.truncated_normal([pixels, labels], 
                stddev=1./math.sqrt(pixels))) 
    
  2. cross_entropyは、たとえばtf.reduce_mean()のために使用し、それを最小化する前に、単一の、スカラー値に集約する必要があります:

    cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(y_prime, y_)) 
    
  3. あなたは次のように代わりに、例えば、ランダムに初期化する必要があります一度にデータセット全体を訓練するのではなく、ミニバッチ(または単一の例)を訓練すると、より早く収束する可能性があります。

    for i in range(10): 
         for j in range(4000): 
          res = train_step.run({x: train_images[j:j+1], 
                y_: train_labels[j:j+1]}) 
    
+0

ヒントのおかげで、私は大きな時間を助けました – Johnny000

1

その他の問題は、クラスの不均衡です。他のクラスよりも重要なクラスがあれば、その値に収束する可能性があります。トレーニングサンプルのクラスのバランスをとるとともに、小規模のバッチを使用してください。たとえば、ラベルがバイナリの場合は、トレーニングサンプルにゼロとラベルが同じであることを確認します。

関連する問題