tf.reshape()
分类:
TensorFlow
1.tf.reshape
1 | reshape(tensor, shape, name = None ) |
作用:重塑张量。给定张量,此操作将返回与形状为shape的张量具有相同值的张量。 如果“形状”的一个分量为特殊值-1,则将计算该尺寸的大小,以使总大小保持恒定。 具体来说,[-1]的“形状”会展平为一维。 “形状”的至多一个分量可以为-1。 如果“ shape”为一维或更高,则该操作将返回一个形状为“ shape”的张量,其中填充了“ tensor”的值。 在这种情况下,“形状”所隐含的元素数量必须与“张量”中的元素数量相同。
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | For example: ``` # tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9] # tensor 't' has shape [9] reshape(t, [ 3 , 3 ]) = = > [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]] # tensor 't' is [[[1, 1], [2, 2]], # [[3, 3], [4, 4]]] # tensor 't' has shape [2, 2, 2] reshape(t, [ 2 , 4 ]) = = > [[ 1 , 1 , 2 , 2 ], [ 3 , 3 , 4 , 4 ]] # tensor 't' is [[[1, 1, 1], # [2, 2, 2]], # [[3, 3, 3], # [4, 4, 4]], # [[5, 5, 5], # [6, 6, 6]]] # tensor 't' has shape [3, 2, 3] # pass '[-1]' to flatten 't' reshape(t, [ - 1 ]) = = > [ 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 4 , 4 , 4 , 5 , 5 , 5 , 6 , 6 , 6 ] # -1 can also be used to infer the shape # -1 is inferred to be 9: reshape(t, [ 2 , - 1 ]) = = > [[ 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 ], [ 4 , 4 , 4 , 5 , 5 , 5 , 6 , 6 , 6 ]] # -1 is inferred to be 2: reshape(t, [ - 1 , 9 ]) = = > [[ 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 ], [ 4 , 4 , 4 , 5 , 5 , 5 , 6 , 6 , 6 ]] # -1 is inferred to be 3: reshape(t, [ 2 , - 1 , 3 ]) = = > [[[ 1 , 1 , 1 ], [ 2 , 2 , 2 ], [ 3 , 3 , 3 ]], [[ 4 , 4 , 4 ], [ 5 , 5 , 5 ], [ 6 , 6 , 6 ]]] # tensor 't' is [7] # shape `[]` reshapes to a scalar reshape(t, []) = = > 7 ``` Args: tensor: A `Tensor`. shape: A `Tensor`. Must be one of the following types: `int32`, `int64`. Defines the shape of the output tensor. name: A name for the operation (optional). Returns: A `Tensor`. Has the same type as `tensor`. """ |
bert中源码:
1 2 3 4 5 6 7 8 9 10 11 | # If the input is a 2D tensor of shape [batch_size, seq_length], we # reshape to [batch_size, seq_length, 1]. if input_ids.shape.ndims = = 2 : input_ids = tf.expand_dims(input_ids, axis = [ - 1 ]) embedding_table = tf.get_variable( name = word_embedding_name, shape = [vocab_size, embedding_size], initializer = create_initializer(initializer_range)) flat_input_ids = tf.reshape(input_ids, [ - 1 ]) #【batch_size*seq_length*input_num】 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现