Skip to content

Commit d6c317c

Browse files
committed
Merge branch 'master' of https://github.com/lukas/ml-class
2 parents 25225f8 + cf5fcb9 commit d6c317c

File tree

7 files changed

+89
-40
lines changed

7 files changed

+89
-40
lines changed

examples/keras-perf/wandb/settings

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[default]
22
entity = qualcomm
3-
project = perf-sep26
3+
project = perf-sep27
44
base_url = https://api.wandb.ai

examples/lstm/imdb-classifier/imdb-attention.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import wandb
2-
import imdb
32
import numpy as np
4-
import tensorflow as tf
5-
from tensorflow.keras.preprocessing import sequence, text
6-
from tensorflow.keras import initializers, regularizers, constraints
7-
import tensorflow.keras.backend as K
3+
from util import load_imdb
4+
import keras
5+
from keras.preprocessing import sequence, text
6+
from keras import initializers, regularizers, constraints
7+
import keras.backend as K
88

99
# from https://gist.github.com/cbaziotis/7ef97ccf71cbc14366835198c09809d2
1010

@@ -20,7 +20,7 @@ def dot_product(x, kernel):
2020
return K.squeeze(K.dot(x, K.expand_dims(kernel)), axis=-1)
2121

2222

23-
class AttentionWithContext(tf.keras.layers.Layer):
23+
class AttentionWithContext(keras.layers.Layer):
2424
"""
2525
Attention operation, with a context/query vector, for temporal data.
2626
Supports Masking.
@@ -128,7 +128,7 @@ def compute_output_shape(self, input_shape):
128128
config.hidden_dims = 100
129129
config.epochs = 10
130130

131-
(X_train, y_train), (X_test, y_test) = imdb.load_imdb()
131+
(X_train, y_train), (X_test, y_test) = load_imdb()
132132

133133
tokenizer = text.Tokenizer(num_words=config.vocab_size)
134134
tokenizer.fit_on_texts(X_train)
@@ -138,13 +138,13 @@ def compute_output_shape(self, input_shape):
138138
X_train = sequence.pad_sequences(X_train, maxlen=config.maxlen)
139139
X_test = sequence.pad_sequences(X_test, maxlen=config.maxlen)
140140

141-
model = tf.keras.models.Sequential()
142-
model.add(tf.keras.layers.Embedding(config.vocab_size,
143-
config.embedding_dims,
144-
input_length=config.maxlen))
145-
model.add(tf.keras.layers.CuDNNLSTM(config.hidden_dims, return_sequences=True))
141+
model = keras.models.Sequential()
142+
model.add(keras.layers.Embedding(config.vocab_size,
143+
config.embedding_dims,
144+
input_length=config.maxlen))
145+
model.add(keras.layers.CuDNNLSTM(config.hidden_dims, return_sequences=True))
146146
model.add(AttentionWithContext())
147-
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
147+
model.add(keras.layers.Dense(1, activation='sigmoid'))
148148
model.compile(loss='binary_crossentropy',
149149
optimizer='rmsprop',
150150
metrics=['accuracy'])

examples/lstm/imdb-classifier/imdb.ipynb

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 3,
5+
"execution_count": 8,
66
"metadata": {},
77
"outputs": [],
88
"source": [
99
"import util\n",
10+
"import os\n",
11+
"import math\n",
12+
"import subprocess\n",
1013
"import numpy as np\n",
1114
"from tensorflow.keras.datasets import imdb\n",
1215
"(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=1000)"
@@ -54,13 +57,26 @@
5457
},
5558
{
5659
"cell_type": "code",
57-
"execution_count": null,
60+
"execution_count": 6,
5861
"metadata": {},
59-
"outputs": [],
62+
"outputs": [
63+
{
64+
"name": "stdout",
65+
"output_type": "stream",
66+
"text": [
67+
"Downloading glove embeddings...\n"
68+
]
69+
}
70+
],
6071
"source": [
6172
"# Load embeddings\n",
73+
"if not os.path.exists(\"glove.6B.100d.txt\"):\n",
74+
" print(\"Downloading glove embeddings...\")\n",
75+
" subprocess.check_output(\n",
76+
" \"curl -OL http://nlp.stanford.edu/data/glove.6B.zip && unzip glove.6B.zip\", shell=True)\n",
6277
"embeddings_index = dict()\n",
6378
"f = open('glove.6B.100d.txt')\n",
79+
"print(\"Loading globe embeddings...\")\n",
6480
"for line in f:\n",
6581
" values = line.split()\n",
6682
" word = values[0]\n",
@@ -71,9 +87,20 @@
7187
},
7288
{
7389
"cell_type": "code",
74-
"execution_count": null,
90+
"execution_count": 17,
7591
"metadata": {},
76-
"outputs": [],
92+
"outputs": [
93+
{
94+
"data": {
95+
"text/plain": [
96+
"0.21388251764217375"
97+
]
98+
},
99+
"execution_count": 17,
100+
"metadata": {},
101+
"output_type": "execute_result"
102+
}
103+
],
77104
"source": [
78105
"def cosine_sim(v1,v2):\n",
79106
" \"compute cosine similarity of v1 to v2: (v1 dot v2)/{||v1||*||v2||)\"\n",
@@ -90,28 +117,47 @@
90117
"car = embeddings_index[\"car\"]\n",
91118
"truck = embeddings_index[\"truck\"]\n",
92119
"plane = embeddings_index[\"plane\"]\n",
93-
"cosine_sim(plane, book)"
120+
"cosine_sim(film, truck)"
94121
]
95122
},
96123
{
97124
"cell_type": "code",
98-
"execution_count": 6,
125+
"execution_count": 19,
99126
"metadata": {},
100127
"outputs": [
101128
{
102-
"ename": "NameError",
103-
"evalue": "name 'embeddings_index' is not defined",
104-
"output_type": "error",
105-
"traceback": [
106-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
107-
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
108-
"\u001b[0;32m<ipython-input-6-5ea6505cec73>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0membeddings_index\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m14\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
109-
"\u001b[0;31mNameError\u001b[0m: name 'embeddings_index' is not defined"
110-
]
129+
"data": {
130+
"text/plain": [
131+
"array([-1.9744e-01, 4.4831e-01, 1.3689e-01, -1.5595e-01, 9.3600e-01,\n",
132+
" 7.2986e-01, 3.4099e-01, -3.3896e-01, -8.9569e-02, -4.7706e-01,\n",
133+
" 3.5112e-01, -4.2198e-01, -1.2221e-01, -6.3375e-02, -4.5820e-01,\n",
134+
" 7.8723e-01, 9.4045e-01, 8.1101e-02, -2.3224e-01, 4.0778e-01,\n",
135+
" 3.3258e-01, -4.4458e-01, -4.7117e-01, 1.4852e-01, 9.6308e-01,\n",
136+
" -6.5267e-02, -5.3661e-02, -6.7474e-01, -4.2364e-01, 9.4392e-02,\n",
137+
" -3.8668e-01, 1.8237e-01, -1.2846e-01, -2.1952e-01, -5.8993e-01,\n",
138+
" 7.3602e-01, -2.4009e-01, 3.2392e-01, -2.4663e-01, -4.0684e-01,\n",
139+
" -5.2468e-01, 4.6174e-01, -1.4936e-01, -1.1999e-01, -1.3990e-01,\n",
140+
" -4.4944e-01, -2.6565e-01, -7.0061e-01, 3.0188e-01, -1.1209e-01,\n",
141+
" 6.6323e-01, 3.9698e-01, 6.9158e-01, 8.3442e-01, -5.2717e-01,\n",
142+
" -2.5314e+00, 1.3281e-01, 3.0253e-01, 1.1062e+00, 7.2221e-03,\n",
143+
" 2.6031e-01, 1.1584e+00, -7.9330e-02, -7.6659e-01, 1.2623e+00,\n",
144+
" -6.2071e-01, 5.9821e-01, 7.3539e-01, 3.8573e-01, -4.0293e-01,\n",
145+
" -3.1440e-02, 7.7863e-01, 3.1525e-01, 1.9003e-01, -6.5821e-01,\n",
146+
" 4.0548e-01, 5.3596e-03, 5.5274e-02, -1.2238e+00, -4.8912e-02,\n",
147+
" -3.0511e-01, 4.4473e-01, -3.3826e-01, -2.2133e-01, -1.3214e+00,\n",
148+
" -6.4761e-01, -4.4021e-01, -1.4910e+00, -2.2495e-02, 6.0346e-02,\n",
149+
" 1.4833e-01, 4.4162e-01, 7.9787e-01, -2.8076e-01, -2.9400e-02,\n",
150+
" -1.5656e-01, -1.2650e-01, -5.6968e-01, 1.5374e-03, 6.6600e-01],\n",
151+
" dtype=float32)"
152+
]
153+
},
154+
"execution_count": 19,
155+
"metadata": {},
156+
"output_type": "execute_result"
111157
}
112158
],
113159
"source": [
114-
"embeddings_index[14]"
160+
"embeddings_index[\"book\"]"
115161
]
116162
},
117163
{
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[default]
2-
entity = qualcomm
3-
project = imdb-sep26
2+
entity = bloomberg-class
3+
project = imdb-classifier
44
base_url = https://api.wandb.ai
55

examples/lstm/time-series/plotutil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def on_epoch_end(self, epoch, logs):
5757
preds = self.model.predict(self.testX)
5858

5959
# Generate a figure with matplotlib</font>
60-
figure = matplotlib.pyplot.figure(figsize=(10, 10))
60+
figure = matplotlib.pyplot.figure(figsize=(5, 5))
6161
plot = figure.add_subplot(111)
6262

6363
plot.plot(self.trainY)

examples/lstm/time-series/rnn.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
wandb.init()
88
config = wandb.config
99
config.repeated_predictions = False
10+
config.batch_size = 40
1011
config.look_back = 4
12+
config.epochs = 500
1113

1214

1315
def load_data(data_type="airline"):
@@ -44,7 +46,7 @@ def create_dataset(dataset):
4446
# split into train and test sets
4547
split = int(len(data) * 0.70)
4648
train = data[:split]
47-
test = data[split:]
49+
test = data[split-config.look_back-2:]
4850

4951
trainX, trainY = create_dataset(train)
5052
testX, testY = create_dataset(test)
@@ -56,9 +58,9 @@ def create_dataset(dataset):
5658
# create and fit the RNN
5759
model = tf.keras.models.Sequential()
5860
model.add(tf.keras.layers.SimpleRNN(5, input_shape=(config.look_back, 1)))
59-
model.add(tf.keras.layers.Dense(1))
61+
model.add(tf.keras.layers.Dense(1, activation="sigmoid"))
6062
model.compile(loss='mae', optimizer='rmsprop')
61-
model.fit(trainX, trainY, epochs=1000, batch_size=40, validation_data=(testX, testY), callbacks=[
63+
model.fit(trainX, trainY, epochs=config.epochs, batch_size=config.batch_size, validation_data=(testX, testY), callbacks=[
6264
PlotCallback(trainX, trainY, testX, testY,
6365
config.look_back, config.repeated_predictions),
6466
wandb.keras.WandbCallback(input_type="time")])
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[default]
2-
entity: qualcomm
3-
project: timeseries-sep26
4-
base_url: https://api.wandb.ai
2+
entity = qualcomm
3+
project = timeseries-sep26
4+
base_url = https://api.wandb.ai
5+

0 commit comments

Comments
 (0)