def f11(): tr_img, tr_lab = choose(train_images, train_labels, 100) te_img, te_lab = choose(test_images, test_labels, 50) trX, trT = dt.addChannel( tr_img.astype(np.float32) / 255, dt.onehot(tr_lab)) teX, teT = dt.addChannel( te_img.astype(np.float32) / 255, dt.onehot(te_lab)) # teX = teT = None # print(trX.shape, trT.shape) # print(trX.dtype) # return ################################################ ng = NodeGraphAdaptive() ng.lrMin = np.float32(1.0E-8) ng.lrInit = np.float32(0.0001) ################################################ # ng = NodeGraphBatch() ################################################ # sset(28,28) ---> conv1((3,3), 32) ---> act1 ---> mp1((2,2)) ---> conv2((3,3), 32) ---> act2 ---> mp2((2,2)) # ---> flat ---> den1(100) ---> act1 ---> den2(10) ---> act2 ---> loss sset = ng.add(StartSet2D((trX.shape[1], trX.shape[2]), trX.shape[3])) conv1 = ng.add(Conv2D((3, 3), 32), sset) act1 = ng.add(Relu2D(), conv1) mp1 = ng.add(MaxPool2D((2, 2)), act1) conv2 = ng.add(Conv2D((3, 3), 32), mp1) act2 = ng.add(Relu2D(), conv2) mp2 = ng.add(MaxPool2D((2, 2)), act2) flat = ng.add(Flat2D(), mp2) den1 = ng.add(Dense1D(100), flat) act3 = ng.add(Sigmoid1D(), den1) den2 = ng.add(Dense1D(10), act3) act4 = ng.add(Softmax1D(), den2) loss = ng.add(OneHotCce1D(), act4) ng.compile() ng.epochMax = 50 start = time.time() # ng.fit(trX, trT, teX, teT, batchSize=None, verbose=0) ng.fit(trX, trT) print(f'elapsed: {time.time() - start}') print(f'=== graph info ===\n{ng.graphInfo("detail")}') print(f'=== summary ===\n{ng.summary()}') plt.subplot(221) plt.plot(ng.trAccuracy, label='train accuracy') if teX is not None: plt.plot(ng.teAccuracy, label='test accuracy') plt.legend() plt.subplot(222) plt.plot(ng.trLosses, label='train losses') if teX is not None: plt.plot(ng.teLosses, label='test losses') plt.legend() plt.subplot(223) plt.plot(ng.appLrs, label='learning rates') plt.legend() plt.tight_layout() plt.show()
def f01(): trX = dt.addChannel(train_images / 255).astype(np.float32) trT = dt.addChannel(dt.onehot(train_labels)) teX = dt.addChannel(test_images / 255).astype(np.float32) teT = dt.addChannel(dt.onehot(test_labels)) # sset(784) ---> flat ---> den1(784>100) ---> act1() ---> den2(100>10) ---> act2 ---> cce ng = NodeGraphAdaptiveMinibatch() sset = ng.add(StartSet2D((w, h))) flat = ng.add(Flat2D(), sset) den1 = ng.add(Dense1D(128), flat) act1 = ng.add(Relu1D(), den1) den2 = ng.add(Dense1D(10), act1) act2 = ng.add(Softmax1D(), den2) lossFunc = ng.add(OneHotCce1D(), act2) ng.compile() ng.lrInit = np.float32(0.0001) ng.lrMin = np.float32(1.0e-8) ng.epochMax = 50 start = time.time() # ng.fit(trX, trT, verbose=2, batchSize=32) # teX = teT = None ng.fit(trX, trT, teX, teT, verbose=2, batchSize=6000) print(f'elapsed: {time.time() - start}') print(f'=== graph info ===\n{ng.graphInfo("detail")}') print(f'=== summary ===\n{ng.summary()}') plt.subplot(221) plt.plot(ng.trAccuracy, label='train accuracy') if teX is not None: plt.plot(ng.teAccuracy, label='test accuracy') plt.legend() plt.subplot(223) plt.plot(ng.trLosses, label='train losses') if teX is not None: plt.plot(ng.teLosses, label='test losses') plt.legend() plt.subplot(224) plt.plot(ng.appLrs, label='learning rates') plt.legend() plt.tight_layout() plt.show()
def dim2_quarter_plane_sigmoid(): print( f'******************** {inspect.currentframe().f_code.co_name} ********************' ) grid = 30 trainX = meshgrid_linear(-1, 1, grid, -1, 1, grid).astype(np.float32) n1, n2 = np.array([1, 0]), np.array([0, 1]) trainTB = np.logical_and( np.sum(trainX * n1, axis=1) >= 0, np.sum(trainX * n2, axis=1) >= 0) trainT = trainTB.astype(int).reshape((-1, 1)) trainX, trainT = dt.addChannel(trainX, trainT) # sset(2) ---> den1(2>2) ---> act1(Sigmoid) ---> den2(2>1) ---> act2(Sigmoid) ---> bce(1) ng = NodeGraphAdaptive() sset = ng.add(StartSet1D(2, name='sset')) den1 = Dense1D(2, name='den1') ng.add(den1, sset) act1 = ng.add(Sigmoid1D('act1'), den1) # act1 = ng.add(Relu('act1'), den1) den2 = ng.add(Dense1D(1, name='den2'), act1) act2 = ng.add(Sigmoid1D('act2'), den2) bce = ng.add(ZeroOneBce1D('bce'), act2) den1.initTransf = np.array([[[.3, -.5, 30.2], [-3, 30.2, -.5]]], np.float32) den2.initTransf = np.array([[[-28.3, 19.0, 19.0]]], np.float32) # # relu # den1.initW = np.array([[0.04065937, 0.83962917], [6.2536874, 8.23629256], [-8.99669535, 0.01755958]], np.float32) # den2.initW = np.array([[-7.14937919, -10.94093724, 8.40722765]], np.float32).T ng.lossMax = np.float32(.00001) ng.compile() ng.epochMax = 1000 ng.lrInit = np.float32(0.1) ng.fit(trainX, trainT) print(f'------------ graph info ---------\n{ng.graphInfo()}') print(f'------------ summary ------------\n{ng.summary()}') quad = trainX[trainTB] comp = trainX[~trainTB] plt.subplot(221) plt.title('trainX') plt.plot(quad[:, 0, 0], quad[:, 1, 0], '+k') plt.scatter(comp[:, 0, 0], comp[:, 1, 0], s=1, c='k') plt.subplot(222) plt.title('activation') ng.predict(quad) plt.plot(act1.prY[:, 0, 0], act1.prY[:, 1, 0], '+k') ng.predict(comp) plt.scatter(act1.prY[:, 0, 0], act1.prY[:, 1, 0], s=1, c='k') draw_line(den2.B[0], den2.W[:, :, 0], 'r', '-', -1, 1, -1, 1) plt.subplot(223) x0, x1, y0, y1 = -2, 2, -2, 2 X, Y = np.meshgrid(np.linspace(x0, x1, 30), np.linspace(y0, y1, 30)) Z = np.zeros_like(X) for i in range(Z.shape[0]): for j in range(Z.shape[1]): p = dt.addChannel(np.array([[X[i, j], Y[i, j]]])).astype(np.float32) Z[i, j] = ng.predict(p)[0, 0, 0] cs = plt.contour(X, Y, Z, [.5]) plt.clabel(cs, inline=True) plt.subplot(224) plt.title('loss') plt.plot(ng.trLosses) plt.tight_layout() plt.show()
def xor2(index=0): print( f'******************** {inspect.currentframe().f_code.co_name} ********************' ) trainX = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], np.float32) trainT = np.array([[0, 1], [1, 0], [1, 0], [0, 1]]) testX = np.array([[.5, .5]], np.float32) testT = np.array([[0, 1]]) trainX, trainT = dt.addChannel(trainX), dt.addChannel(trainT) testX, testT = dt.addChannel(testX), dt.addChannel(testT) def f0(): # sset(2) ---> fset1(2>2) ---> act1(Sigmoid) ---> fset2(2>2) ---> act2(Softmax) ---> cce(2) print( f'******************** {inspect.currentframe().f_code.co_name} ********************' ) ####### Batch ############### # ng = NodeGraphBatch() ####### Adaptive ############### ng = NodeGraphAdaptive() ####### Common ############### sset = ng.add(StartSet1D(2, name='sset')) fset1 = ng.add(Dense1D(2, name='fset1'), sset) act1 = ng.add(Sigmoid1D('act1'), fset1) fset2 = ng.add(Dense1D(2, name='fset2'), act1) act2 = ng.add(Softmax1D('act2'), fset2) lossFunc = ng.add(OneHotCce1D('cce'), act2) ng.epochMax = 10000 ng.compile() start = time.time() # ng.fit(trainX, trainT) ng.fit(trainX, trainT, testX, testT) print(f'elapsed: {time.time() - start}') print(f'trainY:\n{lossFunc.trY[:,:,0]}') return ng def f1(): # sset(2) ----> fset11(2->2) --> act11(Sigmoid) --> fset12(2->1) ----> con((1,2)>(1,2)) ---> flat ---> act2(Softmax) ---> cce(2) # \ / # --> fset21(2->2) --> act21(Sigmoid) --> fset22(2->1) - print( f'******************** {inspect.currentframe().f_code.co_name} ********************' ) ####### Batch ############### # ng = NodeGraphBatch() ####### Adaptive ############### ng = NodeGraphAdaptive() ####### Common ############### sset = ng.add(StartSet1D(2, name='sset')) fset11 = ng.add(Dense1D(2, name='fset11'), sset) act11 = ng.add(Sigmoid1D('act11'), fset11) fset12 = ng.add(Dense1D(1, name='fset12'), act11) fset21 = ng.add(Dense1D(2, name='fset21'), sset) act21 = ng.add(Sigmoid1D('act21'), fset21) fset22 = ng.add(Dense1D(1, name='fset22'), act21) con = ng.add(Conv1D(1, 2, biased=True, name='con'), fset12, fset22) flat = ng.add(Flat1D('flt'), con) act2 = ng.add(Softmax1D('act2'), flat) # lossFunc = ng.add(Mse1D('mse'), act2) lossFunc = ng.add(OneHotCce1D('cce'), act2) ng.epochMax = 10000 ng.compile() # trainT = np.array([[0.3, .7], [1, 0], [1, 0], [0.3, .7]], float) start = time.time() # ng.fit(trainX, trainT) ng.fit(trainX, trainT, testX, testT, batchSize=2) print(f'elapsed: {time.time() - start}') print(f'trainY:\n{lossFunc.trY[:,:,0]}') return ng def f2(): # sset1(1) ----> fset11(1->2) --> act11(Sigmoid) ------> con((2, 2)>(1,2)) ---> flt ---> act2(Softmax) ---> cce(2) # / # sset2(1) ----> fset21(1->2) --> act21(Sigmoid) -- print( f'******************** {inspect.currentframe().f_code.co_name} ********************' ) ####### Batch ############### # ng = NodeGraphBatch() # ng.lr = .001 ####### Adaptive ############### ng = NodeGraphAdaptive() ng.lrMin = .001 ####### Common ############### sset1 = ng.add(StartSet1D(1, name='sset1')) sset2 = ng.add(StartSet1D(1, name='sset2')) fset11 = ng.add(Dense1D(3, name='fset11'), sset1) act11 = ng.add(Sigmoid1D('act11'), fset11) fset21 = ng.add(Dense1D(3, name='fset21'), sset2) act21 = ng.add(Sigmoid1D('act21'), fset21) con = ng.add(Conv1D(3, 2, biased=True, padding=0, name='con'), act11, act21) flt = ng.add(Flat1D('flt'), con) # flt = ng.add(Flat1D('flt'), fset12, fset22) act2 = ng.add(Softmax1D('act2'), flt) # lossFunc = MeanSquare('loss') lossFunc = ng.add(OneHotCce1D('cce'), act2) ng.epochMax = 10000 ng.compile() start = time.time() ng.fit((trainX[:, :1], trainX[:, 1:]), trainT) # ng.fit((trainX[:, :1], trainX[:, 1:]), trainT, (testX[:, :1], testX[:, 1:]), testT) print(f'elapsed: {time.time() - start}') print(f'trainY:\n{lossFunc.trY[:,:,0]}') print(con.B) return ng def f3(): # sset(2) ----> fset11(2->4) --> act11(Sigmoid) --> fset12(2->1) -----> act12(Sigmoid) -----> flat ---> mse(2) # \ / # -> fset21(2->12) --> act21(Relu) --> fset22(2->1) -----> act22(Sigmoid) ---/ print( f'******************** {inspect.currentframe().f_code.co_name} ********************' ) ####### Batch ############### # ng = NodeGraphBatch() # ng.lr = .01 ####### Adaptive ############### ng = NodeGraphAdaptive() ng.lrInit = .5 ng.lrMin = .01 ####### Common ############### sset = ng.add(StartSet1D(2, name='sset')) fset11 = ng.add(Dense1D(4, name='fset11'), sset) act11 = ng.add(Sigmoid1D('act11'), fset11) fset12 = ng.add(Dense1D(1, name='fset12'), act11) act12 = ng.add(Sigmoid1D('act12'), fset12) fset21 = ng.add(Dense1D(12, name='fset21'), sset) act21 = ng.add(Relu1D('act21'), fset21) fset22 = ng.add(Dense1D(1, name='fset22'), act21) act22 = ng.add(Sigmoid1D('act22'), fset22) flat = ng.add(Flat1D('flat'), act12, act22) lossFunc = ng.add(Mse1D('mse'), flat) ng.epochMax = 10000 ng.compile() start = time.time() ng.fit(trainX, trainT) # ng.fit(trainX, trainT, testX, testT) print(f'elapsed: {time.time() - start}') print(f'trainY:\n{lossFunc.trY[:,:,0]}') return ng def f4(): # sset(2) ----> fset11(2->4) --> act11(Sigmoid) ---> mp1(4->2) -----> den(4->2) ---> act(Softmax) ---> cce(2) # \ / # -> fset21(2->12) --> act21(Relu) ---> mp2(12->2) -- print( f'******************** {inspect.currentframe().f_code.co_name} ********************' ) ####### Batch ############### # ng = NodeGraphBatch() # ng.lr = .01 ####### Adaptive ############### ng = NodeGraphAdaptiveMinibatch() ng.lrInit = 1.0E-3 ng.lrMin = 1.0E-3 ####### Common ############### sset = ng.add(StartSet1D(2, name='sset')) fset11 = ng.add(Dense1D(4, name='fset11'), sset) act11 = ng.add(Sigmoid1D(name='act11'), fset11) mp1 = ng.add(MaxPool1D(2, name='mp1'), act11) fset21 = ng.add(Dense1D(12, name='fset21'), sset) act21 = ng.add(Relu1D(name='act21'), fset21) mp2 = ng.add(MaxPool1D(6, name='mp2'), act21) flat = ng.add(Flat1D(name='flat'), mp1, mp2) den = ng.add(Dense1D(2, name='den'), flat) act = ng.add(Softmax1D(name='sm'), den) lossFunc = ng.add(OneHotCce1D(name='cce'), act) ng.epochMax = 5000 ng.compile() start = time.time() # ng.fit(trainX, trainT, batchSize=1) ng.fit(trainX, trainT, testX, testT, batchSize=4) print(f'elapsed: {time.time() - start}') print(f'trainY:\n{ng.predict(trainX)[:,:,0]}') return ng ng = eval(f'f{index}()') print(f'=== graph info ===\n{ng.graphInfo("detail")}') print(f'=== summary ===\n{ng.summary()}') pt.contourDouble(ng, 231, 232, [-0.5, 1.5], [-0.5, 1.5]) plt.subplot(234) plt.title('loss') pt.plotLoss(ng) plt.subplot(235) plt.title('accuracy') pt.plotAccuracy(ng) plt.subplot(236) plt.title('learning rates') if isinstance(ng, NodeGraphAdaptive) or isinstance( ng, NodeGraphAdaptiveMinibatch): plt.plot(ng.appLrs) plt.tight_layout() plt.show()
def xor1(index=0): print( f'******************** {inspect.currentframe().f_code.co_name} ********************' ) trainX = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], np.float32) trainX = dt.addChannel(trainX) trainT = np.array([[0], [1], [1], [0]]) trainT = dt.addChannel(trainT) testX = dt.addChannel(np.array([[.5, .5], [1.5, 1.5]], np.float32)) testT = dt.addChannel(np.array([[0], [1]])) # print(trainX.dtype, trainT.dtype) def f0(): print( f'******************** {inspect.currentframe().f_code.co_name} ********************' ) # sset(2) ---> fset1(2>2) ---> act1(Sigmoid) ---> fset2(2>1) ---> act2(Sigmoid) ---> bce(1) ##### Adaptive ########## ng = NodeGraphAdaptive() ##### Batch ############# # ng = NodeGraphBatch() # ng.lr = np.float32(0.5) # ng.mom = 0.0 ##### Common ############### sset = ng.add(StartSet1D(2, name='sset')) fset1 = ng.add(Dense1D(2, name='fset1'), sset) act1 = ng.add(Sigmoid1D('act1'), fset1) fset2 = ng.add(Dense1D(1, name='fset2'), act1) act2 = ng.add(Sigmoid1D('act2'), fset2) lossFunc = ng.add(ZeroOneBce1D('bce'), act2) ng.compile() # ng.reg = Lasso(0.001) ng.epochMax = 10000 start = time.time() ng.fit(trainX, trainT) # ng.fit(trainX, trainT, testX, testT) print(f'elapsed: {time.time() - start}') if isinstance(ng, NodeGraphAdaptive): print('epoch: ', ng.appLrs.shape[0]) print('trainY[:,0,0]:', lossFunc.trY[:, 0, 0]) print('testY:', lossFunc.teY) return ng def f1(): # sset(2) ----> fset11(2>2) --> act11(Sigmoid) ----> con1((2,2)>(1,1)) ---> act2(Sigmoid) ---> bce(1) # \ / # --> fset12(2>2) --> act12(Relu) --- print( f'******************** {inspect.currentframe().f_code.co_name} ********************' ) ####### Batch ############### ng = NodeGraphBatch() ng.mom = np.float32(0.01) ng.reg = Ridge(0.1) ####### Adaptive ############### # ng = NodeGraphAdaptive() # ng.lrMin = np.float32(1.0e-2) ####### Common ############### sset = ng.add(StartSet1D(2, name='sset')) fset11 = ng.add(Dense1D(2, name='fset11'), sset) act11 = ng.add(Sigmoid1D('act11'), fset11) fset12 = ng.add(Dense1D(2, name='fset12'), sset) act12 = ng.add(Relu1D('act12'), fset12) con1 = ng.add(Conv1D(2, 1, biased=True, name='con1'), act11, act12) # fset2 = ng.add(Dense1D(1,2, 'fset2'), act11, act12) act2 = ng.add(Sigmoid1D('act2'), con1) lossFunc = ng.add(ZeroOneBce1D('bce'), act2) ng.compile() ng.epochMax = 5000 # ng.reg = Ridge(0.1) start = time.time() ng.fit(trainX, trainT) # ng.fit(trainX, trainT, testX, testT) print(f'elapsed: {time.time() - start}') print('trainY[:,0, 0]:', lossFunc.trY[:, 0, 0]) # print(ng.predict(trainX)) # print(fset11.Weight) # print(fset12.Weight) # print(fset2.Weight) return ng def f2(): # sset(2) ----> fset11(2>5) ----> act11(Sigmoid) ----> con1((5,2)>(5,1)) ---> act2(Sigmoid) ---> den2(5>1) ---> act3(Sigmoid) ---> bce(1) # \ / # -> fset12(2>5) ----> act12(Relu) ---- print( f'******************** {inspect.currentframe().f_code.co_name} ********************' ) ####### Batch ############### # ng = NodeGraphBatch() # ng.mom = np.float32(0.1) # ng.reg = Ridge(0.01) ####### Adaptive ############### ng = NodeGraphAdaptive() ng.lrMin = np.float32(1.0e-2) # ng.reg = Ridge(0.01) ####### Common ############### sset = ng.add(StartSet1D(2, name='sset')) fset11 = ng.add(Dense1D(5, name='fset11'), sset) fset12 = ng.add(Dense1D(5, name='fset12'), sset) act11 = ng.add(Sigmoid1D('act11'), fset11) act12 = ng.add(Relu1D('act12'), fset12) con1 = ng.add(Conv1D(3, 1, biased=True, name='con1'), act11, act12) act2 = ng.add(Sigmoid1D('act2'), con1) den2 = ng.add(Dense1D(1, name='den1'), act2) act3 = ng.add(Sigmoid1D('act3'), den2) # lossFunc = Mse('loss') lossFunc = ng.add(ZeroOneBce1D('bce'), act3) ng.compile() ng.epochMax = 10000 # ng.reg = Ridge(0.1) # fset2.initW = np.random.rand(5, 1) * 10 - 5 start = time.time() ng.fit(trainX, trainT) # ng.fit(trainX, trainT, testX, testT) print(f'elapsed: {time.time() - start}') print('trainY[:, 0, 0]:', lossFunc.trY[:, 0, 0]) # print(ng.predict(trainX)) # print(fset2.gradX) # print(mp.gradY) # print(mp.trInd) # print(mp.gradX) print('======== B ==========') print(con1.B) print('======== W ==========') print(con1.W) return ng def f3(): # sset(2) ----> fset11(2>6) ----> act11(Sigmoid) ----> mp((6,2)>(3,2)) ---> flat((3,2)>6) ---> den2(10>1) ---> act3(Sigmoid) ---> bce(1) # \ / # -> fset12(2>6) ----> act12(Relu) ---- print( f'******************** {inspect.currentframe().f_code.co_name} ********************' ) ####### Batch ############### # ng = NodeGraphBatch() ####### Adaptive ############### ng = NodeGraphAdaptiveMinibatch() ng.lrMin = np.float32(1.0e-2) ####### Common ############### sset = ng.add(StartSet1D(2, name='sset')) fset11 = ng.add(Dense1D(6, name='fset11'), sset) fset12 = ng.add(Dense1D(6, name='fset12'), sset) act11 = ng.add(Sigmoid1D('act11'), fset11) act12 = ng.add(Relu1D('act12'), fset12) mp = ng.add(MaxPool1D(2, 'mp'), act11, act12) fl1 = ng.add(Flat1D('fl1'), mp) den2 = ng.add(Dense1D(1, name='den1'), fl1) act3 = ng.add(Sigmoid1D('act3'), den2) # lossFunc = Mse('loss') lossFunc = ng.add(ZeroOneBce1D('bce'), act3) ng.compile() ng.epochMax = 1000 # ng.reg = Ridge(0.1) # fset2.initW = np.random.rand(5, 1) * 10 - 5 start = time.time() # ng.fit(trainX, trainT) ng.fit(trainX, trainT, testX, testT, batchSize=1) print(f'elapsed: {time.time() - start}') print(ng.predict(trainX)[:, 0, 0]) # print(fset2.gradX) # print(mp.gradY) # print(mp.trInd) # print(mp.gradX) return ng ng = eval(f'f{index}()') print(f'=== graph info ===\n{ng.graphInfo("detail")}') print(f'=== fit summary ===\n{ng.summary()}') plt.subplot(221) pt.contour(ng, [-0.5, 1.5], [-0.5, 1.5]) # 'xor, nodes = {}'.format(seq.getNodes())) plt.subplot(222) plt.title('accuracy') pt.plotAccuracy(ng) plt.subplot(223) plt.title('loss') pt.plotLoss(ng) plt.subplot(224) if isinstance(ng, NodeGraphAdaptive): plt.title('learning rates') plt.plot(ng.appLrs) plt.tight_layout() plt.show()
def f0(): from sklearn import datasets wine = datasets.load_wine() data = (wine.data - np.mean(wine.data, axis=0)) / np.std(wine.data, axis=0) ########################################################################################### trX, trT, teX, teT = dt.splitTrainTest(data, wine.target) trainX = trX.astype(np.float32) trainT = dt.onehot(trT) testX = teX.astype(np.float32) testT = dt.onehot(teT) trainX, trainT, testX, testT = dt.addChannel(trainX, trainT, testX, testT) ########################################################################################### # trainX, testX = data, None # trainT, testT = dt.vectorizeTarget(wine.target), None # trainX, trainT = dt.addChannel(trainX, trainT) ########################################################################################### # sset(13) ---> den0(13>20) ---> act0(sigmoid) ---> mp0(20>5) --> den1(5>3) ---> act1(softmax) ---> cce ####### Batch ############### # ng = NodeGraphBatch() ####### Adaptive ############### ng = NodeGraphAdaptiveMinibatch() ng.lrMin = np.float32(0.001) ng.lrInit = np.float32(.1) ####### Common ############### sset = ng.add(StartSet1D(trainX.shape[1])) # ss0 den0 = ng.add(Dense1D(20), sset) # dn0 act0 = ng.add(Sigmoid1D(), den0) # ac0 mp0 = ng.add(MaxPool1D(4), act0) den1 = ng.add(Dense1D(3), mp0) # dn1 act1 = ng.add(Softmax1D(), den1) # ac1 lossFunc = ng.add(OneHotCce1D(), act1) # cce ng.compile() ng.epochMax = 1000 start = time.time() # ng.fit(trainX, trainT) ng.fit(trainX, trainT, testX, testT, batchSize=118//5) print('elapsed:', time.time() - start) print('=== graph info ===\n' + ng.graphInfo('detail')) print('=== summary ===\n' + ng.summary()) trainY = ng.predict(trainX) plt.subplot(331) plt.title('accuracy') pt.plotAccuracy(ng) plt.subplot(332) plt.title('erros') pt.plotLoss(ng) plt.subplot(333) if isinstance(ng, NodeGraphAdaptive) or isinstance(ng, NodeGraphAdaptiveMinibatch): plt.plot(ng.appLrs) plt.title('applied learning rate') testY = lossFunc.teY for i in range(3): plt.subplot(334 + i) plt.plot(trainT[:, i, 0]) plt.plot(trainY[:, i, 0]) plt.title(f'train {i}') if testX is not None: plt.subplot(337 + i) plt.plot(testT[:, i, 0]) plt.plot(testY[:, i, 0]) plt.title(f'test {i}') plt.tight_layout() plt.show()
mp3 = ng.add(MaxPool2D((2, 2)), relu3) conv4 = ng.add(Conv2D((3, 3), 8), mp3) conv4.initB, conv4.initW = B2.copy(), W2.copy() relu4 = ng.add(Relu2D(), conv4) mp4 = ng.add(MaxPool2D((2, 2)), relu4) conv5 = ng.add(Conv2D((3, 3), 8), mp4) conv5.initB, conv5.initW = B2.copy(), W2.copy() relu5 = ng.add(Relu2D(), conv5) mp5 = ng.add(MaxPool2D((2, 2)), relu5) flat = ng.add(Flat2D(), mp5) loss = ng.add(Mse1D(), flat) ng.compile() x = dt.addChannel(np.array([gray])) y = dt.addChannel(np.zeros((1, 1), np.float32)) ng.epochMax = 0 ng.fit(x, y) mp1out = mp1.trY[0, :, :].sum(axis=2) mp1out /= mp1out.max() mp2out = mp2.trY[0, :, :].sum(axis=2) mp2out /= mp2out.max() mp3out = mp3.trY[0, :, :].sum(axis=2) mp3out /= mp3out.max() mp4out = mp4.trY[0, :, :].sum(axis=2) mp4out /= mp4out.max() mp5out = mp5.trY[0, :, :].sum(axis=2) mp5out /= mp5out.max() print(mp1out.shape) print(mp2out.shape)
import time import warnings import matplotlib.pyplot as plt import lib32.DataTools as dt import lib32.PlotTools as pt from lib32.NodeGraph import * from sklearn import datasets iris = datasets.load_iris() data = (iris.data - np.mean(iris.data, axis=0)) / np.std(iris.data, axis=0) ############################################################################## trX, trT, teX, teT = dt.splitTrainTest(data, iris.target, .7, 1) trainX, testX = trX.astype(np.float32), teX.astype(np.float32) trainT, testT = dt.onehot((trT, teT)) trainX, trainT, testX, testT = dt.addChannel(trainX, trainT, testX, testT) ############################################################################## # trainX, testX = data.astype(np.float32), None # trainT, testT = dt.onehot(iris.target, np.float32), None # trainX, trainT = dt.addChannel(trainX, trainT) ############################################################################## # print(trainX.dtype, trainT.dtype, testX.dtype, testT.dtype) def f0(): # sset(4) ---> den1(4>4) ---> act1(sigmoid) ---> den2(4>3) ---> act2(softmax) ---> cce ####### Batch ############### # ng = NodeGraphBatch() # ng.lr = np.float32(.01)
import time from cProfile import Profile from pstats import Stats import numpy as np import lib32.DataTools as dt from lib32.NodeGraph import * import matplotlib.pyplot as plt train_labels, train_images = dt.read_mnist_train() test_labels, test_images = dt.read_mnist_test() ntr, nte = train_images.shape[0], test_images.shape[0] w, h = train_images.shape[1], train_images.shape[2] # print(images.shape, labels.shape) def choose(images, labels, n): img = np.empty((10 * n, w, h)) lab = np.empty(10 * n, int) count = np.zeros(10, int) i = j = 0 while not np.all(count == n): if count[labels[i]] < n: count[labels[i]] += 1 lab[j] = labels[i] img[j] = images[i] j += 1 i += 1 return img, lab