2017年7月18日星期二

tensorflow 常见操作

(本文提到的常用操作均是在实现与机器学习相关的代码相关的操作,总结如下)
1.根据索引取tensor中值(tf.gather)

input = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
output = tf.gather(input, 0)
print sess.run(output)  # ==> [1 2 3]
output = tf.gather(input, [0, 2])
print sess.run(output)

2.将tensor值重置为另一个tensor值(tf.assign)

3.循环(有输入和初始化值)(tf.scan)

4.获取tensor的shape(x.get_shape().as_list()或者tf.shape())

5.拼接tensor(tf.concat)

t1 = [[1,1,1]]
t2 = [[2,2,2,2]]
t3 = [[4,4,4,4,4]]
t1 = tf.constant(t1)
t2 = tf.constant(t2)
t3 = tf.constant(t3)

t = tf.concat([t1,t2,t3],axis = 1)

# t1 = [[1,2,3],[4,5,6]]
# t2 = [[7,8,9],[1,2,3]]
# a1 = tf.concat([t1,t2],0)
# a2 = tf.concat([t1,t2],1)

init_op = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init_op)
print sess.run(t)

6 将dimension[14]改成dimension[1,14] 用tf.reshape()

7 两个tensor矩阵相乘  tf.matmul

8 排序 tf.range


没有评论:

发表评论

leetcode 17

17.   Letter Combinations of a Phone Number Medium Given a string containing digits from   2-9   inclusive, return all possible l...