2016-11-04 14 views
2

shuffle.batchを使用して、.csvファイルから読み込んだトレーニング用のデータをバッチ処理しようとしています。しかし、私はコードを実行しているとき、それは動作していないようです。それはエラーを表示しなかったが、完了しなかった。TensorFlow:shuffle_batchにエラーは表示されませんでしたが、完了しませんでした

私のコードで何が間違っているとお考えですか?

また、容量とmin_after_dequeueの値はどれくらいですか?

import tensorflow as tf 
import numpy as np 


test_label = [] 
in_label = [] 

iris_TRAINING = "iris_training.csv" 
iris_TEST = "iris_test.csv" 

# Load datasets. 
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(filename=iris_TRAINING, target_dtype=np.int, features_dtype=np.float32) 
test_set = tf.contrib.learn.datasets.base.load_csv_with_header(filename=iris_TEST, target_dtype=np.int, features_dtype=np.float32) 

x_train, x_test, y_train, y_test = training_set.data, test_set.data, training_set.target, test_set.target 



for n in y_train: 
    targets = np.zeros(3) 
    targets[int(n)] = 1 # one-hot pixs[0] is label and then use that number as index of one-hot 
    in_label.append(targets) #store all of label (one-hot) 
training_label = np.asarray(in_label) 

for i in y_test:  
    test_targets = np.zeros(3) 
    test_targets[int(i)] = 1 # one-hot pixs[0] is label and then use that number as index of one-hot 
    test_label.append(test_targets) 
test_label = np.asarray(test_label) 


x = tf.placeholder(tf.float32, [None,4]) #generate placeholder to store value of features for training 

W = tf.Variable(tf.zeros([4, 3])) #weight 
b = tf.Variable(tf.zeros([3])) #bias 

y = tf.matmul(x, W) + b 

y_ = tf.placeholder(tf.float32, [None, 3]) #generate placeholder to store value of labels 


cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, y_)) 
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) 


sess = tf.InteractiveSession() 
# Train 
tf.initialize_all_variables().run() 

for i in range(5): 
    batch_xt, batch_yt = tf.train.shuffle_batch([x_train,training_label],batch_size=10,capacity=200,min_after_dequeue=10) 
    sess.run(train_step, feed_dict={x: batch_xt.eval(), y_: batch_yt.eval()}) 
    print(i) 

# 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(sess.run(accuracy, feed_dict={x: x_test, y_: test_label})) 

答えて

0

Shuffle_batchビルド:

  1. キューデータセットのバッチがエンキューされますQその中に
  2. 操作Qをデキューし、バッチ
  3. QueueRunnerを取得しますをエンキューするQ

(詳細はhereを参照)

ですから、各繰り返しでShuffle_batchを呼び出す必要がありますが、一度だけ、あなたのループの前にはありません。その後、tf.train.start_queue_runners()に電話する必要があります。だからあなたのコードの終わりのようなものでなければなりません:容量のため

sess = tf.InteractiveSession() 
# Train 
tf.initialize_all_variables().run() 
batch_xt, batch_yt = tf.train.shuffle_batch([x_train,training_label],batch_size=10,capacity=200,min_after_dequeue=10) 
tf.train.start_queue_runners() 

for i in range(5): 
    sess.run(train_step, feed_dict={x: batch_xt.eval(), y_: batch_yt.eval()}) 
    print(i) 

# 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(sess.run(accuracy, feed_dict={x: x_test, y_: test_label})) 

適した値とmin_after_dequeueは、使用可能なメモリとI/Oスループットの依存します。容量は、データセットのメモリ内の場所を制限します。それらは計算時間に影響する可能性がありますが、最終的な結果には影響しません(詳細はhereを参照してください)。

関連する問題