def __init__(self, input_variable=Variable, output_num=int, name=str, scope=''): if not isinstance(input_variable, Variable): raise Exception( "Operator fullyconnected name: %s's input_variable is not instance of Variable" % name) self.batch_size = input_variable.shape[0] input_len = reduce(lambda x, y: x * y, input_variable.shape[1:]) self.output_num = output_num self.weights = Variable([input_len, self.output_num], name='weights', scope=name, init='std_const', trainable=True) self.bias = Variable([self.output_num], name='bias', scope=name, init='zeros', trainable=True) self.output_variables = Variable([self.batch_size, self.output_num], name='out', scope=name) self.input_variables = input_variable Operator.__init__(self, [self.input_variables], [self.output_variables], name, scope)
def __init__(self, input_variable=Variable, name=str, scope='', epsilon=1e-4, moving_decay=1 - 3e-3): self.input_variable = input_variable self.shape = input_variable.shape self.batch_size = self.shape[0] self.bndims = tuple([i for i in range(len(self.shape) - 1)]) self.output_variable = Variable(self.shape, name='out', scope=name) # default BN for final dim self.alpha = Variable([self.shape[-1]], name='alpha', scope=name, grad=True, trainable=True, init='ones') self.beta = Variable([self.shape[-1]], name='beta', scope=name, grad=True, trainable=True, init='zeros') self.moving_mean = np.zeros(self.shape[-1]) self.moving_var = np.zeros(self.shape[-1]) self.moving_decay = moving_decay self.epsilon = epsilon Operator.__init__(self, [self.input_variable], [self.output_variable], name, scope)
def main(): if not os.path.exists('./logs/mnist'): os.makedirs('./logs/mnist') train_imgs, train_labels, test_imgs, test_labels = load_mnist_data( data_home='./mnist_dataset') batch_size = 32 # 准备输入输出 img_placeholder = Variable((batch_size, 28, 28, 1), 'input') label_placeholder = Variable((batch_size, 1), 'label') model_out = build_model(img_placeholder, label_placeholder, 10) train_model(model_out, img_placeholder, label_placeholder, train_imgs, train_labels, epochs=10, batch_size=batch_size) test_model(model_out, img_placeholder, label_placeholder, test_imgs, test_labels, batch_size=batch_size)
def __init__(self, predict=Variable, label=Variable, name=str, scope=''): """multi input and multi output""" self.batch_size = predict.shape[0] self.input_variables = [predict, label] self.loss = Variable([1], name='loss', scope=name, init='zeros') self.prediction = Variable(predict.shape, name='prediction', scope=name) self.output_variables = [self.loss, self.prediction] Operator.__init__(self, self.input_variables, self.output_variables, name, scope)
def __init__(self, input_variable=Variable, name=str, scope='', ksize=2, stride=2): if not isinstance(input_variable, Variable): raise Exception( "Operator Maxpooling name: %s's input_variable is not instance of Variable" % name) self.ksize = ksize self.stride = stride self.batch_size = input_variable.shape[0] self.output_channels = input_variable.shape[-1] self.index = np.zeros(input_variable.shape) self.input_variables = input_variable # default stride == ksize _output_shape = [ self.batch_size, input_variable.shape[1] // stride, input_variable.shape[2] // stride, self.output_channels ] self.output_variables = Variable(_output_shape, name='out', scope=name) Operator.__init__(self, [self.input_variables], [self.output_variables], name, scope)
def __init__(self, input_variable=Variable, name=str): self.input_variables = input_variable self.output_variables = Variable(self.input_variables.shape, name='out', scope=name) Operator.__init__(self, [self.input_variables], [self.output_variables], name)
def __init__(self, input_variable=Variable, name=str, alpha=0.01): self.input_variables = input_variable self.output_variables = Variable(self.input_variables.shape, name='out', scope=name) self.alpha = alpha Operator.__init__(self, [self.input_variables], [self.output_variables], name)
def __init__(self, kernel_shape=list, input_variable=Variable, name=str, scope='', stride=1, padding='SAME'): """ conv layer -> Operator :param kernel_shape: :param input_variable: only one and shape must be 4 dim get input variable and output variable to call Operator.__init__ kernel params save as Variable(trainable=True) scope name of all variables is self.name """ # kernel_shape = [ksize, ksize, input_channels, output_channels] for i in kernel_shape: if not isinstance(i, int): raise Exception("Operator Conv2D name: %s kernel shape is not list of int" % self.name) if not isinstance(input_variable, Variable): raise Exception("Operator Conv2D name: %s's input_variable is not instance of Variable" % name) if len(input_variable.shape)!=4: raise Exception("Operator Conv2D name: %s's input_variable's shape != 4d Variable!" % name) self.ksize = kernel_shape[0] self.stride = stride self.output_num = kernel_shape[-1] self.padding = padding self.col_image = [] self.weights = Variable(kernel_shape, scope=name, name='weights',grad=True, trainable=True) self.bias = Variable([self.output_num], scope=name, name='bias', grad=True, trainable=True) self.batch_size = input_variable.shape[0] # calc output variable shape if self.padding == 'SAME': output_shape = [self.batch_size, input_variable.shape[1]//stride, input_variable.shape[2]//stride, self.output_num] if self.padding == 'VALID': output_shape = [self.batch_size, (input_variable.shape[1] - self.ksize + 1)//stride, (input_variable.shape[2] - self.ksize + 1)//stride, self.output_num] else: raise AttributeError('unsupported padding method') self.input_variables = input_variable self.output_variables = Variable(output_shape, name='out', scope=name) # .name Operator.__init__(self, [self.input_variables], [self.output_variables],name,scope)
def __init__(self, input_variable=Variable, name=str, alpha=0.25): self.input_variables = input_variable self.output_variables = Variable(self.input_variables.shape, name='out', scope=name) self.alpha = alpha self.momentum = 0.9 self.eta = 1e-4 Operator.__init__(self, [self.input_variables], [self.output_variables], name)
def __init__(self, input_variable=Variable, state='test', name=str, scope='', prob=0.7): self.input_variables = input_variable self.output_variables = Variable(shape=input_variable.shape, scope=name, name='out') self.prob = prob self.state = state self.index = np.ones(input_variable.shape) Operator.__init__(self, [self.input_variables], [self.output_variables], name, scope)