tf.argmax() tf.equal() tf.nn.softmax() eval tf.random_normal tf.reduce_mean tf.reduce_sum

先讲几个 函数 然后用代码测试一下

tf.reduce_sum

input_tensor沿着给定的尺寸缩小axis。除非keepdims是真的,否则每个条目的张量等级减少1 axis。如果keepdims为真,则缩小尺寸将保留长度为1。
如果axis为None,则减小所有尺寸,并返回具有单个元素的张量。

    tf.reduce_sum(
        input_tensor,
        axis=None,
        keepdims=None,
        name=None,
        reduction_indices=None,
        keep_dims=None
    )



x = tf.constant([[1, 1, 1], [1, 1, 1]])
tf.reduce_sum(x)  # 6
tf.reduce_sum(x, 0)  # [2, 2, 2]
tf.reduce_sum(x, 1)  # [3, 3]
tf.reduce_sum(x, 1, keepdims=True)  # [[3], [3]]
tf.reduce_sum(x, [0, 1])  # 6
参数:

axis:要减少的尺寸。如果None(默认值),则减少所有尺寸。
keepdims:如果为true,则保留尺寸减小的长度为1。
name:操作的名称(可选)。
reduction_indices: 轴的旧(已弃用)名称。 等价与 axis
keep_dims 等价于 keepdims。
返回:
计算后的张量,与input_tensor具有相同的dtype。
类似于 np.sum

tf.reduce_mean

就是计算沿着某个axis的平均值 和 tf.reduce_sum很类似 这里不再赘述

tf.reduce_mean(
    input_tensor,
    axis=None,
    keepdims=None,
    name=None,
    reduction_indices=None,
    keep_dims=None
)

x = tf.constant([[1., 1.], [2., 2.]])
tf.reduce_mean(x)  # 1.5
tf.reduce_mean(x, 0)  # [1.5, 1.5]
tf.reduce_mean(x, 1)  # [1.,  2.]


tf.random_normal

tf.random_normal(
    shape,
    mean=0.0,
    stddev=1.0,
    dtype=tf.float32,
    seed=None,
    name=None
)

参数:
shape: [d1,d2...] 必须是一个一维的 类似与 list
seed: 一个随机种子 整数
name: A name for the operation (optional).
Returns:
   A tensor of the specified shape filled with random normal values.

eval:

eval(
    feed_dict=None,
    session=None
)


调用此方法将执行所有先前的操作(这些操作仅仅是生成此张量的操作所需的输入。)

注意:在调用Tensor.eval()之前,其图形必须已在会话中启动,并且默认会话必须可用,或者session必须明确指定。

ARGS:
feed_dict:为了获取该tensor的值,所需要的输入
session:(可选。)Session用于评估此张量。如果不是,则使用默认会话。
返回:
以numpy数组的形式返回此张量的值。

tf.nn.softmax()

该函数就是将一个tensor的值转换成概率分布
此函数执行相当于
softmax = tf.exp(logits) / tf.reduce_sum(tf.exp(logits), axis)
tf.nn.softmax(
    logits,
    axis=None,
    name=None,
    dim=None
)

参数:
logits:一个非空的Tensor。必须是下列类型之一:half, float32,float64。
axis:将执行维度softmax。默认值为-1,表示最后一个维度。
name:操作的名称(可选)。
dim:等价于axis。
返回:
一Tensor。具有相同的类型和形状logits。

tf.equal() 等价于 tf.math.equal()

# 别名:

tf.equal
tf.math.equal
tf.math.equal(
    x,
    y,
    name=None
)

==**Returns the truth value of (x == y) element-wise。**==
**注意:math.equal支持广播。更多关于这里的广播**
参数:
x:A Tensor。必须是下列类型之一:bfloat16,half,float32,float64,uint8,int8,int16,int32,int64,complex64,quint8,qint8,qint32,string,bool,complex128。
y:A Tensor。必须与x相同的类型。
name:操作的名称(可选)。
返回:
      A Tensor  bool类型的。
          

tf.argmax()

返回张量沿着指定的轴上最大值的索引。

tf.argmax(
    input,
    axis=None,
    name=None,
    dimension=None,
    output_type=tf.int64
)

参数:
axis:A Tensor。必须是以下类型之一:int32,int64。int32或int64,必须在范围内[-rank(input), rank(input))。描述输入Tensor的哪个轴要减少。对于矢量,使用axis = 0。
output_type:可选tf.DType来自:tf.int32, tf.int64。默认为tf.int64。
name:操作的名称(可选)。
返回:
   A Tensor 类型是 output_type

以上所有知识点 都出现在下面的代码中:该代码就是简单的实现一个逻辑回归

代码的源头

代码用到的数据在这里下载
Extracting ./…/mnist_data/train-images-idx3-ubyte.gz
Extracting ./…/mnist_data/train-labels-idx1-ubyte.gz
Extracting ./…/mnist_data/t10k-images-idx3-ubyte.gz
Extracting ./…/mnist_data/t10k-labels-idx1-ubyte.gz

import tensorflow as tf

# Import MINST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./../mnist_data", one_hot=True)
# Parameters
learning_rate = 0.01
training_epochs = 25
batch_size = 100
display_step = 1

# tf Graph Input
x = tf.placeholder(tf.float32, [None, 784]) # mnist data image of shape 28*28=784
y = tf.placeholder(tf.float32, [None, 10]) # 0-9 digits recognition => 10 classes

# Set model weights
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# Construct model
pred = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax

# Minimize error using cross entropy
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))
# Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()
# Start training
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_xs, batch_ys = mnist.train.next_batch(batch_size)
            # Fit training using batch data
            _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs,
                                                          y: batch_ys})
            # Compute average loss
            avg_cost += c / total_batch
        # Display logs per epoch step
        if (epoch+1) % display_step == 0:
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost))

    print("Optimization Finished!")

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

猜你喜欢

转载自blog.csdn.net/qq_32806793/article/details/83004377