<jupyter_start><jupyter_text>2D Zero-padding  미리 구현<jupyter_code>import random

H, W = 4, 5
data = list()
for _ in range(H):
    data.append([random.randint(0,5) for _ in range(W)])

for data_row in data:
    print(data_row)
print(f"H x W of data = {H} x {W}\n")

# Zero-padding
pad = 3
padded_data = list()

padding_data1 = list()
for _ in range(pad):
    padding_data1.append([0]*(W+pad*2))

padding_data2 = list()
for data_row in data:
    data = [0]*pad + data_row + [0]*pad
    padding_data2.append(data)

padded_data = padding_data1 + padding_data2 + padding_data1

for data in padded_data:
    print(data)<jupyter_output>[5, 5, 2, 4, 0]
[5, 1, 2, 3, 3]
[5, 3, 0, 1, 2]
[0, 2, 2, 3, 5]
H x W of data = 4 x 5

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 5, 5, 2, 4, 0, 0, 0, 0]
[0, 0, 0, 5, 1, 2, 3, 3, 0, 0, 0]
[0, 0, 0, 5, 3, 0, 1, 2, 0, 0, 0]
[0, 0, 0, 0, 2, 2, 3, 5, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]<jupyter_text>복습 구현<jupyter_code>import random

H , W = 4, 4

# generate random data
data = list()
for _ in range(H):
    data.append([random.randint(0,5) for _ in range(W)])
        
for data_row in data:
    print(data_row)
print(f"H x W of data = {H} x {W}\n")

# Zero-padding
pad = 2
padded_data = list()

for _ in range(2):
    padded_data.append([0]*(W+pad*2))
for data_row in data:
    padded_data.append([0]*pad + data_row + [0]*pad)
for _ in range(2):
    padded_data.append([0]*(W+pad*2))
    
data = padded_data
for data_row in data:
    print(data_row)
print(f"H x W of data = {H} x {W}\n")<jupyter_output>[4, 1, 1, 2]
[5, 5, 5, 2]
[4, 2, 4, 1]
[2, 5, 4, 5]
H x W of data = 4 x 4

