Theano 配置和编译模式

配置

之前我们已经知道, theano 的配置可以用 config 模块来查看:

In [1]:

  1. import theano
  2. import theano.tensor as T
  3.  
  4. print theano.config
  1. floatX (('float64', 'float32', 'float16'))
  2. Doc: Default floating-point precision for python casts.
  3.  
  4. Note: float16 support is experimental, use at your own risk.
  5. Value: float32
  6.  
  7. warn_float64 (('ignore', 'warn', 'raise', 'pdb'))
  8. Doc: Do an action when a tensor variable with float64 dtype is created. They can't be run on the GPU with the current(old) gpu back-end and are slow with gamer GPUs.
  9. Value: ignore
  10.  
  11. cast_policy (('custom', 'numpy+floatX'))
  12. Doc: Rules for implicit type casting
  13. Value: custom
  14.  
  15. int_division (('int', 'raise', 'floatX'))
  16. Doc: What to do when one computes x / y, where both x and y are of integer types
  17. Value: int
  18.  
  19. device (cpu, gpu*, opencl*, cuda*)
  20. Doc: Default device for computations. If gpu*, change the default to try to move computation to it and to put shared variable of float32 on it. Do not use upper case letters, only lower case even if NVIDIA use capital letters.
  21. Value: gpu1
  22.  
  23. init_gpu_device (, gpu*, opencl*, cuda*)
  24. Doc: Initialize the gpu device to use, works only if device=cpu. Unlike 'device', setting this option will NOT move computations, nor shared variables, to the specified GPU. It can be used to run GPU-specific tests on a particular GPU.
  25. Value:
  26.  
  27. force_device (<function booltype at 0x7f1125276d70>)
  28. Doc: Raise an error if we can't use the specified device
  29. Value: False
  30.  
  31. <theano.configdefaults.ContextsParam object at 0x7f1129423090>
  32. Doc:
  33. Context map for multi-gpu operation. Format is a
  34. semicolon-separated list of names and device names in the
  35. 'name->dev_name' format. An example that would map name 'test' to
  36. device 'cuda0' and name 'test2' to device 'opencl0:0' follows:
  37. "test->cuda0;test2->opencl0:0".
  38.  
  39. Invalid context names are 'cpu', 'cuda*' and 'opencl*'
  40.  
  41. Value:
  42.  
  43. print_active_device (<function booltype at 0x7f1125279050>)
  44. Doc: Print active device at when the GPU device is initialized.
  45. Value: True
  46.  
  47. enable_initial_driver_test (<function booltype at 0x7f11252791b8>)
  48. Doc: Tests the nvidia driver when a GPU device is initialized.
  49. Value: True
  50.  
  51. cuda.root (<type 'str'>)
  52. Doc: directory with bin/, lib/, include/ for cuda utilities.
  53. This directory is included via -L and -rpath when linking
  54. dynamically compiled modules. If AUTO and nvcc is in the
  55. path, it will use one of nvcc parent directory. Otherwise
  56. /usr/local/cuda will be used. Leave empty to prevent extra
  57. linker directives. Default: environment variable "CUDA_ROOT"
  58. or else "AUTO".
  59.  
  60. Value: /usr/local/cuda-7.0
  61.  
  62. <theano.configparser.ConfigParam object at 0x7f1129423450>
  63. Doc: Extra compiler flags for nvcc
  64. Value:
  65.  
  66. nvcc.compiler_bindir (<type 'str'>)
  67. Doc: If defined, nvcc compiler driver will seek g++ and gcc in this directory
  68. Value:
  69.  
  70. nvcc.fastmath (<function booltype at 0x7f1125279500>)
  71. Doc:
  72. Value: False
  73.  
  74. gpuarray.sync (<function booltype at 0x7f1125279668>)
  75. Doc: If True, every op will make sure its work is done before
  76. returning. Setting this to True will slow down execution,
  77. but give much more accurate results in profiling.
  78. Value: False
  79.  
  80. gpuarray.preallocate (<type 'float'>)
  81. Doc: If 0 it doesn't do anything. If between 0 and 1 it
  82. will preallocate that fraction of the total GPU memory.
  83. If 1 or greater it will preallocate that amount of memory
  84. (in megabytes).
  85. Value: 0.0
  86.  
  87. <theano.configparser.ConfigParam object at 0x7f11252e5710>
  88. Doc: This flag is deprecated; use dnn.conv.algo_fwd.
  89. Value: True
  90.  
  91. <theano.configparser.ConfigParam object at 0x7f11252e57d0>
  92. Doc: This flag is deprecated; use dnn.conv.algo_bwd.
  93. Value: True
  94.  
  95. <theano.configparser.ConfigParam object at 0x7f11252e5810>
  96. Doc: This flag is deprecated; use dnn.conv.algo_bwd_data and dnn.conv.algo_bwd_filter.
  97. Value: True
  98.  
  99. dnn.conv.algo_fwd (('small', 'none', 'large', 'fft', 'fft_tiling', 'guess_once', 'guess_on_shape_change', 'time_once', 'time_on_shape_change'))
  100. Doc: Default implementation to use for CuDNN forward convolution.
  101. Value: small
  102.  
  103. dnn.conv.algo_bwd_data (('none', 'deterministic', 'fft', 'fft_tiling', 'guess_once', 'guess_on_shape_change', 'time_once', 'time_on_shape_change'))
  104. Doc: Default implementation to use for CuDNN backward convolution to get the gradients of the convolution with regard to the inputs.
  105. Value: none
  106.  
  107. dnn.conv.algo_bwd_filter (('none', 'deterministic', 'fft', 'small', 'guess_once', 'guess_on_shape_change', 'time_once', 'time_on_shape_change'))
  108. Doc: Default implementation to use for CuDNN backward convolution to get the gradients of the convolution with regard to the filters.
  109. Value: none
  110.  
  111. dnn.conv.precision (('as_input', 'float16', 'float32', 'float64'))
  112. Doc: Default data precision to use for the computation in CuDNN convolutions (defaults to the same dtype as the inputs of the convolutions).
  113. Value: as_input
  114.  
  115. dnn.include_path (<type 'str'>)
  116. Doc: Location of the cudnn header (defaults to the cuda root)
  117. Value: /usr/local/cuda-7.0/include
  118.  
  119. dnn.library_path (<type 'str'>)
  120. Doc: Location of the cudnn header (defaults to the cuda root)
  121. Value: /usr/local/cuda-7.0/lib64
  122.  
  123. assert_no_cpu_op (('ignore', 'warn', 'raise', 'pdb'))
  124. Doc: Raise an error/warning if there is a CPU op in the computational graph.
  125. Value: ignore
  126.  
  127. mode (('Mode', 'ProfileMode', 'DebugMode', 'FAST_RUN', 'NanGuardMode', 'FAST_COMPILE', 'PROFILE_MODE', 'DEBUG_MODE'))
  128. Doc: Default compilation mode
  129. Value: Mode
  130.  
  131. cxx (<type 'str'>)
  132. Doc: The C++ compiler to use. Currently only g++ is supported, but supporting additional compilers should not be too difficult. If it is empty, no C++ code is compiled.
  133. Value: /usr/bin/g++
  134.  
  135. linker (('cvm', 'c|py', 'py', 'c', 'c|py_nogc', 'vm', 'vm_nogc', 'cvm_nogc'))
  136. Doc: Default linker used if the theano flags mode is Mode or ProfileMode(deprecated)
  137. Value: cvm
  138.  
  139. allow_gc (<function booltype at 0x7f112527b5f0>)
  140. Doc: Do we default to delete intermediate results during Theano function calls? Doing so lowers the memory requirement, but asks that we reallocate memory at the next function call. This is implemented for the default linker, but may not work for all linkers.
  141. Value: True
  142.  
  143. optimizer (('fast_run', 'merge', 'fast_compile', 'None'))
  144. Doc: Default optimizer. If not None, will use this linker with the Mode object (not ProfileMode(deprecated) or DebugMode)
  145. Value: fast_run
  146.  
  147. optimizer_verbose (<function booltype at 0x7f112527b7d0>)
  148. Doc: If True, we print all optimization being applied
  149. Value: False
  150.  
  151. on_opt_error (('warn', 'raise', 'pdb', 'ignore'))
  152. Doc: What to do when an optimization crashes: warn and skip it, raise the exception, or fall into the pdb debugger.
  153. Value: warn
  154.  
  155. <theano.configparser.ConfigParam object at 0x7f11252e5c90>
  156. Doc: This config option was removed in 0.5: do not use it!
  157. Value: True
  158.  
  159. nocleanup (<function booltype at 0x7f112527ba28>)
  160. Doc: Suppress the deletion of code files that did not compile cleanly
  161. Value: False
  162.  
  163. on_unused_input (('raise', 'warn', 'ignore'))
  164. Doc: What to do if a variable in the 'inputs' list of theano.function() is not used in the graph.
  165. Value: raise
  166.  
  167. tensor.cmp_sloppy (<type 'int'>)
  168. Doc: Relax tensor._allclose (0) not at all, (1) a bit, (2) more
  169. Value: 0
  170.  
  171. tensor.local_elemwise_fusion (<function booltype at 0x7f112527bcf8>)
  172. Doc: Enable or not in fast_run mode(fast_run optimization) the elemwise fusion optimization
  173. Value: True
  174.  
  175. gpu.local_elemwise_fusion (<function booltype at 0x7f112527be60>)
  176. Doc: Enable or not in fast_run mode(fast_run optimization) the gpu elemwise fusion optimization
  177. Value: True
  178.  
  179. lib.amdlibm (<function booltype at 0x7f112527f050>)
  180. Doc: Use amd's amdlibm numerical library
  181. Value: False
  182.  
  183. gpuelemwise.sync (<function booltype at 0x7f112527f1b8>)
  184. Doc: when true, wait that the gpu fct finished and check it error code.
  185. Value: True
  186.  
  187. traceback.limit (<type 'int'>)
  188. Doc: The number of stack to trace. -1 mean all.
  189. Value: 8
  190.  
  191. experimental.mrg (<function booltype at 0x7f112527f398>)
  192. Doc: Another random number generator that work on the gpu
  193. Value: False
  194.  
  195. experimental.unpickle_gpu_on_cpu (<function booltype at 0x7f112527f500>)
  196. Doc: Allow unpickling of pickled CudaNdarrays as numpy.ndarrays.This is useful, if you want to open a CudaNdarray without having cuda installed.If you have cuda installed, this will force unpickling tobe done on the cpu to numpy.ndarray.Please be aware that this may get you access to the data,however, trying to unpicke gpu functions will not succeed.This flag is experimental and may be removed any time, whengpu<>cpu transparency is solved.
  197. Value: False
  198.  
  199. numpy.seterr_all (('ignore', 'warn', 'raise', 'call', 'print', 'log', 'None'))
  200. Doc: ("Sets numpy's behaviour for floating-point errors, ", "see numpy.seterr. 'None' means not to change numpy's default, which can be different for different numpy releases. This flag sets the default behaviour for all kinds of floating-point errors, its effect can be overriden for specific errors by the following flags: seterr_divide, seterr_over, seterr_under and seterr_invalid.")
  201. Value: ignore
  202.  
  203. numpy.seterr_divide (('None', 'ignore', 'warn', 'raise', 'call', 'print', 'log'))
  204. Doc: Sets numpy's behavior for division by zero, see numpy.seterr. 'None' means using the default, defined by numpy.seterr_all.
  205. Value: None
  206.  
  207. numpy.seterr_over (('None', 'ignore', 'warn', 'raise', 'call', 'print', 'log'))
  208. Doc: Sets numpy's behavior for floating-point overflow, see numpy.seterr. 'None' means using the default, defined by numpy.seterr_all.
  209. Value: None
  210.  
  211. numpy.seterr_under (('None', 'ignore', 'warn', 'raise', 'call', 'print', 'log'))
  212. Doc: Sets numpy's behavior for floating-point underflow, see numpy.seterr. 'None' means using the default, defined by numpy.seterr_all.
  213. Value: None
  214.  
  215. numpy.seterr_invalid (('None', 'ignore', 'warn', 'raise', 'call', 'print', 'log'))
  216. Doc: Sets numpy's behavior for invalid floating-point operation, see numpy.seterr. 'None' means using the default, defined by numpy.seterr_all.
  217. Value: None
  218.  
  219. warn.ignore_bug_before (('0.6', 'None', 'all', '0.3', '0.4', '0.4.1', '0.5', '0.7'))
  220. Doc: If 'None', we warn about all Theano bugs found by default. If 'all', we don't warn about Theano bugs found by default. If a version, we print only the warnings relative to Theano bugs found after that version. Warning for specific bugs can be configured with specific [warn] flags.
  221. Value: 0.6
  222.  
  223. warn.argmax_pushdown_bug (<function booltype at 0x7f112527fa28>)
  224. Doc: Warn if in past version of Theano we generated a bug with the theano.tensor.nnet.nnet.local_argmax_pushdown optimization. Was fixed 27 may 2010
  225. Value: False
  226.  
  227. warn.gpusum_01_011_0111_bug (<function booltype at 0x7f112527fb90>)
  228. Doc: Warn if we are in a case where old version of Theano had a silent bug with GpuSum pattern 01,011 and 0111 when the first dimensions was bigger then 4096. Was fixed 31 may 2010
  229. Value: False
  230.  
  231. warn.sum_sum_bug (<function booltype at 0x7f112527fcf8>)
  232. Doc: Warn if we are in a case where Theano version between version 9923a40c7b7a and the 2 august 2010 (fixed date), generated an error in that case. This happens when there are 2 consecutive sums in the graph, bad code was generated. Was fixed 2 August 2010
  233. Value: False
  234.  
  235. warn.sum_div_dimshuffle_bug (<function booltype at 0x7f112527fe60>)
  236. Doc: Warn if previous versions of Theano (between rev. 3bd9b789f5e8, 2010-06-16, and cfc6322e5ad4, 2010-08-03) would have given incorrect result. This bug was triggered by sum of division of dimshuffled tensors.
  237. Value: False
  238.  
  239. warn.subtensor_merge_bug (<function booltype at 0x7f1125284050>)
  240. Doc: Warn if previous versions of Theano (before 0.5rc2) could have given incorrect results when indexing into a subtensor with negative stride (for instance, for instance, x[a:b:-1][c]).
  241. Value: False
  242.  
  243. warn.gpu_set_subtensor1 (<function booltype at 0x7f11252841b8>)
  244. Doc: Warn if previous versions of Theano (before 0.6) could have given incorrect results when moving to the gpu set_subtensor(x[int vector], new_value)
  245. Value: False
  246.  
  247. warn.vm_gc_bug (<function booltype at 0x7f1125284320>)
  248. Doc: There was a bug that existed in the default Theano configuration, only in the development version between July 5th 2012 and July 30th 2012. This was not in a released version. If your code was affected by this bug, a warning will be printed during the code execution if you use the `linker=vm,vm.lazy=True,warn.vm_gc_bug=True` Theano flags. This warning is disabled by default as the bug was not released.
  249. Value: False
  250.  
  251. warn.signal_conv2d_interface (<function booltype at 0x7f1125284488>)
  252. Doc: Warn we use the new signal.conv2d() when its interface changed mid June 2014
  253. Value: True
  254.  
  255. warn.reduce_join (<function booltype at 0x7f11252845f0>)
  256. Doc: Your current code is fine, but Theano versions prior to 0.7 (or this development version) might have given an incorrect result. To disable this warning, set the Theano flag warn.reduce_join to False. The problem was an optimization, that modified the pattern "Reduce{scalar.op}(Join(axis=0, a, b), axis=0)", did not check the reduction axis. So if the reduction axis was not 0, you got a wrong answer.
  257. Value: True
  258.  
  259. warn.inc_set_subtensor1 (<function booltype at 0x7f1125284758>)
  260. Doc: Warn if previous versions of Theano (before 0.7) could have given incorrect results for inc_subtensor and set_subtensor when using some patterns of advanced indexing (indexing with one vector or matrix of ints).
  261. Value: True
  262.  
  263. compute_test_value (('off', 'ignore', 'warn', 'raise', 'pdb'))
  264. Doc: If 'True', Theano will run each op at graph build time, using Constants, SharedVariables and the tag 'test_value' as inputs to the function. This helps the user track down problems in the graph before it gets optimized.
  265. Value: off
  266.  
  267. print_test_value (<function booltype at 0x7f1125284938>)
  268. Doc: If 'True', the __eval__ of a Theano variable will return its test_value when this is available. This has the practical conseguence that, e.g., in debugging `my_var` will print the same as `my_var.tag.test_value` when a test value is defined.
  269. Value: False
  270.  
  271. compute_test_value_opt (('off', 'ignore', 'warn', 'raise', 'pdb'))
  272. Doc: For debugging Theano optimization only. Same as compute_test_value, but is used during Theano optimization
  273. Value: off
  274.  
  275. unpickle_function (<function booltype at 0x7f1125284b18>)
  276. Doc: Replace unpickled Theano functions with None. This is useful to unpickle old graphs that pickled them when it shouldn't
  277. Value: True
  278.  
  279. reoptimize_unpickled_function (<function booltype at 0x7f1125284c80>)
  280. Doc: Re-optimize the graph when a theano function is unpickled from the disk.
  281. Value: False
  282.  
  283. exception_verbosity (('low', 'high'))
  284. Doc: If 'low', the text of exceptions will generally refer to apply nodes with short names such as Elemwise{add_no_inplace}. If 'high', some exceptions will also refer to apply nodes with long descriptions like:
  285. A. Elemwise{add_no_inplace}
  286. B. log_likelihood_v_given_h
  287. C. log_likelihood_h
  288. Value: low
  289.  
  290. openmp (<function booltype at 0x7f1125284e60>)
  291. Doc: Allow (or not) parallel computation on the CPU with OpenMP. This is the default value used when creating an Op that supports OpenMP parallelization. It is preferable to define it via the Theano configuration file ~/.theanorc or with the environment variable THEANO_FLAGS. Parallelization is only done for some operations that implement it, and even for operations that implement parallelism, each operation is free to respect this flag or not. You can control the number of threads used with the environment variable OMP_NUM_THREADS. If it is set to 1, we disable openmp in Theano by default.
  292. Value: False
  293.  
  294. openmp_elemwise_minsize (<type 'int'>)
  295. Doc: If OpenMP is enabled, this is the minimum size of vectors for which the openmp parallelization is enabled in element wise ops.
  296. Value: 200000
  297.  
  298. check_input (<function booltype at 0x7f11252870c8>)
  299. Doc: Specify if types should check their input in their C code. It can be used to speed up compilation, reduce overhead (particularly for scalars) and reduce the number of generated C files.
  300. Value: True
  301.  
  302. cache_optimizations (<function booltype at 0x7f1125287230>)
  303. Doc: WARNING: work in progress, does not work yet. Specify if the optimization cache should be used. This cache will any optimized graph and its optimization. Actually slow downs a lot the first optimization, and could possibly still contains some bugs. Use at your own risks.
  304. Value: False
  305.  
  306. unittests.rseed (<type 'str'>)
  307. Doc: Seed to use for randomized unit tests. Special value 'random' means using a seed of None.
  308. Value: 666
  309.  
  310. compile.wait (<type 'int'>)
  311. Doc: Time to wait before retrying to aquire the compile lock.
  312. Value: 5
  313.  
  314. compile.timeout (<type 'int'>)
  315. Doc: In seconds, time that a process will wait before deciding to
  316. override an existing lock. An override only happens when the existing
  317. lock is held by the same owner *and* has not been 'refreshed' by this
  318. owner for more than this period. Refreshes are done every half timeout
  319. period for running processes.
  320. Value: 120
  321.  
  322. compiledir_format (<type 'str'>)
  323. Doc: Format string for platform-dependent compiled module subdirectory
  324. (relative to base_compiledir). Available keys: gxx_version, hostname,
  325. numpy_version, platform, processor, python_bitwidth,
  326. python_int_bitwidth, python_version, short_platform, theano_version.
  327. Defaults to 'compiledir_%(short_platform)s-%(processor)s-%(python_vers
  328. ion)s-%(python_bitwidth)s'.
  329. Value: compiledir_%(short_platform)s-%(processor)s-%(python_version)s-%(python_bitwidth)s
  330.  
  331. <theano.configparser.ConfigParam object at 0x7f110f7f8f90>
  332. Doc: platform-independent root directory for compiled modules
  333. Value: /home/lijin/.theano
  334.  
  335. <theano.configparser.ConfigParam object at 0x7f110f80c050>
  336. Doc: platform-dependent cache directory for compiled modules
  337. Value: /home/lijin/.theano/compiledir_Linux-3.13--generic-x86_64-with-Ubuntu-14.04-trusty-x86_64-2.7.6-64
  338.  
  339. cmodule.mac_framework_link (<function booltype at 0x7f110f802cf8>)
  340. Doc: If set to True, breaks certain MacOS installations with the infamous Bus Error
  341. Value: False
  342.  
  343. cmodule.warn_no_version (<function booltype at 0x7f110f802e60>)
  344. Doc: If True, will print a warning when compiling one or more Op with C code that can't be cached because there is no c_code_cache_version() function associated to at least one of those Ops.
  345. Value: False
  346.  
  347. cmodule.remove_gxx_opt (<function booltype at 0x7f110f80b050>)
  348. Doc: If True, will remove the -O* parameter passed to g++.This is useful to debug in gdb modules compiled by Theano.The parameter -g is passed by default to g++
  349. Value: False
  350.  
  351. cmodule.compilation_warning (<function booltype at 0x7f110f80b1b8>)
  352. Doc: If True, will print compilation warnings.
  353. Value: False
  354.  
  355. cmodule.preload_cache (<function booltype at 0x7f110f80b320>)
  356. Doc: If set to True, will preload the C module cache at import time
  357. Value: False
  358.  
  359. gcc.cxxflags (<type 'str'>)
  360. Doc: Extra compiler flags for gcc
  361. Value:
  362.  
  363. metaopt.verbose (<function booltype at 0x7f110f7e0ed8>)
  364. Doc: Enable verbose output for meta optimizers
  365. Value: False
  366.  
  367. optdb.position_cutoff (<type 'float'>)
  368. Doc: Where to stop eariler during optimization. It represent the position of the optimizer where to stop.
  369. Value: inf
  370.  
  371. optdb.max_use_ratio (<type 'float'>)
  372. Doc: A ratio that prevent infinite loop in EquilibriumOptimizer.
  373. Value: 5.0
  374.  
  375. profile (<function booltype at 0x7f110f7825f0>)
  376. Doc: If VM should collect profile information
  377. Value: False
  378.  
  379. profile_optimizer (<function booltype at 0x7f110f782758>)
  380. Doc: If VM should collect optimizer profile information
  381. Value: False
  382.  
  383. profile_memory (<function booltype at 0x7f110f7828c0>)
  384. Doc: If VM should collect memory profile information and print it
  385. Value: False
  386.  
  387. <theano.configparser.ConfigParam object at 0x7f110f79bc50>
  388. Doc: Useful only for the vm linkers. When lazy is None, auto detect if lazy evaluation is needed and use the apropriate version. If lazy is True/False, force the version used between Loop/LoopGC and Stack.
  389. Value: None
  390.  
  391. optimizer_excluding (<type 'str'>)
  392. Doc: When using the default mode, we will remove optimizer with these tags. Separate tags with ':'.
  393. Value:
  394.  
  395. optimizer_including (<type 'str'>)
  396. Doc: When using the default mode, we will add optimizer with these tags. Separate tags with ':'.
  397. Value:
  398.  
  399. optimizer_requiring (<type 'str'>)
  400. Doc: When using the default mode, we will require optimizer with these tags. Separate tags with ':'.
  401. Value:
  402.  
  403. DebugMode.patience (<type 'int'>)
  404. Doc: Optimize graph this many times to detect inconsistency
  405. Value: 10
  406.  
  407. DebugMode.check_c (<function booltype at 0x7f110f74a8c0>)
  408. Doc: Run C implementations where possible
  409. Value: True
  410.  
  411. DebugMode.check_py (<function booltype at 0x7f110f74aa28>)
  412. Doc: Run Python implementations where possible
  413. Value: True
  414.  
  415. DebugMode.check_finite (<function booltype at 0x7f110f74ab90>)
  416. Doc: True -> complain about NaN/Inf results
  417. Value: True
  418.  
  419. DebugMode.check_strides (<type 'int'>)
  420. Doc: Check that Python- and C-produced ndarrays have same strides. On difference: (0) - ignore, (1) warn, or (2) raise error
  421. Value: 0
  422.  
  423. DebugMode.warn_input_not_reused (<function booltype at 0x7f110f74ade8>)
  424. Doc: Generate a warning when destroy_map or view_map says that an op works inplace, but the op did not reuse the input for its output.
  425. Value: True
  426.  
  427. DebugMode.check_preallocated_output (<type 'str'>)
  428. Doc: Test thunks with pre-allocated memory as output storage. This is a list of strings separated by ":". Valid values are: "initial" (initial storage in storage map, happens with Scan),"previous" (previously-returned memory), "c_contiguous", "f_contiguous", "strided" (positive and negative strides), "wrong_size" (larger and smaller dimensions), and "ALL" (all of the above).
  429. Value:
  430.  
  431. DebugMode.check_preallocated_output_ndim (<type 'int'>)
  432. Doc: When testing with "strided" preallocated output memory, test all combinations of strides over that number of (inner-most) dimensions. You may want to reduce that number to reduce memory or time usage, but it is advised to keep a minimum of 2.
  433. Value: 4
  434.  
  435. profiling.time_thunks (<function booltype at 0x7f110ef81320>)
  436. Doc: Time individual thunks when profiling
  437. Value: True
  438.  
  439. profiling.n_apply (<type 'int'>)
  440. Doc: Number of Apply instances to print by default
  441. Value: 20
  442.  
  443. profiling.n_ops (<type 'int'>)
  444. Doc: Number of Ops to print by default
  445. Value: 20
  446.  
  447. profiling.output_line_width (<type 'int'>)
  448. Doc: Max line width for the profiling output
  449. Value: 512
  450.  
  451. profiling.min_memory_size (<type 'int'>)
  452. Doc: For the memory profile, do not print Apply nodes if the size
  453. of their outputs (in bytes) is lower than this threshold
  454. Value: 1024
  455.  
  456. profiling.min_peak_memory (<function booltype at 0x7f110ef81848>)
  457. Doc: The min peak memory usage of the order
  458. Value: False
  459.  
  460. profiling.destination (<type 'str'>)
  461. Doc:
  462. File destination of the profiling output
  463.  
  464. Value: stderr
  465.  
  466. profiling.debugprint (<function booltype at 0x7f110ef81a28>)
  467. Doc:
  468. Do a debugprint of the profiled functions
  469.  
  470. Value: False
  471.  
  472. ProfileMode.n_apply_to_print (<type 'int'>)
  473. Doc: Number of apply instances to print by default
  474. Value: 15
  475.  
  476. ProfileMode.n_ops_to_print (<type 'int'>)
  477. Doc: Number of ops to print by default
  478. Value: 20
  479.  
  480. ProfileMode.min_memory_size (<type 'int'>)
  481. Doc: For the memory profile, do not print apply nodes if the size of their outputs (in bytes) is lower then this threshold
  482. Value: 1024
  483.  
  484. ProfileMode.profile_memory (<function booltype at 0x7f110ef90c80>)
  485. Doc: Enable profiling of memory used by Theano functions
  486. Value: False
  487.  
  488. on_shape_error (('warn', 'raise'))
  489. Doc: warn: print a warning and use the default value. raise: raise an error
  490. Value: warn
  491.  
  492. tensor.insert_inplace_optimizer_validate_nb (<type 'int'>)
  493. Doc: -1: auto, if graph have less then 500 nodes 1, else 10
  494. Value: -1
  495.  
  496. experimental.local_alloc_elemwise (<function booltype at 0x7f11065ed7d0>)
  497. Doc: DEPRECATED: If True, enable the experimental optimization local_alloc_elemwise. Generates error if not True. Use optimizer_excluding=local_alloc_elemwise to dsiable.
  498. Value: True
  499.  
  500. experimental.local_alloc_elemwise_assert (<function booltype at 0x7f11065ed848>)
  501. Doc: When the local_alloc_elemwise is applied, add an assert to highlight shape errors.
  502. Value: True
  503.  
  504. blas.ldflags (<type 'str'>)
  505. Doc: lib[s] to include for [Fortran] level-3 blas implementation
  506. Value: -lblas
  507.  
  508. warn.identify_1pexp_bug (<function booltype at 0x7f110653a5f0>)
  509. Doc: Warn if Theano versions prior to 7987b51 (2011-12-18) could have yielded a wrong result due to a bug in the is_1pexp function
  510. Value: False
  511.  
  512. scan.allow_gc (<function booltype at 0x7f11042e07d0>)
  513. Doc: Allow/disallow gc inside of Scan (default: False)
  514. Value: False
  515.  
  516. scan.allow_output_prealloc (<function booltype at 0x7f1104271668>)
  517. Doc: Allow/disallow memory preallocation for outputs inside of scan (default: True)
  518. Value: True
  519.  
  520. pycuda.init (<function booltype at 0x7f110429a2a8>)
  521. Doc: If True, always initialize PyCUDA when Theano want to
  522. initilize the GPU. Currently, we must always initialize
  523. PyCUDA before Theano do it. Setting this flag to True,
  524. ensure that, but always import PyCUDA. It can be done
  525. manually by importing theano.misc.pycuda_init before theano
  526. initialize the GPU device.
  527.  
  528. Value: False
  529.  
  530. cublas.lib (<type 'str'>)
  531. Doc: Name of the cuda blas library for the linker.
  532. Value: cublas
  533.  
  534. lib.cnmem (<type 'float'>)
  535. Doc: Do we enable CNMeM or not (a faster CUDA memory allocator).
  536.  
  537. The parameter represent the start size (in MB or % of
  538. total GPU memory) of the memory pool.
  539.  
  540. 0: not enabled.
  541. 0 < N <= 1: % of the total GPU memory (clipped to .985 for driver memory)
  542. > 0: use that number of MB of memory.
  543.  
  544.  
  545. Value: 0.0
  546.  
  547.  
  1. Using gpu device 1: Tesla K10.G2.8GB (CNMeM is disabled)

