代码之家  ›  专栏  ›  技术社区  ›  Chong Lip Phang

你应该在Keras LSTM做什么?

  •  0
  • Chong Lip Phang  · 技术社区  · 7 年前

    我参考了Keras网站上给出的例子 here :

    from keras.models import Sequential
    from keras.layers import LSTM, Dense
    import numpy as np
    
    data_dim = 16
    timesteps = 8
    num_classes = 10
    
    # expected input data shape: (batch_size, timesteps, data_dim)
    model = Sequential()
    model.add(LSTM(32, return_sequences=True,
               input_shape=(timesteps, data_dim)))  # returns a sequence of vectors of dimension 32
    model.add(LSTM(32, return_sequences=True))  # returns a sequence of vectors of dimension 32
    model.add(LSTM(32))  # return a single vector of dimension 32
    model.add(Dense(10, activation='softmax'))
    
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
    
    # Generate dummy training data
    x_train = np.random.random((1000, timesteps, data_dim))
    y_train = np.random.random((1000, num_classes))
    
    # Generate dummy validation data
    x_val = np.random.random((100, timesteps, data_dim))
    y_val = np.random.random((100, num_classes))
    
    model.fit(x_train, y_train, batch_size=64, epochs=5, validation_data=(x_val, y_val))
    

    另外,我应该如何理解data\u dim和num\u类?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Pranav Vempati    7 年前

    因为你的参数 return_sequences = True [batch_size, time_steps, input_features] 并执行“多对多”映射。 Data_dim y_train 一定会成形的 [[1000, 10]]

    理解您提供的代码摘录的关键是设置参数 return\u sequences=真 使LSTM层能够传播 序列 网络中上游层的值。请注意,在10路softmax之前的最后一个LSTM层没有设置 time_steps 维度被折叠,密集层接收一个输入向量,它可以毫无问题地处理这个向量。

        2
  •  -1
  •   Chong Lip Phang    7 年前