Skip to main content

tf.squeeze和tf.expand_dims的用法

import numpy as np
import tensorflow as tf
a=[[2,2,3,3],[4,5,6,4]]
t1=tf.expand_dims(a,0,name="t1")
t2=tf.expand_dims(a,1,name="t2")
with tf.Session() as sess:
    print(sess.run(t1).shape)
    print(sess.run(tf.squeeze(t1,0)).shape)
    print(sess.run(t2).shape)
    print(sess.run(tf.squeeze(t2,1)).shape)
输出如下:
(1, 2, 4)
(2, 4)
(2, 1, 4)
(2, 4)


Comments

Popular posts from this blog

Session Run的用法

feed_dict参数的作用是替换图中的某个tensor的值。例如: a = tf.add(2, 5)                        #a=7 b = tf.multiply(a, 3)                 #b=3*7=21 with tf.Session() as sess:     print(sess.run(b))     replace_dict = {a:15}           #用15代替b算式中的a     print(sess.run(b, feed_dict = replace_dict)) --------------------- 输出如下: 21 45