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 torchvision.datasets import MNIST | |
| from torchvision import transforms | |
| from torch.utils.data import DataLoader | |
| class Model(Layer): | |
| def __init__(self, lr=0.00001): | |
| self.lr = lr | |
| self.layers = [ | |
| Linear(784,100, lr=self.lr), |
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 Model(Layer): | |
| def __init__(self, lr=0.0001): | |
| self.lr = lr | |
| self.layers = [ | |
| Linear(3,15, lr=self.lr), | |
| Relu(), | |
| Linear(15,1, lr=self.lr) | |
| ] |
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 Layer: | |
| def forward(self): | |
| raise NotImplemented | |
| def backward(self, grad): | |
| raise NotImplemented | |
| def __call__(self, *args): | |
| return self.forward(*args) |
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 = torch.tensor(x,requires_grad=True) | |
| _a = torch.tensor(a,requires_grad=True) | |
| _b = torch.tensor(b,requires_grad=True) | |
| y = (_a*_x + _b) * _a | |
| print('y = ', y) | |
| print() | |
| print('y.grad_fn = ', y.grad_fn) | |
| print() | |
| y.backward(torch.tensor(loss)) | |
| print('_x.grad = ',_x.grad) |
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
| y = Tensor([[0.45119641 0.25401614 0.36177802] | |
| [0.2445061 0.22747291 0.41594346] | |
| [0.35465603 0.26046455 1.26796941]]) | |
| y.grad_fn = <__main__.MulOp object at 0x7f169e332358> | |
| _x.grad = Tensor([[0.08710775 0.00380129 0.14389783] | |
| [0.09573756 0.05524222 0.07022905] | |
| [0.03903106 0.09778539 0.79086884]]) |
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 = np.random.random((3,3)) | |
| a = np.random.random((3,3)) | |
| b = np.random.random((3,)) | |
| loss = np.random.random((3,3)) | |
| _x = Tensor(x,requires_grad=True) | |
| _a = Tensor(a,requires_grad=True) | |
| _b = Tensor(b,requires_grad=True) |
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 Tensor: | |
| def __init__(self, data, requires_grad=False): | |
| self.data = data | |
| if not isinstance(data, np.ndarray): | |
| self.data = np.array(data) | |
| # whether to run backpropagation or not | |
| self.requires_grad = requires_grad | |
| # tensor gradient | |
| self._grad = None |
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 IPython.display import display, Javascript, Image | |
| from google.colab.output import eval_js | |
| from base64 import b64decode | |
| def take_photo(filename='photo.jpg', quality=0.8): | |
| js = Javascript(''' | |
| async function takePhoto(quality) { | |
| const div = document.createElement('div'); | |
| const capture = document.createElement('button'); | |
| capture.textContent = 'Capture'; |
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
| function IMPORTJSON(url, keys){ | |
| //keys = "id,message,likes,comments,shares,insights.post_impressions,insights.post_impressions_fan,insights.post_engaged_users" | |
| var res = UrlFetchApp.fetch(url); | |
| var content = res.getContentText(); | |
| var json = JSON.parse(content); | |
| var data = []; | |
| keys = keys.split(',') | |
| for(var i=0;i<json['data'].length;i++){ |
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 skimage import data | |
| from skimage import exposure | |
| from skimage.transform import match_histograms | |
| img = cv2.imread('surfing-1.jpg')[...,::-1] | |
| matched = match_histograms(img, imgy, multichannel=True) | |
| plt.imshow(img) | |
| plt.show() |