In [1]:
from keras.models import Sequential
from keras.layers import LSTM
import numpy as np
In [12]:
# 入力系列は長さ3だが、出力の系列は1(最後の入力に対してだけ出力する)
model = Sequential()
model.add(LSTM(5, input_shape=(3, 1)))
model.compile(optimizer='adam', loss='mse')
# input time steps
data = np.array([0.1, 0.2, 0.3]).reshape((1, 3, 1))
res = model.predict(data)
print(res)
print(res.shape)
In [13]:
# LSTMをstackするには入力系列のそれぞれに対して出力する必要がある
# return_sequences=TrueとすればOK
model = Sequential()
model.add(LSTM(5, return_sequences=True, input_shape=(3, 1)))
model.compile(optimizer='adam', loss='mse')
data = np.array([0.1, 0.2, 0.3]).reshape((1, 3, 1))
res = model.predict(data)
print(res)
print(res.shape)
In [16]:
from math import sin
from math import pi
import matplotlib.pyplot as plt
%matplotlib inline
length = 100
freq = 5
sequence = [sin(2 * pi * freq * (i / length)) for i in range(length)]
plt.plot(sequence)
Out[16]:
In [17]:
from math import sin, pi, exp
import matplotlib.pyplot as plt
length = 100
period = 10
decay = 0.05
sequence = [0.5 + 0.5 * sin(2 * pi * i / period) * exp(-decay * i) for i in range(length)]
plt.plot(sequence)
Out[17]:
In [18]:
# generate damped sine wave in [0, 1]
def generate_sequence(length, period, decay):
return [0.5 + 0.5 * sin(2 * pi * i / period) * exp(-decay * i) for i in range(length)]
In [19]:
seq = generate_sequence(100, 10, 0.05)
plt.plot(seq)
Out[19]:
In [21]:
seq = generate_sequence(100, 20, 0.05)
plt.plot(seq)
Out[21]:
In [24]:
# generate input and output pairs of damped sine waves
import numpy as np
import random
def generate_examples(length, n_patterns, output):
X, y = list(), list()
for _ in range(n_patterns):
p = random.randint(10, 20) # period
d = random.uniform(0.01, 0.1) # decay
sequence = generate_sequence(length + output, p, d)
X.append(sequence[:-output])
y.append(sequence[-output:])
X = np.array(X).reshape(n_patterns, length, 1) # (samples, time steps, features)の3Dテンソル
y = np.array(y).reshape(n_patterns, output)
return X, y
In [31]:
X, y = generate_examples(20, 5, 5)
for i in range(len(X)):
plt.plot([x for x in X[i, :, 0]] + [x for x in y[i]], 'o-')
In [32]:
from keras.models import Sequential
from keras.layers import LSTM, Dense
length = 50
output = 5 # 最後の5ステップの値を予測する
model = Sequential()
model.add(LSTM(20, return_sequences=True, input_shape=(length, 1)))
model.add(LSTM(20))
model.add(Dense(output))
model.compile(loss='mae', optimizer='adam')
model.summary()
In [34]:
# fit model
X, y = generate_examples(length, 10000, output)
history = model.fit(X, y, batch_size=10, epochs=1)
In [38]:
# evaluate model
X, y = generate_examples(length, 1000, output)
loss = model.evaluate(X, y, verbose=0)
print('MAE: %f' % loss)
In [79]:
# prediction on new data
X, y = generate_examples(length, 1, output)
yhat = model.predict(X, verbose=0)
X = X.reshape(50)
y = y.reshape(5)
yhat = yhat.reshape(5)
plt.plot(np.concatenate((X, y)), label='y')
plt.plot(np.concatenate((X, yhat)), label='yhat')
plt.legend()
Out[79]:
In [97]:
from keras.models import Sequential
from keras.layers import LSTM, Dense
length = 50
output = 5 # 最後の5ステップの値を予測する
model = Sequential()
model.add(LSTM(20, input_shape=(length, 1)))
model.add(Dense(output))
model.compile(loss='mae', optimizer='adam')
model.summary()
# fit model
X, y = generate_examples(length, 10000, output)
history = model.fit(X, y, batch_size=10, epochs=1)
# evaluate model
X, y = generate_examples(length, 1000, output)
loss = model.evaluate(X, y, verbose=0)
print('MAE: %f' % loss)
In [105]:
# prediction on new data
X, y = generate_examples(length, 1, output)
yhat = model.predict(X, verbose=0)
X = X.reshape(50)
y = y.reshape(5)
yhat = yhat.reshape(5)
plt.plot(np.concatenate((X, y)), label='y')
plt.plot(np.concatenate((X, yhat)), label='yhat')
plt.legend()
Out[105]:
In [ ]: