短小精悍算例:TensorFlow中concat()函数实现矩阵拼接操作

import tensorflow as tf
import numpy as np

A = tf.placeholder(dtype=tf.float32, shape=[2, 2])
#矩阵连接操作,在大型神经网络中用的比较多
AA0 = tf.concat([A, A], axis=0)  #上下拼接
AA1 = tf.concat([A, A], axis=1)  #左右拼接
#初始化
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
print(sess.run(AA0, feed_dict={
    
    A: [[1, 2], [-1, 1]]}))
print(sess.run(AA1, feed_dict={
    
    A: [[1, 2], [-1, 1]]}))


输出结果:
[[ 1.  2.]
 [-1.  1.]
 [ 1.  2.]
 [-1.  1.]]
 
[[ 1.  2.  1.  2.]
 [-1.  1. -1.  1.]]

猜你喜欢

转载自blog.csdn.net/weixin_39464400/article/details/106141663