2017-11-02 28 views
0

私はtensorflowを使ってgoogleから提供されたSVHNデータセットを使用して数字を分類できるモデルを作成しています。マイクレートは実際には低いです(〜25%)が、正確さ。 Reference NotebookTensorflowの予測精度を上げる方法

私のモデルをより良くするために、正確さをどのように向上させるべきか、誰かに私にいくつかのヒントを教えてもらえないかと思っていました。

ここは私のモデルコードです。

filename='extra.pickle' 

with open(filename,'rb') as f: 
    other=pickle.load(f) 
    train_data=other['train_dataset'] 
    test_data=other['test_dataset'] 

    del other 

train_dataset=train_data['X'] 
test_dataset=test_data['X'] 
train_labels=train_data['y'] 
test_labels=test_data['y'] 

print(len(test_dataset)) 
print(len(test_labels)) 
print(test_dataset.shape) 
#print(train_dataset.length()) 

classes=10 

batch_size=32 

num_steps = 200000 

graph=tf.Graph() 

#Placeholder for the data 
with graph.as_default(): 

    data_placeholder = tf.placeholder(tf.float32, shape=(batch_size,32,32,3)) 
    label_placeolder = tf.placeholder(tf.int64, shape=(batch_size, classes)) 

    tf_test_dataset = tf.placeholder(tf.float32, shape=(batch_size,32,32,3)) 

    tf_label_dataset = tf.placeholder(tf.float32, shape=(batch_size, classes)) 

    layer1_weights=tf.Variable(tf.truncated_normal([3,3,3,16])) 
    layer1_biases=tf.Variable(tf.zeros([16])) 

    layer2_weights=tf.Variable(tf.truncated_normal([3,3,16,32])) 
    layer2_biases=tf.Variable(tf.zeros([32])) 

    layer3_weights=tf.Variable(tf.truncated_normal([2,2,32,64])) 
    layer3_biases=tf.Variable(tf.zeros([64])) 

    layer4_weights=tf.Variable(tf.truncated_normal([1024,10])) 
    layer4_biases=tf.Variable(tf.zeros([10])) 

    layer5_weights=tf.Variable(tf.truncated_normal([10,classes])) 
    layer5_biases=tf.Variable(tf.zeros([classes])) 


    def layer_multiplication(data_input_given,dropping=False): 

     #Convolutional Layer 1 

     CNN1=tf.nn.relu(tf.nn.conv2d(data_input_given,layer1_weights,strides=[1,1,1,1],padding='SAME')+layer1_biases) 

     print('CNN1 Done!!') 

     #Pooling Layer 

     Pool1=tf.nn.max_pool(CNN1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME') 
     print('Pool1 DOne') 

     #second Convolution layer 

     CNN2=tf.nn.relu(tf.nn.conv2d(Pool1,layer2_weights,strides=[1,1,1,1],padding='SAME'))+layer2_biases 
     print('CNN2 Done') 
     #Second Pooling 

     Pool2 = tf.nn.max_pool(CNN2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 
     print('pool2 Done') 
     #Third Convolutional Layer 

     print(Pool2.shape) 

     CNN3 = tf.nn.relu(tf.nn.conv2d(Pool2, layer3_weights, strides=[1, 1, 1, 1], padding='SAME')) + layer3_biases 
     print('CNN3 Done') 
     #Third Pooling Layer 

     Pool3 = tf.nn.max_pool(CNN3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 
     print('Pool3 DOne') 
     #Fully Connected Layer 

     #print(Pool3.shape) 

     shape = Pool3.get_shape().as_list() 

     # print(shape) 

     reshape = tf.reshape(Pool3, [shape[0], shape[1] * shape[2] * shape[3]]) 

     #print(reshape.shape) 

     FullyCon = tf.nn.relu(tf.matmul(reshape, layer4_weights) + layer4_biases) 

     #print(FullyCon.shape) 

     if dropping==False: 
      print('Training') 
      dropout = tf.nn.dropout(FullyCon, 0.6) 
      z=tf.matmul(dropout,layer5_weights)+layer5_biases 
      return z 

     else: 
      print('Testing') 
      z = tf.matmul(FullyCon, layer5_weights) + layer5_biases 
      return z 


    gloabl_step = tf.Variable(0, trainable=False) 

    decay_rate=tf.train.exponential_decay(1e-6,gloabl_step,4000,0.96,staircase=False,) 

    train_input=layer_multiplication(data_placeholder,False) 

    test_prediction = tf.nn.softmax(layer_multiplication(tf_test_dataset,True)) 

    loss=(tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=label_placeolder,logits=train_input)) 
            + 0.01 * tf.nn.l2_loss(layer1_weights) 
            + 0.01 * tf.nn.l2_loss(layer2_weights) 
            + 0.01 * tf.nn.l2_loss(layer3_weights) 
            + 0.01 * tf.nn.l2_loss(layer4_weights) 
            + 0.01 * tf.nn.l2_loss(layer5_weights) 
            ) 

    optimizer = tf.train.GradientDescentOptimizer(name='Stochastic', learning_rate=decay_rate).minimize(loss,global_step=gloabl_step) 


    def accuracy(predictions, labels): 
     print(predictions.shape[0]) 
     return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) 
       /predictions.shape[0]) 

    config=tf.ConfigProto() 
    config.gpu_options.allocator_type ='BFC' 

    saver = tf.train.Saver() 

    test_accuracy=[] 

    with tf.Session(config=config) as session: 
     tf.global_variables_initializer().run() 
     print('Initialized') 
     tf.train.write_graph(session.graph_def, '.', './SVHN.pbtxt') 

     for step in range(num_steps): 
      offset = (step * batch_size) % (train_labels.shape[0] - batch_size) 
      batch_data = train_dataset[offset:(offset + batch_size), :, :] 
      batch_labels = train_labels[offset:(offset + batch_size), :] 

      batch_test_data = test_dataset[offset:(offset + batch_size), :, :] 
      batch_test_labels = test_labels[offset:(offset + batch_size),:] 
      #print(batch_data) 
      #print(batch_test.shape) 

      feed_dict = {data_placeholder:batch_data, label_placeolder:batch_labels} 
      _, l, predictions = session.run(
       [optimizer, loss, train_input], feed_dict=feed_dict) 
      if (step % 500 == 0): 
       #print(session.run(decay_rate)) 
       print('Minibatch loss at step %d: %f' % (step, l)) 
       print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) 
       if(batch_test_data.shape!=(32,32,32,3)): 
        print('Skip') 
       else: 
        correct_prediction = tf.equal(tf.argmax(test_prediction, 1), tf.argmax(tf_label_dataset, 1)) 
        accuracy_for_test = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
        print("Test Accuracy") 

        test_accuracy.append(accuracy_for_test.eval(feed_dict={tf_test_dataset:batch_test_data, 
               tf_label_dataset:batch_test_labels})) 

        print(accuracy_for_test.eval(feed_dict={tf_test_dataset:batch_test_data, 
               tf_label_dataset:batch_test_labels})) 

     print(np.mean(test_accuracy)) 


     saver.save(sess=session, save_path='./SVHN.ckpt') 

Ps-コードは私のシステムで動作します。この問題は私の考えであると思われます。

答えて

1

異なる体重の初期化を使用して、結果が改善されるはずです。あなたのリファレンスノートブックでは、stddev = 0.1を使用していますが、glorotまたは初期化を見ることもできます。これはさらにうまくいくはずです。

また、あなたの学習率は本当に低く、崩壊は学習率をさらに低くするので、ネットワークはこのように多く学習しません。ネットワークのより良い初期化を行うことで、学習率を高め、データから有用なものを学ぶことができます。

0

まず第一に、精度を高める単一のパラメータはありません。テンソルフローのトレーニングは、初期体重、学習率、訓練するステップ数などのパラメータの多くに依存します。違う組み合わせで試してみるのも一種です。学習率のように低くてはいけません。それほど賢明ではありません。あなたはミニマを逃してしまいます。それは私たちの脳のようなものです。そのことについての明確なデータがあると、より良い方法で事を見つける。

これらのすべてのパラメータの設定を変更してトレーニングしてから、精度を確認してください。

関連する問題