21个项目tensorflow 第一章(2)

iDe :pycharm

深度学习框架:tensorflow-gpu

win10+1050ti

参考书目:21个项目玩转tensorflow

1>tf.reshape(tensor, shape, name=None) 

作用:将tensor转变成shape大小

特殊之处在与,可以存在-1选项

如在书本中,代码

x_image= tf.reshape(x,[-1,28,28,1]

表示的就是讲原图片转变成 ?*28*28*1维的矩阵

官方例子

# 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

2>tf.truncated_normal

tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
  • shape: 一维的张量,也是输出的张量。
  • mean: 正态分布的均值。
  • stddev: 正态分布的标准差。
  • dtype: 输出的类型。
  • seed: 一个整数,当设置之后,每次生成的随机数都一样。
  • name: 操作的名字

在tf.truncated_normal中如果x的取值在区间(μ-2σ,μ+2σ)之外则重新进行选择。这样保证了生成的值都在均值附近。

与之相对的是

tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

其输出的是指定均值和方差的正态分布的随机值

3>tf.constant(x,shape)

tf.constant(0.1,shape=shape)

生成常数

b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2]) => [[7. 8.]
                                                           [9. 10.]
                                                           [11. 12.]]

4>tf.nn.con2d()

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)

这是tensorflow中生成卷积函数的一个函数,

第一个参数input:指需要做卷积的输入图像,它要求是一个Tensor,具有[batch, in_height, in_width, in_channels]这样的shape,具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor,要求类型为float32和float64其中之一

第二个参数filter:相当于CNN中的卷积核,它要求是一个Tensor,具有[filter_height, filter_width, in_channels, out_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维

第三个参数strides:卷积时在图像每一维的步长,这是一个一维的向量,长度4

第四个参数padding:string类型的量,只能是"SAME","VALID"其中之一,这个值决定了不同的卷积方式。“SAME”表示的是,当卷积核划到边界,且原图像不够时,此时,会自动填充0,以补充维度,而“VLID”则不会,而是直接舍弃最后一列

第五个参数:use_cudnn_on_gpu:bool类型,是否使用cudnn加速,默认为true

5>tf.nn.max_pool(value, ksize, strides, padding, name=None)

tf.nn.max_pool(value, ksize, strides, padding, name=None)

这是一种简单的池化函数

第一个参数 value 表示池化的对象,一般是一个tensor ,其内容为[batch数量,height,width,channal]

stride:类似于卷积核,一般为[1,height,width,1]

padding : 依然有“SAME”和“VLID”两种选择

6>

tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None,name=None) 

keep_prob表示的是,drop掉的概率是多少,一般可以表示为0.5

7>前面,在上一篇文章中我们-tf.reduce.sum(y*log(y_))表示其交叉熵,这边有直接的函数

四种交叉熵的计算公式

一 

tf.nn.sigmoid_cross_entropy_with_logits(_sentinel=None,labels=None, logits=None, name=None

logits:一个数据类型(type)是float32或float64,表示的是预测数据

shape:[batch_size,num_classes],单样本是[num_classes]

labels:和logits具有相同的type(float)和shape的张量(tensor),实际数据

name:操作的名字,可填可不填

7>

Tensor.eval(feed_dict=None, session=None):

tf.Tensor的Session.run() 的另外一种写法

https://blog.csdn.net/chengshuhao1991/article/details/78554743

上述博客有官方文档的翻译

总而言之,当tensor是tensor时,两者相当

最大的区别在于,sess可以进行多重计算

t = tf.constant(42.0)
u = tf.constant(37.0)
tu = tf.mul(t, u)
ut = tf.mul(u, t)
with sess.as_default():
   tu.eval()  # runs one step
   ut.eval()  # runs one step
   sess.run([tu, ut])  # evaluates both tensors in a single step

8>

%% 百分号标记 #就是输出一个%

%c 字符及其ASCII码

%s 字符串

%d 有符号整数(十进制)

%u 无符号整数(十进制)

%o 无符号整数(八进制)

%x 无符号整数(十六进制)

%X 无符号整数(十六进制大写字符)

%e 浮点数字(科学计数法)

%E 浮点数字(科学计数法,用E代替e)

%f 浮点数字(用小数点符号)

%g 浮点数字(根据值的大小采用%e或%f)

%G 浮点数字(类似于%g)

%p 指针(用十六进制打印值的内存地址)

%n 存储输出字符的数量放进参数列表的下一个变量中

猜你喜欢

转载自blog.csdn.net/weixin_40085833/article/details/82430229
今日推荐