def testRelu(self): a1 = Array.fromList([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]) r1 = Relu() a2 = r1.forward(a1) a3 = Array.fromList([0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5]) self.assertTrue(a2 == a3) a4 = Array.fromList([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) a5 = r1.backward(a4) a6 = Array.fromList([0, 0, 0, 0, 0, 0, 7, 8, 9, 10, 11]) self.assertTrue(a5 == a6)
def load_param(filename): # N=InputLayerDim, H=MiddleLayerDim, C=OutputLayerDim N = 1024 H = 256 C = 23 # Open File f = open(filename, 'r') # Split into each line data = f.read().split("\n") # index idx = 0 row_w1 = [[0] * N for x in range(H)] row_w2 = [[0] * H for x in range(H)] row_w3 = [[0] * H for x in range(C)] idx_w1 = idx_w2 = idx_w3 = idx_b1 = idx_b2 = idx_b3 = 0 rowlist = [256, 1, 256, 1, 23, 1] for line_str in data: line = line_str.split(" ") if line == ['']: break line = list(map(float, line)) # W1 = H*N # 0~H-1 # b1 = H # H # W2 = H*H # H+1~2H # b2 = H # 2H+1 # W3 = C*H # 2H+3 ~ 2H+C+3 # b3 = C # 2H+C+3 if (idx < H): row_w1[idx_w1] = line idx_w1 += 1 elif (idx == H): row_b1 = line elif (idx >= H + 1 and idx < 2 * H + 1): row_w2[idx_w2] = line idx_w2 += 1 elif (idx == 2 * H + 1): row_b2 = line elif (idx >= 2 * H + 2 and idx < 2 * H + C + 2): row_w3[idx_w3] = line idx_w3 += 1 elif (idx == 2 * H + C + 2): row_b3 = line idx += 1 W1 = Array.fromList(row_w1) b1 = Array.fromList(row_b1) W2 = Array.fromList(row_w2) b2 = Array.fromList(row_b2) W3 = Array.fromList(row_w3) b3 = Array.fromList(row_b3) return W1, W2, W3, b1, b2, b3
def testArgmax(self): a1 = Array.fromList([1, 2, 3, 4, 5]) ans = argmax(a1) self.assertTrue(ans == 5)
def testCrossEntropyError(self): a1 = Array.fromList([1, 2, 3, 4, 5]) t = 2 e = cross_entropy_error(a1, t) print(e)
def testSoftmax(self): a1 = Array.fromList([1, 2, 3, 4, 5]) a2 = softmax(a1) self.assertTrue(sum(a2.rows) == 1) self.assertTrue(a1.shape() == a2.shape())