函数原型为

def reshape(tensor, shape, name=None)

第1个参数为被调整维度的张量。

第2个参数为要调整为的形状。

返回一个shape形状的新tensor

注意shape里最多有一个维度的值可以填写为-1,表示自动计算此维度。

很简单的函数,如下,根据shape为[5,8]的tensor,生成一个新的tensor

import tensorflow as tf

alist = [[1, 2, 3, 4, 5, 6 ,7, 8],
[7, 6 ,5 ,4 ,3 ,2, 1, 0],
[3, 3, 3, 3, 3, 3, 3, 3],
[1, 1, 1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2, 2, 2]]
oriarray = tf.constant(alist) oplist = []
a1 = tf.reshape(oriarray, [1, 2, 5, 4])
oplist.append([a1, 'case 1, 2, 5, 4']) a1 = tf.reshape(oriarray, [-1, 2, 5, 4])
oplist.append([a1, 'case -1, 2, 5, 4']) a1 = tf.reshape(oriarray, [8, 5, 1, 1])
oplist.append([a1, 'case 8, 5, 1, 1']) with tf.Session() as asess:
for aop in oplist:
print('--------{}---------'.format(aop[1]))
print(asess.run(aop[0]))
print('--------------------------\n\n')

运行结果为

--------case 1, 2, 5, 4---------
2017-05-10 15:26:04.020848: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE instructions, but these are available on your machine and could speed up CPU computations.
2017-05-10 15:26:04.020848: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-10 15:26:04.020848: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-10 15:26:04.020848: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-10 15:26:04.021848: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-10 15:26:04.021848: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
[[[[1 2 3 4]
[5 6 7 8]
[7 6 5 4]
[3 2 1 0]
[3 3 3 3]] [[3 3 3 3]
[1 1 1 1]
[1 1 1 1]
[2 2 2 2]
[2 2 2 2]]]]
-------------------------- --------case -1, 2, 5, 4---------
[[[[1 2 3 4]
[5 6 7 8]
[7 6 5 4]
[3 2 1 0]
[3 3 3 3]] [[3 3 3 3]
[1 1 1 1]
[1 1 1 1]
[2 2 2 2]
[2 2 2 2]]]]
-------------------------- --------case 8, 5, 1, 1---------
[[[[1]] [[2]] [[3]] [[4]] [[5]]] [[[6]] [[7]] [[8]] [[7]] [[6]]] [[[5]] [[4]] [[3]] [[2]] [[1]]] [[[0]] [[3]] [[3]] [[3]] [[3]]] [[[3]] [[3]] [[3]] [[3]] [[1]]] [[[1]] [[1]] [[1]] [[1]] [[1]]] [[[1]] [[1]] [[2]] [[2]] [[2]]] [[[2]] [[2]] [[2]] [[2]] [[2]]]]
-------------------------- Process finished with exit code 0

