This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tensorflow as tf | |
| print(tf.__version__) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from tensorflow import keras | |
| (x_train, y_train), (x_test, y_test) = keras.datasets.imdb.load_data(num_words=10000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> print(x_train[0]) | |
| [1, 14, 22, 16, 43, 530, 973, 1622, 1385, 65, 458, 4468, 66, 3941, 4, 173, 36, 256, 5, 25, 100, 43, 838, 112, 50, 670, 2, 9, 35, 480, 284, 5, 150, 4, 172, 112, 167, 2, 336, 385, 39, 4, 172, 4536, 1111, 17, 546, 38, 13, 447, 4, 192, 50, 16, 6, 147, 2025, 19, 14, 22, 4, 1920, 4613, 469, 4, 22, 71, 87, 12, 16, 43, 530, 38, 76, 15, 13, 1247, 4, 22, 17, 515, 17, 12, 16, 626, 18, 2, 5, 62, 386, 12, 8, 316, 8, 106, 5, 4, 2223, 5244, 16, 480, 66, 3785, 33, 4, 130, 12, 16, 38, 619, 5, 25, 124, 51, 36, 135, 48, 25, 1415, 33, 6, 22, 12, 215, 28, 77, 52, 5, 14, 407, 16, 82, 2, 8, 4, 107, 117, 5952, 15, 256, 4, 2, 7, 3766, 5, 723, 36, 71, 43, 530, 476, 26, 400, 317, 46, 7, 4, 2, 1029, 13, 104, 88, 4, 381, 15, 297, 98, 32, 2071, 56, 26, 141, 6, 194, 7486, 18, 4, 226, 22, 21, 134, 476, 26, 480, 5, 144, 30, 5535, 18, 51, 36, 28, 224, 92, 25, 104, 4, 226, 65, 16, 38, 1334, 88, 12, 16, 283, 5, 16, 4472, 113, 103, 32, 15, 16, 5345, 19, 178, 32] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=256) | |
| x_test = keras.preprocessing.sequence.pad_sequences(x_test, maxlen=256) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> print(x_train[0]) | |
| [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
| 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
| 0 0 0 0 0 0 0 0 0 0 1 14 22 16 | |
| 43 530 973 1622 1385 65 458 4468 66 3941 4 173 36 256 | |
| 5 25 100 43 838 112 50 670 2 9 35 480 284 5 | |
| 150 4 172 112 167 2 336 385 39 4 172 4536 1111 17 | |
| 546 38 13 447 4 192 50 16 6 147 2025 19 14 22 | |
| 4 1920 4613 469 4 22 71 87 12 16 43 530 38 76 | |
| 15 13 1247 4 22 17 515 17 12 16 626 18 2 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| x_val = x_train[:10000] | |
| y_val = y_train[:10000] | |
| x_train = x_train[10000:] | |
| y_train = y_train[10000:] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model = keras.Sequential() | |
| model.add(keras.layers.Embedding(10000, 16)) | |
| model.add(keras.layers.SimpleRNN(50)) | |
| model.add(keras.layers.Dense(16, activation=tf.nn.relu)) | |
| model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid)) | |
| model.compile(optimizer=tf.train.AdamOptimizer(), | |
| loss='binary_crossentropy', | |
| metrics=['accuracy']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model.fit(x_train, | |
| y_train, | |
| epochs=15, | |
| batch_size=512, | |
| validation_data=(x_val, y_val), | |
| verbose=1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| evaluation = model.evaluate(x_test, y_test, batch_size=512) | |
| print('Accuracy:', evaluation[1], 'Loss:', evaluation[0]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SimpleRNN: | |
| def __init__(): | |
| self.W_sh = np.zeros(shape=[rnn_size, rnn_size]) | |
| self.W_xh = np.zeros(shape=[word_vecotr_size, rnn_size]) | |
| self.s = np.zeros(shape=[rnn_size,1] | |
| def step(self, x): | |
| # update the state | |
| self.s = np.tanh(np.dot(self.W_sh, self.s) + np.dot(self.W_xh, x)) | |
| # compute the output vector |