Skip to content

Commit 0efb9ff

Browse files
author
l2k2
committed
new timeseries
1 parent d185adb commit 0efb9ff

File tree

5 files changed

+34
-50
lines changed

5 files changed

+34
-50
lines changed

keras-lstm/lstm-gender-predictor.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ def vec2c(vec):
6161
return indices_char[i]
6262
return ""
6363

64-
print(X)
65-
print(y)
66-
6764
model = Sequential()
6865
model.add(LSTM(512, return_sequences=True, input_shape=(maxlen, len(chars))))
6966
model.add(Dropout(0.2))

keras-lstm/lstm-time-series-output.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from sklearn.preprocessing import MinMaxScaler
1212
from sklearn.metrics import mean_squared_error
1313
from keras.optimizers import SGD
14+
import matplotlib.pyplot as plt
1415

1516
# load the dataset
1617
dataframe = pandas.read_csv('international-airline-passengers.csv', usecols=[1], engine='python', skipfooter=3)
@@ -51,32 +52,35 @@ def create_dataset(dataset, look_back=8):
5152
model.add(LSTM(1, input_shape=(1, look_back)))
5253
model.add(Dense(1))
5354
model.compile(loss='mean_squared_error', optimizer=sgd)
54-
model.fit(trainX, trainY, epochs=50, batch_size=1, verbose=2)
55+
model.fit(trainX, trainY, epochs=5, batch_size=1, verbose=2)
5556

5657
# make predictions
5758
trainPredict = model.predict(trainX)
5859
testPredict = model.predict(testX)
60+
5961
# invert predictions
6062
trainPredict = scaler.inverse_transform(trainPredict)
6163
trainY = scaler.inverse_transform([trainY])
6264
testPredict = scaler.inverse_transform(testPredict)
6365
testY = scaler.inverse_transform([testY])
66+
6467
# calculate root mean squared error
6568
trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
6669
print('Train Score: %.2f RMSE' % (trainScore))
6770
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
6871
print('Test Score: %.2f RMSE' % (testScore))
6972

70-
import matplotlib.pyplot as plt
7173

7274
# shift train predictions for plotting
7375
trainPredictPlot = numpy.empty_like(dataset)
7476
trainPredictPlot[:, :] = numpy.nan
7577
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
78+
7679
# shift test predictions for plotting
7780
testPredictPlot = numpy.empty_like(dataset)
7881
testPredictPlot[:, :] = numpy.nan
7982
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict
83+
8084
# plot baseline and predictions
8185
plt.plot(scaler.inverse_transform(dataset))
8286
plt.plot(trainPredictPlot)

keras-lstm/lstm-train.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def on_epoch_end(self, batch, logs={}):
100100
print('----- Generating with seed: "' + sentence + '"')
101101
sys.stdout.write(generated)
102102

103-
for i in range(400):
103+
for i in range(50):
104104
x_pred = np.zeros((1, maxlen, len(chars)))
105105
for t, char in enumerate(sentence):
106106
x_pred[0, t, char_indices[char]] = 1.
@@ -117,8 +117,8 @@ def on_epoch_end(self, batch, logs={}):
117117
print()
118118
# train the model, output generated text after each iteration
119119
filepath=str(run.dir)+"/model-{epoch:02d}-{loss:.4f}.hdf5"
120-
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
120+
121121

122122
model.fit(x, y,
123123
batch_size=config.hidden_nodes,
124-
epochs=1000, callbacks=[SampleText(), WandbKerasCallback(), checkpoint])
124+
epochs=1000, callbacks=[SampleText(), WandbKerasCallback()])

keras-lstm/male.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# List of common male names.
2-
# Copyright (c) January 1991 by Mark Kantrowitz.
3-
# 2940 names
4-
# Thanks to Bill Ross for about 1000 additional names.
5-
# Version 1.3 (29-MAR-94)
6-
71
Aamir
82
Aaron
93
Abbey

keras-seq2seq/train.py

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11

22
from keras.models import Sequential
3-
from keras import layers
3+
from keras.layers import LSTM, TimeDistributed, RepeatVector, Dense
44
import numpy as np
55
import wandb
66
from wandb.keras import WandbCallback
77

88
wandb.init()
9+
config = wandb.config
910

