thensorsflow 矩阵增加/降低一个维度

import tensorflow as tf

a = tf.constant([[1, 2],[2,4]])
b = tf.expand_dims(a,1)

with tf.Session() as sess:
    a_, b_ = sess.run([a, b])

结果:

a: (2,2)

b:(2,1,2)

import tensorflow as tf

a = tf.constant([[1, 2],[2,4]])
b = tf.expand_dims(a,0)

with tf.Session() as sess:
    a_, b_ = sess.run([a, b])

结果:

a: (2,2)

b:(1,2,2)

import tensorflow as tf

a = tf.constant([[1, 2],[2,4]])
b = tf.expand_dims(a,2)

with tf.Session() as sess:
    a_, b_ = sess.run([a, b])

结果:

a: (2,2)

b:(2,2,1)

降低维度

import tensorflow as tf

a = tf.constant([[1, 2, 3]])  
b = tf.squeeze(a)

with tf.Session() as sess:
    a_, b_ = sess.run([a, b])

结果:

a: (1,3)

b:(3,)

猜你喜欢

转载自www.cnblogs.com/ai-learning-blogs/p/13377954.html
今日推荐