把5维张量变成4维张量进行卷积操作,再变回5维张量,类似于tf.keras.layers.TimeDistributed操作,手动操作如下:
import numpy as np
import tensorflow.compat.v1 as tf
# 定义维张量
time_step=6
input = tf.convert_to_tensor(np.random.random((4, time_step, 256, 256, 3)).astype(np.float64))
# 张量变形,5维变成4维
input = [tf.squeeze(t,axis=1) for t in tf.split(input,num_or_size_splits=time_step,axis=1)]
input = tf.concat(input,axis=0)
# 把4维张量再复原成5维张量
input_re = [tf.expand_dims(t,axis=1) for t in tf.split(input,num_or_size_splits=time_step,axis=0)]
input_re = tf.concat(input_re,axis=1)
# 验证是否相等
with tf.Session() as sess:
input,input_re = sess.run([input,input_re])
print(input_re==input)
输出:
[[[[[ True True True]
[ True True True]
[ True True True]
...
[ True True True]
[ True True True]
[

本文详细介绍了如何将5维张量转换为4维张量进行卷积操作,然后再将其转换回5维张量的过程。通过具体实例演示了使用TensorFlow实现这一操作的方法,并验证了转换前后张量的一致性。
最低0.47元/天 解锁文章
949

被折叠的 条评论
为什么被折叠?



