tensorflow的tile使用

时间:2020-12-09 00:03:51

当你需要按照矩阵维度复制数据时候,可以使用tensorflow的tile函数

a1 = tf.tile(a, [2, 2]) 表示把a的第一个维度复制两次,第二个维度复制2次。
注意使用tf.nn.softmax(r, axis=0),表示对每一列取softmax,一定要注意维度,axis=0表示对列取softmax,不然数据会出错
 def tensoflow_test():
# 一个batch有20个样本,每个样本的长度为5,每一个为200维度
lstm_outpus = tf.truncated_normal(shape=[2, 5, 4], mean=0, stddev=1)
# 变形成二维
lstm_o = tf.reshape(lstm_outpus, shape=[-1, 4])
# 经过非线性
M = tf.tanh(lstm_o)
# 初始化权重信息
w = tf.truncated_normal(shape=[4,1], mean=0, stddev=1)
# 权重tf.matmul(M, w)
r = tf.matmul(M, w)
a = tf.nn.softmax(r, axis=0)
alpha = tf.tile(a, (1, 4))
# attention_res = lstm_o * alpha # M = tf.reshape(t, shape=[-1, 200])
# o = tf.Variable(tf.truncated_normal([1, 200]), name='w', dtype=tf.float32)
# a = tf.Variable(tf.truncated_normal([2,3]), dtype=tf.float32)
# b = tf.Variable(tf.truncated_normal([2,3]), dtype=tf.float32)
# a_b = tf.multiply(a,b)
# # a_b = a * b
# w = tf.transpose(o)
# res = tf.matmul(M, w)
# res2 = tf.reshape(res, shape=[-1, 5])
# copy_res = tf.tile(res2, (3,1))
# init_op = tf.global_variables_initializer() with tf.Session() as sess:
# sess.run(init_op)
# print(sess.run(res))
# print(sess.run(res2))
# print(res2)
# print(sess.run(copy_res))
# print(copy_res)
# print(sess.run(lstm_o))
# print(sess.run(lstm_outpus))
# print(sess.run(w))
print(lstm_outpus)
print(lstm_o)
print(alpha)
# print(sess.run(lstm_outpus))
print(sess.run([a, alpha]))
# print(sess.run(alpha))
# print(sess.run(alpha))
# print(sess.run(attention_res))