码迷,mamicode.com
首页 > 其他好文 > 详细

tensorflow中的一些语法问题

时间:2019-12-07 21:12:53      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:包括   exp   ror   python   constant   input   就会   with   expec   

一、tf.range()生成数字序列

range()函数用于创建数字序列变量,有以下两种形式:

range(limit, delta=1, dtype=None, name=range)
range(start, limit, delta=1, dtype=None, name=range)

该数字序列开始于 start 并且将以 delta 为增量扩展到不包括 limit 时的最大值结束,类似python的range函数。

二、tf.expand_dims()

TensorFlow中,想要维度增加一维,可以使用tf.expand_dims(input, dim, name=None)函数。当然,我们常用tf.reshape(input, shape=[])也可以达到相同效果,但是有些时候在构建图的过程中,placeholder没有被feed具体的值,这时就会包下面的错误:TypeError: Expected binary or unicode string, got 1

tf.expand_dims(x,[]) 列表的内容表示在第几个维度上增加

例子如下:

# ‘t‘ is a tensor of shape [2]
shape(expand_dims(t, 0)) ==> [1, 2]
shape(expand_dims(t, 1)) ==> [2, 1]
shape(expand_dims(t, -1)) ==> [2, 1]

# ‘t2‘ is a tensor of shape [2, 3, 5]
shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]
shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]
shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]

三、tf.tile()

tf.tile()在指定维度上复制tensor一定次数 [2,1]表示在第一个维度上(行)重复2次,列上重复1次 
import tensorflow as tf

with tf.Session() as sess:
    a = tf.constant([[15, 16], [17, 18]])
    b = tf.tile(a, [1, 3])
    c = tf.tile(a, [3, 2])
    print(------------------------------------)
    print(sess.run(a))
    print(------------------------------------)
    print(sess.run(b))
    print(------------------------------------)
    print(sess.run(c))

输出值如下:

[[15 16]
 [17 18]]
------------------------------------
[[15 16 15 16 15 16]
 [17 18 17 18 17 18]]
------------------------------------
[[15 16 15 16]
 [17 18 17 18]
 [15 16 15 16]
 [17 18 17 18]
 [15 16 15 16]
 [17 18 17 18]]

 

tensorflow中的一些语法问题

标签:包括   exp   ror   python   constant   input   就会   with   expec   

原文地址:https://www.cnblogs.com/flightless/p/12003032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!