def get_function(self, func_name): if func_name == 'tanh': return T.tanh elif func_name == 'hardtanh': L.warning('Current hardTanh implementation is slow!') return lambda x: ((abs(x) <= 1) * x) + ((1 < abs(x)) * T.sgn(x)) elif func_name == 'xtanh': return lambda x: T.tanh(x) + 0.1 * x elif func_name == 'sigmoid': return T.nnet.sigmoid elif func_name == 'fastsigmoid': L.error('T.nnet.ultra_fast_sigmoid function has some problems') elif func_name == 'hardsigmoid': return T.nnet.hard_sigmoid elif func_name == 'xsigmoid': return lambda x: T.nnet.sigmoid(x) + 0.1 * x elif func_name == 'softplus': return T.nnet.softplus elif func_name == 'relu': #return lambda x: T.maximum(x, 0) return lambda x: x * (x > 0) #return T.nnet.relu # Update theano and then use this one instead elif func_name == 'leakyrelu': return lambda x: T.maximum(x, 0.01 * x) elif func_name == 'cappedrelu': return lambda x: T.minimum(x * (x > 0), 6) elif func_name == 'softmax': return T.nnet.softmax elif func_name == 'norm1': return lambda x: x / T.nlinalg.norm(x, 1) elif func_name == 'norm2': #return lambda x: x / T.nlinalg.norm(x, 2) return lambda x: x / T.dot(x, x)**0.5 else: L.error('Invalid function name given: ' + func_name)
def curr_version(): import dlm.io.logging as L info_path = os.path.dirname(sys.argv[0]) + '/.git/refs/heads/master' if os.path.exists(info_path): with open(info_path, 'r') as info_file: return info_file.next().strip() L.warning('Unable to read current version.') return None
def set_theano_device(device, threads): import sys import dlm.io.logging as L xassert(device == "cpu" or device.startswith("gpu"), "The device can only be 'cpu', 'gpu' or 'gpu<id>'") if device.startswith("gpu") and len(device) > 3: try: gpu_id = int(device[3:]) if not is_gpu_free(gpu_id): L.warning('The selected GPU (GPU' + str(gpu_id) + ') is apparently busy.') except ValueError: L.error("Unknown GPU device format: " + device) if device.startswith("gpu"): L.warning('Running on GPU yields non-deterministic results.') xassert(sys.modules.has_key('theano') == False, "dlm.utils.set_theano_device() function cannot be called after importing theano") os.environ['OMP_NUM_THREADS'] = str(threads) os.environ['THEANO_FLAGS'] = 'device=' + device os.environ['THEANO_FLAGS'] += ',force_device=True' os.environ['THEANO_FLAGS'] += ',floatX=float32' os.environ['THEANO_FLAGS'] += ',warn_float64=warn' os.environ['THEANO_FLAGS'] += ',cast_policy=numpy+floatX' #os.environ['THEANO_FLAGS'] += ',allow_gc=True' os.environ['THEANO_FLAGS'] += ',print_active_device=False' os.environ['THEANO_FLAGS'] += ',exception_verbosity=high' # Highly verbose debugging os.environ['THEANO_FLAGS'] += ',mode=FAST_RUN' os.environ['THEANO_FLAGS'] += ',nvcc.fastmath=False' # True: makes div and sqrt faster at the cost of precision, and possible bugs #os.environ['THEANO_FLAGS'] += ',optimizer_including=cudnn' # Comment out if CUDNN is not available # change theano to wrapper try: #import theano import backend.nn_wrapper as K except EnvironmentError: L.exception() global logger #if theano.config.device == "gpu": # L.info( # "Device: " + theano.config.device.upper() + " " # + str(theano.sandbox.cuda.active_device_number()) # + " (" + str(theano.sandbox.cuda.active_device_name()) + ")" # ) #else: # L.info("Device: " + theano.config.device.upper()) #global K try: K.set_platform('tensorflow') # theano is working L.info("Creating a variable inside utils") import numpy as np val = np.random.random((4, 2)) tmp = K.variable(val) except: print >> sys.stderr, "Unexpected error:", sys.exc_info() raise TypeError("Cannot set the platform")
def set_theano_device(device, threads): import sys import dlm.io.logging as L xassert(device == "cpu" or device.startswith("gpu"), "The device can only be 'cpu', 'gpu' or 'gpu<id>'") if device.startswith("gpu") and len(device) > 3: try: gpu_id = int(device[3:]) if not is_gpu_free(gpu_id): L.warning('The selected GPU (GPU' + str(gpu_id) + ') is apparently busy.') except ValueError: L.error("Unknown GPU device format: " + device) if device.startswith("gpu"): L.warning('Running on GPU yields non-deterministic results.') xassert( sys.modules.has_key('theano') == False, "dlm.utils.set_theano_device() function cannot be called after importing theano" ) os.environ['OMP_NUM_THREADS'] = str(threads) os.environ['THEANO_FLAGS'] = 'device=' + device os.environ['THEANO_FLAGS'] += ',force_device=True' os.environ['THEANO_FLAGS'] += ',floatX=float32' os.environ['THEANO_FLAGS'] += ',warn_float64=warn' os.environ['THEANO_FLAGS'] += ',cast_policy=numpy+floatX' #os.environ['THEANO_FLAGS'] += ',allow_gc=True' os.environ['THEANO_FLAGS'] += ',print_active_device=False' os.environ[ 'THEANO_FLAGS'] += ',exception_verbosity=high' # Highly verbose debugging os.environ['THEANO_FLAGS'] += ',mode=FAST_RUN' os.environ[ 'THEANO_FLAGS'] += ',nvcc.fastmath=False' # True: makes div and sqrt faster at the cost of precision, and possible bugs #os.environ['THEANO_FLAGS'] += ',optimizer_including=cudnn' # Comment out if CUDNN is not available try: import theano except EnvironmentError: L.exception() global logger if theano.config.device == "gpu": L.info("Device: " + theano.config.device.upper() + " " + str(theano.sandbox.cuda.active_device_number()) + " (" + str(theano.sandbox.cuda.active_device_name()) + ")") else: L.info("Device: " + theano.config.device.upper())
def set_theano_device(device, threads): import sys import dlm.io.logging as L xassert(device == "cpu" or device.startswith("gpu"), "The device can only be 'cpu', 'gpu' or 'gpu<id>'") if device.startswith("gpu") and len(device) > 3: try: gpu_id = int(device[3:]) if not is_gpu_free(gpu_id): L.warning('The selected GPU (GPU' + str(gpu_id) + ') is apparently busy.') except ValueError: L.error("Unknown GPU device format: " + device) if device.startswith("gpu"): L.warning('Running on GPU yields non-deterministic results.') xassert(sys.modules.has_key('theano') == False, "dlm.utils.set_theano_device() function cannot be called after importing theano") os.environ['OMP_NUM_THREADS'] = str(threads) os.environ['THEANO_FLAGS'] = 'device=' + device os.environ['THEANO_FLAGS'] += ',force_device=True' os.environ['THEANO_FLAGS'] += ',floatX=float32' os.environ['THEANO_FLAGS'] += ',warn_float64=warn' os.environ['THEANO_FLAGS'] += ',cast_policy=numpy+floatX' # os.environ['THEANO_FLAGS'] += ',cuda.root=/usr/local/cuda' #os.environ['THEANO_FLAGS'] += ',allow_gc=True' os.environ['THEANO_FLAGS'] += ',print_active_device=False' os.environ['THEANO_FLAGS'] += ',exception_verbosity=high' # Highly verbose debugging os.environ['THEANO_FLAGS'] += ',mode=FAST_RUN' os.environ['THEANO_FLAGS'] += ',nvcc.fastmath=False' # True: makes div and sqrt faster at the cost of precision, and possible bugs #os.environ['THEANO_FLAGS'] += ',optimizer_including=cudnn' # Comment out if CUDNN is not available try: import theano except EnvironmentError: L.exception() global logger if theano.config.device == "gpu": L.info( "Device: " + theano.config.device.upper() + " " + str(theano.sandbox.cuda.active_device_number()) + " (" + str(theano.sandbox.cuda.active_device_name()) + ")" ) else: L.info("Device: " + theano.config.device.upper())
from dlm.reranker import augmenter output_nbest_path = args.out_dir + '/augmented.nbest' if args.no_aug: shutil.copy(args.input_nbest, output_nbest_path) else: augmenter.augment(args.model_path, args.input_nbest, args.vocab_path, output_nbest_path) with open(args.weights, 'r') as input_weights: lines = input_weights.readlines() if len(lines) > 1: L.warning( "Weights file has more than one line. I'll read the 1st and ignore the rest." ) weights = np.asarray(lines[0].strip().split(" "), dtype=float) prefix = os.path.basename(args.input_nbest) input_aug_nbest = NBestList(output_nbest_path, mode='r') output_nbest = NBestList(args.out_dir + '/' + prefix + '.reranked.nbest', mode='w') output_1best = codecs.open(args.out_dir + '/' + prefix + '.reranked.1best', mode='w', encoding='UTF-8') def is_number(s): try: float(s)
def set_theano_device(device, threads): import sys import dlm.io.logging as L xassert(device == "cpu" or device.startswith("gpu"), "The device can only be 'cpu', 'gpu' or 'gpu<id>'") if device.startswith("gpu") and len(device) > 3: try: gpu_id = int(device[3:]) if not is_gpu_free(gpu_id): L.warning('The selected GPU (GPU' + str(gpu_id) + ') is apparently busy.') except ValueError: L.error("Unknown GPU device format: " + device) if device.startswith("gpu"): L.warning('Running on GPU yields non-deterministic results.') xassert( sys.modules.has_key('theano') == False, "dlm.utils.set_theano_device() function cannot be called after importing theano" ) os.environ['OMP_NUM_THREADS'] = str(threads) os.environ['THEANO_FLAGS'] = 'device=' + device os.environ['THEANO_FLAGS'] += ',force_device=True' os.environ['THEANO_FLAGS'] += ',floatX=float32' os.environ['THEANO_FLAGS'] += ',warn_float64=warn' os.environ['THEANO_FLAGS'] += ',cast_policy=numpy+floatX' #os.environ['THEANO_FLAGS'] += ',allow_gc=True' os.environ['THEANO_FLAGS'] += ',print_active_device=False' os.environ[ 'THEANO_FLAGS'] += ',exception_verbosity=high' # Highly verbose debugging os.environ['THEANO_FLAGS'] += ',mode=FAST_RUN' os.environ[ 'THEANO_FLAGS'] += ',nvcc.fastmath=False' # True: makes div and sqrt faster at the cost of precision, and possible bugs #os.environ['THEANO_FLAGS'] += ',optimizer_including=cudnn' # Comment out if CUDNN is not available # change theano to wrapper try: #import theano import backend.nn_wrapper as K except EnvironmentError: L.exception() global logger #if theano.config.device == "gpu": # L.info( # "Device: " + theano.config.device.upper() + " " # + str(theano.sandbox.cuda.active_device_number()) # + " (" + str(theano.sandbox.cuda.active_device_name()) + ")" # ) #else: # L.info("Device: " + theano.config.device.upper()) #global K try: K.set_platform('tensorflow') # theano is working L.info("Creating a variable inside utils") import numpy as np val = np.random.random((4, 2)) tmp = K.variable(val) except: print >> sys.stderr, "Unexpected error:", sys.exc_info() raise TypeError("Cannot set the platform")
U.mkdir_p(args.out_dir) from dlm.reranker import augmenter output_nbest_path = args.out_dir + '/augmented.nbest' if args.no_aug: shutil.copy(args.input_nbest, output_nbest_path) else: augmenter.augment(args.model_path, args.input_nbest, args.vocab_path, output_nbest_path) with open(args.weights, 'r') as input_weights: lines = input_weights.readlines() if len(lines) > 1: L.warning("Weights file has more than one line. I'll read the 1st and ignore the rest.") weights = np.asarray(lines[0].strip().split(" "), dtype=float) prefix = os.path.basename(args.input_nbest) input_aug_nbest = NBestList(output_nbest_path, mode='r') output_nbest = NBestList(args.out_dir + '/' + prefix + '.reranked.nbest', mode='w') output_1best = codecs.open(args.out_dir + '/' + prefix + '.reranked.1best', mode='w', encoding='UTF-8') def is_number(s): try: float(s) return True except ValueError: return False counter = 0