等価変換がうまくいきません.原因が分かる方がいらっしゃれば教えていただきたいです.
python
1#coding:utf-8 2 3import sys 4import numpy 5import csv 6 7class Sandglass: 8 9 def __init__(self, n_input, n_hidden): 10 self.hidden_weight = numpy.random.randn(n_hidden, n_input + 1) 11 self.output_weight = numpy.random.randn(n_input, n_hidden + 1) 12 13 def fit(self, X, epsilon, epoch): 14 self.error = numpy.zeros(epoch) 15 N = X.shape[0] 16 for epo in range(epoch): 17 print (u'epoch: %d' % epo) 18 count = 0 19 for x in X: 20 count += 1 21 self.__update_weight(x, epsilon, count) 22 23 self.error[epo] = self.__calc_error(X) 24 with open('error15.csv','a',newline='') as f: 25 writer = csv.writer(f) 26 writer.writerow(self.error) 27 f.close() 28 29 def encode(self, X): 30 Z = numpy.zeros((X.shape[0], self.hidden_weight.shape[0])) 31 Y = numpy.zeros(X.shape) 32 for (i, x) in enumerate(X): 33 z, y = self.__forward(x) 34 Z[i, :] = z 35 Y[i, :] = y 36 37 return (Z, Y) 38 39 def __forward(self, x): 40 z = self.hidden_weight.dot(numpy.hstack((1, x))) 41 y = self.output_weight.dot(numpy.hstack((1, z))) 42 43 return (z, y) 44 45 def __update_weight(self, x, epsilon, c): 46 z, y = self.__forward(x) 47 48 # update output_weight 49 output_delta = y - x 50 self.output_weight -= epsilon * output_delta.reshape(-1, 1) * numpy.hstack((1, z)) 51 52 # update hidden_weight 53 hidden_delta = self.output_weight[:, 1:].T.dot(output_delta) 54 self.hidden_weight -= epsilon * hidden_delta.reshape(-1, 1) * numpy.hstack((1, x)) 55 56 if (c == 1): 57 with open('inout21.csv','a',newline='') as f: 58 writer = csv.writer(f) 59 writer.writerow(x) 60 writer.writerow(y) 61 f.close() 62 63 def __calc_error(self, X): 64 N = X.shape[0] 65 err = 0.0 66 for x in X: 67 _, y = self.__forward(x) 68 err += (y - x).dot(y - x) / 2.0 69 70 return err 71 72def ReadDatafile(args): 73 return numpy.loadtxt(args, delimiter=',', dtype='float') 74 75def min_max(x, axis=None): 76 # 正規化(0,1) 77 min = x.min(axis=axis, keepdims=True) 78 max = x.max(axis=axis, keepdims=True) 79 result = (x-min)/(max-min) 80 return result 81 82if __name__ == '__main__': 83 84 args = sys.argv 85 86 print ('read data...') 87 train = ReadDatafile(args[1]) 88 x = min_max(train) 89# x = train 90 input_size = x.shape[1] 91 hidden_size = 2 92 epsilon = 0.01 93 epoch = 10000 94 stride = 2 95 96 print ('Sandglass init...') 97 sand = Sandglass(input_size, hidden_size) 98 99 print ('train...') 100 sand.fit(x[::stride], epsilon, epoch) 101 102 test = ReadDatafile(args[2]) 103 y = min_max(test) 104# y = test 105 106 print ('encode...') 107 Z, Y = sand.encode(y[::stride]) 108 with open('out21.csv','a',newline='') as f: 109 writer = csv.writer(f) 110 writer.writerow(Y) 111 f.close()
元データと学習後の出力値
[0.147804867 0.300808975 0.468231146 0.657795291 0.0211587]
[0.164786818 0.290982418 0.467763505 0.661981791 0.026323475]
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/14 09:12
2018/02/14 09:39
2018/02/14 10:08
2018/02/14 10:15
2018/02/14 10:20