def grade_Q8(): relu = nn.ReLU() flatten = nn.Flatten() f1 = nn.Linear("f1", 28 * 28, 100) f2 = nn.Linear("f2", 100, 10) x = nn.Input2d("images", 28, 28, 1) x = flatten(x) x = f2(relu(f1(x))) x.resolve(np.load("msimple_params.npz")) mnist_test = np.load("mnist_test.npz") images = mnist_test["images"][:1000] infer0 = x.compile(golden.Builder()) infer1 = x.compile(cpp.Builder()) label0 = infer0(images=images).argmax(axis=1) label1 = infer1(images=images).argmax(axis=1) return np.allclose(label0, label1)
def grade_Q4(): relu = nn.ReLU() flatten = nn.Flatten() f1 = nn.Linear("f1", 28 * 28, 32) f2 = nn.Linear("f2", 32, 10) x = nn.Input2d("images", 28, 28, 1) x = flatten(x) x = f2(relu(f1(x))) x.resolve(np.load("p5_params.npz")) mnist_test = np.load("mnist_test.npz") images = mnist_test["images"] labels = mnist_test["labels"] infer = x.compile(golden.Builder()) pred_labels = infer(images=images).argmax(axis=1) count = sum(labels == pred_labels) return count > 9500
def grade_Q4(): pool = nn.MaxPool2d(2, 2) relu = nn.ReLU() flatten = nn.Flatten() x = nn.Input2d("images", 28, 28, 1) c1 = nn.Conv2d("c1", 1, 8, 5) # 28->24 x = pool(relu(c1(x))) # 24->12 c2 = nn.Conv2d("c2", 8, 8, 5) # 12->8 x = pool(relu(c2(x))) # 8->4 f = nn.Linear("f", 8*4*4, 10) x = f(flatten(x)) x.resolve(np.load("p6_params.npz")) mnist_test = np.load("mnist_test.npz") images = mnist_test["images"] labels = mnist_test["labels"] infer = x.compile(golden.Builder()) pred_labels = infer(images = images).argmax(axis = 1) count = sum(labels == pred_labels) return count > 9500
def grade_Q10(): pool = nn.MaxPool2d(2, 2) relu = nn.ReLU() flatten = nn.Flatten() x = nn.Input2d("images", 28, 28, 1) c1 = nn.Conv2d("c1", 1, 8, 3) # 28->26 c2 = nn.Conv2d("c2", 8, 8, 3) # 26->24 x = pool(relu(c2(relu(c1(x))))) # 24->12 c3 = nn.Conv2d("c3", 8, 16, 3) # 12->10 c4 = nn.Conv2d("c4", 16, 16, 3) # 10->8 x = pool(relu(c4(relu(c3(x))))) # 8->4 f = nn.Linear("f", 16 * 4 * 4, 10) x = f(flatten(x)) x.resolve(np.load("mnist_params.npz")) mnist_test = np.load("mnist_test.npz") images = mnist_test["images"][:1000] infer0 = x.compile(golden.Builder()) infer1 = x.compile(cpp.Builder()) label0 = infer0(images=images).argmax(axis=1) label1 = infer1(images=images).argmax(axis=1) return np.allclose(label0, label1)
def grade_Q2(): flatten = nn.Flatten() x = flatten(nn.Input("x")) return is_same(x, 1, x=(10, 11, 12, 13))