相同批次但不同批次大小会产生不同的结果

时间:2016-11-06 15:23:00

标签: python tensorflow deep-learning

例如,我们有64 * 100输入数据将被发送到张量流图,它将在输入softmax或任何损失函数之前生成64 *(n_hidden节点)输出。我们将1 * 100放入同一个图形中,结果应该是前一个输出的第一行,但结果不是。我在Mnist上使用张量流示例来测试比较。

'''
A Multilayer Perceptron implementation example using TensorFlow library.
This example is using the MNIST database of handwritten digits
(http://yann.lecun.com/exdb/mnist/)
Author: Aymeric Damien
Project: https://github.com/aymericdamien/TensorFlow-Examples/
'''

from __future__ import print_function
import numpy as np
# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

import tensorflow as tf



# Parameters
learning_rate = 0.001
training_epochs = 15
batch_size = 100
display_step = 1

# Network Parameters
n_hidden_1 = 256 # 1st layer number of features
n_hidden_2 = 256 # 2nd layer number of features
n_input = 784 # MNIST data input (img shape: 28*28)
n_classes = 10 # MNIST total classes (0-9 digits)

# tf Graph input
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])




# Create model
def multilayer_perceptron(x, weights, biases):
    # Hidden layer with RELU activation
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    layer_1 = tf.nn.relu(layer_1)
    # Hidden layer with RELU activation
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    layer_2 = tf.nn.relu(layer_2)
    # Output layer with linear activation
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
    return out_layer

# Store layers weight & bias
weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1]), name ='layer1'),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2]), name = 'layer2'),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]), name = 'layer3')
}
biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1]), name = 'layer1_b'),
    'b2': tf.Variable(tf.random_normal([n_hidden_2]), name = 'layer2_b'),
    'out': tf.Variable(tf.random_normal([n_classes]), name = 'layer3_b')
}

# Construct model
pred = multilayer_perceptron(x, weights, biases)

# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
#optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
var = tf.all_variables()
trainer = tf.train.AdamOptimizer(learning_rate=learning_rate)
grads = trainer.compute_gradients(cost, var)
update = trainer.apply_gradients(grads)

# Initializing the variables
init = tf.initialize_all_variables()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)

    # Training cycle
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(mnist.train.num_examples/batch_size)
        # Loop over all batches
        for i in range(total_batch):
            batch_x, batch_y = mnist.train.next_batch(batch_size)
            # Run optimization op (backprop) and cost op (to get loss value)
            #_, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})
            #c, v,grad, Pred, bi = sess.run([cost, var,grads, pred, biases], feed_dict={x: batch_x, y: batch_y})

            Pred_2 = sess.run(pred, feed_dict={x: batch_x, y: batch_y})

            Pred_1 = sess.run(pred , feed_dict={x: batch_x[0:1,:], y: batch_y[0:1]})
            print(Pred_2[0] == Pred_1)
            # Compute average loss
            avg_cost += c / total_batch
        # Display logs per epoch step
        if epoch % display_step == 0:
            print("Epoch:", '%04d' % (epoch+1), "cost=", \
                "{:.9f}".format(avg_cost))
            # print(len(v))
            # g1 = np.array(grad[0])
            # g2 = np.array(grad[1])
            # g3 = np.array(grad[2])
            # g4 = np.array(grad[3])
            # g5 = np.array(grad[4])
            # g6 = np.array(grad[5])
            # print(g1.shape)
            # print(g2.shape)
            # print(g3.shape)
            # print(g4.shape)
            # print(g5.shape)
            # print(g6.shape)
            # print(g6[0,:])
            # print(g6[1,:])
            # print(bi['out'])

            #print(type(updating))
    print("Optimization Finished!")

    # Test model
    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
    # Calculate accuracy
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

print(Pred_2 [0] == Pred_1)应该是相同的,但它们不是。这很奇怪。

1 个答案:

答案 0 :(得分:0)

如果您的体重和偏差初始化是随机的并且每次的渐变不同,则梯度下降路径应该不同,它可能需要朝向不同的最小值的路径。