Advertisement

如何在输入层之后添加一个LSTM层

阅读量:

在通过keras的Input函数设定网络输入层之后,若尝试添加LSTM层时频繁遇到各类错误,可参考以下解决方式:

复制代码
    import tensorflow as tf
    from tensorflow.keras.layers import Input
    #想要输入的数据
    inputs = np.random.random([32,8]).astype(np.float32)
    #输入数据的特征个数
    num_features=inputs.shape[1]
    #定义输入层
    input_layer = Input(shape=(num_features,),name='Input_layer')
    #需要将输入的数据形状改一下,下边的[-1,4,2]中的-1是只inputs的最后一维的维度
    #4是指时间步,也就是timestamp_size的大小,2是指每一个时间步输入的数据的维度。
    #4*2这两个数据相乘必须是等于输入数据最后一个维度的大小,比如我的输入数据的维度是8,那就是4*2,或者是2*4也可
    #如果你有一个[None,500]的数据,那么你的可以设置成100*5
    X = tf.reshape(input_layer, [-1, 4, 2])
    #定义LSTM层
    #

全部评论 (0)

还没有任何评论哟~