tensorflow 之 tf.reshape 之 -1

最近压力好大,写点东西可能对心情有好处。

reshape即把矩阵的形状变一下,这跟matlab一样的,但如果参数是-1的话是什么意思呢?

看一下例子哈:

. . .

In [21]:

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

. . .

In [22]:

 
sess.run(tf.initialize_all_variables())

. . .

In [23]:

print(sess.run(tensor)) 
 
print(sess.run(tensor))
[1 2 3 4 5 6 7 8]

. . .

In [24]:

 
tensorReshape = tf.reshape(tensor,[2,4])

. . .

In [25]:

 
print(sess.run(tensorReshape))
[[1 2 3 4]
 [5 6 7 8]]

. . .

In [26]:

 
tensorReshape = tf.reshape(tensor,[1,2,4])

. . .

In [27]:

 
print(sess.run(tensorReshape))
[[[1 2 3 4]
  [5 6 7 8]]]

. . .

In [28]:

tensorResha
 
tensorReshape = tf.reshape(tensor,[-1,2,2])

. . .

In [29]:

 
print(sess.run(tensorReshape))
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]所以-1,就是缺省值,就是先以你们合适,到时总数除以你们几个的乘积,我该是几就是几。

. . .

时间: 2024-08-06 20:06:06

tensorflow 之 tf.reshape 之 -1的相关文章

TensorFlow之tf.nn.dropout():防止模型训练过程中的过拟合问题

一:适用范围: tf.nn.dropout是TensorFlow里面为了防止或减轻过拟合而使用的函数,它一般用在全连接层 二:原理: dropout就是在不同的训练过程中随机扔掉一部分神经元.也就是让某个神经元的激活值以一定的概率p,让其停止工作,这次训练过程中不更新权值,也不参加神经网络的计算.但是它的权重得保留下来(只是暂时不更新而已),因为下次样本输入时它可能又得工作了 三:函数介绍: tf.nn.drop(x,  keep_prob, noise_shape=None, seed=Non

TensorFlow之tf.unstack学习循环神经网络中用到!

unstack( value, num=None, axis=0, name='unstack' ) tf.unstack() 将给定的R维张量拆分成R-1维张量 将value根据axis分解成num个张量,返回的值是list类型,如果没有指定num则根据axis推断出! DEMO: import tensorflow as tf a = tf.constant([3,2,4,5,6]) b = tf.constant([1,6,7,8,0]) c = tf.stack([a,b],axis=0

TensorFlow基础——tf.Variable()和tf.constant()

tf.Variable()生成变量 tf.constant()生成常量 变量需要初始化: init = tf.initialize_all_variables() sess.run(init) 1 import tensorflow as tf 2 3 state = tf.Variable(0,name='counter') 4 print(state.name) 5 one = tf.constant(1) 6 7 new_value = tf.add(state,one) 8 update

【Tensorflow】tf.argmax函数

tf.argmax(input, axis=None, name=None, dimension=None) 此函数是对矩阵按行或列计算最大值 参数 input:输入Tensor axis:0表示按列,1表示按行 name:名称 dimension:和axis功能一样,默认axis取值优先.新加的字段 返回:Tensor  一般是行或列的最大值下标向量 例: import tensorflow as tf a=tf.get_variable(name='a', shape=[3,4], dtyp

新安装完 tensorflow 后import tensorflow as tf 报错

>>> import tensorflow as tf /opt/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 =

【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法

在计算loss的时候,最常见的一句话就是 tf.nn.softmax_cross_entropy_with_logits ,那么它到底是怎么做的呢? 首先明确一点,loss是代价值,也就是我们要最小化的值 tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None) 除去name参数用以指定该操作的name,与方法有关的一共两个参数: 第一个参数logits:就是神经网络最后一层的输出,如果有batch的话,它的大小就是[b

[Tensorflow]激励函数tf.nn.relu样例

代码: import tensorflow as tf import numpy as np ### 定义添加神经网络层函数 START ### def add_layer(inputs,in_size,out_size,activation_function=None): """描述: 添加神经网络层函数. :param inputs: 输入神经层 :param in_size: 输入神经层的神经元个数 :param out_size: 输出神经层的神经元个数 :param

tensorflow op tf.global_variables_initializer

一.安装目前用了tensorflow.deeplearning4j两个深度学习框架, tensorflow 之前一直支持到python 3.5,目前以更新到3.6,故安装最新版体验使用. 慢慢长征路:安装过程如下 WIN10: anaconda3.5: PYTHON3.6: tensorflow1.4: 二.TensorFlow 基本概念与原理理解 1.TensorFlow 的工作原理 TensorFlow是用数据流图(data flow graphs)技术来进行数值计算的.数据流图是描述有向图

TensorFlow 之tf.placeholder()

tf.placeholder( dtype, shape=None, name=None ) # In[] #dtype:被填充的张量(Tensor)的元素类型. #shape:被填充张量(Tensor)的形状(可选参数).如果没有指定张量(Tensor)打形状,你可 #以填充该张量(Tensor)为任意形状. #name:为该操作提供一个名字(可选参数). import tensorflow as tf # 定义placeholder input1 = tf.placeholder(tf.fl