lstm模型的5個步驟

the 5 steps in the LSTM model life-cycle in Keras that we are going to look at.

  1. Define Network
  2. Compile Network
  3. Fit Network
  4. Evaluate Network
  5. Make Predictions

An End-to-End Worked Example from "https://machinelearningmastery.com/5-step-life-cycle-long-short-term-memory-models-keras/"

 This example will use a simple problem of learning a sequence of 10 numbers. We will show the network a number, such as 0.0 and expect it to predict 0.1. Then show it 0.1 and expect it to predict 0.2, and so on to 0.9.

  1. Define Network: We will construct an LSTM neural network with a 1 input timestep and 1 input feature in the visible layer, 10 memory units in the LSTM hidden layer, and 1 neuron in the fully connected output layer with a linear (default) activation function.
  2. Compile Network: We will use the efficient ADAM optimization algorithm with default configuration and the mean squared error loss function because it is a regression problem.
  3. Fit Network: We will fit the network for 1,000 epochs and use a batch size equal to the number of patterns in the training set. We will also turn off all verbose output.
  4. Evaluate Network. We will evaluate the network on the training dataset. Typically we would evaluate the model on a test or validation set.
  5. Make Predictions. We will make predictions for the training input data. Again, typically we would make predictions on data where we do not know the right answer.

The complete code listing is provided below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

# Example of LSTM to learn a sequence

from pandas import DataFrame

from pandas import concat

from keras.models import Sequential

from keras.layers import Dense

from keras.layers import LSTM

# create sequence

length = 10

sequence = [i/float(length) for i in range(length)]

print(sequence)

# create X/y pairs

df = DataFrame(sequence)

df = concat([df.shift(1), df], axis=1)

df.dropna(inplace=True)

# convert to LSTM friendly format

values = df.values

X, y = values[:, 0], values[:, 1]

X = X.reshape(len(X), 1, 1)

# 1. define network

model = Sequential()

model.add(LSTM(10, input_shape=(1,1)))

model.add(Dense(1))

# 2. compile network

model.compile(optimizer='adam', loss='mean_squared_error')

# 3. fit network

history = model.fit(X, y, epochs=1000, batch_size=len(X), verbose=0)

# 4. evaluate network

loss = model.evaluate(X, y, verbose=0)

print(loss)

# 5. make predictions

predictions = model.predict(X, verbose=0)

print(predictions[:, 0])

Running this example produces the following output, showing the raw input sequence of 10 numbers, the mean squared error loss of the network when making predictions for the entire sequence, and the predictions for each input pattern.

Outputs were spaced out for readability.

We can see the sequence is learned well, especially if we round predictions to the first decimal place.

 

1

2

3

4

5

6

[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

 

4.54527471447e-05

 

[ 0.11612834 0.20493418 0.29793766 0.39445466 0.49376178 0.59512401

0.69782174 0.80117452 0.90455914]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章