Created
September 23, 2017 04:47
-
-
Save akimach/ca39e0b49257088cd3f6736393be4e2e to your computer and use it in GitHub Desktop.
怠け者のためのディープラーニング入門 - Scikit-learn・Keras ref: http://qiita.com/akimach/items/e9088ab1b98a06ae9454
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
| pip3 install scikit-learn |
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 sklearn import svm | |
| X_train = [[0, 0], [1, 1]] | |
| y_train = [0, 1] | |
| X_test = [[1, 1], [0, 1]] | |
| y_test = [1, 0] | |
| clf = svm.SVC(C=1.0) | |
| clf.fit(X_train, y_train) | |
| acc = clf.score(X_test, y_test) | |
| print("Accuracy:", acc) | |
| X_new = [[0, 0], [0, 1]] | |
| pred = clf.predict(X_new) | |
| print(pred) |
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
| Accuracy: 0.5 | |
| Prediction result: [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
| import numpy as np | |
| from sklearn.utils import shuffle | |
| from sklearn.model_selection import train_test_split | |
| np.random.seed(1234) | |
| X = np.random.randint(10, size=(10, 4)) | |
| y = np.random.randint(4, size=(10)) | |
| # Randomly permute | |
| X, y = shuffle(X, y) | |
| # Split dataset | |
| # Train : Test = 8 : 2 | |
| X_train, X_test, y_train, y_test =\ | |
| train_test_split(X, y, test_size=0.2) | |
| print("Train:", X_train.shape, y_train.shape) | |
| print("Test:", X_test.shape, y_test.shape) |
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
| Train: (8, 4) (8,) | |
| Test: (2, 4) (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
| pip3 install keras |
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
| Using TensorFlow backend. | |
| Epoch 1/10 | |
| 160/160 [==============================] - 0s - loss: 0.6587 - acc: 0.7938 | |
| Epoch 2/10 | |
| 160/160 [==============================] - 0s - loss: 0.6303 - acc: 0.8625 | |
| Epoch 3/10 | |
| 160/160 [==============================] - 0s - loss: 0.6040 - acc: 0.8625 | |
| Epoch 4/10 | |
| 160/160 [==============================] - 0s - loss: 0.5795 - acc: 0.8812 | |
| Epoch 5/10 | |
| 160/160 [==============================] - 0s - loss: 0.5562 - acc: 0.8875 | |
| Epoch 6/10 | |
| 160/160 [==============================] - 0s - loss: 0.5345 - acc: 0.8875 | |
| Epoch 7/10 | |
| 160/160 [==============================] - 0s - loss: 0.5143 - acc: 0.8937 | |
| Epoch 8/10 | |
| 160/160 [==============================] - 0s - loss: 0.4950 - acc: 0.9000 | |
| Epoch 9/10 | |
| 160/160 [==============================] - 0s - loss: 0.4770 - acc: 0.9125 | |
| Epoch 10/10 | |
| 160/160 [==============================] - 0s - loss: 0.4599 - acc: 0.9125 | |
| 32/40 [=======================>......] - ETA: 0s | |
| Loss: 0.464164888859 Accuracy: 0.9 | |
| [[ 0.57916182] | |
| [ 0.50237983] | |
| [ 0.70500255] | |
| [ 0.63764608] | |
| [ 0.33048654]] | |
| [[ 0.42043626] | |
| [ 0.32738858] | |
| [ 0.27511841] | |
| [ 0.32091939] | |
| [ 0.32354838]] |
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 keras.models import Sequential | |
| from keras.layers import Dense, Activation | |
| from keras import optimizers | |
| from sklearn.utils import shuffle | |
| from sklearn.model_selection import train_test_split | |
| # Setup dataset | |
| np.random.seed(1234) | |
| n_data = 200 | |
| n_class = n_data//2 | |
| X_positive = np.random.normal(loc=1.0, scale=1.0, size=(n_class, 2)) | |
| X_negative = np.random.normal(loc=-1.0, scale=1.0, size=(n_class, 2)) | |
| X = np.r_[X_positive, X_negative] | |
| y = np.r_[[1]*n_class, [0]*n_class] | |
| # Split dataset | |
| X, y = shuffle(X, y) | |
| X_train, X_test, y_train, y_test =\ | |
| train_test_split(X, y, test_size=0.2) | |
| # Build a model | |
| model = Sequential() | |
| model.add(Dense(units=128, input_dim=2)) | |
| model.add(Activation('relu')) | |
| model.add(Dense(units=128)) | |
| model.add(Activation('relu')) | |
| model.add(Dense(units=1)) | |
| model.add(Activation('sigmoid')) | |
| model.compile(loss='binary_crossentropy', | |
| optimizer=optimizers.SGD(lr=0.01), | |
| metrics=['accuracy',]) | |
| # Training | |
| model.fit(X_train, y_train, batch_size=20, epochs=10) | |
| # Evaluation | |
| score = model.evaluate(X_test, y_test) | |
| print("\nLoss:", score[0], "Accuracy:", score[1]) | |
| # Prediction | |
| X_positive_new = np.random.normal(loc=1.0, scale=1.0, size=(5, 2)) | |
| X_negative_new = np.random.normal(loc=-1.0, scale=1.0, size=(5, 2)) | |
| print(model.predict(X_positive_new)) | |
| print(model.predict(X_negative_new)) |
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
| # mlp.pyに追加してください | |
| # ... 略 ... | |
| from keras.utils import plot_model | |
| # ... 略 ... | |
| from keras.utils import plot_model | |
| plot_model(model, to_file='model.png') | |
| """ | |
| Jupyter Notebook上で可視化する場合は以下のようにする : | |
| from IPython.display import SVG | |
| g = model_to_dot(model=m) | |
| SVG(g.create(prog='dot', format='svg')) | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment