5

मैं टेन्सरफ्लो का उपयोग करके अपने तंत्रिका नेटवर्क को प्रशिक्षित करने के लिए बैच सामान्यीकरण का उपयोग करने की कोशिश कर रहा था लेकिन यह मुझे अस्पष्ट था कि the official layer implementation of Batch Normalization का उपयोग कैसे करें (ध्यान दें कि यह API से एक से अलग है)।कोई टेंसरफ्लो में आधिकारिक बैच सामान्यीकरण परत का उपयोग कैसे करता है?

उनके github issues पर कुछ दर्दनाक खुदाई के बाद ऐसा लगता है एक एक tf.cond की जरूरत है कि यह ठीक से उपयोग करने के लिए और यह भी एक 'resue = सच' फ्लैग ताकि बी एन पारी और बड़े पैमाने चर ठीक से पुन: उपयोग किया जाता है। यह पता लगाने के बाद कि मैंने इसका एक छोटा सा विवरण प्रदान किया है कि मुझे विश्वास है कि इसका उपयोग करने का सही तरीका here है।

अब मैंने इसका परीक्षण करने के लिए एक छोटी सी लिपि लिखी है (केवल एक परत और एक रेलू, इसे इससे छोटा बनाने में कठोर)। हालांकि, मुझे 100% यकीन नहीं है कि इसका परीक्षण कैसे किया जाए। अभी मेरा कोड कोई त्रुटि संदेश नहीं चलाता है लेकिन अप्रत्याशित रूप से NaNs देता है। जो मेरा विश्वास कम करता है कि मैंने जो पोस्ट दिया है वह अन्य पोस्ट में सही हो सकता है। या शायद मेरे पास नेटवर्क अजीब है। किसी भी तरह से, क्या कोई जानता है कि क्या गलत है?

import tensorflow as tf 
# download and install the MNIST data automatically 
from tensorflow.examples.tutorials.mnist import input_data 
from tensorflow.contrib.layers.python.layers import batch_norm as batch_norm 

def batch_norm_layer(x,train_phase,scope_bn): 
    bn_train = batch_norm(x, decay=0.999, center=True, scale=True, 
    is_training=True, 
    reuse=None, # is this right? 
    trainable=True, 
    scope=scope_bn) 

    bn_inference = batch_norm(x, decay=0.999, center=True, scale=True, 
    is_training=False, 
    reuse=True, # is this right? 
    trainable=True, 
    scope=scope_bn) 

    z = tf.cond(train_phase, lambda: bn_train, lambda: bn_inference) 
    return z 

def get_NN_layer(x, input_dim, output_dim, scope, train_phase): 
    with tf.name_scope(scope+'vars'): 
     W = tf.Variable(tf.truncated_normal(shape=[input_dim, output_dim], mean=0.0, stddev=0.1)) 
     b = tf.Variable(tf.constant(0.1, shape=[output_dim])) 
    with tf.name_scope(scope+'Z'): 
     z = tf.matmul(x,W) + b 
    with tf.name_scope(scope+'BN'): 
     if train_phase is not None: 
      z = batch_norm_layer(z,train_phase,scope+'BN_unit') 
    with tf.name_scope(scope+'A'): 
     a = tf.nn.relu(z) # (M x D1) = (M x D) * (D x D1) 
    return a 

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 
# placeholder for data 
x = tf.placeholder(tf.float32, [None, 784]) 
# placeholder that turns BN during training or off during inference 
train_phase = tf.placeholder(tf.bool, name='phase_train') 
# variables for parameters 
hiden_units = 25 
layer1 = get_NN_layer(x, input_dim=784, output_dim=hiden_units, scope='layer1', train_phase=train_phase) 
# create model 
W_final = tf.Variable(tf.truncated_normal(shape=[hiden_units, 10], mean=0.0, stddev=0.1)) 
b_final = tf.Variable(tf.constant(0.1, shape=[10])) 
y = tf.nn.softmax(tf.matmul(layer1, W_final) + b_final) 

### training 
y_ = tf.placeholder(tf.float32, [None, 10]) 
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) 
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) 
with tf.Session() as sess: 
    sess.run(tf.initialize_all_variables()) 
    steps = 3000 
    for iter_step in xrange(steps): 
     #feed_dict_batch = get_batch_feed(X_train, Y_train, M, phase_train) 
     batch_xs, batch_ys = mnist.train.next_batch(100) 
     # Collect model statistics 
     if iter_step%1000 == 0: 
      batch_xstrain, batch_xstrain = batch_xs, batch_ys #simualtes train data 
      batch_xcv, batch_ycv = mnist.test.next_batch(5000) #simualtes CV data 
      batch_xtest, batch_ytest = mnist.test.next_batch(5000) #simualtes test data 
      # do inference 
      train_error = sess.run(fetches=cross_entropy, feed_dict={x: batch_xs, y_:batch_ys, train_phase: False}) 
      cv_error = sess.run(fetches=cross_entropy, feed_dict={x: batch_xcv, y_:batch_ycv, train_phase: False}) 
      test_error = sess.run(fetches=cross_entropy, feed_dict={x: batch_xtest, y_:batch_ytest, train_phase: False}) 

      def do_stuff_with_errors(*args): 
       print args 
      do_stuff_with_errors(train_error, cv_error, test_error) 
     # Run Train Step 
     sess.run(fetches=train_step, feed_dict={x: batch_xs, y_:batch_ys, train_phase: True}) 
    # list of booleans indicating correct predictions 
    correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 
    # accuracy 
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
    print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels, train_phase: False})) 

