会社でクソコード選手権をやって、お題は「半角小文字→半角大文字変換」。
無駄にAutoEncoderで変換モデルを作って、重みをハードコーディングするというクソコードを書いてみた。
# python with numpy
# AutoEncoder
import numpy as np
classes_count = 26
b0 = [1.9279792, 1.9710962, 1.9424249, 1.9897052]
w0 = [[2.309757, 0.7547041, -1.9422939, 3.0054893],
[1.6183586, 1.2462339, 2.4038823, 2.9719875],
[2.8043113, 2.8134964, 0.72427326, 0.3583362],
[0.20511475, -1.9713944, -1.9424013, -1.9897984],
[-1.9280654, -0.48014322, 0.45302996, -1.9897668],
[-1.9282027, -1.9710783, -1.9423842, -1.9899224],
[-1.928004, 2.557961, -1.9424723, -1.0925087],
[1.5173174, 1.5919207, 2.7471325, -1.9896569],
[3.5479364, -1.8404503, 1.8733085, 1.306567],
[3.5097265, 1.4852432, -1.9424261, -1.9638648],
[1.252702, 3.6489222, -1.9424807, 1.5639682],
[-1.9283006, 0.6655391, 1.1782262, 3.3881247],
[0.68708503, 3.14599, -1.7762166, -1.9898092],
[-1.928185, 2.7594948, -1.9424036, 3.0924065],
[1.8528862, -1.9709246, 3.3643696, -1.9895091],
[-1.9279757, -1.9710975, -1.9424291, 3.535092],
[-1.1504349, 4.002852, 1.5237356, 1.7777781],
[-1.6072675, -0.9366767, -1.9425055, -0.4903113],
[-1.9281032, -1.9712533, 3.1861846, -1.9887195],
[-1.3142217, 3.936389, 1.4064066, -1.989424],
[0.33986467, -1.8973224, 3.8670864, 1.327125],
[2.9535072, -1.9712133, -1.9424362, 1.1061064],
[-1.9281094, -1.9709018, 2.2743092, 2.863814],
[-1.9251063, 0.9448357, 3.495823, 0.9195421],
[0.94772035, -1.9711103, -0.04879162, 3.3886907],
[3.6051297, -0.7936871, 0.54140764, -1.9895425]]
b1 = [-3.2851863, -2.9647694, -3.6111405, 1.4170918, 1.3457484, 5.22797, -1.3684282, -3.2605622, -3.5486267, -3.6093266, -5.3173175, -3.2167525, -1.8680226, -4.928767, -4.661813, -1.2277495, -3.9108891, 2.198298, -1.5269516, -3.9566426, -3.8383358, -3.6490152, -2.919431, -5.1314454, -3.239594, -3.3298953]
w1 = [[1.2646724, 0.31275505, 1.6889216, 3.3750176, -2.8819494, -2.548596, -3.711818, 0.9762763, 2.403093, 3.1849651, -0.08144544, -2.8706245, 0.31729156, -3.5019252, 1.6947399, -2.6303012, -2.2230513, -1.7331878, -2.1491013, -2.2053993, -0.28057584, 3.3401384, -3.3331957, -2.509661, 0.5082899, 3.4180963],
[-0.048662465, -0.15332778, 1.5667863, -2.5473564, 0.911334, -2.29295, 4.056799, 0.7523658, -2.9911072, 1.0334494, 2.977426, 0.06812619, 3.2897608, 2.5707793, -3.0320544, -3.0847163, 2.7576232, 0.6176467, -3.3509967, 3.371486, -3.0566683, -2.9486477, -3.857905, 0.4766686, -3.0888762, -1.4443694],
[-3.0428183, 0.9807628, -0.3547033, -3.2143867, 2.1938412, -2.2032397, -2.8719687, 2.0071719, 0.99081326, -2.8648622, -3.0383508, 0.11498415, -2.492656, -3.3404727, 3.0969126, -3.3540788, 0.008456038, -2.5335064, 3.8224077, 0.91351014, 3.0225298, -2.7089133, 1.7429779, 3.006785, -0.47158808, 0.45485628],
[1.9420044, 1.009939, -0.9811265, -3.2010233, -3.4930437, -3.0813622, -1.4928288, -3.340037, -0.018877853, -2.820237, 0.6523254, 2.6365535, -3.1293674, 2.3563485, -3.1929681, 3.4789667, 0.32632697, 1.7273486, -2.6779497, -3.3148255, 0.24277075, 0.6410459, 2.4112182, -0.23278825, 2.685414, -3.2953565]]
w = [[np.array(b0), np.array(w0)], [np.array(b1), np.array(w1)]]
def dense_forward(x, w, b, activation):
return activation(x.dot(w) + b)
def relu(x):
return np.maximum(0, x)
def forward(x, w):
a1 = dense_forward(x, w[0][1], w[0][0], relu)
a2 = dense_forward(a1, w[1][1], w[1][0], relu)
return a2
def conv(ch):
x = ord(ch) - ord('a')
x = np.eye(classes_count)[x]
pred = forward(x, w)
return chr(np.argmax(pred) + ord('A'))
# てすとこーど
for i in range(97, 123):
print(conv(chr(i)), end="")
print()