tf函数总结(一)

部署运行你感兴趣的模型镜像

1、tf.concat

concat(
    values,
    axis,
    name='concat'
)
将一组张量沿某一维度连接起来。

当axis=i时,如果values[i].shape=[d1,d2, ....,daxis[i], ..., dn],则连接后的张量维度为[d1,d2,..., Raxis,...,dn],其中Raxis=sum(daxis[i]),这就是输入的张量数据沿着某一维度连接。

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]

# tensor t3 with shape [2, 3]
# tensor t4 with shape [2, 3]
tf.shape(tf.concat([t3, t4], 0)) ==> [4, 3]
tf.shape(tf.concat([t3, t4], 1)) ==> [2, 6]

2、tf.split

split(
    value,
    num_or_size_splits,
    axis=0,
    num=None,
    name='split'
)
将一个张量分开成多个子张量

如果num_or_size_splits是一个整数为num_splits,则会将value沿着axis维度分为num_splits个子张量,如果不为整数可以是一个一维张量,将axis维度分为len(num_splits)个子张量,每个子张量在axis的维度上等于num_splits[i]

# 'value' is a tensor with shape [5, 30]
# Split 'value' into 3 tensors with sizes [4, 15, 11] along dimension 1
split0, split1, split2 = tf.split(value, [4, 15, 11], 1)
tf.shape(split0) ==> [5, 4]
tf.shape(split1) ==> [5, 15]
tf.shape(split2) ==> [5, 11]
# Split 'value' into 3 tensors along dimension 1
split0, split1, split2 = tf.split(value, num_or_size_splits=3, axis=1)
tf.shape(split0) ==> [5, 10] 
3、tf.expand_dims
expand_dims(
    input,
    axis=None,
    name=None,
    dim=None
)
为一个张量添加一个维度

axis为增加维度的位置,0表示第一个位置,如果是负数,则从后面计算,如-1表示从最后位置增加一个维度。

# 't' is a tensor of shape [2]
shape(expand_dims(t, 0)) ==> [1, 2]
shape(expand_dims(t, 1)) ==> [2, 1]
shape(expand_dims(t, -1)) ==> [2, 1]

# 't2' is a tensor of shape [2, 3, 5]
shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]
shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]
shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]
4、tf.constant
constant(
    value,
    dtype=None,
    shape=None,
    name='Const',
    verify_shape=False
)
创建一个常亮的张量

其中value给出常亮的值,dtype和shape都是可选的,如果不指定则以value的准。

tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]

tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.] [-1. -1. -1.]]


5、tf.placeholder_with_default

placeholder_with_default(
    input,
    shape,
    name=None
)
创建一个placeholder操作,如果在输出时没有给feed,则以input为值

tf.placeholder_with_default(tf.constant(1.0),shape=[],  name='use_dropout')


6、tf.global_variables

返回去全局变量。全局变量指的是在分布式环境中可以跨机器进行共享的变量。通过Variable()构造函数或get_variable()创建的新变量会自动添加到图的集合GraphKeys.GLOBAL_VARIABLES中,这个函数可以方便的返回这个集合的内容。




您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

### 关于 TensorFlow 中常用函数的概述 TensorFlow 是由 Google 开发的个强大的开源机器学习框架,支持多种编程语言,其中 Python 接口最为广泛使用[^3]。以下是几个常用的 TensorFlow 函数及其基本用法: #### 1. `tf.constant()` `tf.constant()` 用于创建个常量张量。 ```python import tensorflow as tf constant_tensor = tf.constant([1, 2, 3], dtype=tf.int32) print(constant_tensor) ``` 此函数允许指定数据类型 (`dtype`) 并初始化张量值[^4]。 --- #### 2. `tf.Variable()` `tf.Variable()` 创建可训练变量,通常用于存储模型参数。 ```python variable_tensor = tf.Variable([4, 5, 6], dtype=tf.int32) print(variable_tensor) ``` 这些变量可以在计算图中被修改并参与梯度下降等优化过程。 --- #### 3. `tf.placeholder()` (仅限 TensorFlow 1.x) 在 TensorFlow 1.x 版本中,`tf.placeholder()` 定义占位符,在运行会话时提供输入数据。 ```python placeholder_tensor = tf.compat.v1.placeholder(tf.float32, shape=[None, 3]) ``` 注意:该函数已被弃用,推荐使用 Eager Execution 或 Keras API 替代。 --- #### 4. `tf.matmul()` 矩阵乘法操作可以通过 `tf.matmul()` 实现。 ```python matrix_a = tf.constant([[1, 2], [3, 4]], dtype=tf.float32) matrix_b = tf.constant([[5, 6], [7, 8]], dtype=tf.float32) result = tf.matmul(matrix_a, matrix_b) print(result.numpy()) ``` --- #### 5. `tf.keras.Sequential()` 构建顺序模型可以利用 `tf.keras.Sequential()` 方法。 ```python model = tf.keras.Sequential([ tf.keras.layers.Dense(64, activation='relu', input_shape=(32,)), tf.keras.layers.Dense(10, activation='softmax') ]) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) ``` 这是现代 TensorFlow 高级接口的部分,简化了神经网络的设计流程。 --- #### 6. `tf.GradientTape()` 自动微分机制可通过 `tf.GradientTape()` 提供支持,适用于自定义训练循环。 ```python with tf.GradientTape() as tape: y_pred = model(x_train) # 前向传播 loss_value = loss_fn(y_true, y_pred) gradients = tape.gradient(loss_value, model.trainable_variables) optimizer.apply_gradients(zip(gradients, model.trainable_variables)) ``` 这种动态图模式增强了调试和实验能力。 --- #### 总结 上述函数展示了 TensorFlow 的核心功能,包括张量操作、变量管理、矩阵运算以及高级建模工具。为了更高效地掌握其特性,建议参考官方文档或在线资源进步实践。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值