Beispiel #1
0
def runCNN(arg):
	
	if type(arg) is dict:
		model_config = arg
	else :
		model_config = load_model(arg,'CNN')
	
	conv_config,conv_layer_config,mlp_config = load_conv_spec(
			model_config['nnet_spec'],
			model_config['batch_size'],
			model_config['input_shape'])

	data_spec =  load_data_spec(model_config['data_spec'],model_config['batch_size']);

	
	numpy_rng = numpy.random.RandomState(89677)
	theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
	
	logger.info('> ... building the model')
	conv_activation = parse_activation(conv_config['activation']);
	hidden_activation = parse_activation(mlp_config['activation']);

	createDir(model_config['wdir']);
	#create working dir

	batch_size = model_config['batch_size'];
	if mlp_config['do_dropout']:
		cnn = DropoutCNN(numpy_rng,theano_rng,conv_layer_configs = conv_layer_config, batch_size = batch_size,
				n_outs=model_config['n_outs'],hidden_layer_configs=mlp_config, 
				conv_activation = conv_activation,hidden_activation = hidden_activation,
				use_fast = conv_config['use_fast'],l1_reg = mlp_config['l1_reg'],
				l2_reg = mlp_config['l1_reg'],max_col_norm = mlp_config['max_col_norm'])
	else:
		cnn = CNN(numpy_rng,theano_rng,conv_layer_configs = conv_layer_config, batch_size = batch_size,
				n_outs=model_config['n_outs'],hidden_layer_configs=mlp_config, 
				conv_activation = conv_activation,hidden_activation = hidden_activation,
				use_fast = conv_config['use_fast'],l1_reg = mlp_config['l1_reg'],
				l2_reg = mlp_config['l1_reg'],max_col_norm = mlp_config['max_col_norm'])
				
	########################
	 # Loading  THE MODEL #
	########################
	try:
		# pretraining
		ptr_file = model_config['input_file']
		pretrained_layers = mlp_config['pretrained_layers']
		logger.info("Loading the pretrained network..")
		cnn.load(filename=ptr_file,max_layer_num = pretrained_layers,  withfinal=True)
	except KeyError, e:
		logger.warning("Pretrained network missing in working directory, skipping model loading")
Beispiel #2
0
def runDNN(arg):


	if type(arg) is dict:
		model_config = arg
	else :
		model_config = load_model(arg,'DNN')

	dnn_config = load_dnn_spec(model_config['nnet_spec'])
	data_spec =  load_data_spec(model_config['data_spec'],model_config['batch_size']);


	#generating Random
	numpy_rng = numpy.random.RandomState(model_config['random_seed'])
	theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
	
	activationFn = parse_activation(dnn_config['activation']);

	#create working dir
	createDir(model_config['wdir']);
	
	batch_size = model_config['batch_size'];
	n_ins = model_config['n_ins']
	n_outs = model_config['n_outs']
	
	max_col_norm = dnn_config['max_col_norm']
	l1_reg = dnn_config['l1_reg']
	l2_reg = dnn_config['l2_reg']	
	adv_activation = dnn_config['adv_activation']
	hidden_layers_sizes = dnn_config['hidden_layers']
	do_dropout = dnn_config['do_dropout']
	logger.info('Building the model')

	if do_dropout:
		dropout_factor = dnn_config['dropout_factor']
		input_dropout_factor = dnn_config['input_dropout_factor']

		dnn = DNN_Dropout(numpy_rng=numpy_rng, theano_rng = theano_rng, n_ins=n_ins,
			  hidden_layers_sizes=hidden_layers_sizes, n_outs=n_outs,
			  activation = activationFn, dropout_factor = dropout_factor,
			  input_dropout_factor = input_dropout_factor, adv_activation = adv_activation,
			  max_col_norm = max_col_norm, l1_reg = l1_reg, l2_reg = l2_reg)
	else:
		
		dnn = DNN(numpy_rng=numpy_rng, theano_rng = theano_rng, n_ins=n_ins,
			  hidden_layers_sizes=hidden_layers_sizes, n_outs=n_outs,
			  activation = activationFn, adv_activation = adv_activation,
			  max_col_norm = max_col_norm, l1_reg = l1_reg, l2_reg = l2_reg)


	logger.info("Loading Pretrained network weights")
	try:
		# pretraining
		ptr_file = model_config['input_file']
		pretrained_layers = dnn_config['pretrained_layers']
		dnn.load(filename=ptr_file,max_layer_num = pretrained_layers,  withfinal=True)
	except KeyError, e:
		logger.critical("KeyMissing:"+str(e));
		logger.error("Pretrained network Missing in configFile")
		sys.exit(2)
