This file contains 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: [12, 8]) | |
let accuracyAxes = plt.subplot(2, 1, 1) | |
accuracyAxes.set_ylabel("Train Accuracy") | |
accuracyAxes.plot(trainAccuracyResults, color: "blue") | |
let lossAxes = plt.subplot(2, 1, 2) | |
lossAxes.set_ylabel("Test Accuracy") | |
lossAxes.set_xlabel("Epoch") | |
lossAxes.plot(testAccuracyResults, color: "yellow") |
This file contains 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("Beginning training...") | |
struct Statistics { | |
var correctGuessCount: Int = 0 | |
var totalGuessCount: Int = 0 | |
var totalLoss: Float = 0 | |
} | |
// Store accuracy results during training | |
var trainAccuracyResults: [Float] = [] |
This file contains 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 Gradient Descent as the optimizer | |
let optimizer = SGD(for: classifier, learningRate: 0.1) |
This file contains 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 | |
let epochCount = 100 | |
let batchSize = 128 | |
// The LeNet-5 model | |
var classifier = Sequential { | |
Conv2D<Float>(filterShape: (5, 5, 1, 6), padding: .same, activation: relu) | |
AvgPool2D<Float>(poolSize: (2, 2), strides: (2, 2)) | |
Conv2D<Float>(filterShape: (5, 5, 6, 16), activation: relu) |
This file contains 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
# Display first 5 images | |
for img in imgs{ | |
plt.imshow(img.reshape(28,28)) | |
plt.show() | |
} |
This file contains 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
%include "/content/swift-datascience/MNIST.swift" | |
// Load dataset | |
let dataset = MNIST(batchSize: 128) | |
// Get first 5 images | |
let imgs = dataset.trainingImages.minibatch(at: 0, batchSize: 5).makeNumpyArray() | |
print(imgs.shape) |
This file contains 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
%include "EnableIPythonDisplay.swift" | |
IPythonDisplay.shell.enable_matplotlib("inline") | |
import Foundation | |
import Python | |
let os = Python.import("os") | |
let plt = Python.import("matplotlib.pyplot") | |
os.system("git clone https://github.com/mohdsanadzakirizvi/swift-datascience.git") |
This file contains 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.metrics import accuracy_score | |
print(accuracy_score(y_val, pred_bert)) |
This file contains 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.linear_model import LogisticRegression | |
# LR model | |
model_bert = LogisticRegression() | |
# train | |
model_bert = model_bert.fit(X_tr_bert, y_tr) | |
# predict | |
pred_bert = model_bert.predict(X_val_bert) |
This file contains 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 bert_serving.client import BertClient | |
# make a connection with the BERT server using it's ip address | |
bc = BertClient(ip="YOUR_SERVER_IP") | |
# get the embedding for train and val sets | |
X_tr_bert = bc.encode(X_tr.tolist()) | |
X_val_bert = bc.encode(X_val.tolist()) |