在张量流中为相同数据类型获得不同的精度

时间:2019-03-06 07:21:56

标签: python tensorflow

当我在此处打印精度值时,得到的值是:“精度-0.0714285746216774”,精度最高为16点。

learning_rate = 0.001
epochs = 10
display_step = 5
batch_size = 28

train_path = "mnist_train.csv"


# Divide into batches.
def divide_batches(input_batch, batch_size):
    output_batch = []
    for i in range(0, len(input_batch), batch_size):
        output_batch.append(input_batch[i: i + batch_size])
    return output_batch


# Read data
df_train = pd.read_csv(train_path)
train_data = df_train.iloc[:,0:784].values
train_label = df_train.iloc[:,784:794].values

# Divide into batches.
train_x = divide_batches(train_data, batch_size)
train_y = divide_batches(train_label, batch_size)

x = tf.placeholder(tf.float32, [None, 784])  # Input
y = tf.placeholder(tf.float32, [None, 10])  # Output


# Convolutuion.
def conv2d(x, weights, biases, strides = 1):
    x = tf.nn.conv2d(x, weights, strides=[1, strides, strides, 1], padding='SAME')
    x = tf.nn.bias_add(x, biases)

    return tf.nn.relu(x)


# Max Pooling.
def maxpool2d(x, k=2):
    return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME')


# Weights.
weights = {
    # 5 x 5 filter, 1 input, 32 outputs.
    'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32], seed=1, dtype=tf.float32)),
    # 5 x 5 filter, 32 inputs, 64 outputs.
    'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64], seed=1, dtype=tf.float32)),
    # Fully connected, 7 x 7 x 64 inputs, 1024 inputs.
    'wf1': tf.Variable(tf.random_normal([7*7*64, 1024], seed=1, dtype=tf.float32)),
    # 1024 inputs, 10 outputs
    'out': tf.Variable(tf.random_normal([1024, 10], seed=1, dtype=tf.float32))
}

# Biases.
biases = {
    'bc1': tf.Variable(tf.random_normal([32], seed=1, dtype=tf.float32)),
    'bc2': tf.Variable(tf.random_normal([64], seed=1, dtype=tf.float32)),
    'bf1': tf.Variable(tf.random_normal([1024], seed=1, dtype=tf.float32)),
    'out': tf.Variable(tf.random_normal([10], seed=1, dtype=tf.float32))
}


# Model.
def model(x, weights, biases):

    # Reshape the data.
    x = tf.reshape(x, shape=[-1, 28, 28, 1])

    # Layer 1.
    conv1 = conv2d(x, weights['wc1'], biases['bc1'])
    conv1 = maxpool2d(conv1, k=2)

    # Layer 2.
    conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
    conv2 = maxpool2d(conv2, k=2)

    # Fully connected layer.
    intermediate = tf.reshape(conv2, shape=[-1, 7 * 7 * 64])
    intermediate = tf.matmul(intermediate, weights['wf1'])
    intermediate = tf.add(intermediate, biases['bf1'])
    intermediate = tf.nn.sigmoid(intermediate)

    # Output layer.
    out = tf.add(tf.matmul(intermediate, weights['out']), biases['out'])

    return out


y_ = model(x, weights, biases)
pred = tf.nn.softmax(y_)  # Prediction

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=y_, labels=y))  # Cost
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)  # Optimizer

cost_accumulator = tf.Variable(0, dtype=tf.float32, trainable=False)
tf.add_to_collection(name=tf.GraphKeys.LOCAL_VARIABLES, value=cost_accumulator)
cost_accumulator = cost_accumulator.assign_add(cost)
cost_divisor = tf.Variable(0, dtype=tf.float32, trainable=False)
tf.add_to_collection(name=tf.GraphKeys.LOCAL_VARIABLES, value=cost_divisor)
cost_divisor = cost_divisor.assign_add(1)
cost_aggregate = tf.div(cost_accumulator, cost_divisor)


accuracy = tf.metrics.accuracy(labels=tf.argmax(y, axis=1),
                                                        predictions=tf.argmax(pred, axis=1))[1]
print(f"Accuracy type - {accuracy.dtype}")
init = tf.global_variables_initializer()
init_l = tf.local_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    start = time.time()

    for i in range(epochs):
        sess.run(init_l)
        count = 0
        for train_data_input, train_label_input in zip(train_x, train_y):
            count+=1
            _, c, acc = sess.run([optimizer, cost_aggregate, accuracy], feed_dict = {x: train_data_input, y: train_label_input})
            print(sess.run(pred, feed_dict= {x: train_data_input, y: train_label_input}))

            print(f"Accuracy - {acc}")
            exit()

然后,我将代码的精度部分和打印的精度值得到并得到:'0.071428575'。我检查了数据类型,它是float32。在这里,即使它是相同的数据类型,我的精度也达到了9点。但是,当我将精度转换为float64时,我的精度高达16分。

predictions = np.asarray([[0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
 [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
 [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],])


labels = np.asarray([[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
 [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
 [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
 [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
 [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
 [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
 [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
 [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
 [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
 [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
 [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
 [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
 [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
 [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
 [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],])
tf_val = tf.metrics.accuracy(labels=tf.argmax(labels, axis=1),
                                                        predictions=tf.argmax(predictions, axis=1))[1]
# tf_val = tf.cast(tf_val, tf.float64)

print(tf_val.dtype)

init = tf.global_variables_initializer()
loc_init = tf.local_variables_initializer()
logdir = "/tmp/"
with tf.Session() as sess:
    sess.run(init)
    sess.run(loc_init)
    start = time.time()
    # Summary Writer.
    writer = tf.summary.FileWriter(logdir, sess.graph)
    writer.add_graph(sess.graph)
    print(sess.run(tf_val))

为什么即使数据类型相同,它的行为也不同?

0 个答案:

没有答案