def count_not_target_points(self, idxs): answer = [] for idx in idxs: partition = self.graphics.pixels[idx[0]:idx[1], idx[2]:idx[3], config['target_channel']] answer.append( np.prod(partition.shape) - np.count_nonzero(partition == FULL)) return np.count_nonzero(np.array(answer) < 5)
def classification_accuracy(model, X, Y): import minpy.numpy as np N = X.shape[0] predictions = model.forward(X, 'test') predicted_Y = np.argmax(predictions, axis=1) errors = np.count_nonzero(predicted_Y - Y) return 1 - errors / float(N)
def training_accuracy(weights, inputs): preds = predict(weights, inputs) error = np.count_nonzero( np.argmax( preds, axis=1) - np.argmax( targets, axis=1)) return (256 - error) * 100 / 256.0
def accuracy(p, l): import minpy.numpy as np if len(l.shape) == 1: return 1 - np.count_nonzero(p - l).val / float(p.shape[0]) else: inputs, labels = p, l return np.mean(np.sum((inputs - labels)**2, axis=1))
def calc_prospective_reward(self, angle=None): if angle == None: angle = self.angle prospect_width = config['prospect_width'] else: prospect_width = config['target_line_width'] / 2 fps = self.calc_prepend_points( (self.tx + math.cos(angle) * config['target_line_width'] / 2, self.ty + math.sin(angle) * config['target_line_width'] / 2), angle, prospect_width) prospect = config['prospect_length'] npoint = [] for pr in prospect: npoint.append((math.cos(angle) * pr + self.tx, math.sin(angle) * pr + self.ty)) sps = [] for nps in npoint: sps.append(self.calc_prepend_points(nps, angle, prospect_width)) #TODO does not work properly for fractional degrees prospect_reward = [] for index, point in enumerate(sps): xs = [fps[0][0], fps[1][0], point[0][0], point[1][0]] ys = [fps[0][1], fps[1][1], point[0][1], point[1][1]] minx = max(min(xs), 0) maxx = min(max(xs), self.screen_width) miny = max(min(ys), 0) maxy = min(max(ys), self.screen_height) rectangle_view = np.array(self.graphics.pixels[minx:maxx, miny:maxy, :]) target_points = np.logical_and( rectangle_view[:, :, config['target_channel']] == FULL, rectangle_view[:, :, config['draw_channel']] == EMPTY) total_points = 2 * prospect_width * prospect[index] prospect_reward.append( np.count_nonzero(target_points) / total_points) rewards = prospect_reward * np.array(config['prospect_reward']) rewards *= ((1 + self.coverage - self.basic_coverage) * self.turn_effect)**3 return sum(rewards)
t1 = time.time() resnet.forward(data) # TODO only forward once forward_time += time.time() - t1 t2 = time.time() grad_dict, loss = resnet.grad_and_loss(data, labels) backward_time += time.time() - t2 t3 = time.time() updater(grad_dict) updating_time += time.time() - t3 if (iteration + 1) % 100 == 0: print 'epoch %d iteration %d loss %f' % (epoch, iteration + 1, loss[0]) print 'epoch %d %f seconds consumed' % (epoch, time.time() - t0) print 'forward %f' % forward_time print 'backward %f' % backward_time print 'updating %f' % updating_time # validation val_data_iter.reset() # data iterator must be reset every epoch n_errors, n_samples = 0.0, 0.0 for batch in val_data_iter: data, labels = unpack_batch(batch) probs = resnet.forward(data, True) preds = np.argmax(probs, axis=1) n_errors += np.count_nonzero(preds - labels) n_samples += len(data) print 'epoch %d validation error %f' % (epoch, n_errors / n_samples) # TODO dump model
train_data_iter.reset() for iteration,batch in enumerate(train_data_iter): iteration_number +=1 if iteration_number > 64000: terminated = True break if iteration_number in (32000,48000): updater.learning_rate *= 0.1 data,labels = unpack_batch(batch) loss = model(data,labels = labels) grad_dict = model.backward() updater(grad_dict) if iteration_number % 100 == 0: print 'iteration %d loss %f' %(iteration_number, loss) # validation val_data_iter.reset() errors,samples = 0.0, 0.0 for batch in val_data_iter: data,labels = unpack_batch(batch) scores = model.forward(data,'inference') predictions = np.argmax (scores, axis = 1) errors += np.count_nonzero(predictions - labels) samples += len(data) print 'epoch %d validation error %f' % (epoch_number, errors / samples)
resnet.forward(data) # TODO only forward once forward_time += time.time() - t1 t2 = time.time() grad_dict, loss = resnet.grad_and_loss(data, labels) backward_time += time.time() - t2 t3 = time.time() updater(grad_dict) updating_time += time.time() - t3 if (iteration + 1) % 100 == 0: print 'epoch %d iteration %d loss %f' % (epoch, iteration + 1, loss[0]) print 'epoch %d %f seconds consumed' % (epoch, time.time() - t0) print 'forward %f' % forward_time print 'backward %f' % backward_time print 'updating %f' % updating_time # validation val_data_iter.reset() # data iterator must be reset every epoch n_errors, n_samples = 0.0, 0.0 for batch in val_data_iter: data, labels = unpack_batch(batch) probs = resnet.forward(data, True) preds = np.argmax(probs, axis=1) n_errors += np.count_nonzero(preds - labels) n_samples += len(data) print 'epoch %d validation error %f' % (epoch, n_errors / n_samples) # TODO dump model
def get_accuracy(output, label): return 1 - np.count_nonzero(output - label).val / float(label.shape[0])
def get_sparsity(activation): return 1 - np.count_nonzero(activation).val / get_size(activation)
iterations = 10000 interval = 10 validation_interval = 1000 validation_X, validation_Y = data[2 : 4] validation_X = validation_X[:1024] validation_Y = validation_Y[:1024] settings = {'learning_rate' : 0.01} initialize(model) updater = Updater(model, 'sgd', settings) for i in range(iterations): X_batch = data[0][batch_index * batch_size : (batch_index + 1) * batch_size] Y_batch = data[1][batch_index * batch_size : (batch_index + 1) * batch_size] batch_index = (batch_index + 1) % batches gradients, loss = stochastic_gradient_loss(model, X_batch, Y_batch, 0.000) loss = loss.asnumpy()[0] updater.update(gradients) if (i + 1) % interval == 0: print 'iteration %d loss %f' % (i + 1, loss) if (i + 1) % validation_interval == 0: outputs = model.forward(validation_X, 'train') predicted_Y = np.argmax(outputs, axis=1) errors = np.count_nonzero(predicted_Y - validation_Y) accuracy = 1 - errors / float(validation_Y.shape[0]) print accuracy
def test_numeric(): # 'newaxis', 'ndarray', 'flatiter', 'nditer', 'nested_iters', 'ufunc', # 'arange', 'array', 'zeros', 'count_nonzero', 'empty', 'broadcast', # 'dtype', 'fromstring', 'fromfile', 'frombuffer', 'int_asbuffer', # 'where', 'argwhere', 'copyto', 'concatenate', 'fastCopyAndTranspose', # 'lexsort', 'set_numeric_ops', 'can_cast', 'promote_types', # 'min_scalar_type', 'result_type', 'asarray', 'asanyarray', # 'ascontiguousarray', 'asfortranarray', 'isfortran', 'empty_like', # 'zeros_like', 'ones_like', 'correlate', 'convolve', 'inner', 'dot', # 'einsum', 'outer', 'vdot', 'alterdot', 'restoredot', 'roll', # 'rollaxis', 'moveaxis', 'cross', 'tensordot', 'array2string', # 'get_printoptions', 'set_printoptions', 'array_repr', 'array_str', # 'set_string_function', 'little_endian', 'require', 'fromiter', # 'array_equal', 'array_equiv', 'indices', 'fromfunction', 'isclose', 'load', # 'loads', 'isscalar', 'binary_repr', 'base_repr', 'ones', 'identity', # 'allclose', 'compare_chararrays', 'putmask', 'seterr', 'geterr', # 'setbufsize', 'getbufsize', 'seterrcall', 'geterrcall', 'errstate', # 'flatnonzero', 'Inf', 'inf', 'infty', 'Infinity', 'nan', 'NaN', 'False_', # 'True_', 'bitwise_not', 'full', 'full_like', 'matmul' x = np.arange(6) x = x.reshape((2, 3)) np.zeros_like(x) y = np.arange(3, dtype=np.float) np.zeros_like(y) np.ones(5) np.ones((5, ), dtype=np.int) np.ones((2, 1)) s = (2, 2) np.ones(s) x = np.arange(6) x = x.reshape((2, 3)) np.ones_like(x) y = np.arange(3, dtype=np.float) np.ones_like(y) np.full((2, 2), np.inf) x = np.arange(6, dtype=np.int) np.full_like(x, 1) np.full_like(x, 0.1) np.full_like(y, 0.1) np.count_nonzero(np.eye(4)) np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]]) np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=0) np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=1) a = [1, 2] np.asarray(a) a = np.array([1, 2]) np.asarray(a) is a a = np.array([1, 2], dtype=np.float32) np.asarray(a, dtype=np.float32) is a np.asarray(a, dtype=np.float64) is a np.asarray(a) is a np.asanyarray(a) is a a = [1, 2] np.asanyarray(a) np.asanyarray(a) is a x = np.arange(6).reshape(2, 3) np.ascontiguousarray(x, dtype=np.float32) x = np.arange(6).reshape(2, 3) y = np.asfortranarray(x) x = np.arange(6).reshape(2, 3) y = np.require(x, dtype=np.float32, requirements=['A', 'O', 'W', 'F']) a = np.array([[1, 2, 3], [4, 5, 6]], order='C') np.isfortran(a) b = np.array([[1, 2, 3], [4, 5, 6]], order='FORTRAN') np.isfortran(b) a = np.array([[1, 2, 3], [4, 5, 6]], order='C') np.isfortran(a) b = a.T np.isfortran(b) np.isfortran(np.array([1, 2], order='FORTRAN')) x = np.arange(6).reshape(2, 3) np.argwhere(x > 1) x = np.arange(-2, 3) np.flatnonzero(x) np.correlate([1, 2, 3], [0, 1, 0.5]) np.correlate([1, 2, 3], [0, 1, 0.5], "same") np.correlate([1, 2, 3], [0, 1, 0.5], "full") np.correlate([1 + 1j, 2, 3 - 1j], [0, 1, 0.5j], 'full') np.correlate([0, 1, 0.5j], [1 + 1j, 2, 3 - 1j], 'full') np.convolve([1, 2, 3], [0, 1, 0.5]) np.convolve([1, 2, 3], [0, 1, 0.5], 'same') np.convolve([1, 2, 3], [0, 1, 0.5], 'valid') rl = np.outer(np.ones((5, )), np.linspace(-2, 2, 5)) # im = np.outer(1j*np.linspace(2, -2, 5), np.ones((5,))) # grid = rl + im x = np.array(['a', 'b', 'c'], dtype=object) np.outer(x, [1, 2, 3]) a = np.arange(60.).reshape(3, 4, 5) b = np.arange(24.).reshape(4, 3, 2) c = np.tensordot(a, b, axes=([1, 0], [0, 1])) c.shape # A slower but equivalent way of computing the same... d = np.zeros((5, 2)) a = np.array(range(1, 9)) A = np.array(('a', 'b', 'c', 'd'), dtype=object) x = np.arange(10) np.roll(x, 2) x2 = np.reshape(x, (2, 5)) np.roll(x2, 1) np.roll(x2, 1, axis=0) np.roll(x2, 1, axis=1) a = np.ones((3, 4, 5, 6)) np.rollaxis(a, 3, 1).shape np.rollaxis(a, 2).shape np.rollaxis(a, 1, 4).shape x = np.zeros((3, 4, 5)) np.moveaxis(x, 0, -1).shape np.moveaxis(x, -1, 0).shape np.transpose(x).shape np.moveaxis(x, [0, 1], [-1, -2]).shape np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape x = [1, 2, 3] y = [4, 5, 6] np.cross(x, y) x = [1, 2] y = [4, 5, 6] np.cross(x, y) x = [1, 2, 0] y = [4, 5, 6] np.cross(x, y) x = [1, 2] y = [4, 5] np.cross(x, y) x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.array([[4, 5, 6], [1, 2, 3]]) np.cross(x, y) np.cross(x, y, axisc=0) x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) y = np.array([[7, 8, 9], [4, 5, 6], [1, 2, 3]]) np.cross(x, y) np.cross(x, y, axisa=0, axisb=0) # np.array_repr(np.array([1,2])) # np.array_repr(np.ma.array([0.])) # np.array_repr(np.array([], np.int32)) x = np.array([1e-6, 4e-7, 2, 3]) # np.array_repr(x, precision=6, suppress_small=True) # np.array_str(np.arange(3)) a = np.arange(10) x = np.arange(4) np.set_string_function(lambda x: 'random', repr=False) grid = np.indices((2, 3)) grid.shape grid[0] # row indices grid[1] # column indices x = np.arange(20).reshape(5, 4) row, col = np.indices((2, 3)) x[row, col] np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int) np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int) np.isscalar(3.1) np.isscalar([3.1]) np.isscalar(False) # np.binary_repr(3) # np.binary_repr(-3) # np.binary_repr(3, width=4) # np.binary_repr(-3, width=3) # np.binary_repr(-3, width=5) # np.base_repr(5) # np.base_repr(6, 5) # np.base_repr(7, base=5, padding=3) # np.base_repr(10, base=16) # np.base_repr(32, base=16) np.identity(3) np.allclose([1e10, 1e-7], [1.00001e10, 1e-8]) np.allclose([1e10, 1e-8], [1.00001e10, 1e-9]) np.allclose([1e10, 1e-8], [1.0001e10, 1e-9]) # np.allclose([1.0, np.nan], [1.0, np.nan]) # np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True) np.isclose([1e10, 1e-7], [1.00001e10, 1e-8]) np.isclose([1e10, 1e-8], [1.00001e10, 1e-9]) np.isclose([1e10, 1e-8], [1.0001e10, 1e-9]) # np.isclose([1.0, np.nan], [1.0, np.nan]) # np.isclose([1.0, np.nan], [1.0, np.nan], equal_nan=True) np.array_equal([1, 2], [1, 2]) np.array_equal(np.array([1, 2]), np.array([1, 2])) np.array_equal([1, 2], [1, 2, 3]) np.array_equal([1, 2], [1, 4]) np.array_equiv([1, 2], [1, 2]) np.array_equiv([1, 2], [1, 3]) np.array_equiv([1, 2], [[1, 2], [1, 2]]) np.array_equiv([1, 2], [[1, 2, 1, 2], [1, 2, 1, 2]]) np.array_equiv([1, 2], [[1, 2], [1, 3]])
def sparsity(l): return 1 - np.count_nonzero(l).val / float(l.asnumpy().size)
def test_numeric(): # 'newaxis', 'ndarray', 'flatiter', 'nditer', 'nested_iters', 'ufunc', # 'arange', 'array', 'zeros', 'count_nonzero', 'empty', 'broadcast', # 'dtype', 'fromstring', 'fromfile', 'frombuffer', 'int_asbuffer', # 'where', 'argwhere', 'copyto', 'concatenate', 'fastCopyAndTranspose', # 'lexsort', 'set_numeric_ops', 'can_cast', 'promote_types', # 'min_scalar_type', 'result_type', 'asarray', 'asanyarray', # 'ascontiguousarray', 'asfortranarray', 'isfortran', 'empty_like', # 'zeros_like', 'ones_like', 'correlate', 'convolve', 'inner', 'dot', # 'einsum', 'outer', 'vdot', 'alterdot', 'restoredot', 'roll', # 'rollaxis', 'moveaxis', 'cross', 'tensordot', 'array2string', # 'get_printoptions', 'set_printoptions', 'array_repr', 'array_str', # 'set_string_function', 'little_endian', 'require', 'fromiter', # 'array_equal', 'array_equiv', 'indices', 'fromfunction', 'isclose', 'load', # 'loads', 'isscalar', 'binary_repr', 'base_repr', 'ones', 'identity', # 'allclose', 'compare_chararrays', 'putmask', 'seterr', 'geterr', # 'setbufsize', 'getbufsize', 'seterrcall', 'geterrcall', 'errstate', # 'flatnonzero', 'Inf', 'inf', 'infty', 'Infinity', 'nan', 'NaN', 'False_', # 'True_', 'bitwise_not', 'full', 'full_like', 'matmul' x = np.arange(6) x = x.reshape((2, 3)) np.zeros_like(x) y = np.arange(3, dtype=np.float) np.zeros_like(y) np.ones(5) np.ones((5,), dtype=np.int) np.ones((2, 1)) s = (2,2) np.ones(s) x = np.arange(6) x = x.reshape((2, 3)) np.ones_like(x) y = np.arange(3, dtype=np.float) np.ones_like(y) np.full((2, 2), np.inf) x = np.arange(6, dtype=np.int) np.full_like(x, 1) np.full_like(x, 0.1) np.full_like(y, 0.1) np.count_nonzero(np.eye(4)) np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]]) np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=0) np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=1) a = [1, 2] np.asarray(a) a = np.array([1, 2]) np.asarray(a) is a a = np.array([1, 2], dtype=np.float32) np.asarray(a, dtype=np.float32) is a np.asarray(a, dtype=np.float64) is a np.asarray(a) is a np.asanyarray(a) is a a = [1, 2] np.asanyarray(a) np.asanyarray(a) is a x = np.arange(6).reshape(2,3) np.ascontiguousarray(x, dtype=np.float32) x = np.arange(6).reshape(2,3) y = np.asfortranarray(x) x = np.arange(6).reshape(2,3) y = np.require(x, dtype=np.float32, requirements=['A', 'O', 'W', 'F']) a = np.array([[1, 2, 3], [4, 5, 6]], order='C') np.isfortran(a) b = np.array([[1, 2, 3], [4, 5, 6]], order='FORTRAN') np.isfortran(b) a = np.array([[1, 2, 3], [4, 5, 6]], order='C') np.isfortran(a) b = a.T np.isfortran(b) np.isfortran(np.array([1, 2], order='FORTRAN')) x = np.arange(6).reshape(2,3) np.argwhere(x>1) x = np.arange(-2, 3) np.flatnonzero(x) np.correlate([1, 2, 3], [0, 1, 0.5]) np.correlate([1, 2, 3], [0, 1, 0.5], "same") np.correlate([1, 2, 3], [0, 1, 0.5], "full") np.correlate([1+1j, 2, 3-1j], [0, 1, 0.5j], 'full') np.correlate([0, 1, 0.5j], [1+1j, 2, 3-1j], 'full') np.convolve([1, 2, 3], [0, 1, 0.5]) np.convolve([1,2,3],[0,1,0.5], 'same') np.convolve([1,2,3],[0,1,0.5], 'valid') rl = np.outer(np.ones((5,)), np.linspace(-2, 2, 5)) # im = np.outer(1j*np.linspace(2, -2, 5), np.ones((5,))) # grid = rl + im x = np.array(['a', 'b', 'c'], dtype=object) np.outer(x, [1, 2, 3]) a = np.arange(60.).reshape(3,4,5) b = np.arange(24.).reshape(4,3,2) c = np.tensordot(a,b, axes=([1,0],[0,1])) c.shape # A slower but equivalent way of computing the same... d = np.zeros((5,2)) a = np.array(range(1, 9)) A = np.array(('a', 'b', 'c', 'd'), dtype=object) x = np.arange(10) np.roll(x, 2) x2 = np.reshape(x, (2,5)) np.roll(x2, 1) np.roll(x2, 1, axis=0) np.roll(x2, 1, axis=1) a = np.ones((3,4,5,6)) np.rollaxis(a, 3, 1).shape np.rollaxis(a, 2).shape np.rollaxis(a, 1, 4).shape x = np.zeros((3, 4, 5)) np.moveaxis(x, 0, -1).shape np.moveaxis(x, -1, 0).shape np.transpose(x).shape np.moveaxis(x, [0, 1], [-1, -2]).shape np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape x = [1, 2, 3] y = [4, 5, 6] np.cross(x, y) x = [1, 2] y = [4, 5, 6] np.cross(x, y) x = [1, 2, 0] y = [4, 5, 6] np.cross(x, y) x = [1,2] y = [4,5] np.cross(x, y) x = np.array([[1,2,3], [4,5,6]]) y = np.array([[4,5,6], [1,2,3]]) np.cross(x, y) np.cross(x, y, axisc=0) x = np.array([[1,2,3], [4,5,6], [7, 8, 9]]) y = np.array([[7, 8, 9], [4,5,6], [1,2,3]]) np.cross(x, y) np.cross(x, y, axisa=0, axisb=0) # np.array_repr(np.array([1,2])) # np.array_repr(np.ma.array([0.])) # np.array_repr(np.array([], np.int32)) x = np.array([1e-6, 4e-7, 2, 3]) # np.array_repr(x, precision=6, suppress_small=True) # np.array_str(np.arange(3)) a = np.arange(10) x = np.arange(4) np.set_string_function(lambda x:'random', repr=False) grid = np.indices((2, 3)) grid.shape grid[0] # row indices grid[1] # column indices x = np.arange(20).reshape(5, 4) row, col = np.indices((2, 3)) x[row, col] np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int) np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int) np.isscalar(3.1) np.isscalar([3.1]) np.isscalar(False) # np.binary_repr(3) # np.binary_repr(-3) # np.binary_repr(3, width=4) # np.binary_repr(-3, width=3) # np.binary_repr(-3, width=5) # np.base_repr(5) # np.base_repr(6, 5) # np.base_repr(7, base=5, padding=3) # np.base_repr(10, base=16) # np.base_repr(32, base=16) np.identity(3) np.allclose([1e10,1e-7], [1.00001e10,1e-8]) np.allclose([1e10,1e-8], [1.00001e10,1e-9]) np.allclose([1e10,1e-8], [1.0001e10,1e-9]) # np.allclose([1.0, np.nan], [1.0, np.nan]) # np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True) np.isclose([1e10,1e-7], [1.00001e10,1e-8]) np.isclose([1e10,1e-8], [1.00001e10,1e-9]) np.isclose([1e10,1e-8], [1.0001e10,1e-9]) # np.isclose([1.0, np.nan], [1.0, np.nan]) # np.isclose([1.0, np.nan], [1.0, np.nan], equal_nan=True) np.array_equal([1, 2], [1, 2]) np.array_equal(np.array([1, 2]), np.array([1, 2])) np.array_equal([1, 2], [1, 2, 3]) np.array_equal([1, 2], [1, 4]) np.array_equiv([1, 2], [1, 2]) np.array_equiv([1, 2], [1, 3]) np.array_equiv([1, 2], [[1, 2], [1, 2]]) np.array_equiv([1, 2], [[1, 2, 1, 2], [1, 2, 1, 2]]) np.array_equiv([1, 2], [[1, 2], [1, 3]])