コード例 #1
0
def f2():
	# tr_img, tr_lab = choose(train_images, train_labels, 100)
	# te_img, te_lab = choose(test_images, test_labels, 10)
	# trX, trT = dt.addChannel(tr_img / 255.0, dt.vectorizeTarget(tr_lab).astype(float))
	# teX, teT = dt.addChannel(te_img / 255.0, dt.vectorizeTarget(te_lab).astype(float))
	trX, trT = dt.addChannel(train_images / 255.0, dt.onehot(train_labels).astype(float))
	teX, teT = dt.addChannel(test_images / 255.0, dt.onehot(test_labels).astype(float))

	# print(trX.shape, trT.shape)
	# print(trX.dtype)
	# return
	################################################
	ng = NodeGraphAdaptiveMinibatch()
	ng.lrMin = 1.0E-6
	ng.lrInit = 0.0001
	################################################
	# ng = NodeGraphBatch()
	################################################
	# sset(28,28) ---> conv((3,3), 32) ---> act1 ---> mp((2,2)) ---> flat ---> den1(100) ---> act1 ---> den2(10) ---> act2 ---> loss
	sset = ng.add(StartSet2D((trX.shape[1], trX.shape[2]), trX.shape[0]))
	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)
	act1 = ng.add(Sigmoid1D(), den1)
	den2 = ng.add(Dense1D(10), act1)
	act2 = ng.add(Softmax1D(), den2)
	loss = ng.add(Cce1D(), act2)
	ng.compile()
	ng.epochMax = 10
	start = time.time()
	ng.fit(trX, trT, teX, teT, batchSize=10000, verbose=2)
	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()
コード例 #2
0
def f0():
	trX = dt.addChannel(train_images.reshape(ntr , w*h)).astype(np.float32)
	trT = dt.addChannel(dt.onehot(train_labels)).astype(np.float32)
	teX = dt.addChannel(test_images.reshape(nte, w * h)).astype(np.float32)
	teT = dt.addChannel(dt.onehot(test_labels)).astype(np.float32)
	# print(trX.shape, trT.shape)
	# return
	# sset(784) ---> den1(784>100) ---> act1() ---> den2(100>10) ---> act2 ---> cce
	ng = NodeGraphAdaptive()
	sset = ng.add(StartSet1D(trX.shape[1]))
	den1 = ng.add(Dense1D(100), sset)
	act1 = ng.add(Sigmoid1D(), den1)
	den2 = ng.add(Dense1D(10), act1)
	act2 = ng.add(Softmax1D(), den2)
	lossFunc = ng.add(Cce1D(), act2)
	ng.compile()
	ng.lrInit = 0.00001
	ng.epochMax = 50
	start = time.time()
	ng.fit(trX, trT, teX, teT, verbose=0)
	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()
コード例 #3
0
ファイル: Iris32.py プロジェクト: ggorr/Machine-Learning
import lib32_ch1st.PlotTools as pt
from lib32_ch1st.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)
# # print(trX.dtype, trT.dtype, teX.dtype, teT.dtype)
# 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), 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)
    # ng.epochMax = 100
    ####### Adaptive ###############
    ng = NodeGraphAdaptive()
    # ng.lrMin = np.float32(1.0e-2)
    ng.epochMax = 10000
コード例 #4
0
import time
from cProfile import Profile
from pstats import Stats

import numpy as np
import lib32_ch1st.DataTools as dt
from lib32_ch1st.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

コード例 #5
0
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]], np.float32)
	testX = np.array([[.5, .5]], np.float32)
	testT = np.array([[0, 1]], np.float32)
	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(Cce1D('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}')
		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(MeanSquare('loss'), act2)
		lossFunc = ng.add(Cce1D('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)
		print(f'elapsed: {time.time() - start}')
		print(f'trainY:\n{lossFunc.trY}')
		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(Cce1D('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}')
		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}')
		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.lrMin = 1.0E-3
		####### Common ###############
		sset = ng.add(StartSet1D(2, name='sset'))

		fset11 = ng.add(Dense1D(4, name='fset11'), sset)
		act11 = ng.add(Sigmoid1D('act11'), fset11)
		mp1 = ng.add(MaxPool1D(2, 'mp1'), act11)

		fset21 = ng.add(Dense1D(12, name='fset21'), sset)
		act21 = ng.add(Relu1D('act21'), fset21)
		mp2 = ng.add(MaxPool1D(6, 'mp2'), act21)

		flat = ng.add(Flat1D('flat'), mp1, mp2)
		den = ng.add(Dense1D(2, name='den'), flat)
		act = ng.add(Softmax1D('sm'), den)
		lossFunc = ng.add(Cce1D('cce'), act)

		ng.epochMax = 5000

		ng.compile()

		start = time.time()
		# ng.fit(trainX, trainT, batchSize=1)
		ng.fit(trainX, trainT, testX, testT, batchSize=2)
		print(f'elapsed: {time.time() - start}')

		print(f'trainY:\n{ng.predict(trainX)}')
		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()
コード例 #6
0
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]], np.float32)
	trainT = dt.addChannel(trainT)
	testX = dt.addChannel(np.array([[.5, .5], [1.5, 1.5]], np.float32))
	testT = dt.addChannel(np.array([[0], [1]], np.float32))

	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(Bce1D('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(Bce1D('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(Bce1D('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(Bce1D('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))

		# 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()
コード例 #7
0
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).astype(np.float32)
    testX = teX.astype(np.float32)
    testT = dt.onehot(teT).astype(np.float32)
    trainX, trainT, testX, testT = dt.addChannel(trainX, trainT, testX, testT)
    ###########################################################################################
    # trainX, testX = data, None
    # trainT, testT = dt.vectorizeTarget(wine.target).astype(float), 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(Cce1D(), 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[0, i, :])
        plt.plot(trainY[0, i, :])
        plt.title(f'train {i}')
        if testX is not None:
            plt.subplot(337 + i)
            plt.plot(testT[0, i, :])
            plt.plot(testY[0, i, :])
            plt.title(f'test {i}')
    plt.tight_layout()
    plt.show()
コード例 #8
0
ファイル: Dim232.py プロジェクト: ggorr/Machine-Learning
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(np.float32).reshape((-1, 1))
    trainX, trainT = dt.addChannel(trainX, trainT)

    # sset(2) ---> den1(2>2) ---> act1(Sigmoid) ---> den2(2>1) ---> act2(Sigmoid) ---> mse(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)
    lossFunc = ng.add(Mse1D('mse'), 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]])
    # den2.initW = np.array([[-7.14937919, -10.94093724, 8.40722765]]).T
    ng.lossMax = np.float32(.00001)
    ng.compile()
    ng.epochMax = 1000
    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[0, 1, :], '+k')
    plt.scatter(comp[0, 0, :], comp[0, 1, :], s=1, c='k')
    plt.subplot(222)
    plt.title('activation')
    ng.predict(quad)
    plt.plot(act1.prY[0, 0, :], act1.prY[0, 1, :], '+k')
    ng.predict(comp)
    plt.scatter(act1.prY[0, 0, :], act1.prY[0, 1, :], s=1, c='k')
    draw_line(den2.B[0].T, den2.W[0].T, '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]]]))
            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()