函数原型为

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. html 复制 有时不显示样式

    是因为有中文的空格 /**HTML**/ div.ani /**css**/ .ani{ width:480px; height:320px; margin:50px auto; overflow:  ...

  2. 开工:创建虚拟机,xshell连接虚拟机,复制虚拟机,docker安装,添加加速器

    创建虚拟机:http://www.linuxidc.com/Linux/2015-08/121807.htm http://www.linuxidc.com/Linux/2010-04/25573.h ...

  3. Android(一) 动态菜单

    1.android的一个activity可以再选中某项之后按menu键弹出特定的菜单,也就是动态菜单.动态菜单的实现是靠menu类中的addIntentOptions函数实现的,具体的声明如下: in ...

  4. csv参数化,数据驱动

    首先我们要有一个接口测试用例存放的地方,我们这里用EXCEL模板管理,里面包含用例编号.入参.优先级.请求方式.url等等. 1:新建一个txt文件,命名为sjqd,后缀名改为csv,右键excel格 ...

  5. SQL SERVER错误代码

    官方错误代码:https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2008-r2/cc645601(v%3dsql.10 ...

  6. 【Espruino】NO.07 获取电压值

    http://blog.csdn.net/qwert1213131/article/details/27985645 本文属于个人理解,能力有限,纰漏在所难免.还望指正! [小鱼有点电] 前几节的内容 ...

  7. python爬虫CSDN文章抓取

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/nealgavin/article/details/27230679 CSDN原则上不让非人浏览訪问. ...

  8. python的时间差计算

    import time start = time.clock() #当中是你的程序 elapsed = (time.clock() - start) print("Time used:&qu ...

  9. this与$scope

    最近在Angular项目中遇到关于controller内使用$scope&this 暴露数据的问题,下面来分析一下: "controller as" 是Angular在1. ...

  10. 多张图片合成一个tif

    可以利用ACDSEE6.0打开你要合成的多张图片,CTRL全部选中,打开工具--转化文件格式-选择格式tif---所有页----合并---