-
Largest Sum of Averages https://leetcode.com/problems/largest-sum-of-averages/
-
The maximum-subarray problem: 68
-
Dynamic programming: https://leetcode.com/explore/interview/card/top-interview-questions-hard/121/dynamic-programming/862/discuss/151887/Classic-DP-Python-beats-97
-
practice trie
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 torch | |
import torch.nn as nn | |
import numpy as np | |
from onnx_coreml import convert | |
from torch.autograd import Variable | |
import torch.onnx | |
import torchvision | |
import onnx | |
model_name = "dog_vs_cat_image.onnx" |
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
class ImageScale(nn.Module): | |
def __init__(self): | |
super().__init__() | |
self.denorminator = torch.full((3, sz, sz), 255.0, device=torch.device("cuda")) | |
def forward(self, x): return torch.div(x, self.denorminator).unsqueeze(0) | |
# We need to: | |
# - Add ImageScale by 255.0 at the front | |
# - Replace LogSoftmax layer with Softmax at the end to get probability instead of loss/cost | |
final_model = [ImageScale()] + (list(learn.model.children())[:-1] + [nn.Softmax()]) |
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 torch | |
from torch import nn | |
class MyAdaptiveMaxPool2d(nn.Module): | |
def __init__(self, sz=None): | |
super().__init__() | |
self.p = nn.MaxPool2d((10, 10), padding=0) | |
# why (10, 10)? Because input image size is 299, \ | |
# if you use 224, this should be (7, 7) | |
# if you want to know which number for other image size, |
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 torch | |
import torch.nn as nn | |
import numpy as np | |
from onnx_coreml import convert | |
from torch.autograd import Variable | |
import torch.onnx | |
import torchvision | |
import onnx | |
def check_onnx_compatible(model, model_name, sz, input_names, output_names): | |
dummy_input = Variable(torch.randn(3, sz, sz)).cuda() |
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
class DepthToSpace(nn.Module): | |
def __init__(self, block_size): | |
super(DepthToSpace, self).__init__() | |
self.block_size = block_size | |
self.block_size_sq = block_size*block_size | |
def forward(self, input): | |
output = input.permute(0, 2, 3, 1) | |
(batch_size, d_height, d_width, d_depth) = output.size() | |
s_depth = int(d_depth / self.block_size_sq) |
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
//alpha = 1; beta = 3.14 | |
private static long retrieval(double x) { | |
createPascal((int) x); | |
long sum = 0; | |
int level = (int) (x / beta) - 1; // first level | |
int k = level; | |
int remember = 0; | |
while (k >= 0) { | |
double delta = x - (level - k) * alpha - k * beta; // the last P will be processed first |