TF-调整矩阵维度 tf.reshape 介绍的更多相关文章

  1. 深度学习原理与框架-递归神经网络-RNN_exmaple(代码) 1.rnn.BasicLSTMCell(构造基本网络) 2.tf.nn.dynamic_rnn(执行rnn网络) 3.tf.expand_dim(增加输入数据的维度) 4.tf.tile(在某个维度上按照倍数进行平铺迭代) 5.tf.squeeze(去除维度上为1的维度)

    1. rnn.BasicLSTMCell(num_hidden) #  构造单层的lstm网络结构 参数说明:num_hidden表示隐藏层的个数 2.tf.nn.dynamic_rnn(cell, ...

  2. 深度学习原理与框架-图像补全(原理与代码) 1.tf.nn.moments(求平均值和标准差) 2.tf.control_dependencies(先执行内部操作) 3.tf.cond(判别执行前或后函数) 4.tf.nn.atrous_conv2d 5.tf.nn.conv2d_transpose(反卷积) 7.tf.train.get_checkpoint_state(判断sess是否存在

    1. tf.nn.moments(x, axes=[0, 1, 2])  # 对前三个维度求平均值和标准差,结果为最后一个维度,即对每个feature_map求平均值和标准差 参数说明:x为输入的fe ...

  3. 深度学习原理与框架-Tensorflow基本操作-变量常用操作 1.tf.random_normal(生成正态分布随机数) 2.tf.random_shuffle(进行洗牌操作) 3. tf.assign(赋值操作) 4.tf.convert_to_tensor(转换为tensor类型) 5.tf.add(相加操作) tf.divide(相乘操作) 6.tf.placeholder(输入数据占位

    1. 使用tf.random_normal([2, 3], mean=-1, stddev=4) 创建一个正态分布的随机数 参数说明:[2, 3]表示随机数的维度,mean表示平均值,stddev表示 ...

  4. deep_learning_Function_tf.equal(tf.argmax(y, 1),tf.argmax(y_, 1))用法

    [Tensorflow] tf.equal(tf.argmax(y, 1),tf.argmax(y_, 1))用法 作用:输出正确的预测结果利用tf.argmax()按行求出真实值y_.预测值y最大值 ...

  5. tensorflow中 tf.train.slice_input_producer 和 tf.train.batch 函数(转)

    tensorflow数据读取机制 tensorflow中为了充分利用GPU,减少GPU等待数据的空闲时间,使用了两个线程分别执行数据读入和数据计算. 具体来说就是使用一个线程源源不断的将硬盘中的图片数 ...

  6. TensorFlow 辨异 —— tf.add(a, b) 与 a+b(tf.assign 与 =)、tf.nn.bias_add 与 tf.add(转)

    1. tf.add(a, b) 与 a+b 在神经网络前向传播的过程中,经常可见如下两种形式的代码: tf.add(tf.matmul(x, w), b) tf.matmul(x, w) + b 简而 ...

  7. tensorflow中 tf.train.slice_input_producer 和 tf.train.batch 函数

    tensorflow数据读取机制 tensorflow中为了充分利用GPU,减少GPU等待数据的空闲时间,使用了两个线程分别执行数据读入和数据计算. 具体来说就是使用一个线程源源不断的将硬盘中的图片数 ...

  8. tensorflow 的 Batch Normalization 实现(tf.nn.moments、tf.nn.batch_normalization)

    tensorflow 在实现 Batch Normalization(各个网络层输出的归一化)时,主要用到以下两个 api: tf.nn.moments(x, axes, name=None, kee ...

  9. TensorFlow 辨异 —— tf.add(a, b) 与 a+b(tf.assign 与 =)、tf.nn.bias_add 与 tf.add

    1. tf.add(a, b) 与 a+b 在神经网络前向传播的过程中,经常可见如下两种形式的代码: tf.add(tf.matmul(x, w), b) tf.matmul(x, w) + b 简而 ...

随机推荐

  1. 2018java面试知识汇总

    1. 多线程 1.1 多线程7种同步方法? 答:同步方法 同步代码块 使用重入锁实现线程同步(ReentrantLock) 使用特殊域变量(volatile)实现同步(每次重新计算,安全但并非一致) ...

  2. count(*) count(1) count(column)区别

    count(*) 和count(1)的效果是一样的.在某些情况下效率不一样.也会统计包含null的记录. count(column)会返回当前字段不为null的记录数.

  3. Find The Multiple--POJ1426

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...

  4. 洛谷P3966 单词 [TJOI2013] AC自动机

    正解:AC自动机 解题报告: 传送门! 先来提供一个40pts错解QAQ 首先看到这题就会想到AC自动机板子题2鸭!然后就照着那题的套路打一下,随便改一点儿,简单来说就是每次经过一个节点都要++,然后 ...

  5. CSS3实现背景透明,文字不透明

    最近遇到一个需求,要在图片上显示带有半透明背景的文字,效果如下图所示: 看到这个需求之后,第一反应是使用CSS3中的opacity设置元素的透明度. <!DOCTYPE html> < ...

  6. web 开发常见问题--GET POST 区别

    首先,get和post是什么? --两种 HTTP 请求方法:GET 和 POST HTTP Request Methods GET.POST 专业名称是 HTTP Request Methods.但 ...

  7. EasyUI 基本的拖动和放置

    <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Ba ...

  8. ext3日志模式

    ext3日志模式 http://blog.sina.com.cn/s/blog_5d4ab4b40100dosx.html ext3支持多种日志模式 ext3 是ext2文件系统的高一级版本,完全兼容 ...

  9. (转)Mysql 多表查询详解

    MySQL 多表查询详解 一.前言  二.示例 三.注意事项 一.前言  上篇讲到mysql中关键字执行的顺序,只涉及了一张表:实际应用大部分情况下,查询语句都会涉及到多张表格 : 1.1 多表连接有 ...

  10. PAT Maximum Subsequence Sum[最大子序列和,简单dp]

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...