TensorFlow 基本用法(一)

介绍在官方文档中MNIST入门中用到的函数

1. tf.placeholder(dtype, shape=None, name=None)

  • dtype:数据类型
  • shape:数据形状
  • name:操作的命名(可以不指定)
  • 返回值为Tensor类型
x = tf.placeholder("float", [None,784]) #None表示该维度长度任意
#返回值为Tensor("Placeholder:0",shape=(?, 784),dtype=float32)

2. tf.Variable(initial-value, name= optional-name)

  • initial-value:初始值,用来指定变量的类型和形状
  • optional_name:操作的命名(可以不指定)
  • 返回值为Variable类型
W = tf.Variable(tf.zeros([784,10]))
#返回值为Variable("Variable:0",shape=(784, 10),dtype=float32_ref)
w = tf.Variable([784,10]) 
#返回值为Variable("Variable_1:0",shape=(2,),dtype=int32_ref)

创建变量时始终需要指定一个默认值

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

  • input_tensor:输入的张量
  • axis:进行操作的轴,0按列求和,1按行求和
  • keepdims:当值为False时,张量秩减一,如二阶张量降为一阶张量;当值为True,张量所在轴降维,长度变为1
  • name:操作的命名(可以不指定)
  • reduction_indices:axis以前的命名,已弃用
  • keep_dims:keepdims已弃用的别名
  • 返回值为Tensor类型
x = tf.constant([[1, 1, 1], [1, 1, 1]])
x1 = tf.reduce_sum(x)  
#求和,输出为6
x2 = tf.reduce_sum(x, 0)  
#按列求和,行被压缩,shape=(2,3)变为shape=(3,),输出[2, 2, 2]
x3 = tf.reduce_sum(x, 1)  
#按行求和,列被压缩,shape=(2,3)变为shape=(2,),输出[3, 3]
x4 = tf.reduce_sum(x, 1, keepdims=True)  
#按行求和,列降到一维,shape=(2,3)变为shape=(2,1),输出[[3], [3]]
x5 = tf.reduce_sum(x, [0, 1])  
#按行列求和,输出为6

相关函数
tf.reduce_prod
tf.reduce_min
tf.reduce_max
tf.reduce_mean
tf.reduce_all
tf.reduce_any

4. tf.nn.softmax(logits, axis=None, name=None, dim=None)

  • logits:输入的张量
  • axis:进行操作的轴
  • name:操作的命名(可以不指定)
  • dim:axis已弃用的命名
  • 返回值为Tensor类型
A = [1.0,2.0,3.0,4.0,5.0,6.0]
with tf.Session() as sess:
print sess.run(tf.nn.softmax(A))
#输出[ 0.00426978 0.01160646 0.03154963 0.08576079 0.23312201 0.63369131]

5. tf.train.GradientDescentOptimizer()

这是个optimizer优化器类
详细使用在这篇博客讲的比较详细https://blog.csdn.net/xierhacker/article/details/53174558

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

  • input:输入的张量
  • axis:轴,0代表按列返回数据最大值所在的索引值,1代表按行
  • name:操作的命名(可以不指定)
  • dimension:与axis功能一样
  • output_type:返回值类型
  • 返回值为数据最大值所在的索引值
a = tf.constant([5,2,3,4,1])
b = tf.constant([[2,3,1],[3,2,1]])
sess =tf.InteractiveSession()
a1 = tf.argmax(a,0)
print(a1.eval())
#输出0
b1 = tf.argmax(b,0)
print(b1.eval())
#输出[1 0 0]
b2 = tf.argmax(b,1)
print(b2.eval())
#输出[1 0]
sess.close()

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

  • x,y:进行比较的两个张量
  • name:操作的命名(可以不指定)
  • 返回值为Tensor类型,元素为布尔值
a = [1,2,3]
b = [2,2,2]
op = tf.equal(a,b)
sess = tf.InteractiveSession()
print(op.eval())
sess.close()
#输出[False  True False]

8. tf.matmul(a, b, transpose_a=False, transpose_b=False, adjoint_a=False, adjoint_b=False, a_is_sparse=False, b_is_sparse=False, name=None)

  • a,b:两个张量秩大于等于二的张量(二维矩阵),类型相同
  • transpose_a:如果为真,a在矩阵乘法之前进行转置
  • transpose_b:如果为真,b在矩阵乘法之前进行转置
  • adjoint_a:如果为真,a在矩阵乘法之前进行共轭或转置
  • adjoint_b:如果为真,b在矩阵乘法之前进行共轭或转置
  • a_is_sparse:如果为真,a将被处理为稀疏矩阵
  • b_is_sparse:如果为真,b将被处理为稀疏矩阵
  • name:操作的命名(可以不指定)
x = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])
y = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2])
z = tf.matmul(x, y)
sess = tf.InteractiveSession()
print(x.eval())
sess.close()
#[[1 2 3],[4 5 6]]

9. tf.cast(x, dtype, name=None)

  • x:输入张量
  • dtype:目标类型
  • name:操作的命名(可以不指定)
a = tf.constant([1,2,3])
print(a)
#Tensor("Const_2:0", shape=(3,), dtype=int32)
b = tf.cast(a,"float")
sess = tf.InteractiveSession()
b.eval()
print(b)
#Tensor("Cast:0", shape=(3,), dtype=float32)
sess.close()

猜你喜欢

转载自blog.csdn.net/L_Icarus/article/details/82287947