【问题标题】:tensor concat output to input to feed new lstm layer张量 concat 输出到输入以提供新的 lstm 层
【发布时间】:2022-01-02 07:42:53
【问题描述】:

我尝试重塑和连接一些输出以补充原始输入并在我的模型的下一阶段使用它。 尺寸似乎匹配,但我收到此错误:

Concatenate(axis=2)([tensor_input2, out_first_try])
*** ValueError: A `Concatenate` layer requires inputs with matching 
shapes except for the concat axis. Got inputs shapes: [(64, 10, 8), [(), 
(), ()]]

我也试试:

tf.concat([tensor_input2, out_first_try], 2)

出现此错误:

tf.concat([tensor_input2, out_first_try], 2)
*** ValueError: Shape must be rank 3 but is rank 1 for '{{node 
tf.concat/concat}} = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32] 
(Placeholder, tf.concat/concat/values_1, tf.concat/concat/axis)' with 
input shapes: [64,10,8], [3], [].

原因似乎相同,但我不知道如何处理,

    # tensor_input1 = [64,365,9]
    tensor_input1 = Input(batch_size=batch, shape=(X.shape[1], 
                    X.shape[2]), name='input1')
    # tensor_input2 = [64,10,8]
    tensor_input2 = Input(batch_size=batch, shape=(X2.shape[1], 
                    X2.shape[2]), name='input2')

    extractor = CuDNNLSTM(100, return_sequences=False, 
                 stateful=False, name='LSTM1')(tensor_input2)
    extractor = Dropout(rate = .2)(extractor)
   
    extractor = Dense(100, activation='softsign')(extractor)

    out_1 = Dense(10, activation='linear')(extractor2)

    # add a dimension to out_1 [64,10] to fit tensor_input2 
    out_first_try = tf.expand_dims(out_1, axis=2).shape.as_list()

    # concat in 3d dim the output to the original input
    # tensor_input2 =[64,10,8] 
    # out_first_try, after tf.expend [64,10,1]
    forcast_input  = Concatenate(axis=2)([tensor_input2, 
                     out_first_try])

    # forcast_input expected size [64,10,9]

    # finaly concat tensor_input1, new tensor_input2 side to side
    allin_input = Concatenate(axis=1)([tensor_input1, forcast_input])
    # allin_input  expected size [64,365+10,9]

    extractor2 = CuDNNLSTM(100, return_sequences=False, 
                 stateful=False, name='LSTM1')(allin_input )
    ...

【问题讨论】:

    标签: python tensorflow concatenation tensorflow2.0 tensor


    【解决方案1】:

    将张量与列表连接是行不通的。所以,也许尝试这样的事情:

    out_first_try = tf.expand_dims(out_1, axis=2)
    forcast_input  = Concatenate(axis=2)([tensor_input2, out_first_try])
    

    请注意,我删除了shape.as_list(),因为顾名思义,它以列表的形式返回张量的形状。你可以用这个例子来验证:

    import tensorflow as tf
    
    out_1 = tf.random.normal((5, 10))
    out_first_try = tf.expand_dims(out_1, axis=2).shape.as_list()
    tf.print(type(out_first_try))
    #<class 'list'>
    

    【讨论】:

    • 就是这样,效果很好,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 2016-05-05
    • 1970-01-01
    • 2020-08-31
    • 2018-03-25
    • 1970-01-01
    • 2018-12-29
    相关资源
    最近更新 更多