[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 4, 1, 1, 2, 0, 0]
[0, 0, 5, 5, 5, 2, 0, 0]
[0, 0, 4, 2, 4, 1, 0, 0]
[0, 0, 2, 5, 4, 5, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
H x W of data = 4 x 4<jupyter_text>2D Max Pooling with Zero-padding    $$ p[i,j] = max(W_{i,j}) $$    $$ L_{o} = L_{i} + 2p - L_{f} + 1 $$<jupyter_code>import random

# set parameters
H , W = 10, 15
pad = 2
pool_size = 3

# generate random image
data = list()
for _ in range(H):
    data.append([random.randint(0,5) for _ in range(W)])

# Zero-padding
padded_data = list()

for _ in range(pad):
    padded_data.append([0]*(W+pad*2))
for row in data:
    padded_data.append([0]*pad + row + [0]*pad)
for _ in range(pad):
    padded_data.append([0]*(W+pad*2))
    
data = padded_data
H , W = len(data), len(data[0])
for data_row in data:
    print(data_row)
print(f"H x W of data = {H} x {W}\n")

# Max pooling
pooled = list()
valid_H = H - pool_size + 1  # 2 
valid_W = W - pool_size + 1  # 3 

for h in range(valid_H):
    pool_vec = list()
    for w in range(valid_W):
        window = list()
        
        for inner_h in range(pool_size):
            window.append(data[h+inner_h][w:w+pool_size])
        
        M = window[0][0]
        for window_row in window:
            for window_val in window_row:
                if M < window_val:
                    M = window_val
        pool_vec.append(M)
    pooled.append(pool_vec)

for row in pooled:
    print(row)

H , W = len(pooled), len(pooled[0])
print(f"H x W of data = {H} x {W}")<jupyter_output>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 2, 4, 3, 2, 2, 2, 3, 2, 4, 2, 5, 4, 5, 1, 0, 0, 0]
[0, 0, 4, 5, 2, 5, 2, 1, 4, 5, 0, 4, 1, 3, 2, 1, 0, 0, 0]
[0, 0, 3, 2, 4, 5, 3, 1, 0, 4, 0, 1, 5, 5, 1, 3, 3, 0, 0]
[0, 0, 5, 3, 3, 0, 2, 0, 1, 5, 0, 3, 1, 5, 0, 0, 3, 0, 0]
[0, 0, 0, 1, 4, 1, 1, 5, 0, 4, 0, 5, 3, 2, 4, 5, 2, 0, 0]
[0, 0, 1, 4, 1, 1, 2, 0, 3, 3, 5, 5, 3, 3, 2, 3, 1, 0, 0]
[0, 0, 5, 0, 0, 4, 5, 4, 1, 1, 0, 0, 4, 1, 2, 2, 5, 0, 0]
[0, 0, 1, 4, 2, 0, 1, 5, 3, 3, 4, 3, 1, 5, 4, 2, 5, 0, 0]
[0, 0, 5, 0, 1, 3, 5, 2, 4, 3, 3, 0, 3, 4, 0, 4, 4, 0, 0]
[0, 0, 2, 4, 1, 3, 5, 0, 0, 1, 2, 2, 3, 2, 0, 0, 5, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
H x W of data = 14 x 19

[2, 4, 4, 4, 3, 2, 3, 3, 4, 4, 5, 5, 5, 5, 5, 1, 0]
[4, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 1, 0]
[4, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3]
[5, 5, [...]<jupyter_text>2D Correlation with Zero-padding    $$ \vec{x} \otimes \vec{f} [i,j] =    \sum_{u=-k}^{k} \sum_{v=-k}^{k}    x[i+u, j+v] f[u, v]$$    $$ L_{o} = L_{i} + 2p - L_{f} + 1 $$<jupyter_code>import random

# parameter setting
H , W = 4, 5
pad = 2
filter_size = 5

# generate random image
data = list()
for _ in range(H):
    data.append([random.randint(0,5) for _ in range(W)])

for data_row in data:
    print(data_row)
print(f"H x W of data = {H} x {W}\n")

# Zero-padding
padded_data = list()
for _ in range(pad):
    padded_data.append([0]*(W+pad*2))
for data_row in data:
    padded_data.append([0]*pad + data_row + [0]*pad)
for _ in range(pad):
    padded_data.append([0]*(W+pad*2))
    
data = padded_data
H , W = len(data), len(data[0])
for data_row in data:
    print(data_row)
print(f"H x W of data = {H} x {W}\n")

# initialize correlation
corr_filter = list()
for _ in range(filter_size):
    corr_filter.append([random.randint(0,10) for _ in range(filter_size)])
for row in corr_filter:
    print(row)
print("Correlation filter\n")

corr_mat = list()
valid_H = H - filter_size + 1
valid_W = W - filter_size + 1

for h in range(valid_H):
    corr_vec = list()
    for w in range(valid_W):
        window = list()
        
        for inner_h in range(filter_size):
            window.append(data[h+inner_h][w:w+filter_size])
        
        window_flatten = list()
        filter_flatten = list()
        for window_row, filter_row in zip(window, corr_filter):
            for window_val, filter_val in zip(window_row, filter_row):
                window_flatten.append(window_val)
                filter_flatten.append(filter_val)
        
        corr_sum = 0
        for w, co in zip(window_flatten, filter_flatten):
            corr_sum += w*co
        corr_vec.append(corr_sum)
    corr_mat.append(corr_vec)

data = corr_mat
H , W = len(data), len(data[0])
for data_row in data:
    print(data_row)
print(f"H x W of data = {H} x {W}\n")
import random

H , W = 4, 5
pad = 2
filter_size = 5

# random img
data = list()
for _ in range(H):
    data.append([random.randint(0,9) for _ in range(W)])
for data_row in data:
    print(data_row)
print(f"H x W of data = {H} x {W}\n")

# zero-padding
padded_data = list()
for _ in range(pad):
    padded_data.append([0]*(W+pad*2))
for data_row in data:
    padded_data.append([0]*pad + data_row + [0]*pad)
for _ in range(pad):
    padded_data.append([0]*(W+pad*2))
    
data = padded_data
H , W = len(data), len(data[0])
for data_row in data:
    print(data_row)
print(f"H x W of data = {H} x {W}\n")

# corr
corr_filter= list()
for _ in range(filter_size):
    corr_filter.append([random.randint(0,9) for _ in range(filter_size)])
for row in corr_filter:
    print(row)
print("Correlation filter\n")

corr_mat = list()
valid_H = H - filter_size + 1
valid_W = W - filter_size + 1

for h in range(valid_H):
    corr_vec = list()
    for w in range(valid_W):
        window = list()
        
        for inner_h in range(filter_size):
            window.append(data[h+inner_h][w:w+filter_size])
            
        corr_sum = 0
        for window_row, filter_row in zip(window, corr_filter):
            for window_val, filter_val in zip(window_row, filter_row):
                corr_sum += window_val * filter_val
        corr_vec.append(corr_sum)
    corr_mat.append(corr_vec)

data = corr_mat
H , W = len(data), len(data[0])
for data_row in data:
    print(data_row)
print(f"H x W of data = {H} x {W}\n")<jupyter_output>[5, 2, 7, 4, 8]
[5, 9, 5, 1, 9]
[9, 7, 2, 7, 9]
[6, 3, 6, 3, 9]
H x W of data = 4 x 5

[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 5, 2, 7, 4, 8, 0, 0]
[0, 0, 5, 9, 5, 1, 9, 0, 0]
[0, 0, 9, 7, 2, 7, 9, 0, 0]
[0, 0, 6, 3, 6, 3, 9, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
H x W of data = 8 x 9

[3, 6, 9, 4, 7]
[6, 2, 5, 8, 1]
[1, 1, 9, 5, 0]
[7, 8, 5, 3, 8]
[8, 7, 3, 7, 9]
Correlation filter

[241, 327, 554, 374, 263]
[313, 480, 584, 443, 388]
[407, 387, 498, 485, 395]
[288, 277, 402, 336, 263]
H x W of data = 4 x 5
{'fraction_non_alphanumeric': 0.19305804907241172, 'fraction_numerical': 0.09802513464991024, 'mean_word_length': 2.7571942446043165, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '442095', 'license': 'MIT', 'n_tokens_mistral': 4647, 'n_tokens_neox': 3932, 'n_words': 1325, 'path': 'Python Practice/20_2D Zero-padding.ipynb', 'repo_name': 'kimbyeolhee/TIL', 'size': '8355'}