जब मैं इसे चलाने मैं:

Extracting MNIST_data/train-images-idx3-ubyte.gz 
Extracting MNIST_data/train-labels-idx1-ubyte.gz 
Extracting MNIST_data/t10k-images-idx3-ubyte.gz 
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz 
(2.3474066, 2.3498712, 2.3461707) 
(0.49414295, 0.88536006, 0.91152304) 
(0.51632041, 0.393666, nan) 
0.9296 

यह सब पिछले वाले हुआ करते थे नेन थे और अब केवल एक उनमें से कुछ यहाँ कोड है। क्या सब ठीक है या मैं पागल हूँ?

सबसे पहले:

+0

उपयोग करने के लिए https://stackoverflow.com/questions/33949786/how-could-i में मेरा उत्तर की जाँच की जरूरत है प्रयोग बैच-सामान्य-इन-tensorflow/38325288 # 38325288। जब मैंने शुरुआत में इसे आजमाया तो मुझे हानि समारोह में नाइन भी था और मैंने दो मुद्दों को संबोधित किया: 1) सीखने की दर कम हो गई 2) सुनिश्चित किया गया कि बैच सामान्यीकरण प्रत्येक परत के लिए किया जाता है। मतलब मैं छुपा परत और छुपा परत के आउटपुट में इनपुट सामान्यीकृत बैच। –

+0

यदि मैं कुछ ऐसा उपयोग करता हूं: tf.name_scope ('batch_norm') के साथ, क्या यह अभी भी batch_norm फ़ंक्शन में स्कोप चर को पारित करने के लिए आवश्यक है? – resistancefm

उत्तर

6

मुझे यकीन है कि अगर यह आपकी समस्या का समाधान होगा नहीं कर रहा हूँ, BatchNorm के लिए दस्तावेज़/जानकारीपूर्ण काफी उपयोग में आसान नहीं है, इसलिए यहाँ कैसे सरल BatchNorm उपयोग करने के लिए पर एक संक्षिप्त संक्षिप्त है , आप अपनी बैच नॉर्म परत को परिभाषित करते हैं। आप एक affine/पूरी तरह से जुड़ा हुआ परत के बाद इसका उपयोग करना चाहते हैं, तो आप ऐसा करते हैं (सिर्फ एक उदाहरण है, ताकि अलग हो सकता है/के रूप में आप की इच्छा):

... 
inputs = tf.matmul(inputs, W) + b 
inputs = tf.layers.batch_normalization(inputs, training=is_training) 
inputs = tf.nn.relu(inputs) 
... 

समारोह tf.layers.batch_normalization कॉल चर initializers। ये आंतरिक-चर हैं और जिन्हें कॉल करने के लिए एक विशेष दायरा की आवश्यकता है, जो tf.GraphKeys.UPDATE_OPS में है। जैसे, यदि आप अपने अनुकूलक समारोह के रूप में इस कॉल करना होगा (के बाद सभी परतों में परिभाषित किया गया है!):

... 
extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) 
with tf.control_dependencies(extra_update_ops): 
    trainer = tf.train.AdamOptimizer() 
    updateModel = trainer.minimize(loss, global_step=global_step) 
... 

आप इसे here के बारे में अधिक पढ़ सकते हैं। मुझे पता है कि आपके प्रश्न का उत्तर देने में थोड़ा देर हो चुकी है, लेकिन यह अन्य लोगों को टेन्सफोर्लो में बैचनोर्म समस्याओं में आने में मदद कर सकता है! :)

0
training =tf.placeholder(tf.bool, name = 'training') 

lr_holder = tf.placeholder(tf.float32, [], name='learning_rate') 
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) 
with tf.control_dependencies(update_ops): 
     optimizer = tf.train.AdamOptimizer(learning_rate = lr).minimize(cost) 

जब परतों को परिभाषित करने के लिए, आप प्लेसहोल्डर 'प्रशिक्षण'

batchNormal_layer = tf.layers.batch_normalization(pre_batchNormal_layer, training=training) 
संबंधित मुद्दे