这些配置影响着 theano 的运行,很多的参数都是只读的,因此,我们应当尽量避免在程序中直接修改这些参数

大部分参数都有指定的默认值,我们可以在 .theanorc 文件中对配置进行修改,也可以在环境变量 THEANO_FLAGS 中进行修改,它们的优先级顺序如下:

  • 首先是对 theano.config.<property> 的赋值
  • 然后是 THEANO_FLAGS 环境变量指定的内容
  • 最后是 .theanorc 文件或者 THEANORC 环境变量所指示的文件中的内容 具体的参数含义可以参考:

http://deeplearning.net/software/theano/library/config.html

环境变量 THEANO_FLAGS

使用 THEANO_FLAGS 环境变量,运行程序的方法如下:

  1. THEANO_FLAGS='floatX=float32,device=gpu0,nvcc.fastmath=True' python <myscript>.py

如果是 window 下,则需要进行稍微的改动:

  1. set THEANO_FLAGS='floatX=float32,device=gpu0,nvcc.fastmath=True' && python <myscript>.py

示例中的配置将浮点数的精度设为了 32 位,并将使用 GPU 0CUDAfastmath 模式进行编译和运算。

配置文件 THEANORC

环境变量 THEANORC 的默认位置为 $HOME/.theanorcwindows 下为 $HOME/.theanorc:$HOME/.theanorc.txt)。

