1、tf2中的constant
import tensorflow as tf
# 1.创建输入常量
a = tf.constant(3.) b = tf.constant(5.)
# 2.直接计算并打印
print('a+b=',a+b)
a+b= tf.Tensor(8.0, shape=(), dtype=float32)
import tensorflow as tf
a = tf.constant([1, 5], dtype=tf.int64)
print("a:", a)
# 1.打印a的类型
print("a.dtype:", a.dtype)
# 2.打印a的维度
print("a.shape:", a.shape)
a: tf.Tensor([1 5], shape=(2,), dtype=int64)
a.dtype: <dtype: 'int64'>
a.shape: (2,)
2、tf2中的Variable
import tensorflow as tf
# 1.生成一个1*3的Variable
a = tf.Variable(tf.constant([1.0,2.0,3.0]))
print("a:", a)
print("a.dtype:", a.dtype)
print("a.shape:", a.shape)
a: <tf.Variable 'Variable:0' shape=(3,) dtype=float32, numpy=array([1., 2., 3.], dtype=float32)>
a.dtype: <dtype: 'float32'>
a.shape: (3,)
3、tf2中的convert_to_tensor
import tensorflow as tf
import numpy as np
a = np.arange(1, 6)
# 1.将numpy中的array转为tensor
b = tf.convert_to_tensor(a, dtype=tf.int64)
print("a:", a,"type:",type(a))
print("b:", b,"type:",b.dtype)
a: [1 2 3 4 5] type: <class 'numpy.ndarray'>
b: tf.Tensor([1 2 3 4 5], shape=(5,), dtype=int64) type: <dtype: 'int64'>
4、tf2中的zeros、ones、fill
import tensorflow as tf
# 1.生成3*4的tensor
a = tf.zeros([3, 4])
# 2.生成1*3的值为1的tensor b = tf.ones(3)
# 2.生成2*2的值为9的tensor
c = tf.fill([2, 2], 9)
print("a:", a)
print("b:", b)
print("c:", c)
a: tf.Tensor(
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]], shape=(3, 4), dtype=float32)
b: tf.Tensor([1. 1. 1.], shape=(3,), dtype=float32)
c: tf.Tensor(
[[9 9]
[9 9]], shape=(2, 2), dtype=int32)
5、tf2中的random.normal、random.truncated_normal
import tensorflow as tf
# 1.服从0.5为均值,1为方差的高斯分布数据生成一个2*3的tensor
d = tf.random.normal([2, 3], mean=0.5, stddev=1)
print("d:", d)
# 2.在tf.truncated_normal中如果x的取值在区间(μ-2σ,μ+2σ)之外则重新进行选择。这样保证了生成的值都在均值附近。
e = tf.random.truncated_normal([2, 3], mean=0.5, stddev=1)
print("e:", e)
d: tf.Tensor(
[[ 1.4263012 1.32322 0.80783033]
[-0.7430316 -1.0754745 1.8083907 ]], shape=(2, 3), dtype=float32)
e: tf.Tensor(
[[ 1.4304564 0.33300626 1.0539885 ]
[-0.48905843 -0.3516363 1.8399377 ]], shape=(2, 3), dtype=float32
6、 tf2中的random.uniform
import tensorflow as tf
# 1.返回2*2的矩阵,产生于-1和0之间,产生的值是均匀分布的。
f = tf.random.uniform([2, 2], minval=-1, maxval=0)
print("f:", f)
f: tf.Tensor(
[[-0.88596225 -0.68621707]
[-0.7717371 -0.53044343]], shape=(2, 2), dtype=float32)
7、tf2中的cast、reduce_min、reduce_max
import tensorflow as tf
x1 = tf.constant([1., 2., 3.,4.], dtype=tf.float64)
print("x1:", x1)
# 1.将值类型float64修改为int32
x2 = tf.cast(x1, tf.int32)
print("x2", x2)
# 2.获取tensor中的最小值
print("minimum of x1:", tf.reduce_min(x1))
print("minimum of x2:", tf.reduce_min(x2))
# 3.获取tensor中的最大值
print("maxmum of x2:", tf.reduce_max(x2))
x1: tf.Tensor([1. 2. 3.], shape=(3,), dtype=float64)
x2 tf.Tensor([1 2 3], shape=(3,), dtype=int32)
minimum of x1: tf.Tensor(1.0, shape=(), dtype=float64)
minimum of x2: tf.Tensor(1, shape=(), dtype=int32)
maxmum of x2: tf.Tensor(3, shape=(), dtype=int32)
8、tf2中的reduce_mean、reduce_sum
import tensorflow as tf
x = tf.constant([[1, 2, 3], [2, 2, 3]]) print("x:", x)
# 1.求x中所有数的均值
print("mean of x:", tf.reduce_mean(x))
# 2.求每一行的和
print("sum of x:", tf.reduce_sum(x, axis=1))
x: tf.Tensor(
[[1 2 3]
[2 2 3]], shape=(2, 3), dtype=int32)
mean of x: tf.Tensor(2, shape=(), dtype=int32)
sum of x: tf.Tensor([6 7], shape=(2,), dtype=int32)
9、tf2中的add、subtract、multiply、divide
import tensorflow as tf
a = tf.ones([1, 3]) b = tf.fill([1, 3], 3.)
print("a:", a)
print("b:", b)
print("a+b:", tf.add(a, b))
print("a-b:", tf.subtract(a, b))
print("a*b:", tf.multiply(a, b))
print("b/a:", tf.divide(b, a))
a: tf.Tensor([[1. 1. 1.]], shape=(1, 3), dtype=float32)
b: tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)
a+b: tf.Tensor([[4. 4. 4.]], shape=(1, 3), dtype=float32)
a-b: tf.Tensor([[-2. -2. -2.]], shape=(1, 3), dtype=float32)
a*b: tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)
b/a: tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)
10、tf2中的pow、square、sqrt
import tensorflow as tf
a = tf.fill([1, 2], 3.)
print("a:", a)
print("a的次方:", tf.pow(a, 3))
print("a的平方:", tf.square(a))
print("a的开方:", tf.sqrt(a))
a: tf.Tensor([[3. 3.]], shape=(1, 2), dtype=float32)
a的次方: tf.Tensor([[27. 27.]], shape=(1, 2), dtype=float32)
a的平方: tf.Tensor([[9. 9.]], shape=(1, 2), dtype=float32)
a的开方: tf.Tensor([[1.7320508 1.7320508]], shape=(1, 2), dtype=float32)