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 numpy as np | |
| from matplotlib import pyplot as plt | |
| from keras.utils.np_utils import to_categorical | |
| from keras.models import Sequential | |
| from keras.layers.core import Dense, Dropout, Flatten | |
| from keras.layers import Conv2D, MaxPooling2D |
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 keras.datasets import mnist | |
| (X_train, y_train), (X_test, y_test) = mnist.load_data() | |
| rows, cols = X_train[0].shape[0], X_train[0].shape[1] | |
| X_train = X_train.reshape(X_train.shape[0], rows, cols, 1) | |
| X_test = X_test.reshape(X_test.shape[0], rows, cols, 1) | |
| X_train = X_train.astype('float32')/255 | |
| X_test = X_test.astype('float32')/255 |
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 = Sequential() | |
| model.add(Conv2D(32, kernel_size=(3, 3), activation='rectifier', input_shape=(rows, cols, 1))) | |
| model.add(Conv2D(64, kernel_size=(3, 3), activation='rectifier')) | |
| model.add(Conv2D(128, kernel_size=(3, 3), activation='rectifier')) | |
| model.add(Dropout(0.5)) | |
| model.add(MaxPooling2D(pool_size = (2, 2))) |
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 keras.layers.advanced_activations import LeakyReLU | |
| model.add(Conv2D(32, kernel_size=(3, 3), activation='linear',input_shape=(rows, cols, 1))) | |
| model.add(LeakyReLU(alpha=0.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
| model.fit(X_train, y_train, batch_size=128, epochs=20, verbose=1, validation_split=0.2) | |
| score = model.evaluate(X_test, y_test, verbose=0) | |
| print('Accuracy:', score[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
| predictions = model.predict(X_test) |
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
| plt.figure(figsize=(15, 15)) | |
| for i in range(10): | |
| ax = plt.subplot(2, 10, i + 1) | |
| plt.imshow(X_test[i, :, :, 0], cmap='gray') | |
| plt.title("Digit: {}\nPredicted: {}".format(np.argmax(y_test[i]), np.argmax(predictions[i]))) | |
| plt.axis('off') | |
| plt.show() |
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 numpy as np | |
| class RNN: | |
| def step(self, x): | |
| # Update the state | |
| self.h = np.tanh(np.dot(self.W_hh, self.h) + np.dot(self.W_xh, x)) | |
| # Calculate the output | |
| y = np.dot(self.W_hy, self.h) | |
| return y |
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
| public class RNN | |
| { | |
| private Matrix<double> _state; | |
| private Matrix<double> _inputWeights; | |
| private Matrix<double> _recurrentWeights; | |
| private Matrix<double> _outputWeights; | |
| public RNN(Matrix<double> initialInputWeights, Matrix<double> initialReccurentWeights, Matrix<double> initialOutputWeights) | |
| { | |
| _inputWeights = initialInputWeights; |
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 numpy as np | |
| import collections | |
| class DataHandler: | |
| def read_data(self, fname): | |
| with open(fname) as f: | |
| content = f.readlines() | |
| content = [x.strip() for x in content] | |
| content = [content[i].split() for i in range(len(content))] | |
| content = np.array(content) |