在Keras中沿特定轴拼接三维张量列表

for i in range(y_word_max_len):    sub_decoder_input = gather(main_decoder,(i))    # print(sub_decoder_input)    sub_decoder_input_repeated = RepeatVector(y_char_max_len)(sub_decoder_input)    sub_decoder = LSTM(256,return_sequences=True,name='sub_decoder')(sub_decoder_input_repeated)    sub_decoder_output = TimeDistributed(Dense(58,activation='softmax'),name='sub_decoder_output')(sub_decoder)    sub_decoder_output_reshaped = Reshape((1,y_char_max_len,58))(sub_decoder_output)    print("Sub decoder output is ",sub_decoder_output_reshaped)

我已经编写了上述代码片段,其中 y_word_max_len = 9

并且 main_decoder 是一个形状为 (None,9,256) 的张量

并且 y_char_max_len = 7

58 是我的输出大小,代码执行后输出的结果是

Sub decoder output is Tensor(“reshape_2/Reshape:0”, shape=(?, 1, 7, 58), dtype=float32)

Sub decoder output is Tensor(“reshape_3/Reshape:0”, shape=(?, 1, 7, 58), dtype=float32)

Sub decoder output is Tensor(“reshape_4/Reshape:0”, shape=(?, 1, 7, 58), dtype=float32)

Sub decoder output is Tensor(“reshape_5/Reshape:0”, shape=(?, 1, 7, 58), dtype=float32)

Sub decoder output is Tensor(“reshape_6/Reshape:0”, shape=(?, 1, 7, 58), dtype=float32)

Sub decoder output is Tensor(“reshape_7/Reshape:0”, shape=(?, 1, 7, 58), dtype=float32)

Sub decoder output is Tensor(“reshape_8/Reshape:0”, shape=(?, 1, 7, 58), dtype=float32)

Sub decoder output is Tensor(“reshape_9/Reshape:0”, shape=(?, 1, 7, 58), dtype=float32)

Sub decoder output is Tensor(“reshape_10/Reshape:0”, shape=(?, 1, 7, 58), dtype=float32)


现在我想将所有获得的9个张量拼接成一个结果张量,形状为

shape (?,9,7,58)

我该如何在Keras中实现这一点?谢谢


回答:

添加一个Concatenate层:

joined = Concatenate(axis=1)([sub1, sub2, sub3, sub4, sub5....])

为此,最好是创建一个子张量列表,并使用循环将结果添加到这个列表中:

subTensors = []for ..... :    #calculations    subTensors.append(sub_decoder_output_reshaped)joined = Concatenate(axis=1)(subTensors)

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注