You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 KiB

Yong (永) Brainsim

Tribute to BRAINSIM, a neural network on the Commodore 64

My discussion (with @nickm) on BRAINSIM can be found in the archive of the Critical Code Studies Working Group 2022: https://wg.criticalcodestudies.com/index.php?p=/discussion/117/brainsim-neural-network-on-a-commodore-64-2022-code-critique#latest

In [160]:
import numpy as np
In [161]:
# a representation of "永" in matrix notation
yong_grid = [[0,0,0,0,1,1,0,0,0,0],
             [0,0,1,1,1,1,0,0,1,0],
             [0,0,0,0,0,1,0,1,0,0],
             [0,0,0,0,0,1,1,0,0,0],
             [0,1,1,1,1,1,1,0,0,0],
             [0,0,0,1,0,1,1,0,0,0],
             [0,0,1,0,0,1,0,1,0,0],
             [0,1,0,0,0,1,0,0,1,0],
             [0,0,0,1,0,1,0,0,0,0],
             [0,0,0,0,1,1,0,0,0,0]]
In [162]:
print(np.matrix(yong_grid))
[[0 0 0 0 1 1 0 0 0 0]
 [0 0 1 1 1 1 0 0 1 0]
 [0 0 0 0 0 1 0 1 0 0]
 [0 0 0 0 0 1 1 0 0 0]
 [0 1 1 1 1 1 1 0 0 0]
 [0 0 0 1 0 1 1 0 0 0]
 [0 0 1 0 0 1 0 1 0 0]
 [0 1 0 0 0 1 0 0 1 0]
 [0 0 0 1 0 1 0 0 0 0]
 [0 0 0 0 1 1 0 0 0 0]]
In [163]:
from matplotlib import pyplot as plt
im = plt.imshow(yong_grid, cmap="copper_r")
plt.show()
In [164]:
import seaborn as sns; sns.set()
ax = sns.heatmap(yong_grid, annot=True, fmt="d")
plt.show()
In [165]:
# access item in matrix
# notation is [column][row]
print(yong_grid[0][4])
print(yong_grid[0][5])
1
1
In [166]:
twenty_p_one = [1,1,0,0,0,0,0,0,0,0]
twenty_p_zero = [0,0,1,1,1,1,1,1,1,1]
In [167]:
import random
import math
z_count = 0
one_count = 0
import math
for i in range(10):
    for j in range(10):
        if yong_grid[i][j] == 0:
            # reassign
            yong_grid[i][j] = random.choice(twenty_p_one)  
        else:
            yong_grid[i][j] = random.choice(twenty_p_zero)         
In [168]:
ax = sns.heatmap(yong_grid, annot=True, fmt="d")
In [169]:
# lists for 5%, 10% and 15% randomization
five_p_one = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
five_p_zero = [0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
ten_p_one = [1,0,0,0,0,0,0,0,0,0]
ten_p_zero = [0,1,1,1,1,1,1,1,1,1]
fifteen_p_one = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
fifteen_p_zero = [0,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
In [170]:
# random image generator
# generate heatmap and save in folder 
def randImg(list_zero, list_one):
    z_count = 0
    one_count = 0
    for i in range(10):
        for j in range(10):
            if yong_grid[i][j] == 0:
                yong_grid[i][j] = random.choice(list_zero)  
            else:
                yong_grid[i][j] = random.choice(list_one) 
    ax = sns.heatmap(yong_grid, annot=True, fmt="d")
    # clear randomization; reassign var yong_grid hold values of original text input matrix
In [171]:
# call function with 5%
# clear randomization; reassign var yong_grid hold values of original text input matrix
yong_grid = [[0,0,0,0,1,1,0,0,0,0],
             [0,0,1,1,1,1,0,0,1,0],
             [0,0,0,0,0,1,0,1,0,0],
             [0,0,0,0,0,1,1,0,0,0],
             [0,1,1,1,1,1,1,0,0,0],
             [0,0,0,1,0,1,1,0,0,0],
             [0,0,1,0,0,1,0,1,0,0],
             [0,1,0,0,0,1,0,0,1,0],
             [0,0,0,1,0,1,0,0,0,0],
             [0,0,0,0,1,1,0,0,0,0]]
randImg(five_p_zero, five_p_one)
In [172]:
# call function with 10%
yong_grid = [[0,0,0,0,1,1,0,0,0,0],
             [0,0,1,1,1,1,0,0,1,0],
             [0,0,0,0,0,1,0,1,0,0],
             [0,0,0,0,0,1,1,0,0,0],
             [0,1,1,1,1,1,1,0,0,0],
             [0,0,0,1,0,1,1,0,0,0],
             [0,0,1,0,0,1,0,1,0,0],
             [0,1,0,0,0,1,0,0,1,0],
             [0,0,0,1,0,1,0,0,0,0],
             [0,0,0,0,1,1,0,0,0,0]]
randImg(ten_p_zero, ten_p_one)
In [173]:
# fifteen percent
yong_grid = [[0,0,0,0,1,1,0,0,0,0],
             [0,0,1,1,1,1,0,0,1,0],
             [0,0,0,0,0,1,0,1,0,0],
             [0,0,0,0,0,1,1,0,0,0],
             [0,1,1,1,1,1,1,0,0,0],
             [0,0,0,1,0,1,1,0,0,0],
             [0,0,1,0,0,1,0,1,0,0],
             [0,1,0,0,0,1,0,0,1,0],
             [0,0,0,1,0,1,0,0,0,0],
             [0,0,0,0,1,1,0,0,0,0]]
randImg(fifteen_p_zero, fifteen_p_one)
In [ ]:
# TODO
# clear randomization; reassign var yong_grid hold values of original text input matrix
# add annotation to specify level of randomization 
# run 100 times and save pictures