在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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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