Beispiel #3
0
def runRBM(arg):

    if type(arg) is dict:
        model_config = arg
    else :
        model_config = load_model(arg,'RBM')

    rbm_config = load_rbm_spec(model_config['nnet_spec'])
    data_spec =  load_data_spec(model_config['data_spec'],model_config['batch_size']);


    #generating Random
    numpy_rng = numpy.random.RandomState(model_config['random_seed'])
    theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))

    activationFn = parse_activation(rbm_config['activation']);
 
    createDir(model_config['wdir']);
    #create working dir

    batch_size = model_config['batch_size']
    wdir = model_config['wdir']
    

    dbn = DBN(numpy_rng=numpy_rng, theano_rng = theano_rng, n_ins=model_config['n_ins'],
            hidden_layers_sizes=rbm_config['hidden_layers'],n_outs=model_config['n_outs'],
            first_layer_gb = rbm_config['first_layer_gb'],
            pretrainedLayers=rbm_config['pretrained_layers'],
            activation=activationFn)

    #########################
    # PRETRAINING THE MODEL #
    #########################
    if model_config['processes']['pretraining']:
        train_sets, train_xy, train_x, train_y = read_dataset(data_spec['training'])
        preTraining(dbn,train_sets,train_xy,train_x,model_config['pretrain_params'])

    ########################
    # FINETUNING THE MODEL #
    ########################
    if model_config['processes']['finetuning']:
        fineTunning(dbn,model_config,data_spec)

    ########################
    #  TESTING THE MODEL   #
    ########################
    if model_config['processes']['testing']:
        testing(dbn,data_spec)

    ##########################
    ##   Export Features    ##
    ##########################
    if model_config['processes']['export_data']:
        exportFeatures(dbn,model_config,data_spec)

    logger.info('Saving model to ' + str(model_config['output_file']) + ' ....')
    dbn.save(filename=model_config['output_file'], withfinal=True);
    logger.info('Saved model to ' + str(model_config['output_file']))
Beispiel #4
0
def runSdA(arg):

    if type(arg) is dict:
        model_config = arg
    else :
        model_config = load_model(arg,'SDA')
        
    sda_config = load_sda_spec(model_config['nnet_spec'])
    data_spec =  load_data_spec(model_config['data_spec'],model_config['batch_size']);

    # numpy random generator
    numpy_rng = numpy.random.RandomState(model_config['random_seed'])
    #theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))

    #get Activation function
    activationFn = parse_activation(sda_config['activation']);

    createDir(model_config['wdir']);
    #create working dir

    logger.info('building the model')
    # construct the stacked denoising autoencoder class
    sda = SDA(numpy_rng=numpy_rng, n_ins=model_config['n_ins'],
              hidden_layers_sizes=sda_config['hidden_layers'],
              n_outs=model_config['n_outs'],activation=activationFn)

    batch_size = model_config['batch_size'];


    #########################
    # PRETRAINING THE MODEL #
    #########################
    if model_config['processes']['pretraining']:
        
        train_sets, train_xy, train_x, train_y = read_dataset(data_spec['training'])
        pretraining_config= model_config['pretrain_params']
        corruption_levels =sda_config['corruption_levels']

        preTraining(sda,train_sets,train_xy,train_x,corruption_levels,pretraining_config);


    ########################
    # FINETUNING THE MODEL #
    ########################
    if model_config['processes']['finetuning']:
        fineTunning(sda,model_config,data_spec)

    ########################
    #  TESTING THE MODEL   #
    ########################
    if model_config['processes']['testing']:
        testing(sda,data_spec)

    ##########################
    ##   Export Features    ##
    ##########################
    if model_config['processes']['export_data']:
        exportFeatures(sda,model_config,data_spec)

    # save the pretrained nnet to file
    logger.info('Saving model to ' + str(model_config['output_file']) + '....')
    sda.save(filename=model_config['output_file'], withfinal=True)
    logger.info('Saved model to ' + str(model_config['output_file']))