与前面 THEANO_FLAGS 指定的内容相同的配置文件为:

  1. [global]
  2. floatX = float32
  3. device = gpu0
  4. [nvcc]
  5. fastmath = True

这里 [golbal] 对应的是 config 中的参数,如 config.device, config.modeconfig 的子模块中的参数,如 config.nvcc.fastmath, config.blas.ldflags 则需要用 [nvcc], [blas] 的部分去设置。

模式

每次调用 theano.function 的时候,那些符号变量之间的结构会被优化和计算,而优化和计算的模式都是由 config.mode 所决定的。

Theano 中定义了这四种模式:

  • FAST_COMPILE
    • compile.mode.Mode(linker='py', optimizer='fast_compile')
    • Python 实现,构造很快,运行慢
  • FAST_RUN
    • compile.mode.Mode(linker='cvm', optimizer='fast_run')
    • C 实现,构造较慢,运行快
  • DebugMode
    • compile.debugmode.DebugMode()
    • 调试模式,两种实现都可以
  • ProfileMode
    • compile.profilemode.ProfileMode()
    • C 实现,已经停用,使用 theano.profile 替代 更多的细节,可以参考:

http://deeplearning.net/software/theano/library/compile/mode.html#libdoc-compile-mode

Linkers

从上面的定义可以看出,一个模式由两部分构成,optimizerlinkerProfileModeDebugMode 模式使用自带的 linker

