@vzhou842/

A CNN from scratch, Part 1

Python

A Convolutional Neural Network implemented from scratch (using only numpy) in Python. https://github.com/vzhou842/cnn-from-scratch

fork
loading
Files
  • main.py
  • conv.py
  • maxpool.py
  • requirements.txt
  • softmax.py

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.7397116906574841","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"IyAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCiMgR2l0aHViOiBodHRwczovL2dpdGh1Yi5jb20vdnpob3U4NDIvY25uLWZyb20tc2NyYXRjaAojIFJlYWQgdGhlIGJsb2cgcG9zdDogaHR0cHM6Ly92aWN0b3J6aG91LmNvbS9ibG9nL2ludHJvLXRvLWNubnMtcGFydC0xLwojIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0KaW1wb3J0IG1uaXN0CmltcG9ydCBudW1weSBhcyBucApmcm9tIGNvbnYgaW1wb3J0IENvbnYzeDMKZnJvbSBtYXhwb29sIGltcG9ydCBNYXhQb29sMgpmcm9tIHNvZnRtYXggaW1wb3J0IFNvZnRtYXgKCiMgVGhlIG1uaXN0IHBhY2thZ2UgdGFrZXMgY2FyZSBvZiBoYW5kbGluZyB0aGUgTU5JU1QgZGF0YXNldCBmb3IgdXMhCiMgTGVhcm4gbW9yZSBhdCBodHRwczovL2dpdGh1Yi5jb20vZGF0YXB5dGhvbmlzdGEvbW5pc3QKIyBXZSBvbmx5IHVzZSB0aGUgZmlyc3QgMWsgdGVzdGluZyBleGFtcGxlcyAob3V0IG9mIDEwayB0b3RhbCkgaW4gdGhlIGludGVyZXN0IG9mIHRpbWUuCiMgRmVlbCBmcmVlIHRvIGNoYW5nZSB0aGlzIGlmIHlvdSB3YW50Lgp0ZXN0X2ltYWdlcyA9IG1uaXN0LnRlc3RfaW1hZ2VzKClbOjEwMDBdCnRlc3RfbGFiZWxzID0gbW5pc3QudGVzdF9sYWJlbHMoKVs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BsYWJlbCkgaW4gZW51bWVyYXRlKHppcCh0ZXN0X2ltYWdlcywgdGVzdF9sYWJlbHMpKToKICAjIERvIGEgZm9yd2FyZCBwYXNzLgogIF8sIGwsIGFjYyA9IGZvcndhcmQoaW0sIGxhYmVsKQogIGxvc3MgKz0gbAogIG51bV9jb3JyZWN0ICs9IGFjYwoKICAjIFByaW50IHN0YXRzIGV2ZXJ5IDEwMCBzdGVwcy4KICBpZiBpICUgMTAwID09IDk5OgogICAgcHJpbnQoCiAgICAgICdbU3RlcCAlZF0gUGFzdCAxMDAgc3RlcHM6IEF2ZXJhZ2UgTG9zcyAlLjNmIHwgQWNjdXJhY3k6ICVkJSUnICUKICAgICAgKGkgKyAxLCBsb3NzIC8gMTAwLCBudW1fY29ycmVjdCkKICAgICkKICAgIGxvc3MgPSAwCiAgICBudW1fY29ycmVjdCA9IDAK"},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -------------------------------
# Github: https://github.com/vzhou842/cnn-from-scratch
# Read the blog post: https://victorzhou.com/blog/intro-to-cnns-part-1/
# -------------------------------
import mnist
import numpy as np
from conv import Conv3x3
from maxpool import MaxPool2
from softmax import Softmax

# The mnist package takes care of handling the MNIST dataset for us!
# Learn more at https://github.com/datapythonista/mnist
# We only use the first 1k testing examples (out of 10k total) in the interest of time.
# Feel free to change this if you want.
test_images = mnist.test_images()[:1000]
test_labels = mnist.test_labels()[:1000]

conv = Conv3x3(8)                  # 28x28x1 -> 26x26x8
pool = MaxPool2()                  # 26x26x8 -> 13x13x8
softmax = Softmax(13 * 13 * 8, 10) # 13x13x8 -> 10

def forward(image, label):
  '''
  Completes a forward pass of the CNN and calculates the accuracy and
  cross-entropy loss.
  - image is a 2d numpy array
  - label is a digit
  '''
  # We transform the image from [0, 255] to [-0.5, 0.5] to make it easier
  # to work with. This is standard practice.
  out = conv.forward((image / 255) - 0.5)
  out = pool.forward(out)
  out = softmax.forward(out)

  # Calculate cross-entropy loss and accuracy. np.log() is the natural log.
  loss = -np.log(out[label])
  acc = 1 if np.argmax(out) == label else 0

  return out, loss, acc

print('MNIST CNN initialized!')

loss = 0
num_correct = 0
for i, (im, label) in enumerate(zip(test_images, test_labels)):
  # Do a forward pass.
  _, l, acc = forward(im, label)
  loss += l
  num_correct += acc

  # Print stats every 100 steps.
  if i % 100 == 99:
    print(
      '[Step %d] Past 100 steps: Average Loss %.3f | Accuracy: %d%%' %
      (i + 1, loss / 100, num_correct)
    )
    loss = 0
    num_correct = 0