سلام ومن برای دسته بندی دیتاست MNIST با استفاده از شبکه های کانولوشن از شبکه network in network استفاده کردم و داخل مقاله ذکر شده که باید نرمال سازی داده ها توسط روش gcn و zca_whiten صورت بگیرهومن این کا رو انجام دادم و لایه های شبکه دقیقا طبق مقاله نوشته شده اما خروجی تابع softmax ، به طور کلی nan تولید می کنه و فرآیند یادگیری انجام نمی شه ،ایا معماری لایه ها مشکلی داره؟و این که نرمال سازی ورودی چقدر می تونه تاثیر بذاره؟
معماری لایه ها به شکل زیر:
img_out =tf.reshape(x, [-1, 28, 28, 1])
with tf.name_scope('conv1'):
W_conv1 = weight_variable([5, 5, 1, 96])
b_conv1 = bias_variable([96])
h_conv1 = tf.nn.relu(conv2d(img_out, W_conv1) + b_conv1)
with tf.name_scope('cccp1'):
W_cccp1 = weight_variable([1, 1, 96, 64])
b_cccp1 = bias_variable([64])
h_cccp1 = tf.nn.relu(conv2d(h_conv1, W_cccp1) + b_cccp1)
with tf.name_scope('cccp2'):
W_cccp2 = weight_variable([1, 1, 64, 48])
b_cccp2 = bias_variable([48])
h_cccp2 = tf.nn.relu(conv2d(h_cccp1, W_cccp2) + b_cccp2)
# Pooling layer - downsamples by 2X.
with tf.name_scope('pool1'):
h_pool1 = max_pool_3x3(h_cccp2)
with tf.name_scope('dropout1'):
h_drop1=tf.nn.dropout(h_pool1,0.5)
# Second convolutional layer -- maps 32 feature maps to 64.
with tf.name_scope('conv2'):
W_conv2 = weight_variable([5, 5, 48, 128])
b_conv2 = bias_variable([128])
h_conv2 = tf.nn.relu(conv2d(h_drop1, W_conv2) + b_conv2)
with tf.name_scope('cccp3'):
W_cccp3 = weight_variable([1, 1, 128, 96])
b_cccp3 = bias_variable([96])
h_cccp3 = tf.nn.relu(conv2d(h_conv2, W_cccp3) + b_cccp3)
with tf.name_scope('cccp4'):
W_cccp4 = weight_variable([1, 1, 96, 48])
b_cccp4 = bias_variable([48])
h_cccp4 = tf.nn.relu(conv2d(h_cccp3, W_cccp4) + b_cccp4)
# Second pooling layer.
with tf.name_scope('pool2'):
h_pool2 = max_pool_3x3(h_cccp4)
with tf.name_scope('dropout2'):
h_drop2=tf.nn.dropout(h_pool2,0.5)
with tf.name_scope('conv3'):
W_conv3 = weight_variable([5, 5, 48, 128])
b_conv3 = bias_variable([128])
h_conv3 = tf.nn.relu(conv2d(h_drop2, W_conv3) + b_conv3)
with tf.name_scope('cccp5'):
W_cccp5 = weight_variable([1, 1, 128, 96])
b_cccp5 = bias_variable([96])
h_cccp5 = tf.nn.relu(conv2d(h_conv3, W_cccp5) + b_cccp5)
with tf.name_scope('cccp6'):
W_cccp6 = weight_variable([1, 1, 96, 10])
b_cccp6 = bias_variable([10])
h_cccp6 = tf.nn.relu(conv2d(h_cccp5, W_cccp6) + b_cccp6)
with tf.name_scope('ave_pool'):
ave_pool=tf.nn.avg_pool(h_cccp6,[1, 7, 7, 1],[1,1,1,1],'VALID')
y_conv=tf.nn.softmax(ave_pool)
y_conv=tf.reshape(y_conv,(128, 10))
آپدیت :
مرسی از پاسختون من کاری که گفتید رو انجام دادم متاسفانه دقت خیلی پایینه.فکر می کنم لایه ها اشتباه گذاشته شده، می تونید نگاهی به کد بندازید.مرسی
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
def conv2d(x, W):
"""conv2d returns a 2d convolution layer with full stride."""
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_3x3(x):
"""max_pool_2x2 downsamples a feature map by 2X."""
return tf.nn.max_pool(x, ksize=[1, 3, 3, 1],
strides=[1, 2, 2, 1], padding='SAME')
def weight_variable(shape):
"""weight_variable generates a weight variable of a given shape."""
initial = tf.random_normal(shape,mean=0.0,stddev=0.05)
return tf.Variable(initial)
def bias_variable(shape):
"""bias_variable generates a bias variable of a given shape."""
initial = tf.random_normal(shape,mean=0.0,stddev=0.05)
return tf.Variable(initial)
img_out =tf.reshape(x, [-1, 28, 28, 1])
with tf.name_scope('conv1'):
W_conv1 = weight_variable([5, 5, 1, 96])
b_conv1 = bias_variable([96])
h_conv1 = tf.nn.relu(conv2d(img_out, W_conv1) + b_conv1)
with tf.name_scope('cccp1'):
W_cccp1 = weight_variable([1, 1, 96, 64])
b_cccp1 = bias_variable([64])
h_cccp1 = tf.nn.relu(conv2d(h_conv1, W_cccp1) + b_cccp1)
with tf.name_scope('cccp2'):
W_cccp2 = weight_variable([1, 1, 64, 48])
b_cccp2 = bias_variable([48])
h_cccp2 = tf.nn.relu(conv2d(h_cccp1, W_cccp2) + b_cccp2)
# Pooling layer - downsamples by 2X.
with tf.name_scope('pool1'):
h_pool1 = max_pool_3x3(h_cccp2)
with tf.name_scope('dropout1'):
h_drop1=tf.nn.dropout(h_pool1,0.5)
# Second convolutional layer -- maps 32 feature maps to 64.
with tf.name_scope('conv2'):
W_conv2 = weight_variable([5, 5, 48, 128])
b_conv2 = bias_variable([128])
h_conv2 = tf.nn.relu(conv2d(h_drop1, W_conv2) + b_conv2)
with tf.name_scope('cccp3'):
W_cccp3 = weight_variable([1, 1, 128, 96])
b_cccp3 = bias_variable([96])
h_cccp3 = tf.nn.relu(conv2d(h_conv2, W_cccp3) + b_cccp3)
with tf.name_scope('cccp4'):
W_cccp4 = weight_variable([1, 1, 96, 48])
b_cccp4 = bias_variable([48])
h_cccp4 = tf.nn.relu(conv2d(h_cccp3, W_cccp4) + b_cccp4)
# Second pooling layer.
with tf.name_scope('pool2'):
h_pool2 = max_pool_3x3(h_cccp4)
with tf.name_scope('dropout2'):
h_drop2=tf.nn.dropout(h_pool2,0.5)
with tf.name_scope('conv3'):
W_conv3 = weight_variable([5, 5, 48, 128])
b_conv3 = bias_variable([128])
h_conv3 = tf.nn.relu(conv2d(h_drop2, W_conv3) + b_conv3)
with tf.name_scope('cccp5'):
W_cccp5 = weight_variable([1, 1, 128, 96])
b_cccp5 = bias_variable([96])
h_cccp5 = tf.nn.relu(conv2d(h_conv3, W_cccp5) + b_cccp5)
with tf.name_scope('cccp6'):
W_cccp6 = weight_variable([1, 1, 96, 10])
b_cccp6 = bias_variable([10])
h_cccp6 = tf.nn.relu(conv2d(h_cccp5, W_cccp6) + b_cccp6)
with tf.name_scope('ave_pool'):
ave_pool=tf.nn.avg_pool(h_cccp6,[1, 7, 7, 1],[1,1,1,1],'VALID')
y_conv=tf.nn.softmax(ave_pool)
y_conv=tf.reshape(y_conv,(128,10))
with tf.name_scope('loss'):
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_,
logits=y_conv)
cross_entropy = tf.reduce_mean(cross_entropy)
with tf.name_scope('adam_optimizer'):
train_step = tf.train.AdamOptimizer(0.05).minimize(cross_entropy)
with tf.name_scope('accuracy'):
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
correct_prediction = tf.cast(correct_prediction, tf.float32)
accuracy = tf.reduce_mean(correct_prediction)
graph_location = tempfile.mkdtemp()
print('Saving graph to: %s' % graph_location)
train_writer = tf.summary.FileWriter(graph_location)
train_writer.add_graph(tf.get_default_graph())
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(100):
batch = mnist.train.next_batch(128)
train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1]})
print('step %d, training accuracy %g' % (i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1]})
print(sess.run(y_conv,feed_dict={x: batch[0], y_: batch[1]}))