转置张量流最小模型的结果差异很大

时间:2018-04-04 03:06:50

标签: python tensorflow

我为tensorflow中的最小数据集编写了两个版本代码。第一个与输入的示例代码类似[无,784] 然而,第二部分是我改变的。我只是将输入比例修改为[784,无]并更改一些相应的矩阵格式。 我对结果的不同感到困惑。我是tensorflow的初学者。真的想要你的帮助。

由于

import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets(r"C:\Ruigy\NN\minst", one_hot=True)

def hidden_layer(X, sizeOutput, non_linear_name = ''):
    sizeInput = X.shape[1]
    W = tf.Variable(tf.zeros([sizeInput,sizeOutput]))
    B = tf.Variable(tf.zeros([sizeOutput]))
    Y = tf.matmul(X,W) + B
    if non_linear_name == '':             return   Y
    elif non_linear_name == 'softmax':    A = tf.nn.softmax(Y)
    elif non_linear_name == 'ReLU':       A = tf.nn.relu(Y)
    return A



X       = tf.placeholder(tf.float32, [None, 784],name = 'Input')
Y_LABEL = tf.placeholder(tf.float32, [None, 10], name = 'Label')
Y_linear= hidden_layer(X,Y_LABEL.shape[1])

cross_entropy = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(labels=Y_LABEL, 
logits=Y_linear))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()

for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={X: batch_xs, Y_LABEL: batch_ys})

correct_prediction = tf.equal(tf.argmax(Y_linear, 1), tf.argmax(Y_LABEL, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={X: mnist.test.images, Y_LABEL: mnist.test.labels}))

结果是0.9188

import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets(r"C:\Ruigy\NN\minst", one_hot=True)

def hidden_layer(X, sizeOutput, non_linear_name = ''):
    sizeInput = X.shape[0]
    W = tf.Variable(tf.zeros([sizeInput,sizeOutput]))
    B = tf.Variable(tf.zeros([sizeOutput,1]))
    Y = tf.matmul(W,X,True) + B
    if non_linear_name == '':             return   Y
    elif non_linear_name == 'softmax':    A = tf.nn.softmax(Y)
    elif non_linear_name == 'ReLU':       A = tf.nn.relu(Y)
    return A



X       = tf.placeholder(tf.float32, [784,None],name = 'Input')
Y_LABEL = tf.placeholder(tf.float32, [10,None], name = 'Label')
Y_linear= hidden_layer(X,Y_LABEL.shape[0])

cross_entropy = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(labels=Y_LABEL, 
logits=Y_linear))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()

for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={X: batch_xs.T, Y_LABEL: batch_ys.T})

correct_prediction = tf.equal(tf.argmax(Y_linear, 0), tf.argmax(Y_LABEL, 0))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={X: mnist.test.images.T, Y_LABEL: mnist.test.labels.T}))

结果是0.6638

我真的很困惑。我哪里错了?我只是想改变格式。

1 个答案:

答案 0 :(得分:0)

数据采用与数据集中给出的格式相同的格式。如果要更改格式,请在分配给占位符后为输入取一个tf.tranpose()