Beispiel #5
0
        momentum = float(arguments['momentum'])

    nnet_layers = nnet_spec.split(":")
    n_ins = int(nnet_layers[0])
    hidden_layers_sizes = []
    for i in range(1, len(nnet_layers)-1):
        hidden_layers_sizes.append(int(nnet_layers[i]))
    n_outs = int(nnet_layers[-1])

    activation = T.nnet.sigmoid
    do_maxout = False
    pool_size = 1
    do_pnorm = False
    pnorm_order = 1
    if arguments.has_key('activation'):
        activation = parse_activation(arguments['activation'])
        if arguments['activation'].startswith('maxout'):
            do_maxout = True
            pool_size = int(arguments['activation'].replace('maxout:',''))
        elif arguments['activation'].startswith('pnorm'):
            do_pnorm = True
            pool_size, pnorm_order = parse_two_integers(arguments['activation'])

    # deal with dropout
    do_dropout = False
    dropout_factor = [0.0]
    input_dropout_factor = 0.0
    if arguments.has_key('dropout_factor'):
        do_dropout = True
        if arguments.has_key('input_dropout_factor'):
            input_dropout_factor = float(arguments['input_dropout_factor'])
Beispiel #6
0
    # full layer configurations
    nnet_layers = full_nnet_spec.split(":")
    hidden_layers_sizes = []
    for i in range(0, len(nnet_layers)-1):
        hidden_layers_sizes.append(int(nnet_layers[i]))
    n_outs = int(nnet_layers[-1])
    # ivec layer configurations
    nnet_layers = ivec_nnet_spec.split(":")
    ivec_layers_sizes = []
    for i in xrange(len(nnet_layers)):
        ivec_layers_sizes.append(int(nnet_layers[i]))

    conv_activation = T.nnet.sigmoid
    full_activation = T.nnet.sigmoid
    if arguments.has_key('conv_activation'):
        conv_activation = parse_activation(arguments['conv_activation'])
    if arguments.has_key('full_activation'):
        full_activation = parse_activation(arguments['full_activation'])

    # which part of the network to be updated
    update_part = []
    for part in arguments['update_part'].split(':'):
        update_part.append(int(part))

    # the dimension of the ivectors
    ivec_dim = int(arguments['ivec_dim'])

    # whether to use the fast version of CNN with pylearn2
    use_fast = False
    if arguments.has_key('use_fast'):
        use_fast = string_2_bool(arguments['use_fast'])        
Beispiel #7
0
    conv_layer_number = int(arguments['conv_layer_number'])
    wdir = arguments['wdir']
    output_file_prefix = arguments['output_file_prefix']
    conv_net_file = arguments['conv_net_file']
    # network structure
    conv_configs = []
    for i in xrange(conv_layer_number):
      config_path = wdir + '/conv.config.' + str(i)
      if os.path.exists(config_path) == False:
          print "Error: config files for convolution layers do not exist."
          exit(1)
      else:
          with open(config_path, 'rb') as fp:
              conv_configs.append(json.load(fp))
          # convert string activaton to theano
          conv_configs[i]['activation'] = parse_activation(conv_configs[i]['activation'])

    # whether to use the fast mode
    use_fast = False
    if arguments.has_key('use_fast'):
        use_fast = string_2_bool(arguments['use_fast'])

    # paths for output files
    output_scp = output_file_prefix + '.scp'
    output_ark = output_file_prefix + '.ark'

    start_time = time.clock()
    feat_rows = []
    feat_mats_np = []
    uttIDs = []
Beispiel #8
0
    n_ins = int(nnet_layers[0])
    hidden_layers_sizes = []
    for i in range(1, len(nnet_layers)-1):
        hidden_layers_sizes.append(int(nnet_layers[i]))
    n_outs = int(nnet_layers[-1])

    ptr_layer_number = len(hidden_layers_sizes)
    if arguments.has_key('ptr_layer_number'):
        ptr_layer_number = int(arguments['ptr_layer_number'])

    hidden_activation = T.nnet.sigmoid
    do_maxout = False
    pool_size = 1
    first_reconstruct_activation = T.nnet.sigmoid
    if arguments.has_key('hidden_activation'):
        hidden_activation = parse_activation(arguments['hidden_activation'])
        if arguments['hidden_activation'].startswith('maxout'):
            do_maxout = True
            pool_size = int(arguments['hidden_activation'].replace('maxout:',''))
    if arguments.has_key('first_reconstruct_activation'):
        first_reconstruct_activation = parse_activation(arguments['first_reconstruct_activation'])

    # if initialized with current
    keep_layer_num=0
    current_nnet = wdir + 'nnet.ptr.current'
  
    train_sets, train_xy, train_x, train_y = read_dataset(dataset, dataset_args)

    # numpy random generator
    numpy_rng = numpy.random.RandomState(89677)
    theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))