68 lines
1.6 KiB
Python
68 lines
1.6 KiB
Python
# This is a sample Python script.
|
|
|
|
# Press Umschalt+F10 to execute it or replace it with your code.
|
|
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
|
|
|
|
import numpy as np
|
|
|
|
|
|
def conv2d(z, h):
|
|
sub_shape = h.shape
|
|
view_shape = tuple(np.subtract(z.shape, sub_shape) + 1) + sub_shape
|
|
strides = z.strides + z.strides
|
|
sub_matrices = np.lib.stride_tricks.as_strided(z, view_shape, strides)
|
|
m = np.einsum('ij,klij->kl', h, sub_matrices)
|
|
|
|
return m
|
|
|
|
|
|
def state_expand(state, h, ovl_init=0):
|
|
hw, hh = h.shape
|
|
xw, xh = state.shape
|
|
ovl_w = int((hw-1)/2)
|
|
ovl_h = int((hh-1)/2)
|
|
z = np.ones((xw+2*ovl_w, xh+2*ovl_h))*ovl_init
|
|
z[ovl_w:ovl_w+xw, ovl_h:ovl_w+xh] = state
|
|
|
|
return z
|
|
|
|
|
|
def state_init_incremental(size):
|
|
state = np.zeros((size, size))
|
|
k = 1
|
|
m, n = np.shape(state)
|
|
for i in range(0, m):
|
|
for j in range(0, n):
|
|
state[i, j] = k
|
|
k += 1
|
|
|
|
return state
|
|
|
|
|
|
def state_init_constant(size, ic=0):
|
|
h = np.ones((size, size))*ic
|
|
return h
|
|
|
|
|
|
def h_init(s=1):
|
|
h = np.array([[1,1,1], [1,0,1], [1,1,1]])*s
|
|
return h
|
|
|
|
|
|
# Press the green button in the gutter to run the script.
|
|
if __name__ == '__main__':
|
|
state = state_init_incremental(5)
|
|
state = state_init_constant(5, 0)
|
|
h = h_init(1.0/8)
|
|
print (h)
|
|
for i in range(0,10,1):
|
|
state[0, 0] = 0
|
|
if i == 0:
|
|
state[0, 0] = 1
|
|
state_ex = state_expand(state, h)
|
|
state = 0.5*state + 0.5*conv2d(state_ex, h)
|
|
print(state)
|
|
|
|
|
|
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
|