Skip to content

Commit 0bb1508

Browse files
authored
Merge pull request #841 from Kaggle/fix-keras
Use Keras LSTM layer.
2 parents 6c6b7ba + fbb00e8 commit 0bb1508

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

tests/test_keras.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pandas as pd
66

77
from keras.models import Sequential
8-
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, CuDNNLSTM
8+
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, LSTM
99
from keras.optimizers import RMSprop, SGD
1010
from keras.utils.np_utils import to_categorical
1111

@@ -62,29 +62,21 @@ def test_conv2d(self):
6262

6363
model.evaluate(x_test, y_test, batch_size=32)
6464

65-
# Tensorflow 2.0 doesn't support the contrib package.
66-
#
67-
# Error:
68-
# from tensorflow.contrib.cudnn_rnn.python.ops import cudnn_rnn_ops
69-
# ModuleNotFoundError: No module named 'tensorflow.contrib'
70-
#
71-
# tf.keras should be used instead until this is fixed.
72-
# @gpu_test
73-
# def test_cudnn_lstm(self):
74-
# x_train = np.random.random((100, 100, 100))
75-
# y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
76-
# x_test = np.random.random((20, 100, 100))
77-
# y_test = keras.utils.to_categorical(np.random.randint(10, size=(20, 1)), num_classes=10)
78-
79-
# sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
80-
81-
# model = Sequential()
82-
# model.add(CuDNNLSTM(32, return_sequences=True, input_shape=(100, 100)))
83-
# model.add(Flatten())
84-
# model.add(Dense(10, activation='softmax'))
85-
86-
87-
# model.compile(loss='categorical_crossentropy', optimizer=sgd)
88-
# model.fit(x_train, y_train, batch_size=32, epochs=1)
89-
# model.evaluate(x_test, y_test, batch_size=32)
65+
def test_lstm(self):
66+
x_train = np.random.random((100, 100, 100))
67+
y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
68+
x_test = np.random.random((20, 100, 100))
69+
y_test = keras.utils.to_categorical(np.random.randint(10, size=(20, 1)), num_classes=10)
70+
71+
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
72+
73+
model = Sequential()
74+
model.add(LSTM(32, return_sequences=True, input_shape=(100, 100)))
75+
model.add(Flatten())
76+
model.add(Dense(10, activation='softmax'))
77+
78+
79+
model.compile(loss='categorical_crossentropy', optimizer=sgd)
80+
model.fit(x_train, y_train, batch_size=32, epochs=1)
81+
model.evaluate(x_test, y_test, batch_size=32)
9082

0 commit comments

Comments
 (0)