def parse_tanh_table(self, tanh_table_path): with open(tanh_table_path, mode="rb") as file: raw_tanh = file.read() self.tanh_table = [] for i in range(10): entry = [] entry.append(Fxp(raw_tanh[2 * i * 16:(2 * i + 1) * 16])) entry.append(Fxp(raw_tanh[(2 * i + 1) * 16:(2 * i + 2) * 16])) self.tanh_table.append(entry)
def matrix_add(self, X, Y): result = [[Fxp([0] * 16) for y in range(len(X[0]))] for x in range(len(X))] for i in range(len(X)): for j in range(len(X[0])): result[i][j] += X[i][j] + Y[i][j] return result
def matrix_mul(self, X, Y): result = [[Fxp([0] * 16) for y in range(len(Y[0]))] for x in range(len(X))] for i in range(len(X)): for j in range(len(Y[0])): for k in range(len(Y)): result[i][j] += X[i][k] * Y[k][j] return result
def tanh(self, i): i = i.value() sign = i < 0 x = i if (sign): x = -i y = 0 if (x < 0.5): y = x elif (x < 1.2): y = x / 2 + 0.25 elif (x < 2.4): y = x / 8 + 0.7 else: y = 1 if (sign): y = -y return Fxp(Fxp2(y, signed=True, n_word=16, n_frac=8).bin())
def forward_prop(self, input_string): x_data = [] for char in input_string: try: x_data.append(self.charList[char]) except: print('unexpected char' + ' : ' + char) x_data.append(0) x2 = self.process_embedding(x_data) # self.print_layer(self.embedding_layer) # self.print_layer(x2) prev_out = [[Fxp([0] * 16)] * 32] #initialize hidden state for input in x2: self.expand(input) hidden = self.matrix_add( self.matrix_mul(self.transpose(input), self.rnn_w), self.rnn_b) # hidden = self.tanh_matrix(self.matrix_add(self.matrix_add(self.matrix_mul(self.rnn_w, input), # self.matrix_mul(self.rnn_u, hidden)), # self.rnn_b)) # print(len(hidden)) # print(len(hidden[0])) # print(len(prev_out)) # print(len(prev_out[0])) # print(len(self.rnn_u)) # print(len(self.rnn_u[0])) prev_out = self.tanh_matrix( self.matrix_add(hidden, self.matrix_mul(prev_out, self.rnn_u))) x3 = self.matrix_add(self.matrix_mul(prev_out, self.dense_w), self.dense_b) # for x in x3: # self.print_layer(x) # x4 = [] # for input in x3: # nxt = self.matrix_add(self.matrix_mul(self.transpose(input), self.dense_w), self.dense_b) # x4.append(nxt) print(x3[0][0].value()) x4 = self.sigmoid_matrix(x3) print(x4[0][0].value())
def sigmoid(self, fxp_num): h_fxp = Fxp2(1 / (1 + math.exp(-fxp_num.value())), signed=True, n_word=16, n_frac=8) return Fxp(h_fxp.bin())
def reLu(self, fxp_num): if fxp_num.value() < 0: return Fxp([0] * 16) else: return fxp_num
def get_byte_number(self, index): return Fxp(self.raw_binary[index * 16:(index + 1) * 16])
def get_char_vector(self, index): result = [Fxp([0] * 16)] * 39 result[index] = Fxp([0, 0, 0, 1] + [0] * 12) return result