可用的 linker 可以从下表中查看:

http://deeplearning.net/software/theano/tutorial/modes.html#linkers

使用 DebugMode

一般在使用 FAST_RUN 或者 FAST_COMPILE 模式之前,最好先用 DebugMode 进行调试,不过速度会比前两个模式慢得多。

我们用一个实例看一下两者的区别:

In [2]:

  1. x = T.dvector('x')
  2.  
  3. f_1 = theano.function([x], 10 / x)
  4.  
  5. print f_1([5])
  6. print f_1([0])
  7. print f_1([7])
  1. [ 2.]
  2. [ inf]
  3. [ 1.42857143]

在非 Debug 模式下,除以 0 是合法的,但是在 DebugMode 下,会给出错误,帮助我们进行调试:

In [3]:

  1. f_2 = theano.function([x], 10 / x, mode='DebugMode')
  2.  
  3. print f_2([5])
  4. print f_2([0])
  5. print f_2([7])
  1. [ 2.]
  1. ---------------------------------------------------------------------------
  2. InvalidValueError Traceback (most recent call last)
  3. <ipython-input-3-ad8c4ba988ad> in <module>()
  4. 2
  5. 3 print f_2([5])
  6. ----> 4 print f_2([0])
  7. 5 print f_2([7])
  8.  
  9. /usr/local/lib/python2.7/dist-packages/theano/compile/function_module.pyc in __call__(self, *args, **kwargs)
  10. 857 t0_fn = time.time()
  11. 858 try:
  12. --> 859 outputs = self.fn()
  13. 860 except Exception:
  14. 861 if hasattr(self.fn, 'position_of_error'):
  15.  
  16. /usr/local/lib/python2.7/dist-packages/theano/compile/debugmode.pyc in deco()
  17. 2339 self.maker.mode.check_isfinite
  18. 2340 try:
  19. -> 2341 return f()
  20. 2342 finally:
  21. 2343 # put back the filter_checks_isfinite
  22.  
  23. /usr/local/lib/python2.7/dist-packages/theano/compile/debugmode.pyc in f()
  24. 2079 raise InvalidValueError(r, storage_map[r][0],
  25. 2080 hint='perform output',
  26. -> 2081 specific_hint=hint2)
  27. 2082 warn_inp = config.DebugMode.warn_input_not_reused
  28. 2083 py_inplace_outs = _check_inputs(
  29.  
  30. InvalidValueError: InvalidValueError
  31. type(variable) = TensorType(float64, vector)
  32. variable = Elemwise{true_div,no_inplace}.0
  33. type(value) = <type 'numpy.ndarray'>
  34. dtype(value) = float64
  35. shape(value) = (1,)
  36. value = [ inf]
  37. min(value) = inf
  38. max(value) = inf
  39. isfinite = False
  40. client_node = None
  41. hint = perform output
  42. specific_hint = non-finite elements not allowed
  43. context = ...
  44. Elemwise{true_div,no_inplace} [id A] ''
  45. |TensorConstant{(1,) of 10.0} [id B]
  46. |x [id C]
  47.  

更多细节可以参考:

http://deeplearning.net/software/theano/library/compile/debugmode.html#debugmode

原文: https://nbviewer.jupyter.org/github/lijin-THU/notes-python/blob/master/09-theano/09.05-configuration-settings-and-compiling-modes.ipynb