4

私はTensorFlowを学んでおり、TensorFlowの初心者のためのMNISTで説明されているような単純なニューラルネットワークを実装していました。ここにはlinkがあります。予想通り、精度は約80〜90%でした。なぜこのニューラルネットワークは何も学習しませんか?

次に、ConvNetを使用しているエキスパートのためのMNISTでした。それを実装する代わりに、私は初心者の部分を改善することに決めました。私はNeural Netsについて知っており、彼らがどのように学び、そして深いネットワークが浅いネットワークよりも優れた性能を発揮できるという事実を知っています。私は初心者のためのMNISTの元のプログラムを変更して、それぞれ16個のニューロンの2つの隠れた層を持つニューラルネットワークを実装しました。期待される出力を用いて達成することができるものよりも良いことになっていたこと

import tensorflow as tf 
from tensorflow.examples.tutorials.mnist import input_data 

mnist = input_data.read_data_sets('MNIST_data', one_hot=True) 

x = tf.placeholder(tf.float32, [None, 784], 'images') 
y = tf.placeholder(tf.float32, [None, 10], 'labels') 

# We are going to make 2 hidden layer neurons with 16 neurons each 

# All the weights in network 
W0 = tf.Variable(dtype=tf.float32, name='InputLayerWeights', initial_value=tf.zeros([784, 16])) 
W1 = tf.Variable(dtype=tf.float32, name='HiddenLayer1Weights', initial_value=tf.zeros([16, 16])) 
W2 = tf.Variable(dtype=tf.float32, name='HiddenLayer2Weights', initial_value=tf.zeros([16, 10])) 

# All the biases for the network 
B0 = tf.Variable(dtype=tf.float32, name='HiddenLayer1Biases', initial_value=tf.zeros([16])) 
B1 = tf.Variable(dtype=tf.float32, name='HiddenLayer2Biases', initial_value=tf.zeros([16])) 
B2 = tf.Variable(dtype=tf.float32, name='OutputLayerBiases', initial_value=tf.zeros([10])) 


def build_graph(): 
    """This functions wires up all the biases and weights of the network 
    and returns the last layer connections 
    :return: returns the activation in last layer of network/output layer without softmax 
    """ 
    A1 = tf.nn.relu(tf.matmul(x, W0) + B0) 
    A2 = tf.nn.relu(tf.matmul(A1, W1) + B1) 
    return tf.matmul(A2, W2) + B2 


def print_accuracy(sx, sy, tf_session): 
    """This function prints the accuracy of a model at the time of invocation 
    :return: None 
    """ 
    correct_prediction = tf.equal(tf.argmax(y), tf.argmax(tf.nn.softmax(build_graph()))) 
    correct_prediction_float = tf.cast(correct_prediction, dtype=tf.float32) 
    accuracy = tf.reduce_mean(correct_prediction_float) 

    print(accuracy.eval(feed_dict={x: sx, y: sy}, session=tf_session)) 


y_predicted = build_graph() 

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=y_predicted)) 

model = tf.train.GradientDescentOptimizer(0.03).minimize(cross_entropy) 

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    for _ in range(1000): 
     batch_x, batch_y = mnist.train.next_batch(50) 
     if _ % 100 == 0: 
      print_accuracy(batch_x, batch_y, sess) 
     sess.run(model, feed_dict={x: batch_x, y: batch_y}) 

ため

the neural network i build

コードネットワークの

画像:

は、それは次のようになります(W0の形状は[784,10]、B0の形状は[10]と仮定)

def build_graph(): 
    return tf.matmul(x,W0) + B0 

代わりに、ネットワークにはトレーニングが全く行われていないと表示されます。正確さはどの反復においても20%を超えていませんでした。

出力

抽出MNIST_data /電車-画像-IDX3-ubyte.gz

抽出MNIST_data /列車のラベル-IDX1-ubyte.gz

抽出MNIST_data/t10k-images- IDX3-ubyte.gz

抽出MNIST_data/t10k-ラベル-IDX1-ubyte.gz

0.1

0.1

0.1

0.1

0.1

0.1

0.1

0.1

0.1

0.1

それがすべてで一般化しません上記のプログラムと間違っている何私の質問

?畳み込みニューラルネットワークを使用せずにもっと改善するにはどうすればよいですか?

答えて

5

すべてのゼロからゼロに初期化されたため、主な間違いはnetwork symmetryです。その結果、重みは更新されません。小さな乱数に変更すると、学習が始まります。バイアスをゼロで初期化することは大丈夫です。

もう1つの問題は、純粋な技術です:print_accuracy関数は計算グラフに新しいノードを作成しています。ループ内で関数を呼び出すので、グラフが膨れてしまい、最終的にすべてのメモリが使い果たされます。

ハイパーパラメータで再生し、ネットワークを大きくして容量を増やしたい場合もあります。

編集:また、精度の計算にバグが見つかりました。

import tensorflow as tf 
from tensorflow.examples.tutorials.mnist import input_data 

x = tf.placeholder(tf.float32, [None, 784], 'images') 
y = tf.placeholder(tf.float32, [None, 10], 'labels') 

W0 = tf.Variable(dtype=tf.float32, name='InputLayerWeights', initial_value=tf.truncated_normal([784, 16]) * 0.001) 
W1 = tf.Variable(dtype=tf.float32, name='HiddenLayer1Weights', initial_value=tf.truncated_normal([16, 16]) * 0.001) 
W2 = tf.Variable(dtype=tf.float32, name='HiddenLayer2Weights', initial_value=tf.truncated_normal([16, 10]) * 0.001) 

B0 = tf.Variable(dtype=tf.float32, name='HiddenLayer1Biases', initial_value=tf.ones([16])) 
B1 = tf.Variable(dtype=tf.float32, name='HiddenLayer2Biases', initial_value=tf.ones([16])) 
B2 = tf.Variable(dtype=tf.float32, name='OutputLayerBiases', initial_value=tf.ones([10])) 

A1 = tf.nn.relu(tf.matmul(x, W0) + B0) 
A2 = tf.nn.relu(tf.matmul(A1, W1) + B1) 
y_predicted = tf.matmul(A2, W2) + B2 
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_predicted, 1)) 
correct_prediction_float = tf.cast(correct_prediction, dtype=tf.float32) 
accuracy = tf.reduce_mean(correct_prediction_float) 
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=y_predicted)) 
optimizer = tf.train.AdamOptimizer(0.001).minimize(cross_entropy) 

mnist = input_data.read_data_sets('mnist', one_hot=True) 
with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    for i in range(20000): 
    batch_x, batch_y = mnist.train.next_batch(64) 
    _, cost_val, acc_val = sess.run([optimizer, cross_entropy, accuracy], feed_dict={x: batch_x, y: batch_y}) 
    if i % 100 == 0: 
     print('cost=%.3f accuracy=%.3f' % (cost_val, acc_val)) 
+0

はい、私はそのバグを発見し、(1 Y、)tf.argmaxのバグを修正:それは

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_predicted, 1)) 

でなければなりません。ここの完全なコードです。あなたのプログラムをチェックし、それが動作するかどうかを見てみましょう。また、隠されたレイヤーがなくても、ゼロに初期化する理由を質問したいと思います。このように多層でも同じことはできません – coder3101

関連する問題