1011
class CharacterTable(object):
1112
"""Given a set of characters:
@@ -38,17 +39,16 @@ def decode(self, x, calc_argmax=True):
3839
x = x.argmax(axis=-1)
3940
return ''.join(self.indices_char[x] for x in x)
4041

41-
42-
43-
4442
# Parameters for the model and dataset.
45-
TRAINING_SIZE = 50000
46-
DIGITS = 3
47-
REVERSE = True
43+
config.training_size = 50000
44+
config.digits = 3
45+
config.reverse = True
46+
config.hidden_size = 128
47+
config.batch_size = 128
4848

4949
# Maximum length of input is 'int + int' (e.g., '345+678'). Maximum length of
5050
# int is DIGITS.
51-
MAXLEN = DIGITS + 1 + DIGITS
51+
maxlen = config.digits + 1 + config.digits
5252

5353
# All the numbers, plus sign and space for padding.
5454
chars = '0123456789+ '
@@ -58,9 +58,9 @@ def decode(self, x, calc_argmax=True):
5858
expected = []
5959
seen = set()
6060
print('Generating data...')
61-
while len(questions) < TRAINING_SIZE:
61+
while len(questions) < config.training_size:
6262
f = lambda: int(''.join(np.random.choice(list('0123456789'))
63-
for i in range(np.random.randint(1, DIGITS + 1))))
63+
for i in range(np.random.randint(1, config.digits + 1))))
6464
a, b = f(), f()
6565
# Skip any addition questions we've already seen
6666
# Also skip any such that x+Y == Y+x (hence the sorting).
@@ -70,25 +70,26 @@ def decode(self, x, calc_argmax=True):
7070
seen.add(key)
7171
# Pad the data with spaces such that it is always MAXLEN.
7272
q = '{}+{}'.format(a, b)
73-
query = q + ' ' * (MAXLEN - len(q))
73+
query = q + ' ' * (maxlen - len(q))
7474
ans = str(a + b)
7575
# Answers can be of maximum size DIGITS + 1.
76-
ans += ' ' * (DIGITS + 1 - len(ans))
77-
if REVERSE:
76+
ans += ' ' * (config.digits + 1 - len(ans))
77+
if config.reverse:
7878
# Reverse the query, e.g., '12+345 ' becomes ' 543+21'. (Note the
7979
# space used for padding.)
8080
query = query[::-1]
8181
questions.append(query)
8282
expected.append(ans)
83+
8384
print('Total addition questions:', len(questions))
8485

8586
print('Vectorization...')
86-
x = np.zeros((len(questions), MAXLEN, len(chars)), dtype=np.bool)
87-
y = np.zeros((len(questions), DIGITS + 1, len(chars)), dtype=np.bool)
87+
x = np.zeros((len(questions), maxlen, len(chars)), dtype=np.bool)
88+
y = np.zeros((len(questions), config.digits + 1, len(chars)), dtype=np.bool)
8889
for i, sentence in enumerate(questions):
89-
x[i] = ctable.encode(sentence, MAXLEN)
90+
x[i] = ctable.encode(sentence, maxlen)
9091
for i, sentence in enumerate(expected):
91-
y[i] = ctable.encode(sentence, DIGITS + 1)
92+
y[i] = ctable.encode(sentence, config.digits + 1)
9293

9394
# Shuffle (x, y) in unison as the later parts of x will almost all be larger
9495
# digits.
@@ -110,34 +111,22 @@ def decode(self, x, calc_argmax=True):
110111
print(x_val.shape)
111112
print(y_val.shape)
112113

113-
# Try replacing GRU, or SimpleRNN.
114-
RNN = layers.LSTM
115-
HIDDEN_SIZE = 128
116-
BATCH_SIZE = 128
117-
LAYERS = 1
118114

119-
print('Build model...')
115+
120116
model = Sequential()
121117
# "Encode" the input sequence using an RNN, producing an output of HIDDEN_SIZE.
122118
# Note: In a situation where your input sequences have a variable length,
123119
# use input_shape=(None, num_feature).
124-
model.add(RNN(HIDDEN_SIZE, input_shape=(MAXLEN, len(chars))))
120+
model.add(LSTM(config.hidden_size, input_shape=(maxlen, len(chars))))
125121
# As the decoder RNN's input, repeatedly provide with the last hidden state of
126122
# RNN for each time step. Repeat 'DIGITS + 1' times as that's the maximum
127123
# length of output, e.g., when DIGITS=3, max output is 999+999=1998.
128-
model.add(layers.RepeatVector(DIGITS + 1))
129-
# The decoder RNN could be multiple layers stacked or a single layer.
130-
for _ in range(LAYERS):
131-
# By setting return_sequences to True, return not only the last output but
132-
# all the outputs so far in the form of (num_samples, timesteps,
133-
# output_dim). This is necessary as TimeDistributed in the below expects
134-
# the first dimension to be the timesteps.
135-
model.add(RNN(HIDDEN_SIZE, return_sequences=True))
124+
model.add(RepeatVector(config.digits + 1))
125+
model.add(LSTM(config.hidden_size, return_sequences=True))
136126

137127
# Apply a dense layer to the every temporal slice of an input. For each of step
138128
# of the output sequence, decide which character should be chosen.
139-
model.add(layers.TimeDistributed(layers.Dense(len(chars))))
140-
model.add(layers.Activation('softmax'))
129+
model.add(TimeDistributed(Dense(len(chars), activation='softmax')))
141130
model.compile(loss='categorical_crossentropy',
142131
optimizer='adam',
143132
metrics=['accuracy'])
@@ -150,7 +139,7 @@ def decode(self, x, calc_argmax=True):
150139
print('-' * 50)
151140
print('Iteration', iteration)
152141
model.fit(x_train, y_train,
153-
batch_size=BATCH_SIZE,
142+
batch_size=config.batch_size,
154143
epochs=1,
155144
validation_data=(x_val, y_val),callbacks=[WandbCallback()])
156145
# Select 10 samples from the validation set at random so we can visualize
@@ -162,7 +151,7 @@ def decode(self, x, calc_argmax=True):
162151
q = ctable.decode(rowx[0])
163152
correct = ctable.decode(rowy[0])
164153
guess = ctable.decode(preds[0], calc_argmax=False)
165-
print('Q', q[::-1] if REVERSE else q, end=' ')
154+
print('Q', q[::-1] if config.reverse else q, end=' ')
166155
print('T', correct, end=' ')
167156
if correct == guess:
168157
print('☑', end=' ')

0 commit comments

Comments
 (0)