├── 0_julia_basics.ipynb ├── EasyDatasets └── Reading20newsgroup.ipynb ├── GPUArrays ├── .ipynb_checkpoints │ └── Introduction-checkpoint.ipynb ├── Benchmarks.ipynb ├── Introduction.ipynb ├── benchmark_arrays.jl ├── introduction_CLArrays.ipynb ├── results_timing_arrays_fireproD300.txt └── timing_arrays.jl ├── GPU_compute ├── .ipynb_checkpoints │ ├── Untitled-checkpoint.ipynb │ └── introduction_to_arrayfire-checkpoint.ipynb ├── CLArrays │ └── test_array_operations.jl ├── GPUArrays │ ├── .ipynb_checkpoints │ │ └── GPUArrays_convolution-checkpoint.ipynb │ └── GPUArrays_convolution.ipynb ├── OpenCL │ ├── Mandelbrot_fractal.ipynb │ ├── basics.ipynb │ ├── julia_set_fractal.ipynb │ └── vector_sum.ipynb ├── Untitled-2018-04-04_20-41-49-109.ipynb ├── Untitled.ipynb ├── blackscholes-2018-04-04_20-41-46-545.jl ├── blackscholes.jl ├── blackscholes_gpuarrays-2018-04-04_20-41-40-565.jl ├── blackscholes_gpuarrays.jl ├── blackscholes_results-2018-04-04_20-41-44-197.csv ├── blackscholes_results.csv ├── introduction_to_arrayfire-2018-04-04_20-41-42-490.ipynb └── introduction_to_arrayfire.ipynb ├── JuliaImages ├── .ipynb_checkpoints │ ├── 1_images_intro-checkpoint.ipynb │ └── 2_image_features-checkpoint.ipynb ├── 1_images_intro.ipynb ├── 2_image_features.ipynb ├── 2_image_features_Haar.ipynb ├── 2_image_features_brief_orb_brisk_freak.ipynb ├── 2_image_features_hog.ipynb ├── 2_image_features_hog_improved.ipynb ├── 2_image_filtering.ipynb ├── Convolutions.ipynb ├── basics_plotting.ipynb ├── hogmap │ ├── .ipynb_checkpoints │ │ └── 1_assigning_orientations_to_bins-checkpoint.ipynb │ └── 1_assigning_orientations_to_bins.ipynb ├── images │ ├── brief_example.jpg │ ├── brisk_example.jpg │ ├── freak_example.jpg │ └── orb_example.jpg ├── pedestrian_detection.ipynb ├── pedestrian_detection_perceptron.ipynb ├── pedestrian_detection_perceptron_customhog.ipynb ├── pedestrian_detection_perceptron_customhogJulia10.ipynb ├── pedestrian_detection_perceptron_customhog_juliaV1.ipynb └── tutorial │ └── humans.jpg ├── JuliaML_tutorials ├── MulticlassPerceptron │ ├── .DS_Store │ ├── benchmarks │ │ ├── .DS_Store │ │ ├── Perceptron_Benchmark.ipynb │ │ ├── Perceptron_Benchmark_sparse.ipynb │ │ ├── benchmark.jl │ │ ├── percep_fit.jl │ │ └── percep_python.py │ └── source │ │ ├── .DS_Store │ │ ├── 10_epoch_MulticlassPerceptron.jl.mem │ │ ├── 10_epoch_viewsprecomputed_MulticlassPerceptron.jl.mem │ │ ├── 1_epoch_MulticlassPerceptron.jl.mem │ │ ├── MulticlassPerceptron.jl │ │ └── s_MulticlassPerceptron.jl.mem ├── README.md ├── notebooks_machine_learning │ ├── 1_basics.ipynb │ ├── 2_Pipelines.ipynb │ └── gridsearch_ram_test.py ├── packages_to_install.jl └── tutorial │ └── introduction_to_lossfunctions.ipynb ├── MLJ ├── 01_getting_started_classification.ipynb └── 01_getting_started_classification_extradetailed.ipynb ├── MultiThreading └── 01_multithreading_examples.ipynb ├── MulticlassPerceptron ├── benchmarks │ ├── .ipynb_checkpoints │ │ └── Perceptron_Benchmark-checkpoint.ipynb │ ├── Perceptron_Benchmark_v5.ipynb │ ├── Perceptron_Benchmark_v6.ipynb │ └── benchmark.jl ├── source │ ├── .DS_Store │ ├── .MulticlassPerceptron3.jl.swp │ ├── MulticlassPerceptron1.jl │ ├── MulticlassPerceptron2.jl │ ├── MulticlassPerceptron3.jl │ └── MulticlassPerceptron4.jl └── source2 │ ├── MulticlassPerceptron1.jl │ ├── MulticlassPerceptron2.jl │ ├── MulticlassPerceptron3.jl │ └── MulticlassPerceptron4.jl ├── MulticlassPerceptronBycol ├── benchmarks │ ├── .ipynb_checkpoints │ │ ├── Perceptron_Benchmark_v5-checkpoint.ipynb │ │ └── Perceptron_Benchmark_v6_xeon-checkpoint.ipynb │ ├── Perceptron_Benchmark_v5.ipynb │ ├── Perceptron_Benchmark_v6.ipynb │ ├── Perceptron_Benchmark_v6_xeon.ipynb │ └── benchmark.jl ├── source │ ├── MulticlassPerceptron1.jl │ ├── MulticlassPerceptron2.jl │ ├── MulticlassPerceptron3.jl │ └── MulticlassPerceptron4.jl └── source2 │ ├── MulticlassPerceptron1.jl │ ├── MulticlassPerceptron2.jl │ ├── MulticlassPerceptron3.jl │ └── MulticlassPerceptron4.jl ├── MulticlassPerceptronMLJ ├── AmazonBookReviews.jl ├── LICENSE ├── MulticlassPerceptronMLJ.ipynb ├── README.md ├── REQUIRE ├── basic_usage_train.jl ├── negative.review ├── positive.review ├── sparse_affine_transformation-Copy1.ipynb ├── sparse_affine_transformation_githubissue.ipynb └── src │ ├── MulticlassPerceptron.jl │ └── MulticlassPerceptronOLD.jl ├── NLP ├── .ipynb_checkpoints │ └── Introduction_to_strings-checkpoint.ipynb └── Introduction_to_strings.ipynb ├── NNets ├── .ipynb_checkpoints │ └── mlp-checkpoint.ipynb ├── mlp.ipynb └── mlp2.ipynb ├── RBM ├── .ipynb_checkpoints │ ├── RBM_test_MNIST_tunned_separate_opt-checkpoint.ipynb │ ├── rbm_comparisson-checkpoint.ipynb │ ├── rbm_tut-checkpoint.ipynb │ └── rbm_tut_2-checkpoint.ipynb ├── RBM.ipynb ├── RBM_test_MNIST_tunned_separate_opt.ipynb ├── notebooks │ ├── .ipynb_checkpoints │ │ └── RBM_test_MNIST_tunned_separate_opt-checkpoint.ipynb │ ├── CRBM.ipynb │ ├── Generate_MNIST_sizes.ipynb │ ├── RBM_dfdx.ipynb │ ├── RBM_test_MNIST.ipynb │ ├── RBM_test_MNIST_tunned.ipynb │ ├── RBM_test_MNIST_tunned_separate_opt.ipynb │ ├── benchmark_blas_float64_32.ipynb │ ├── rbm.jil │ ├── rbm_tut.ipynb │ ├── rbm_tut_2_mac.ipynb │ └── test_equivalence.ipynb └── src │ ├── RBM_models.jl │ └── RBM_models_blas.jl ├── README.md ├── code_optimization ├── Basics.ipynb ├── basic_strategies.md └── related_work │ ├── C-Optimization-Techniques.pdf │ ├── CodeOptimization.pdf │ ├── Optimistic Loop Optimization.pdf │ ├── Optimizing Program Performance.pdf │ ├── Sparse Tensor Algebra Optimizations with Workspaces.pdf │ ├── Using Polly to Optimise Julia Arrays in Loops.pdf │ ├── code_optimization_Slides19.pdf │ └── machine_independent_opt.pdf ├── dataframe_tutorial └── dataframe_tutorial.ipynb ├── datasets ├── GenerateDatasets.jl └── bars_and_stripes.ipynb ├── flux ├── TODO_MLP flux.ipynb ├── TODO_introduction_to_flux.ipynb └── convnet_mnist.ipynb ├── install_common_packages.jl ├── introduction_to_julia ├── .ipynb_checkpoints │ ├── 1_Bonus_Arrays-checkpoint.ipynb │ ├── 1_types-checkpoint.ipynb │ ├── 2_functions-checkpoint.ipynb │ ├── 3_custom_types-checkpoint.ipynb │ ├── 3_optimize_code-checkpoint.ipynb │ ├── 4_importance_type_stability-checkpoint.ipynb │ ├── 5_parallel_capabilities-checkpoint.ipynb │ ├── 6_high_performant_julia-checkpoint.ipynb │ ├── 7_dot_vectorization-checkpoint.ipynb │ ├── Sequences-checkpoint.ipynb │ └── hmm_from_code-checkpoint.ipynb ├── 1.1_types.ipynb ├── 1.2__strings.ipynb ├── 10_parallel_spawn_remotecall.ipynb ├── 10_plotting_pyplot.ipynb ├── 10_profiling_code.ipynb ├── 11_Iterables_in_julia.ipynb ├── 12_generated_functions.ipynb ├── 1_2_strings.ipynb ├── 1_Bonus_Arrays.ipynb ├── 2_functions.ipynb ├── 3.1_custom_types.ipynb ├── 3.2_optimize_code.ipynb ├── 4_importance_type_stability.ipynb ├── 5_parallel_capabilities.ipynb ├── 6_high_performant_julia.ipynb ├── 7_dot_vectorization.ipynb ├── 8_macros.ipynb ├── 8_usefull_functions_for_custom_types.ipynb ├── 9_asyncronous_capabilities.ipynb ├── 9_testing_code.ipynb ├── Sequences.ipynb ├── hmm.jl └── hmm_from_code.ipynb ├── julia_basics.ipynb ├── julia_presentation ├── J_examples.ipynb ├── Julia_vs_C.ipynb ├── P_examples.ipynb └── Quora.ipynb ├── knet_tutorials ├── .ipynb_checkpoints │ └── Knet introduction-checkpoint.ipynb ├── Knet introduction.ipynb └── rnns │ └── charlm.jl ├── macros ├── Symbolic expression manipulation.ipynb └── macros.ipynb ├── metaprogramming └── metaprogramming_01.ipynb ├── mlj └── 02_ml_networks.ipynb ├── optimize_examples ├── Epot.ipynb └── Epot_to_the_solution.ipynb ├── plotting_and_diagrams ├── Untitled.ipynb ├── tikzgraph │ ├── diagrams │ │ └── graph.svg │ ├── graph.tex │ └── tikzgraphs_basics.ipynb ├── tikzpicture.log └── tikzpicture.tex ├── python_vs_julia ├── compute_pi │ ├── compute_pi.jl │ ├── compute_pi.py │ ├── compute_pi_distributed.jl │ ├── compute_pi_multithread.jl │ └── compute_pi_parallel.jl ├── count_triangles.jl ├── count_triangles.py ├── count_triangles │ ├── count_triangles.jl │ ├── count_triangles.py │ ├── count_triangles_multithreaded.jl │ └── generate_graph.py ├── elementwise_functions │ ├── myfunc_jl.ipynb │ └── myfunc_py.ipynb └── generate_graph.py ├── simd_examples ├── .ipynb_checkpoints │ └── using_simd-checkpoint.ipynb ├── basic_simd.ipynb ├── execute_this.jl ├── explicit_simd.ipynb └── using_simd.ipynb ├── speed_tests ├── .DS_Store ├── .ipynb_checkpoints │ └── comparing_functions-checkpoint.ipynb ├── comparing_functions.ipynb ├── rbm.jl ├── rbm.m ├── rbm.py ├── rbm2.py ├── rbm_evizero.jl └── rbm_opt.jl └── urls.txt /EasyDatasets/Reading20newsgroup.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 20 newsgroup\n", 8 | "\n", 9 | "Sklearn info \n", 10 | "\n", 11 | "- http://scikit-learn.org/stable/datasets/twenty_newsgroups.html\n", 12 | "\n", 13 | "Dataset \n", 14 | "\n", 15 | "- http://people.csail.mit.edu/jrennie/20Newsgroups/20news-bydate.tar.gz \n" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 12, 21 | "metadata": { 22 | "collapsed": true 23 | }, 24 | "outputs": [], 25 | "source": [ 26 | "#wij ←wij −η(yj −tj)·xi" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 13, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "data": { 36 | "text/plain": [ 37 | "read20newsgroup" 38 | ] 39 | }, 40 | "execution_count": 13, 41 | "metadata": {}, 42 | "output_type": "execute_result" 43 | } 44 | ], 45 | "source": [ 46 | "\"\"\"\n", 47 | "This function retrieves the raw textual data\n", 48 | "from the 20 newsgroup dataset and returns \n", 49 | "\"\"\"\n", 50 | "function read20newsgroup(path)\n", 51 | " class_names = readdir(path)\n", 52 | " X = Array{String}([])\n", 53 | " y = Array{String}([])\n", 54 | " doc_paths = Array{String}([])\n", 55 | " for class_name in class_names\n", 56 | " docs = readdir(path * \"/\" * class_name)\n", 57 | " for doc in docs\n", 58 | " data = readlines(path * \"/\" * class_name * \"/\" * doc)\n", 59 | " datajoined = \"\"\n", 60 | " # This is wrong, but some files contain extrange characters at the last\n", 61 | " # position. Need to do it more elegantly.\n", 62 | " for line in data[1:end-1] \n", 63 | " datajoined = datajoined * line * \"\\n\"\n", 64 | " end\n", 65 | " push!(X, datajoined)\n", 66 | " push!(y, class_name)\n", 67 | " end\n", 68 | " end\n", 69 | " return (X,y)\n", 70 | "end" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 14, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "\"/Users/macpro/Documents/Datasets/20news-bydate/20news-bydate-train/\"" 82 | ] 83 | }, 84 | "execution_count": 14, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "datapathtrain = homedir() * \"/Documents/Datasets/20news-bydate/20news-bydate-train/\"" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 15, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "\"/Users/macpro/Documents/Datasets/20news-bydate/20news-bydate-test/\"" 102 | ] 103 | }, 104 | "execution_count": 15, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "datapathtest = homedir() * \"/Documents/Datasets/20news-bydate/20news-bydate-test/\"" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 16, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "ename": "LoadError", 120 | "evalue": "\u001b[91mSystemError: unable to read directory /Users/macpro/Documents/Datasets/20news-bydate/20news-bydate-train/: No such file or directory\u001b[39m", 121 | "output_type": "error", 122 | "traceback": [ 123 | "\u001b[91mSystemError: unable to read directory /Users/macpro/Documents/Datasets/20news-bydate/20news-bydate-train/: No such file or directory\u001b[39m", 124 | "", 125 | "Stacktrace:", 126 | " [1] \u001b[1mreaddir\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::String\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./file.jl:413\u001b[22m\u001b[22m", 127 | " [2] \u001b[1mread20newsgroup\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::String\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./In[13]:6\u001b[22m\u001b[22m" 128 | ] 129 | } 130 | ], 131 | "source": [ 132 | "@time (X_tr,y_tr) = read20newsgroup(datapathtrain);" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 17, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "ename": "LoadError", 142 | "evalue": "\u001b[91mSystemError: unable to read directory /Users/macpro/Documents/Datasets/20news-bydate/20news-bydate-test/: No such file or directory\u001b[39m", 143 | "output_type": "error", 144 | "traceback": [ 145 | "\u001b[91mSystemError: unable to read directory /Users/macpro/Documents/Datasets/20news-bydate/20news-bydate-test/: No such file or directory\u001b[39m", 146 | "", 147 | "Stacktrace:", 148 | " [1] \u001b[1mreaddir\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::String\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./file.jl:413\u001b[22m\u001b[22m", 149 | " [2] \u001b[1mread20newsgroup\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::String\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./In[13]:6\u001b[22m\u001b[22m" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "@time (X_te,y_te) = read20newsgroup(datapathtest);" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 18, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "ename": "LoadError", 164 | "evalue": "\u001b[91mUndefVarError: X_tr not defined\u001b[39m", 165 | "output_type": "error", 166 | "traceback": [ 167 | "\u001b[91mUndefVarError: X_tr not defined\u001b[39m", 168 | "" 169 | ] 170 | } 171 | ], 172 | "source": [ 173 | "length(X_tr)" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 19, 179 | "metadata": {}, 180 | "outputs": [ 181 | { 182 | "ename": "LoadError", 183 | "evalue": "\u001b[91mUndefVarError: X_tr not defined\u001b[39m", 184 | "output_type": "error", 185 | "traceback": [ 186 | "\u001b[91mUndefVarError: X_tr not defined\u001b[39m", 187 | "" 188 | ] 189 | } 190 | ], 191 | "source": [ 192 | "X_tr[4]" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 25, 198 | "metadata": { 199 | "collapsed": true 200 | }, 201 | "outputs": [], 202 | "source": [ 203 | "#Pkg.add(\"TextAnalysis\")" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 26, 209 | "metadata": { 210 | "collapsed": true 211 | }, 212 | "outputs": [], 213 | "source": [ 214 | "#using TextAnalysis" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 27, 220 | "metadata": { 221 | "collapsed": true 222 | }, 223 | "outputs": [], 224 | "source": [ 225 | "#a = TextAnalysis.tokenize(\"lala\",8)" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 28, 231 | "metadata": { 232 | "collapsed": true 233 | }, 234 | "outputs": [], 235 | "source": [ 236 | "#tok(X_tr[4])" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": null, 242 | "metadata": { 243 | "collapsed": true 244 | }, 245 | "outputs": [], 246 | "source": [] 247 | } 248 | ], 249 | "metadata": { 250 | "anaconda-cloud": {}, 251 | "kernelspec": { 252 | "display_name": "Julia 0.6.0-pre.beta", 253 | "language": "julia", 254 | "name": "julia-0.6" 255 | }, 256 | "language_info": { 257 | "file_extension": ".jl", 258 | "mimetype": "application/julia", 259 | "name": "julia", 260 | "version": "0.6.0" 261 | } 262 | }, 263 | "nbformat": 4, 264 | "nbformat_minor": 1 265 | } 266 | -------------------------------------------------------------------------------- /GPUArrays/benchmark_arrays.jl: -------------------------------------------------------------------------------- 1 | 2 | using GPUArrays 3 | using BenchmarkTools 4 | 5 | sizes = [x for x in 100:100:400]; 6 | cpu_times = Dict() 7 | gpu_times = Dict() 8 | 9 | println("\nCPU times") 10 | for s in sizes 11 | X = rand(Float32,s,s); 12 | X_result = zeros(X); 13 | res_cpu = @benchmark A_mul_B!($X_result, $X, $X) 14 | println("size: ", s, " x ", s, " seconds: ", mean(res_cpu.times)/10^6, " seconds") 15 | #cpu_times[s] = mean(res_cpu.times)/10^6 16 | end 17 | 18 | 19 | println("\nGPU times") 20 | for s in sizes 21 | X = rand(Float32,s,s); 22 | X_result = zeros(X); 23 | X_gpu = GPUArray(X); 24 | X_result_gpu = GPUArray(zeros(Float32,s,s)); 25 | 26 | res_gpu = @benchmark A_mul_B!($X_result_gpu, $X_gpu, $X_gpu) 27 | println("size: ", s, " x ", s, " seconds: ", mean(res_gpu.times)/10^6, " seconds") 28 | gpu_times[s] = mean(res_gpu.times)/10^6 29 | end 30 | -------------------------------------------------------------------------------- /GPUArrays/results_timing_arrays_fireproD300.txt: -------------------------------------------------------------------------------- 1 | ulia06 timing_arrays.jl 2 | 3 | CPU times 4 | size: 100 x 100 seconds: 0.000133769 seconds 5 | size: 200 x 200 seconds: 0.000231602 seconds 6 | size: 300 x 300 seconds: 0.000557603 seconds 7 | size: 400 x 400 seconds: 0.000922378 seconds 8 | 9 | GPU times 10 | size: 100 x ERROR (unhandled task failure): OpenCL Error: OpenCL.Context error: 11 | Stacktrace: 12 | [1] raise_context_error(::String, ::String) at /Users/macpro/.julia/v0.6/OpenCL/src/context.jl:109 13 | [2] macro expansion at /Users/macpro/.julia/v0.6/OpenCL/src/context.jl:148 [inlined] 14 | [3] (::OpenCL.cl.##43#44)() at ./task.jl:335 15 | 100 seconds: 9.95407535e-7 seconds 16 | size: 200 x 200 seconds: 8.31729e-10 seconds 17 | size: 300 x 300 seconds: 5.8994e-11 seconds 18 | size: 400 x 400 seconds: 5.0479999999999997e-11 seconds 19 | 20 | -------------------------------------------------------------------------------- /GPUArrays/timing_arrays.jl: -------------------------------------------------------------------------------- 1 | 2 | using GPUArrays 3 | using CLArrays 4 | using BenchmarkTools 5 | 6 | sizes = [x for x in 100:200:1000]; 7 | cpu_times = Dict() 8 | gpu_times = Dict() 9 | 10 | println("\nCPU times") 11 | for s in sizes 12 | X = rand(Float32,s,s); 13 | X_result = zeros(X); 14 | res_cpu = @elapsed A_mul_B!(X_result, X, X) 15 | println("size: ", s, " x ", s, " seconds: ", res_cpu, " seconds") 16 | #println("size: ", s, " x ", s, " seconds: ", mean(res_cpu.times)/10^6, " seconds") 17 | end 18 | 19 | 20 | println("\nGPU times") 21 | for s in sizes 22 | X = rand(Float32,s,s); 23 | X_result = zeros(X); 24 | X_gpu = CLArray(X); 25 | X_result_gpu = CLArray(zeros(Float32,s,s)); 26 | 27 | res_gpu = @elapsed A_mul_B!(X_result_gpu, X_gpu, X_gpu) 28 | println("size: ", s, " x ", s, " seconds: ", res_gpu, " seconds") 29 | 30 | #println("size: ", s, " x ", s, " seconds: ", mean(res_gpu.times)/10^6, " seconds") 31 | end 32 | 33 | 34 | println("\nGPU times adding time to bring the array to the CPU") 35 | for s in sizes 36 | X = rand(Float32,s,s); 37 | X_result = zeros(X); 38 | X_gpu = CLArray(X); 39 | X_result_gpu = CLArray(zeros(Float32,s,s)); 40 | 41 | res_gpu = @elapsed synchronize(A_mul_B!(X_result_gpu, X_gpu, X_gpu)) 42 | println("size: ", s, " x ", s, " seconds: ", res_gpu, " seconds") 43 | 44 | #println("size: ", s, " x ", s, " seconds: ", mean(res_gpu.times)/10^6, " seconds") 45 | 46 | end 47 | -------------------------------------------------------------------------------- /GPU_compute/CLArrays/test_array_operations.jl: -------------------------------------------------------------------------------- 1 | using CLArrays 2 | 3 | 4 | sizes = [100,500,1000] 5 | 6 | 7 | for s in sizes 8 | 9 | srand(123) 10 | X = rand(Float32,s,s) 11 | Xcl = CLArray(X) 12 | aux1 = Xcl * Xcl 13 | 14 | println("result of the sum: ", sum(aux1)) 15 | end 16 | 17 | -------------------------------------------------------------------------------- /GPU_compute/blackscholes-2018-04-04_20-41-46-545.jl: -------------------------------------------------------------------------------- 1 | using ArrayFire 2 | 3 | function blackscholes_serial(sptprice::AbstractArray{Float32}, 4 | strike::AbstractArray{Float32}, 5 | rate::AbstractArray{Float32}, 6 | volatility::AbstractArray{Float32}, 7 | time::AbstractArray{Float32}) 8 | logterm = log10( sptprice ./ strike) 9 | powterm = .5f0 .* volatility .* volatility 10 | den = volatility .* sqrt(time) 11 | d1 = (((rate .+ powterm) .* time) .+ logterm) ./ den 12 | d2 = d1 .- den 13 | NofXd1 = cndf2(d1) 14 | NofXd2 = cndf2(d2) 15 | futureValue = strike .* exp(- rate .* time) 16 | c1 = futureValue .* NofXd2 17 | call_ = sptprice .* NofXd1 .- c1 18 | put = call_ .- futureValue .+ sptprice 19 | end 20 | 21 | @inline function cndf2(in::AbstractArray{Float32}) 22 | out = 0.5f0 .+ 0.5f0 .* erf(0.707106781f0 .* in) 23 | return out 24 | end 25 | 26 | function run(iterations) 27 | sptprice = Float32[ 42.0 for i = 1:iterations ] 28 | initStrike = Float32[ 40.0 + (i / iterations) for i = 1:iterations ] 29 | rate = Float32[ 0.5 for i = 1:iterations ] 30 | volatility = Float32[ 0.2 for i = 1:iterations ] 31 | time = Float32[ 0.5 for i = 1:iterations ] 32 | 33 | sptprice_gpu = AFArray(sptprice) 34 | initStrike_gpu = AFArray(initStrike) 35 | rate_gpu = AFArray(rate) 36 | volatility_gpu = AFArray(volatility) 37 | time_gpu = AFArray(time) 38 | 39 | tic() 40 | put1 = blackscholes_serial(sptprice, initStrike, rate, volatility, time) 41 | t1 = toq() 42 | println("Serial checksum: ", sum(put1)) 43 | tic() 44 | put2 = blackscholes_serial(sptprice_gpu, initStrike_gpu, rate_gpu, volatility_gpu, time_gpu) 45 | t2 = toq() 46 | println("Parallel checksum: ", sum(put2)) 47 | return t1, t2 48 | end 49 | 50 | function driver() 51 | srand(0) 52 | tic() 53 | iterations = 10^7 54 | blackscholes_serial(Float32[], Float32[], Float32[], Float32[], Float32[]) 55 | blackscholes_serial(AFArray(Float32[1., 2.]), AFArray(Float32[1., 2.]), 56 | AFArray(Float32[1., 2.]), AFArray(Float32[1., 2.]), AFArray(Float32[1., 2.])) 57 | println("SELFPRIMED ", toq()) 58 | tserial, tparallel = run(iterations) 59 | println("Time taken for CPU = $tserial") 60 | println("Time taken for GPU = $tparallel") 61 | println("Speedup = $(tserial / tparallel)") 62 | println("CPU rate = ", iterations / tserial, " opts/sec") 63 | println("GPU rate = ", iterations / tparallel, " opts/sec") 64 | end 65 | driver() 66 | -------------------------------------------------------------------------------- /GPU_compute/blackscholes.jl: -------------------------------------------------------------------------------- 1 | using ArrayFire 2 | 3 | function blackscholes_serial(sptprice::AbstractArray{Float32}, 4 | strike::AbstractArray{Float32}, 5 | rate::AbstractArray{Float32}, 6 | volatility::AbstractArray{Float32}, 7 | time::AbstractArray{Float32}) 8 | logterm = log10( sptprice ./ strike) 9 | powterm = .5f0 .* volatility .* volatility 10 | den = volatility .* sqrt(time) 11 | d1 = (((rate .+ powterm) .* time) .+ logterm) ./ den 12 | d2 = d1 .- den 13 | NofXd1 = cndf2(d1) 14 | NofXd2 = cndf2(d2) 15 | futureValue = strike .* exp(- rate .* time) 16 | c1 = futureValue .* NofXd2 17 | call_ = sptprice .* NofXd1 .- c1 18 | put = call_ .- futureValue .+ sptprice 19 | end 20 | 21 | @inline function cndf2(in::AbstractArray{Float32}) 22 | out = 0.5f0 .+ 0.5f0 .* erf(0.707106781f0 .* in) 23 | return out 24 | end 25 | 26 | function run(iterations) 27 | sptprice = Float32[ 42.0 for i = 1:iterations ] 28 | initStrike = Float32[ 40.0 + (i / iterations) for i = 1:iterations ] 29 | rate = Float32[ 0.5 for i = 1:iterations ] 30 | volatility = Float32[ 0.2 for i = 1:iterations ] 31 | time = Float32[ 0.5 for i = 1:iterations ] 32 | 33 | sptprice_gpu = AFArray(sptprice) 34 | initStrike_gpu = AFArray(initStrike) 35 | rate_gpu = AFArray(rate) 36 | volatility_gpu = AFArray(volatility) 37 | time_gpu = AFArray(time) 38 | 39 | tic() 40 | put1 = blackscholes_serial(sptprice, initStrike, rate, volatility, time) 41 | t1 = toq() 42 | println("Serial checksum: ", sum(put1)) 43 | tic() 44 | put2 = blackscholes_serial(sptprice_gpu, initStrike_gpu, rate_gpu, volatility_gpu, time_gpu) 45 | t2 = toq() 46 | println("Parallel checksum: ", sum(put2)) 47 | return t1, t2 48 | end 49 | 50 | function driver() 51 | srand(0) 52 | tic() 53 | iterations = 10^7 54 | blackscholes_serial(Float32[], Float32[], Float32[], Float32[], Float32[]) 55 | blackscholes_serial(AFArray(Float32[1., 2.]), AFArray(Float32[1., 2.]), 56 | AFArray(Float32[1., 2.]), AFArray(Float32[1., 2.]), AFArray(Float32[1., 2.])) 57 | println("SELFPRIMED ", toq()) 58 | tserial, tparallel = run(iterations) 59 | println("Time taken for CPU = $tserial") 60 | println("Time taken for GPU = $tparallel") 61 | println("Speedup = $(tserial / tparallel)") 62 | println("CPU rate = ", iterations / tserial, " opts/sec") 63 | println("GPU rate = ", iterations / tparallel, " opts/sec") 64 | end 65 | driver() 66 | -------------------------------------------------------------------------------- /GPU_compute/blackscholes_gpuarrays-2018-04-04_20-41-40-565.jl: -------------------------------------------------------------------------------- 1 | 2 | if Pkg.installed("BenchmarkTools") == nothing || 3 | Pkg.installed("Query") == nothing || 4 | # Pkg.installed("CUDAnative") == nothing || 5 | Pkg.installed("DataFrames") == nothing 6 | 7 | error("Please install BenchmarkTools, Query, CUDAnative and DataFrames") 8 | # Pkg.add("Query") 9 | # Pkg.add("DataFrames") 10 | end 11 | using GPUArrays, SpecialFunctions 12 | using GPUArrays: perbackend, synchronize, free 13 | using SpecialFunctions: erf 14 | 15 | function blackscholes( 16 | sptprice, 17 | strike, 18 | rate, 19 | volatility, 20 | time 21 | ) 22 | logterm = log( sptprice / strike) 23 | powterm = .5f0 * volatility * volatility 24 | den = volatility * sqrt(time) 25 | d1 = (((rate + powterm) * time) + logterm) / den 26 | d2 = d1 - den 27 | NofXd1 = cndf2(d1) 28 | NofXd2 = cndf2(d2) 29 | futureValue = strike * exp(-rate * time) 30 | c1 = futureValue * NofXd2 31 | call_ = sptprice * NofXd1 - c1 32 | put = call_ - futureValue + sptprice 33 | return put 34 | end 35 | 36 | @inline function cndf2(x) 37 | 0.5f0 + 0.5f0 * erf(0.707106781f0 * x) 38 | end 39 | 40 | @noinline function test(result, a, b, c, d, e) 41 | result .= blackscholes.(a, b, c, d, e) 42 | GPUArrays.synchronize(result) 43 | end 44 | 45 | if Pkg.installed("CUDAnative") != nothing 46 | using CUDAnative 47 | const cu = CUDAnative 48 | # jeeez -.- So CUDAnative still doesn't recognize e.g. sqrt in the LLVM-IR, 49 | #since it's implemented within a C-library.... Should be fixed soon! 50 | function cu_blackscholes(sptprice, strike, rate, volatility, time) 51 | logterm = cu.log10( sptprice / strike) 52 | powterm = .5f0 * volatility * volatility 53 | den = volatility * cu.sqrt(time) 54 | d1 = (((rate + powterm) * time) + logterm) / den 55 | d2 = d1 - den 56 | NofXd1 = cu_cndf2(d1) 57 | NofXd2 = cu_cndf2(d2) 58 | futureValue = strike * cu.exp(- rate * time) 59 | c1 = futureValue * NofXd2 60 | call_ = sptprice * NofXd1 - c1 61 | put = call_ - futureValue + sptprice 62 | return put 63 | end 64 | 65 | function cu_cndf2(x) 66 | 0.5f0 + 0.5f0 * cu.erf(0.707106781f0 * x) 67 | end 68 | end 69 | function runbench(f, out, a, b, c, d, e) 70 | out .= f.(a, b, c, d, e) 71 | synchronize(out) 72 | end 73 | 74 | 75 | using BenchmarkTools 76 | import BenchmarkTools: Trial 77 | using DataFrames 78 | using Query 79 | 80 | # on 0.6: 81 | # Pkg.clone("https://github.com/davidanthoff/IterableTables.jl.git") 82 | # Pkg.checkout("NamedTuples") 83 | # Pkg.checkout("Query") 84 | # Pkg.checkout("SimpleTraits") 85 | 86 | Nmax = 7 87 | 88 | benchmarks = DataFrame([String, Int64, Trial, Float64], [:Backend, :N, :Trial, :minT], 0) 89 | 90 | NT = Base.Threads.nthreads() 91 | info("Running benchmarks number of threads: $NT") 92 | 93 | for n in 1:Nmax 94 | N = 10^n 95 | sptprice = Float32[42.0 for i = 1:N] 96 | initStrike = Float32[40.0 + (i / N) for i = 1:N] 97 | rate = Float32[0.5 for i = 1:N] 98 | volatility = Float32[0.2 for i = 1:N] 99 | spttime = Float32[0.5 for i = 1:N] 100 | result = similar(spttime) 101 | comparison = blackscholes.(sptprice, initStrike, rate, volatility, spttime) 102 | perbackend() do backend # nice to go through all backends, but for benchmarks we might want to have this more explicit! 103 | if true #backend == :julia 104 | _sptprice = GPUArray(sptprice) 105 | _initStrike = GPUArray(initStrike) 106 | _rate = GPUArray(rate) 107 | _volatility = GPUArray(volatility) 108 | _time = GPUArray(spttime) 109 | _result = GPUArray(result) 110 | f = backend == :cudanative ? cu_blackscholes : blackscholes 111 | b = @benchmark runbench($f, $_result, $_sptprice, $_initStrike, $_rate, $_volatility, $_time) 112 | ctx_str = sprint() do io 113 | show(io, GPUArrays.current_context()) 114 | end 115 | push!(benchmarks, (ctx_str, N, b, minimum(b).time)) 116 | 117 | @assert Array(_result) ≈ comparison 118 | # this is optional, but needed in a loop like this, which allocates a lot of GPUArrays 119 | # for the future, we need a way to tell the Julia gc about GPU memory 120 | free(_sptprice);free(_initStrike);free(_rate);free(_volatility);free(_time);free(_result); 121 | end 122 | end 123 | end 124 | 125 | results = @from b in benchmarks begin 126 | @select {b.Backend, b.N, b.minT} 127 | @collect DataFrame 128 | end 129 | 130 | results = cd(dirname(@__FILE__)) do 131 | file = "blackscholes_results.csv" 132 | # merge 133 | merged = if isfile(file) 134 | merged = readtable(file) 135 | backends = unique(merged[:Backend]) 136 | benched_backends = unique(results[:Backend]) 137 | to_add = setdiff(benched_backends, backends) 138 | for i in 1:size(results, 1) 139 | row = results[i, :] 140 | if row[:Backend][1] in to_add 141 | append!(merged, row) 142 | end 143 | end 144 | merged 145 | else 146 | results 147 | end 148 | writetable(file, merged) 149 | merged 150 | end 151 | 152 | function filterResults(df, n) 153 | dfR = @from r in df begin 154 | @where r.N == 10^n 155 | @select {r.Backend, r.minT} 156 | @collect DataFrame 157 | end 158 | return dfR 159 | end 160 | 161 | io = IOBuffer() 162 | for n in 1:Nmax 163 | df = filterResults(results, n) 164 | println(io, "| Backend | Time (μs) for N = 10^$n |") 165 | println(io, "| ---- | ---- |") 166 | for row in eachrow(df) 167 | b = row[:Backend] 168 | t = row[:minT] 169 | @printf(io, "| %s | %6.2f μs|\n", b, t / 10^9) 170 | end 171 | display(Markdown.parse(io)) 172 | seekstart(io) 173 | println(String(take!(io))) 174 | end 175 | -------------------------------------------------------------------------------- /GPU_compute/blackscholes_gpuarrays.jl: -------------------------------------------------------------------------------- 1 | 2 | if Pkg.installed("BenchmarkTools") == nothing || 3 | Pkg.installed("Query") == nothing || 4 | # Pkg.installed("CUDAnative") == nothing || 5 | Pkg.installed("DataFrames") == nothing 6 | 7 | error("Please install BenchmarkTools, Query, CUDAnative and DataFrames") 8 | # Pkg.add("Query") 9 | # Pkg.add("DataFrames") 10 | end 11 | using GPUArrays, SpecialFunctions 12 | using GPUArrays: perbackend, synchronize, free 13 | using SpecialFunctions: erf 14 | 15 | function blackscholes( 16 | sptprice, 17 | strike, 18 | rate, 19 | volatility, 20 | time 21 | ) 22 | logterm = log( sptprice / strike) 23 | powterm = .5f0 * volatility * volatility 24 | den = volatility * sqrt(time) 25 | d1 = (((rate + powterm) * time) + logterm) / den 26 | d2 = d1 - den 27 | NofXd1 = cndf2(d1) 28 | NofXd2 = cndf2(d2) 29 | futureValue = strike * exp(-rate * time) 30 | c1 = futureValue * NofXd2 31 | call_ = sptprice * NofXd1 - c1 32 | put = call_ - futureValue + sptprice 33 | return put 34 | end 35 | 36 | @inline function cndf2(x) 37 | 0.5f0 + 0.5f0 * erf(0.707106781f0 * x) 38 | end 39 | 40 | @noinline function test(result, a, b, c, d, e) 41 | result .= blackscholes.(a, b, c, d, e) 42 | GPUArrays.synchronize(result) 43 | end 44 | 45 | if Pkg.installed("CUDAnative") != nothing 46 | using CUDAnative 47 | const cu = CUDAnative 48 | # jeeez -.- So CUDAnative still doesn't recognize e.g. sqrt in the LLVM-IR, 49 | #since it's implemented within a C-library.... Should be fixed soon! 50 | function cu_blackscholes(sptprice, strike, rate, volatility, time) 51 | logterm = cu.log10( sptprice / strike) 52 | powterm = .5f0 * volatility * volatility 53 | den = volatility * cu.sqrt(time) 54 | d1 = (((rate + powterm) * time) + logterm) / den 55 | d2 = d1 - den 56 | NofXd1 = cu_cndf2(d1) 57 | NofXd2 = cu_cndf2(d2) 58 | futureValue = strike * cu.exp(- rate * time) 59 | c1 = futureValue * NofXd2 60 | call_ = sptprice * NofXd1 - c1 61 | put = call_ - futureValue + sptprice 62 | return put 63 | end 64 | 65 | function cu_cndf2(x) 66 | 0.5f0 + 0.5f0 * cu.erf(0.707106781f0 * x) 67 | end 68 | end 69 | function runbench(f, out, a, b, c, d, e) 70 | out .= f.(a, b, c, d, e) 71 | synchronize(out) 72 | end 73 | 74 | 75 | using BenchmarkTools 76 | import BenchmarkTools: Trial 77 | using DataFrames 78 | using Query 79 | 80 | # on 0.6: 81 | # Pkg.clone("https://github.com/davidanthoff/IterableTables.jl.git") 82 | # Pkg.checkout("NamedTuples") 83 | # Pkg.checkout("Query") 84 | # Pkg.checkout("SimpleTraits") 85 | 86 | Nmax = 7 87 | 88 | benchmarks = DataFrame([String, Int64, Trial, Float64], [:Backend, :N, :Trial, :minT], 0) 89 | 90 | NT = Base.Threads.nthreads() 91 | info("Running benchmarks number of threads: $NT") 92 | 93 | for n in 1:Nmax 94 | N = 10^n 95 | sptprice = Float32[42.0 for i = 1:N] 96 | initStrike = Float32[40.0 + (i / N) for i = 1:N] 97 | rate = Float32[0.5 for i = 1:N] 98 | volatility = Float32[0.2 for i = 1:N] 99 | spttime = Float32[0.5 for i = 1:N] 100 | result = similar(spttime) 101 | comparison = blackscholes.(sptprice, initStrike, rate, volatility, spttime) 102 | perbackend() do backend # nice to go through all backends, but for benchmarks we might want to have this more explicit! 103 | if true #backend == :julia 104 | _sptprice = GPUArray(sptprice) 105 | _initStrike = GPUArray(initStrike) 106 | _rate = GPUArray(rate) 107 | _volatility = GPUArray(volatility) 108 | _time = GPUArray(spttime) 109 | _result = GPUArray(result) 110 | f = backend == :cudanative ? cu_blackscholes : blackscholes 111 | b = @benchmark runbench($f, $_result, $_sptprice, $_initStrike, $_rate, $_volatility, $_time) 112 | ctx_str = sprint() do io 113 | show(io, GPUArrays.current_context()) 114 | end 115 | push!(benchmarks, (ctx_str, N, b, minimum(b).time)) 116 | 117 | @assert Array(_result) ≈ comparison 118 | # this is optional, but needed in a loop like this, which allocates a lot of GPUArrays 119 | # for the future, we need a way to tell the Julia gc about GPU memory 120 | free(_sptprice);free(_initStrike);free(_rate);free(_volatility);free(_time);free(_result); 121 | end 122 | end 123 | end 124 | 125 | results = @from b in benchmarks begin 126 | @select {b.Backend, b.N, b.minT} 127 | @collect DataFrame 128 | end 129 | 130 | results = cd(dirname(@__FILE__)) do 131 | file = "blackscholes_results.csv" 132 | # merge 133 | merged = if isfile(file) 134 | merged = readtable(file) 135 | backends = unique(merged[:Backend]) 136 | benched_backends = unique(results[:Backend]) 137 | to_add = setdiff(benched_backends, backends) 138 | for i in 1:size(results, 1) 139 | row = results[i, :] 140 | if row[:Backend][1] in to_add 141 | append!(merged, row) 142 | end 143 | end 144 | merged 145 | else 146 | results 147 | end 148 | writetable(file, merged) 149 | merged 150 | end 151 | 152 | function filterResults(df, n) 153 | dfR = @from r in df begin 154 | @where r.N == 10^n 155 | @select {r.Backend, r.minT} 156 | @collect DataFrame 157 | end 158 | return dfR 159 | end 160 | 161 | io = IOBuffer() 162 | for n in 1:Nmax 163 | df = filterResults(results, n) 164 | println(io, "| Backend | Time (μs) for N = 10^$n |") 165 | println(io, "| ---- | ---- |") 166 | for row in eachrow(df) 167 | b = row[:Backend] 168 | t = row[:minT] 169 | @printf(io, "| %s | %6.2f μs|\n", b, t / 10^9) 170 | end 171 | display(Markdown.parse(io)) 172 | seekstart(io) 173 | println(String(take!(io))) 174 | end 175 | -------------------------------------------------------------------------------- /GPU_compute/blackscholes_results-2018-04-04_20-41-44-197.csv: -------------------------------------------------------------------------------- 1 | "Backend","N","minT" 2 | "JLContext Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz with 1 threads",10,1324.7 3 | "CLContext: AMD Radeon HD - FirePro D300 Compute Engine",10,161766.0 4 | "JLContext Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz with 1 threads",100,9177.0 5 | "CLContext: AMD Radeon HD - FirePro D300 Compute Engine",100,160279.0 6 | "JLContext Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz with 1 threads",1000,87233.0 7 | "CLContext: AMD Radeon HD - FirePro D300 Compute Engine",1000,161160.0 8 | "JLContext Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz with 1 threads",10000,870790.0 9 | "CLContext: AMD Radeon HD - FirePro D300 Compute Engine",10000,167199.0 10 | "JLContext Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz with 1 threads",100000,8.844228e6 11 | "CLContext: AMD Radeon HD - FirePro D300 Compute Engine",100000,533741.0 12 | "JLContext Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz with 1 threads",1000000,9.0622336e7 13 | "CLContext: AMD Radeon HD - FirePro D300 Compute Engine",1000000,4.284151e6 14 | "JLContext Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz with 1 threads",10000000,9.19184123e8 15 | "CLContext: AMD Radeon HD - FirePro D300 Compute Engine",10000000,4.1496741e7 16 | -------------------------------------------------------------------------------- /GPU_compute/blackscholes_results.csv: -------------------------------------------------------------------------------- 1 | "Backend","N","minT" 2 | "JLContext Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz with 1 threads",10,1324.7 3 | "CLContext: AMD Radeon HD - FirePro D300 Compute Engine",10,161766.0 4 | "JLContext Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz with 1 threads",100,9177.0 5 | "CLContext: AMD Radeon HD - FirePro D300 Compute Engine",100,160279.0 6 | "JLContext Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz with 1 threads",1000,87233.0 7 | "CLContext: AMD Radeon HD - FirePro D300 Compute Engine",1000,161160.0 8 | "JLContext Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz with 1 threads",10000,870790.0 9 | "CLContext: AMD Radeon HD - FirePro D300 Compute Engine",10000,167199.0 10 | "JLContext Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz with 1 threads",100000,8.844228e6 11 | "CLContext: AMD Radeon HD - FirePro D300 Compute Engine",100000,533741.0 12 | "JLContext Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz with 1 threads",1000000,9.0622336e7 13 | "CLContext: AMD Radeon HD - FirePro D300 Compute Engine",1000000,4.284151e6 14 | "JLContext Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz with 1 threads",10000000,9.19184123e8 15 | "CLContext: AMD Radeon HD - FirePro D300 Compute Engine",10000000,4.1496741e7 16 | -------------------------------------------------------------------------------- /JuliaImages/images/brief_example.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidbp/learn_julia/a624967aa95ece1912adb216fc652f45fe84d4b1/JuliaImages/images/brief_example.jpg -------------------------------------------------------------------------------- /JuliaImages/images/brisk_example.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidbp/learn_julia/a624967aa95ece1912adb216fc652f45fe84d4b1/JuliaImages/images/brisk_example.jpg -------------------------------------------------------------------------------- /JuliaImages/images/freak_example.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidbp/learn_julia/a624967aa95ece1912adb216fc652f45fe84d4b1/JuliaImages/images/freak_example.jpg -------------------------------------------------------------------------------- /JuliaImages/images/orb_example.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidbp/learn_julia/a624967aa95ece1912adb216fc652f45fe84d4b1/JuliaImages/images/orb_example.jpg -------------------------------------------------------------------------------- /JuliaImages/tutorial/humans.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidbp/learn_julia/a624967aa95ece1912adb216fc652f45fe84d4b1/JuliaImages/tutorial/humans.jpg -------------------------------------------------------------------------------- /JuliaML_tutorials/MulticlassPerceptron/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidbp/learn_julia/a624967aa95ece1912adb216fc652f45fe84d4b1/JuliaML_tutorials/MulticlassPerceptron/.DS_Store -------------------------------------------------------------------------------- /JuliaML_tutorials/MulticlassPerceptron/benchmarks/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidbp/learn_julia/a624967aa95ece1912adb216fc652f45fe84d4b1/JuliaML_tutorials/MulticlassPerceptron/benchmarks/.DS_Store -------------------------------------------------------------------------------- /JuliaML_tutorials/MulticlassPerceptron/benchmarks/benchmark.jl: -------------------------------------------------------------------------------- 1 | using BenchmarkTools 2 | using MLDatasets 3 | source_path = join(push!(split(pwd(),"/")[1:end-1],"source/" ),"/") 4 | using MulticlassPerceptron 5 | 6 | if !contains(==,LOAD_PATH, source_path) 7 | push!(LOAD_PATH, source_path) 8 | end 9 | 10 | T = Float32 11 | println("Starting Benchmark") 12 | 13 | begin 14 | println("Loading Modules") 15 | percep1 = MulticlassPerceptron.MPerceptron(T, 10, 784) 16 | n_classes = 10 17 | n_features = 784 18 | 19 | println("Loading MNIST") 20 | X_train, y_train = MLDatasets.MNIST.traindata(); 21 | X_test, y_test = MLDatasets.MNIST.MNIST.testdata(); 22 | X_train = reshape(X_train, (784,60000)) 23 | X_test = reshape(X_test, (784,10000)) 24 | 25 | y_train = y_train + 1 26 | y_test = y_test + 1; 27 | X_train = Array{T}((X_train - minimum(X_train))/(maximum(X_train) - minimum(X_train))) 28 | y_train = Array{Int64}(y_train) 29 | X_test = Array{T}(X_test - minimum(X_test))/(maximum(X_test) - minimum(X_test)) 30 | y_test = Array{Int64}(y_test); 31 | 32 | println("\n\tMachine information") 33 | println("\n\tPeakflops:", peakflops()) 34 | println("\n\tversioninfo:\n") 35 | versioninfo() 36 | println("\n\tComputing benchmarks:\n\n") 37 | println("\n\tTime to train 1 epoch MNIST:\n\n") 38 | results1 = @benchmark MulticlassPerceptron.fit!(percep1, X_train, y_train; n_epochs=1, print_flag=false, compute_accuracy=false) 39 | 40 | display(results1) 41 | println("\n") 42 | 43 | println("\n\tTraining 40 epochs MNIST:\n\n") 44 | @time MulticlassPerceptron.fit!(percep1, X_train, y_train; n_epochs=40, print_flag=true, compute_accuracy=false) 45 | println("\n") 46 | 47 | end 48 | -------------------------------------------------------------------------------- /JuliaML_tutorials/MulticlassPerceptron/benchmarks/percep_fit.jl: -------------------------------------------------------------------------------- 1 | using BenchmarkTools 2 | using MLDatasets 3 | 4 | source_path = join(push!(split(pwd(),"/")[1:end-1],"source/" ),"/") 5 | 6 | if !contains(==,LOAD_PATH, source_path) 7 | push!(LOAD_PATH, source_path) 8 | end 9 | using MulticlassPerceptron 10 | 11 | 12 | T = Float32 13 | println("Starting Benchmark") 14 | 15 | println("Loading Modules") 16 | percep1 = MulticlassPerceptron.MPerceptron(T, 10, 784) 17 | n_classes = 10 18 | n_features = 784 19 | 20 | println("Loading MNIST") 21 | X_train, y_train = MLDatasets.MNIST.traindata(); 22 | X_test, y_test = MLDatasets.MNIST.MNIST.testdata(); 23 | X_train = reshape(X_train, (784,60000)) 24 | X_test = reshape(X_test, (784,10000)) 25 | 26 | y_train = y_train + 1 27 | y_test = y_test + 1; 28 | X_train = Array{T}((X_train - minimum(X_train))/(maximum(X_train) - minimum(X_train))) 29 | y_train = Array{Int64}(y_train) 30 | X_test = Array{T}(X_test - minimum(X_test))/(maximum(X_test) - minimum(X_test)) 31 | y_test = Array{Int64}(y_test); 32 | 33 | @time fit!(percep1, X_train, y_train; 34 | n_epochs=10, print_flag=false, compute_accuracy=false) 35 | -------------------------------------------------------------------------------- /JuliaML_tutorials/MulticlassPerceptron/benchmarks/percep_python.py: -------------------------------------------------------------------------------- 1 | 2 | import sklearn 3 | from sklearn import linear_model 4 | import numpy as np 5 | import time 6 | 7 | n_samples = 60000 8 | 9 | X = np.random.rand(n_samples,784) 10 | X = np.array(X, dtype="Float32") 11 | y = np.random.randint(1,10,n_samples) 12 | 13 | from sklearn.linear_model import Perceptron 14 | 15 | p = Perceptron(n_iter=1) 16 | 17 | t0 = time.time() 18 | p.fit(X,y) 19 | print("\ntime is:", time.time()-t0) 20 | -------------------------------------------------------------------------------- /JuliaML_tutorials/MulticlassPerceptron/source/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidbp/learn_julia/a624967aa95ece1912adb216fc652f45fe84d4b1/JuliaML_tutorials/MulticlassPerceptron/source/.DS_Store -------------------------------------------------------------------------------- /JuliaML_tutorials/MulticlassPerceptron/source/s_MulticlassPerceptron.jl.mem: -------------------------------------------------------------------------------- 1 | - 2 | - module MulticlassPerceptron 3 | - export predict, MPerceptron, fit! 4 | - 5 | - type MPerceptron{T} 6 | 101480 W::AbstractMatrix 7 | - b::AbstractVector{T} 8 | - n_classes::Int 9 | - n_features::Int 10 | - accuracy::AbstractVector{T} 11 | - end 12 | - 13 | - function Base.show{T}(io::IO, p::MPerceptron{T}) 14 | - n_classes = p.n_classes 15 | - n_features = p.n_features 16 | - print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 17 | - end 18 | - 19 | 0 MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_features, n_classes), 20 | - zeros(T, n_classes), 21 | - n_classes, 22 | - n_features, 23 | - zeros(T,0)) 24 | - """ 25 | - Compute the accuracy betwwen `y` and `y_hat`. 26 | - """ 27 | - function accuracy(y, y_hat) 28 | 0 acc = 0. 29 | 0 @inbounds for k = 1:length(y) 30 | 0 @fastmath acc += y[k] == y_hat[k] 31 | - end 32 | 0 return acc/length(y_hat) 33 | - end 34 | - 35 | - """ 36 | - Function to predict the class for a given input in a `MPerceptron`. 37 | - The placeholder is used to avoid allocating memory for each matrix-vector multiplication. 38 | - 39 | - - Returns the predicted class. 40 | - """ 41 | - function predict(h::MPerceptron, x, placeholder) 42 | 0 placeholder .= At_mul_B!(placeholder, h.W, x) .+ h.b 43 | 0 return indmax(placeholder) 44 | - end 45 | - 46 | - """ 47 | - Function to predict the class for a given input in a `MPerceptron`. 48 | - 49 | - - Returns the predicted class. 50 | - """ 51 | - function predict(h::MPerceptron, x) 52 | - return indmax(h.W' * x .+ h.b) 53 | - end 54 | - 55 | - """ 56 | - > fit!(h::Perceptron, 57 | - > X::Array, 58 | - > y::Array; 59 | - > n_epochs=50, 60 | - > learning_rate=0.1, 61 | - > print_flag=false, 62 | - > compute_accuracy=true, 63 | - > seed=srand(1234), 64 | - > pocket=false, 65 | - > shuffle_data=false) 66 | - 67 | - ##### Arguments 68 | - 69 | - - **`h`**, (MPerceptron{T} type), initialized perceptron. 70 | - - **`X`**, (Array{T,2} type), data contained in the columns of X. 71 | - - **`y`**, (Vector{T} type), class labels (as integers from 1 to n_classes). 72 | - 73 | - ##### Keyword arguments 74 | - 75 | - - **`n_epochs`**, (Integer type), number of passes (epochs) through the data. 76 | - - **`learning_rate`**, (Float type), learning rate (The standard perceptron is with learning_rate=1.) 77 | - - **`compute_accuracy`**, (Bool type), if `true` the accuracy is computed at the end of every epoch. 78 | - - **`print_flag`**, (Bool type), if `true` the accuracy is printed at the end of every epoch. 79 | - - **`seed`**, (MersenneTwister type), seed for the permutation of the datapoints in case there the data is shuffled. 80 | - - **`pocket`** , (Bool type), if `true` the best weights are saved (in the pocket) during learning. 81 | - - **`shuffle_data`**, (Bool type), if `true` the data is shuffled at every epoch (in reality we only shuffle indicies for performance). 82 | - 83 | - """ 84 | - function fit!(h::MPerceptron, X::AbstractArray, y::Vector; 85 | - n_epochs=50, learning_rate=1., print_flag=false, 86 | - compute_accuracy=false, seed=srand(1234), pocket=false, 87 | - shuffle_data=false, average_weights = false) 88 | - 89 | 5177740 n_features, n_samples = size(X) 90 | - #@assert eltype(X) == eltype(h.W) 91 | 0 @assert length(y) == n_samples 92 | - 93 | - T = eltype(X) 94 | 0 learning_rate = T(learning_rate) 95 | - 96 | 0 y_signal_placeholder = zeros(T, h.n_classes) 97 | 480080 y_preds = zeros(Int64, n_samples) 98 | 0 data_indices = Array(1:n_samples) 99 | 0 max_acc = zero(T) 100 | - 101 | 0 if pocket 102 | 0 W_hat = zeros(T, h.n_features, h.n_classes) 103 | 0 b_hat = zeros(T, h.n_classes) 104 | - end 105 | 0 if average_weights 106 | 0 W_his = zeros(T, h.n_features, h.n_classes) 107 | 0 b_his = zeros(T, h.n_classes) 108 | - end 109 | - 110 | 0 for epoch in 1:n_epochs 111 | - 112 | 0 n_mistakes = 0 113 | 0 if shuffle_data 114 | 0 shuffle!(seed, data_indices) 115 | - end 116 | - 117 | 0 @inbounds for m in data_indices 118 | - #x = view(X, :, m) 119 | 2880000 x = view(X, :, m) 120 | - 121 | 0 y_hat = predict(h, x, y_signal_placeholder) 122 | - #y_hat = predict(h, x) 123 | - 124 | 0 if y[m] != y_hat 125 | 0 n_mistakes += 1 126 | - #### wij ← wij − η (yj −tj) · xi 127 | - 128 | 0 h.W[:, y[m]] .= h.W[:, y[m]] .+ learning_rate .* x 129 | 0 h.b[y[m]] .= h.b[y[m]] .+ learning_rate 130 | 0 h.W[:, y_hat] .= h.W[:, y_hat] .- learning_rate .* x 131 | 0 h.b[y_hat] .= h.b[y_hat] .- learning_rate 132 | - 133 | - #h.W[:, y[m]] .= h.W[:, y[m]] .+ learning_rate .* x 134 | - #h.b[y[m]] .= h.b[y[m]] .+ learning_rate 135 | - #h.W[:, y_hat] .= h.W[:, y_hat] .- learning_rate .* x 136 | - #h.b[y_hat] .= h.b[y_hat] .- learning_rate 137 | - end 138 | - end 139 | - 140 | 0 if compute_accuracy 141 | 0 @inbounds for m in data_indices 142 | 0 y_preds[m] = predict(h, view(X, :, m), y_signal_placeholder) 143 | - end 144 | 0 acc = accuracy(y, y_preds) 145 | 0 push!(h.accuracy, acc) 146 | - else 147 | 0 acc = (n_samples - n_mistakes)/n_samples 148 | 0 push!(h.accuracy, acc) 149 | - end 150 | - 151 | 0 if pocket 152 | 0 if acc > max_acc 153 | 0 max_acc = acc 154 | 0 copy!(W_hat, h.W) 155 | 0 copy!(b_hat, h.b) 156 | - end 157 | - end 158 | 0 if average_weights 159 | 0 W_his .= W_his .+ h.W 160 | 0 b_his .= b_his .+ h.b 161 | - end 162 | - 163 | 0 if print_flag 164 | 0 print("Epoch: $(epoch) \t Accuracy: $(round(acc,3))\r") 165 | 0 flush(STDOUT) 166 | - end 167 | - end 168 | - 169 | 0 if compute_accuracy && pocket 170 | 0 h.W .= W_hat 171 | 0 h.b .= b_hat 172 | - end 173 | 0 if average_weights 174 | 0 println("history weights\n",W_his[1:5]) 175 | 0 println("weights\n",h.W[1:5]) 176 | 0 println("history weights scaled\n",W_his[1:5]/n_epochs) 177 | 0 h.W .= W_his/n_epochs 178 | 0 h.b .= b_his/n_epochs 179 | - end 180 | - 181 | 0 return 182 | - end 183 | - 184 | - end 185 | - 186 | -------------------------------------------------------------------------------- /JuliaML_tutorials/README.md: -------------------------------------------------------------------------------- 1 | # JuliaML_tutorials 2 | Tutorials using the JuliaML code 3 | -------------------------------------------------------------------------------- /JuliaML_tutorials/notebooks_machine_learning/2_Pipelines.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# This notebook shows how to build and optimize a pipeline\n", 8 | "\n", 9 | "Generally solving a learning task can involve preprocessing/feature generation. To avoid overfitting it is important to optimize the whole pipeline (feature generation + model fitting) doing crossvalidation" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 31, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "g (generic function with 1 method)" 21 | ] 22 | }, 23 | "execution_count": 31, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "f(x) = 2 * x\n", 30 | "g(z) = z^2" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 32, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "8" 42 | ] 43 | }, 44 | "execution_count": 32, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "f(g(2))" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 33, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "name": "stdout", 60 | "output_type": "stream", 61 | "text": [ 62 | "search: \u001b[1m|\u001b[22m\u001b[1m>\u001b[22m\n", 63 | "\n" 64 | ] 65 | }, 66 | { 67 | "data": { 68 | "text/markdown": [ 69 | "```\n", 70 | "|>(x, f)\n", 71 | "```\n", 72 | "\n", 73 | "Applies a function to the preceding argument. This allows for easy function chaining.\n", 74 | "\n", 75 | "```jldoctest\n", 76 | "julia> [1:5;] |> x->x.^2 |> sum |> inv\n", 77 | "0.01818181818181818\n", 78 | "```\n" 79 | ], 80 | "text/plain": [ 81 | "```\n", 82 | "|>(x, f)\n", 83 | "```\n", 84 | "\n", 85 | "Applies a function to the preceding argument. This allows for easy function chaining.\n", 86 | "\n", 87 | "```jldoctest\n", 88 | "julia> [1:5;] |> x->x.^2 |> sum |> inv\n", 89 | "0.01818181818181818\n", 90 | "```\n" 91 | ] 92 | }, 93 | "execution_count": 33, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "?|>" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 34, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "data": { 109 | "text/plain": [ 110 | "16" 111 | ] 112 | }, 113 | "execution_count": 34, 114 | "metadata": {}, 115 | "output_type": "execute_result" 116 | } 117 | ], 118 | "source": [ 119 | "2 |> f |> g " 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": { 125 | "collapsed": true 126 | }, 127 | "source": [ 128 | "#### About the pipeline operator\n", 129 | "\n", 130 | "\n", 131 | "The pipeline operator can be used even in scripts that are runned outside julia such as\n", 132 | "\n", 133 | "```\n", 134 | " run(`cat $file` |> \"test.txt\")\n", 135 | "``` \n", 136 | "\n", 137 | "\n", 138 | "This pipeline operator can even be chained as follows:\n", 139 | "```\n", 140 | " run(`echo $(\"\\nhi\\nJulia\")` |> `cat` |> `grep -n J`)\n", 141 | "```\n" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": { 148 | "collapsed": true 149 | }, 150 | "outputs": [], 151 | "source": [] 152 | } 153 | ], 154 | "metadata": { 155 | "kernelspec": { 156 | "display_name": "Julia 0.6.0-rc1", 157 | "language": "julia", 158 | "name": "julia-0.6" 159 | }, 160 | "language_info": { 161 | "file_extension": ".jl", 162 | "mimetype": "application/julia", 163 | "name": "julia", 164 | "version": "0.6.0" 165 | } 166 | }, 167 | "nbformat": 4, 168 | "nbformat_minor": 2 169 | } 170 | -------------------------------------------------------------------------------- /JuliaML_tutorials/notebooks_machine_learning/gridsearch_ram_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import sklearn 3 | from sklearn import linear_model 4 | from sklearn import model_selection 5 | 6 | n_samples = 1000000 7 | 8 | X = np.random.rand(n_samples,100) 9 | y = np.random.randint(1,10,n_samples) 10 | 11 | print("\n\tDoing CV Sequential way, monitor you ram\n\n") 12 | clf = sklearn.linear_model.Perceptron() 13 | grid_params = {"alpha":[0.0001]} 14 | grid_clf = model_selection.GridSearchCV(clf, grid_params, cv=10, n_jobs=1, verbose=2) 15 | grid_clf.fit(X,y) 16 | 17 | 18 | print("\n\tDoing CV parallel way, monitor you ram\n\n") 19 | clf = sklearn.linear_model.Perceptron() 20 | grid_params = {"alpha":[0.0001]} 21 | grid_clf = model_selection.GridSearchCV(clf, grid_params, cv=10, n_jobs=-1, verbose=2) 22 | grid_clf.fit(X,y) 23 | 24 | 25 | -------------------------------------------------------------------------------- /JuliaML_tutorials/packages_to_install.jl: -------------------------------------------------------------------------------- 1 | Pkg.add("BenchmarkTools") 2 | Pkg.add("MLDataUtils") 3 | Plg.add("MLDataPattern") 4 | Pkg.add("ProgressMeter") 5 | #Pkg.clone("https://github.com/JuliaML/MLDatasets.jl.git") 6 | Pkg.update() -------------------------------------------------------------------------------- /MulticlassPerceptron/benchmarks/benchmark.jl: -------------------------------------------------------------------------------- 1 | using MNIST 2 | using BenchmarkTools 3 | 4 | source_path = join(push!(split(pwd(),"/")[1:end-1],"source/" ),"/") 5 | 6 | if !contains(==,LOAD_PATH, source_path) 7 | push!(LOAD_PATH, source_path) 8 | end 9 | 10 | T = Float32 11 | 12 | println("Starting Benchmark") 13 | 14 | using MulticlassPerceptron4 15 | using MulticlassPerceptron3 16 | using MulticlassPerceptron2 17 | using MulticlassPerceptron1 18 | 19 | println("Loading Modules") 20 | 21 | percep1 = MulticlassPerceptron1.MPerceptron(T, 10, 784) 22 | percep2 = MulticlassPerceptron2.MPerceptron(T, 10, 784) 23 | percep3 = MulticlassPerceptron3.MPerceptron(T, 10, 784) 24 | percep4 = MulticlassPerceptron4.MPerceptron(T, 10, 784) 25 | 26 | n_classes = 10 27 | n_features = 784 28 | 29 | println("Loading MNIST") 30 | 31 | X_train, y_train = MNIST.traindata(); 32 | X_test, y_test = MNIST.testdata(); 33 | y_train = y_train + 1 34 | y_test = y_test + 1; 35 | 36 | println("Scaling MNIST") 37 | 38 | X_train = Array{T}((X_train - minimum(X_train))/(maximum(X_train) - minimum(X_train))) 39 | y_train = Array{Int64}(y_train) 40 | X_test = Array{T}(X_test - minimum(X_test))/(maximum(X_test) - minimum(X_test)) 41 | y_test = Array{Int64}(y_test); 42 | 43 | println("\n\tPrecompining and timing") 44 | 45 | @time MulticlassPerceptron1.fit!(percep1, X_train, y_train, 1, 0.0001) 46 | @time MulticlassPerceptron2.fit!(percep2, X_train, y_train, 1, 0.0001) 47 | @time MulticlassPerceptron3.fit!(percep3, X_train, y_train, 1, 0.0001) 48 | @time MulticlassPerceptron4.fit!(percep4, X_train, y_train, 1, 0.0001) 49 | 50 | println("\n\tReal timming") 51 | 52 | @time MulticlassPerceptron1.fit!(percep1, X_train, y_train, 1, 0.0001) 53 | @time MulticlassPerceptron2.fit!(percep2, X_train, y_train, 1, 0.0001) 54 | @time MulticlassPerceptron3.fit!(percep3, X_train, y_train, 1, 0.0001) 55 | @time MulticlassPerceptron4.fit!(percep4, X_train, y_train, 1, 0.0001) 56 | 57 | -------------------------------------------------------------------------------- /MulticlassPerceptron/source/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidbp/learn_julia/a624967aa95ece1912adb216fc652f45fe84d4b1/MulticlassPerceptron/source/.DS_Store -------------------------------------------------------------------------------- /MulticlassPerceptron/source/.MulticlassPerceptron3.jl.swp: -------------------------------------------------------------------------------- 1 | b0nano 2.5.3v davidbsc-laptopMulticlassPerceptron3.jl -------------------------------------------------------------------------------- /MulticlassPerceptron/source/MulticlassPerceptron1.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron1 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | immutable MPerceptron{T} 7 | W::Array{T,2} 8 | b::Vector{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_classes, n_features), 14 | zeros(T, n_classes), 15 | n_classes, 16 | n_features) 17 | 18 | function accuracy(y_true, y_hat) 19 | acc = 0. 20 | 21 | @simd for k = 1:length(y_true) 22 | if y_true[k] == y_hat[k] 23 | acc += 1. 24 | end 25 | end 26 | return acc/length(y_hat) 27 | end 28 | 29 | 30 | function predict(h::MPerceptron, x::Array) 31 | return indmax(h.W*x + h.b) 32 | end 33 | 34 | 35 | function Base.show{T}(io::IO, p::MPerceptron{T}) 36 | n_classes = p.n_classes 37 | n_features = p.n_features 38 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 39 | end 40 | 41 | 42 | """ 43 | function to fit a Perceptron 44 | ' 45 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1)' 46 | ' 47 | """ 48 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1; print_flag=false) 49 | 50 | n_samples = size(X_tr, 2) 51 | x = zeros(h.n_features) 52 | 53 | for epoch in 1:n_epochs 54 | for m in 1:n_samples 55 | x .= X_tr[:,m] 56 | y_hat = predict(h,x) 57 | if y_tr[m] != y_hat 58 | h.W[y_tr[m], :] .+= learning_rate * x 59 | h.b[y_tr[m]] += learning_rate 60 | h.W[y_hat, :] .-= learning_rate * x 61 | h.b[y_hat] -= learning_rate 62 | end 63 | end 64 | 65 | y_preds = [] 66 | for m in 1:n_samples 67 | push!(y_preds, predict(h, X_tr[:,m])) 68 | end 69 | if print_flag println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) end 70 | end 71 | 72 | end 73 | 74 | end 75 | -------------------------------------------------------------------------------- /MulticlassPerceptron/source/MulticlassPerceptron2.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron2 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | immutable MPerceptron{T} 7 | W::Array{T,2} 8 | b::Vector{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_classes, n_features), 14 | zeros(T, n_classes), 15 | n_classes, 16 | n_features) 17 | 18 | function accuracy(y_true, y_hat) 19 | acc = 0. 20 | 21 | @simd for k = 1:length(y_true) 22 | if y_true[k] == y_hat[k] 23 | acc += 1. 24 | end 25 | end 26 | return acc/length(y_hat) 27 | end 28 | 29 | 30 | function predict(h::MPerceptron, x) 31 | return indmax(h.W*x + h.b) 32 | end 33 | 34 | 35 | function Base.show{T}(io::IO, p::MPerceptron{T}) 36 | n_classes = p.n_classes 37 | n_features = p.n_features 38 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 39 | end 40 | 41 | 42 | """ 43 | function to fit a Perceptron 44 | ' 45 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1)' 46 | ' 47 | """ 48 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1; print_flag=false) 49 | 50 | n_samples = size(X_tr, 2) 51 | 52 | for epoch in 1:n_epochs 53 | for m in 1:n_samples 54 | x = view(X_tr,:,m) 55 | y_hat = predict(h,x) 56 | if y_tr[m] != y_hat 57 | h.W[y_tr[m], :] .+= learning_rate * x 58 | h.b[y_tr[m]] .+= learning_rate 59 | h.W[y_hat, :] .-= learning_rate * x 60 | h.b[y_hat] .-= learning_rate 61 | end 62 | end 63 | 64 | y_preds = [] 65 | for m in 1:n_samples 66 | push!(y_preds, predict(h, view(X_tr,:,m) )) 67 | end 68 | if print_flag println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) end 69 | end 70 | 71 | end 72 | 73 | end 74 | -------------------------------------------------------------------------------- /MulticlassPerceptron/source/MulticlassPerceptron3.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron3 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | immutable MPerceptron{T} 7 | W::Array{T,2} 8 | b::Vector{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_classes, n_features), 14 | zeros(T, n_classes), 15 | n_classes, 16 | n_features) 17 | 18 | function accuracy(y_true, y_hat) 19 | acc = 0. 20 | 21 | @inbounds for k = 1:length(y_true) 22 | if y_true[k] == y_hat[k] 23 | acc += 1. 24 | end 25 | end 26 | return acc/length(y_hat) 27 | end 28 | 29 | 30 | function predict(h::MPerceptron, x, placeholder) 31 | placeholder .= A_mul_B!(placeholder, h.W,x) .+ h.b 32 | y_hat = indmax(placeholder ) 33 | return y_hat 34 | end 35 | 36 | 37 | function Base.show{T}(io::IO, p::MPerceptron{T}) 38 | n_classes = p.n_classes 39 | n_features = p.n_features 40 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 41 | end 42 | 43 | 44 | """ 45 | function to fit a Perceptron 46 | ' 47 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1)' 48 | ' 49 | """ 50 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1; print_flag=false) 51 | 52 | n_samples = size(X_tr, 2) 53 | y_signal_placeholder = zeros(h.b) 54 | 55 | @inbounds for epoch in 1:n_epochs 56 | for m in 1:n_samples 57 | x = view(X_tr,:,m) 58 | y_hat = predict(h, x, y_signal_placeholder) 59 | if y_tr[m] != y_hat 60 | h.W[y_tr[m], :] .+= learning_rate * x 61 | h.b[y_tr[m]] += learning_rate 62 | h.W[y_hat, :] .-= learning_rate * x 63 | h.b[y_hat] -= learning_rate 64 | end 65 | end 66 | 67 | y_preds = [] 68 | for m in 1:n_samples 69 | push!(y_preds, predict(h, view(X_tr,:,m), y_signal_placeholder)) 70 | end 71 | if print_flag println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) end 72 | end 73 | 74 | end 75 | 76 | end 77 | -------------------------------------------------------------------------------- /MulticlassPerceptron/source/MulticlassPerceptron4.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron4 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | immutable MPerceptron{T} 7 | W::Array{T,2} 8 | b::Vector{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_classes, n_features), 14 | zeros(T, n_classes), 15 | n_classes, 16 | n_features) 17 | 18 | function accuracy(y_true, y_hat) 19 | acc = 0. 20 | @inbounds for k = 1:length(y_true) 21 | acc += y_true[k] == y_hat[k] 22 | end 23 | return acc/length(y_hat) 24 | end 25 | 26 | function predict(h::MPerceptron, x, placeholder) 27 | placeholder .= A_mul_B!(placeholder, h.W,x) .+ h.b 28 | return indmax(placeholder) 29 | end 30 | 31 | function Base.show{T}(io::IO, p::MPerceptron{T}) 32 | n_classes = p.n_classes 33 | n_features = p.n_features 34 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 35 | end 36 | 37 | """ 38 | function to fit a Perceptron 39 | ' 40 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1) 41 | ' 42 | """ 43 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1; print_flag=false) 44 | 45 | T = eltype(X_tr) 46 | n_samples = size(X_tr, 2) 47 | y_signal_placeholder = zeros(T, h.n_classes) 48 | y_preds = zeros(Int64, n_samples) 49 | 50 | @inbounds for epoch in 1:n_epochs 51 | for m in 1:n_samples 52 | x = view(X_tr,:,m) 53 | y_hat = predict(h, x, y_signal_placeholder) 54 | if y_tr[m] != y_hat 55 | h.W[y_tr[m], :] .+= learning_rate .* x 56 | h.b[y_tr[m]] .+= learning_rate 57 | h.W[y_hat, :] .-= learning_rate .* x 58 | h.b[y_hat] .-= learning_rate 59 | end 60 | end 61 | 62 | @inbounds for m in 1:n_samples 63 | y_preds[m] = predict(h, view(X_tr,:,m), y_signal_placeholder) 64 | end 65 | 66 | if print_flag println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) end 67 | end 68 | end 69 | 70 | end 71 | -------------------------------------------------------------------------------- /MulticlassPerceptron/source2/MulticlassPerceptron1.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron1 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | type MPerceptron{T} 7 | W::Array{T,2} 8 | b::Vector{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_classes, n_features), 14 | zeros(T, n_classes), 15 | n_classes, 16 | n_features) 17 | 18 | function accuracy(y_true, y_hat) 19 | acc = 0. 20 | 21 | @simd for k = 1:length(y_true) 22 | if y_true[k] == y_hat[k] 23 | acc += 1. 24 | end 25 | end 26 | return acc/length(y_hat) 27 | end 28 | 29 | 30 | function predict(h::MPerceptron, x::Array) 31 | return indmax(h.W*x + h.b) 32 | end 33 | 34 | 35 | function Base.show{T}(io::IO, p::MPerceptron{T}) 36 | n_classes = p.n_classes 37 | n_features = p.n_features 38 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 39 | end 40 | 41 | 42 | """ 43 | function to fit a Perceptron 44 | ' 45 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1)' 46 | ' 47 | """ 48 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1) 49 | 50 | n_samples = size(X_tr, 2) 51 | x = zeros(h.n_features) 52 | 53 | for epoch in 1:n_epochs 54 | for m in 1:n_samples 55 | x .= X_tr[:,m] 56 | y_hat = predict(h,x) 57 | if y_tr[m] != y_hat 58 | h.W[y_tr[m], :] .+= learning_rate * x 59 | h.b[y_tr[m]] += learning_rate 60 | h.W[y_hat, :] .-= learning_rate * x 61 | h.b[y_hat] -= learning_rate 62 | end 63 | end 64 | 65 | y_preds = [] 66 | for m in 1:n_samples 67 | push!(y_preds, predict(h, X_tr[:,m])) 68 | end 69 | println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) 70 | end 71 | 72 | end 73 | 74 | end 75 | -------------------------------------------------------------------------------- /MulticlassPerceptron/source2/MulticlassPerceptron2.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron2 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | type MPerceptron{T} 7 | W::Array{T} 8 | b::Array{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_classes, n_features), 14 | zeros(T, n_classes), 15 | n_classes, 16 | n_features) 17 | 18 | function accuracy(y_true, y_hat) 19 | acc = 0. 20 | 21 | @simd for k = 1:length(y_true) 22 | if y_true[k] == y_hat[k] 23 | acc += 1. 24 | end 25 | end 26 | return acc/length(y_hat) 27 | end 28 | 29 | 30 | function predict(h::MPerceptron, x) 31 | return indmax(h.W*x + h.b) 32 | end 33 | 34 | 35 | function Base.show{T}(io::IO, p::MPerceptron{T}) 36 | n_classes = p.n_classes 37 | n_features = p.n_features 38 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 39 | end 40 | 41 | 42 | """ 43 | function to fit a Perceptron 44 | ' 45 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1)' 46 | ' 47 | """ 48 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1) 49 | 50 | n_samples = size(X_tr, 2) 51 | 52 | for epoch in 1:n_epochs 53 | for m in 1:n_samples 54 | x = view(X_tr,:,m) 55 | y_hat = predict(h,x) 56 | if y_tr[m] != y_hat 57 | h.W[y_tr[m], :] .+= learning_rate * x 58 | h.b[y_tr[m]] .+= learning_rate 59 | h.W[y_hat, :] .-= learning_rate * x 60 | h.b[y_hat] .-= learning_rate 61 | end 62 | end 63 | 64 | y_preds = [] 65 | for m in 1:n_samples 66 | push!(y_preds, predict(h, view(X_tr,:,m) )) 67 | end 68 | println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) 69 | end 70 | 71 | end 72 | 73 | end 74 | -------------------------------------------------------------------------------- /MulticlassPerceptron/source2/MulticlassPerceptron3.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron3 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | type MPerceptron{T} 7 | W::Array{T} 8 | b::Array{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_classes, n_features), 14 | zeros(T, n_classes), 15 | n_classes, 16 | n_features) 17 | 18 | function accuracy(y_true, y_hat) 19 | acc = 0. 20 | 21 | @inbounds for k = 1:length(y_true) 22 | if y_true[k] == y_hat[k] 23 | acc += 1. 24 | end 25 | end 26 | return acc/length(y_hat) 27 | end 28 | 29 | 30 | function predict(h::MPerceptron, x, placeholder) 31 | placeholder .= A_mul_B!(placeholder, h.W,x) .+ h.b 32 | y_hat = indmax(placeholder ) 33 | return y_hat 34 | end 35 | 36 | 37 | function Base.show{T}(io::IO, p::MPerceptron{T}) 38 | n_classes = p.n_classes 39 | n_features = p.n_features 40 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 41 | end 42 | 43 | 44 | """ 45 | function to fit a Perceptron 46 | ' 47 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1)' 48 | ' 49 | """ 50 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1) 51 | 52 | n_samples = size(X_tr, 2) 53 | y_signal_placeholder = zeros(h.b) 54 | 55 | @inbounds for epoch in 1:n_epochs 56 | for m in 1:n_samples 57 | x = view(X_tr,:,m) 58 | y_hat = predict(h, x, y_signal_placeholder) 59 | if y_tr[m] != y_hat 60 | h.W[y_tr[m], :] .+= learning_rate * x 61 | h.b[y_tr[m]] += learning_rate 62 | h.W[y_hat, :] .-= learning_rate * x 63 | h.b[y_hat] -= learning_rate 64 | end 65 | end 66 | 67 | y_preds = [] 68 | for m in 1:n_samples 69 | push!(y_preds, predict(h, view(X_tr,:,m), y_signal_placeholder)) 70 | end 71 | println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) 72 | end 73 | 74 | end 75 | 76 | end 77 | -------------------------------------------------------------------------------- /MulticlassPerceptron/source2/MulticlassPerceptron4.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron4 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | type MPerceptron{T} 7 | W::Array{T} 8 | b::Array{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_classes, n_features), 14 | zeros(T, n_classes), 15 | n_classes, 16 | n_features) 17 | 18 | function accuracy(y_true, y_hat) 19 | acc = 0. 20 | @inbounds for k = 1:length(y_true) 21 | acc += y_true[k] == y_hat[k] 22 | end 23 | return acc/length(y_hat) 24 | end 25 | 26 | function predict(h::MPerceptron, x, placeholder) 27 | placeholder .= A_mul_B!(placeholder, h.W,x) .+ h.b 28 | return indmax(placeholder) 29 | end 30 | 31 | function Base.show{T}(io::IO, p::MPerceptron{T}) 32 | n_classes = p.n_classes 33 | n_features = p.n_features 34 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 35 | end 36 | 37 | """ 38 | function to fit a Perceptron 39 | ' 40 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1) 41 | ' 42 | """ 43 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1) 44 | 45 | T = eltype(X_tr) 46 | n_samples = size(X_tr, 2) 47 | y_signal_placeholder = zeros(T, h.n_classes) 48 | y_preds = zeros(Int64, n_samples) 49 | 50 | @inbounds for epoch in 1:n_epochs 51 | for m in 1:n_samples 52 | x = view(X_tr,:,m) 53 | y_hat = predict(h, x, y_signal_placeholder) 54 | if y_tr[m] != y_hat 55 | h.W[y_tr[m], :] .+= learning_rate .* x 56 | h.b[y_tr[m]] .+= learning_rate 57 | h.W[y_hat, :] .-= learning_rate .* x 58 | h.b[y_hat] .-= learning_rate 59 | end 60 | end 61 | 62 | @inbounds for m in 1:n_samples 63 | y_preds[m] = predict(h, view(X_tr,:,m), y_signal_placeholder) 64 | end 65 | 66 | println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) 67 | end 68 | end 69 | 70 | end 71 | -------------------------------------------------------------------------------- /MulticlassPerceptronBycol/benchmarks/benchmark.jl: -------------------------------------------------------------------------------- 1 | using MNIST 2 | using BenchmarkTools 3 | 4 | source_path = join(push!(split(pwd(),"/")[1:end-1],"source/" ),"/") 5 | 6 | if !contains(==,LOAD_PATH, source_path) 7 | push!(LOAD_PATH, source_path) 8 | end 9 | 10 | T = Float32 11 | 12 | println("Starting Benchmark") 13 | begin 14 | using MulticlassPerceptron4 15 | using MulticlassPerceptron3 16 | using MulticlassPerceptron2 17 | using MulticlassPerceptron1 18 | 19 | println("Loading Modules") 20 | 21 | percep1 = MulticlassPerceptron1.MPerceptron(T, 10, 784) 22 | percep2 = MulticlassPerceptron2.MPerceptron(T, 10, 784) 23 | percep3 = MulticlassPerceptron3.MPerceptron(T, 10, 784) 24 | percep4 = MulticlassPerceptron4.MPerceptron(T, 10, 784) 25 | 26 | n_classes = 10 27 | n_features = 784 28 | 29 | println("Loading MNIST") 30 | X_train, y_train = MNIST.traindata(); 31 | X_test, y_test = MNIST.testdata(); 32 | y_train = y_train + 1 33 | y_test = y_test + 1; 34 | X_train = Array{T}((X_train - minimum(X_train))/(maximum(X_train) - minimum(X_train))) 35 | y_train = Array{Int64}(y_train) 36 | X_test = Array{T}(X_test - minimum(X_test))/(maximum(X_test) - minimum(X_test)) 37 | y_test = Array{Int64}(y_test); 38 | 39 | println("\n\tMachine information") 40 | 41 | println("\n\tPeakflops:", peakflops()) 42 | 43 | println("\n\tversioninfo:\n") 44 | versioninfo() 45 | 46 | println("\n\tComputing benchmarks... ") 47 | 48 | results1 = @benchmark MulticlassPerceptron1.fit!(percep1, X_train, y_train, 1, 0.0001) 49 | results2 = @benchmark MulticlassPerceptron2.fit!(percep2, X_train, y_train, 1, 0.0001) 50 | results3 = @benchmark MulticlassPerceptron3.fit!(percep3, X_train, y_train, 1, 0.0001) 51 | results4 = @benchmark MulticlassPerceptron4.fit!(percep4, X_train, y_train, 1, 0.0001) 52 | 53 | 54 | println("\nPerceptron1---------------------") 55 | display(results1) 56 | println("\n\nPerceptron2---------------------") 57 | display(results2) 58 | println("\n\nPerceptron3---------------------") 59 | display(results3) 60 | println("\n\nPerceptron4---------------------") 61 | display(results4) 62 | println("\n") 63 | end 64 | -------------------------------------------------------------------------------- /MulticlassPerceptronBycol/source/MulticlassPerceptron1.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron1 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | immutable MPerceptron{T} 7 | W::Array{T,2} 8 | b::Vector{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_features, n_classes), 14 | zeros(T, n_classes), 15 | n_classes, 16 | n_features) 17 | 18 | function accuracy(y_true, y_hat) 19 | acc = 0. 20 | 21 | @simd for k = 1:length(y_true) 22 | if y_true[k] == y_hat[k] 23 | acc += 1. 24 | end 25 | end 26 | return acc/length(y_hat) 27 | end 28 | 29 | 30 | function predict(h::MPerceptron, x::Array) 31 | return indmax( h.W'x + h.b) 32 | end 33 | 34 | 35 | function Base.show{T}(io::IO, p::MPerceptron{T}) 36 | n_classes = p.n_classes 37 | n_features = p.n_features 38 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 39 | end 40 | 41 | 42 | """ 43 | function to fit a Perceptron 44 | ' 45 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1)' 46 | ' 47 | """ 48 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1; print_flag=false) 49 | 50 | n_samples = size(X_tr, 2) 51 | x = zeros(h.n_features) 52 | 53 | for epoch in 1:n_epochs 54 | for m in 1:n_samples 55 | x .= X_tr[:,m] 56 | y_hat = predict(h,x) 57 | if y_tr[m] != y_hat 58 | h.W[:, y_tr[m]] .+= learning_rate * x 59 | h.b[y_tr[m]] += learning_rate 60 | h.W[:, y_hat] .-= learning_rate * x 61 | h.b[y_hat] -= learning_rate 62 | end 63 | end 64 | 65 | y_preds = [] 66 | for m in 1:n_samples 67 | push!(y_preds, predict(h, X_tr[:,m])) 68 | end 69 | if print_flag println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) end 70 | end 71 | 72 | end 73 | 74 | end 75 | -------------------------------------------------------------------------------- /MulticlassPerceptronBycol/source/MulticlassPerceptron2.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron2 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | immutable MPerceptron{T} 7 | W::Array{T,2} 8 | b::Vector{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | 14 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_features, n_classes), 15 | zeros(T, n_classes), 16 | n_classes, 17 | n_features) 18 | 19 | 20 | function accuracy(y_true, y_hat) 21 | acc = 0. 22 | 23 | @simd for k = 1:length(y_true) 24 | if y_true[k] == y_hat[k] 25 | acc += 1. 26 | end 27 | end 28 | return acc/length(y_hat) 29 | end 30 | 31 | 32 | function predict(h::MPerceptron, x) 33 | return indmax( At_mul_B(h.W,x) + h.b) 34 | end 35 | 36 | 37 | function Base.show{T}(io::IO, p::MPerceptron{T}) 38 | n_classes = p.n_classes 39 | n_features = p.n_features 40 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 41 | end 42 | 43 | 44 | """ 45 | function to fit a Perceptron 46 | ' 47 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1)' 48 | ' 49 | """ 50 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1; print_flag=false) 51 | 52 | n_samples = size(X_tr, 2) 53 | 54 | for epoch in 1:n_epochs 55 | for m in 1:n_samples 56 | x = view(X_tr,:,m) 57 | y_hat = predict(h,x) 58 | if y_tr[m] != y_hat 59 | h.W[:, y_tr[m]] .+= learning_rate * x 60 | h.b[y_tr[m]] += learning_rate 61 | h.W[:, y_hat] .-= learning_rate * x 62 | h.b[y_hat] -= learning_rate 63 | end 64 | end 65 | 66 | y_preds = [] 67 | for m in 1:n_samples 68 | push!(y_preds, predict(h, view(X_tr,:,m) )) 69 | end 70 | if print_flag println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) end 71 | end 72 | 73 | end 74 | 75 | end 76 | -------------------------------------------------------------------------------- /MulticlassPerceptronBycol/source/MulticlassPerceptron3.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron3 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | immutable MPerceptron{T} 7 | W::Array{T,2} 8 | b::Vector{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | 14 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_features, n_classes), 15 | zeros(T, n_classes), 16 | n_classes, 17 | n_features) 18 | 19 | 20 | function accuracy(y_true, y_hat) 21 | acc = 0. 22 | 23 | @inbounds for k = 1:length(y_true) 24 | if y_true[k] == y_hat[k] 25 | acc += 1. 26 | end 27 | end 28 | return acc/length(y_hat) 29 | end 30 | 31 | 32 | function predict(h::MPerceptron, x, placeholder) 33 | placeholder .= At_mul_B!(placeholder, h.W,x) .+ h.b 34 | y_hat = indmax(placeholder) 35 | return y_hat 36 | end 37 | 38 | 39 | function Base.show{T}(io::IO, p::MPerceptron{T}) 40 | n_classes = p.n_classes 41 | n_features = p.n_features 42 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 43 | end 44 | 45 | 46 | """ 47 | function to fit a Perceptron 48 | ' 49 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1)' 50 | ' 51 | """ 52 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1; print_flag=false) 53 | 54 | n_samples = size(X_tr, 2) 55 | y_signal_placeholder = zeros(h.b) 56 | 57 | @inbounds for epoch in 1:n_epochs 58 | for m in 1:n_samples 59 | x = view(X_tr,:,m) 60 | y_hat = predict(h, x, y_signal_placeholder) 61 | if y_tr[m] != y_hat 62 | h.W[:, y_tr[m]] .+= learning_rate * x 63 | h.b[y_tr[m]] += learning_rate 64 | h.W[:, y_hat] .-= learning_rate * x 65 | h.b[y_hat] -= learning_rate 66 | end 67 | end 68 | 69 | y_preds = [] 70 | for m in 1:n_samples 71 | push!(y_preds, predict(h, view(X_tr,:,m), y_signal_placeholder)) 72 | end 73 | if print_flag println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) end 74 | end 75 | 76 | end 77 | 78 | end 79 | -------------------------------------------------------------------------------- /MulticlassPerceptronBycol/source/MulticlassPerceptron4.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron4 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | immutable MPerceptron{T} 7 | W::Array{T,2} 8 | b::Vector{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_features, n_classes), 14 | zeros(T, n_classes), 15 | n_classes, 16 | n_features) 17 | 18 | 19 | function accuracy(y_true, y_hat) 20 | acc = 0. 21 | @inbounds for k = 1:length(y_true) 22 | acc += y_true[k] == y_hat[k] 23 | end 24 | return acc/length(y_hat) 25 | end 26 | 27 | function predict(h::MPerceptron, x, placeholder) 28 | placeholder .= At_mul_B!(placeholder, h.W, x) .+ h.b 29 | return indmax(placeholder) 30 | end 31 | 32 | function Base.show{T}(io::IO, p::MPerceptron{T}) 33 | n_classes = p.n_classes 34 | n_features = p.n_features 35 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 36 | end 37 | 38 | """ 39 | function to fit a Perceptron 40 | ' 41 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1) 42 | ' 43 | """ 44 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1; print_flag=false) 45 | 46 | T = eltype(X_tr) 47 | n_samples = size(X_tr, 2) 48 | y_signal_placeholder = zeros(T, h.n_classes) 49 | y_preds = zeros(Int64, n_samples) 50 | 51 | @inbounds for epoch in 1:n_epochs 52 | for m in 1:n_samples 53 | x = view(X_tr, :, m) 54 | y_hat = predict(h, x, y_signal_placeholder) 55 | if y_tr[m] != y_hat 56 | h.W[:, y_tr[m]] .+= learning_rate .* x 57 | h.b[y_tr[m]] .+= learning_rate 58 | h.W[:, y_hat] .-= learning_rate .* x 59 | h.b[y_hat] .-= learning_rate 60 | end 61 | end 62 | 63 | @inbounds for m in 1:n_samples 64 | y_preds[m] = predict(h, view(X_tr, :, m), y_signal_placeholder) 65 | end 66 | 67 | if print_flag println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) end 68 | end 69 | end 70 | 71 | end 72 | -------------------------------------------------------------------------------- /MulticlassPerceptronBycol/source2/MulticlassPerceptron1.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron1 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | type MPerceptron{T} 7 | W::Array{T,2} 8 | b::Vector{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_classes, n_features), 14 | zeros(T, n_classes), 15 | n_classes, 16 | n_features) 17 | 18 | function accuracy(y_true, y_hat) 19 | acc = 0. 20 | 21 | @simd for k = 1:length(y_true) 22 | if y_true[k] == y_hat[k] 23 | acc += 1. 24 | end 25 | end 26 | return acc/length(y_hat) 27 | end 28 | 29 | 30 | function predict(h::MPerceptron, x::Array) 31 | return indmax(h.W*x + h.b) 32 | end 33 | 34 | 35 | function Base.show{T}(io::IO, p::MPerceptron{T}) 36 | n_classes = p.n_classes 37 | n_features = p.n_features 38 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 39 | end 40 | 41 | 42 | """ 43 | function to fit a Perceptron 44 | ' 45 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1)' 46 | ' 47 | """ 48 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1) 49 | 50 | n_samples = size(X_tr, 2) 51 | x = zeros(h.n_features) 52 | 53 | for epoch in 1:n_epochs 54 | for m in 1:n_samples 55 | x .= X_tr[:,m] 56 | y_hat = predict(h,x) 57 | if y_tr[m] != y_hat 58 | h.W[y_tr[m], :] .+= learning_rate * x 59 | h.b[y_tr[m]] += learning_rate 60 | h.W[y_hat, :] .-= learning_rate * x 61 | h.b[y_hat] -= learning_rate 62 | end 63 | end 64 | 65 | y_preds = [] 66 | for m in 1:n_samples 67 | push!(y_preds, predict(h, X_tr[:,m])) 68 | end 69 | println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) 70 | end 71 | 72 | end 73 | 74 | end 75 | -------------------------------------------------------------------------------- /MulticlassPerceptronBycol/source2/MulticlassPerceptron2.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron2 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | type MPerceptron{T} 7 | W::Array{T} 8 | b::Array{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_classes, n_features), 14 | zeros(T, n_classes), 15 | n_classes, 16 | n_features) 17 | 18 | function accuracy(y_true, y_hat) 19 | acc = 0. 20 | 21 | @simd for k = 1:length(y_true) 22 | if y_true[k] == y_hat[k] 23 | acc += 1. 24 | end 25 | end 26 | return acc/length(y_hat) 27 | end 28 | 29 | 30 | function predict(h::MPerceptron, x) 31 | return indmax(h.W*x + h.b) 32 | end 33 | 34 | 35 | function Base.show{T}(io::IO, p::MPerceptron{T}) 36 | n_classes = p.n_classes 37 | n_features = p.n_features 38 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 39 | end 40 | 41 | 42 | """ 43 | function to fit a Perceptron 44 | ' 45 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1)' 46 | ' 47 | """ 48 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1) 49 | 50 | n_samples = size(X_tr, 2) 51 | 52 | for epoch in 1:n_epochs 53 | for m in 1:n_samples 54 | x = view(X_tr,:,m) 55 | y_hat = predict(h,x) 56 | if y_tr[m] != y_hat 57 | h.W[y_tr[m], :] .+= learning_rate * x 58 | h.b[y_tr[m]] .+= learning_rate 59 | h.W[y_hat, :] .-= learning_rate * x 60 | h.b[y_hat] .-= learning_rate 61 | end 62 | end 63 | 64 | y_preds = [] 65 | for m in 1:n_samples 66 | push!(y_preds, predict(h, view(X_tr,:,m) )) 67 | end 68 | println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) 69 | end 70 | 71 | end 72 | 73 | end 74 | -------------------------------------------------------------------------------- /MulticlassPerceptronBycol/source2/MulticlassPerceptron3.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron3 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | type MPerceptron{T} 7 | W::Array{T} 8 | b::Array{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_classes, n_features), 14 | zeros(T, n_classes), 15 | n_classes, 16 | n_features) 17 | 18 | function accuracy(y_true, y_hat) 19 | acc = 0. 20 | 21 | @inbounds for k = 1:length(y_true) 22 | if y_true[k] == y_hat[k] 23 | acc += 1. 24 | end 25 | end 26 | return acc/length(y_hat) 27 | end 28 | 29 | 30 | function predict(h::MPerceptron, x, placeholder) 31 | placeholder .= A_mul_B!(placeholder, h.W,x) .+ h.b 32 | y_hat = indmax(placeholder ) 33 | return y_hat 34 | end 35 | 36 | 37 | function Base.show{T}(io::IO, p::MPerceptron{T}) 38 | n_classes = p.n_classes 39 | n_features = p.n_features 40 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 41 | end 42 | 43 | 44 | """ 45 | function to fit a Perceptron 46 | ' 47 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1)' 48 | ' 49 | """ 50 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1) 51 | 52 | n_samples = size(X_tr, 2) 53 | y_signal_placeholder = zeros(h.b) 54 | 55 | @inbounds for epoch in 1:n_epochs 56 | for m in 1:n_samples 57 | x = view(X_tr,:,m) 58 | y_hat = predict(h, x, y_signal_placeholder) 59 | if y_tr[m] != y_hat 60 | h.W[y_tr[m], :] .+= learning_rate * x 61 | h.b[y_tr[m]] += learning_rate 62 | h.W[y_hat, :] .-= learning_rate * x 63 | h.b[y_hat] -= learning_rate 64 | end 65 | end 66 | 67 | y_preds = [] 68 | for m in 1:n_samples 69 | push!(y_preds, predict(h, view(X_tr,:,m), y_signal_placeholder)) 70 | end 71 | println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) 72 | end 73 | 74 | end 75 | 76 | end 77 | -------------------------------------------------------------------------------- /MulticlassPerceptronBycol/source2/MulticlassPerceptron4.jl: -------------------------------------------------------------------------------- 1 | 2 | module MulticlassPerceptron4 3 | 4 | export predict, MPerceptron, fit! 5 | 6 | type MPerceptron{T} 7 | W::Array{T} 8 | b::Array{T} 9 | n_classes::Int 10 | n_features::Int 11 | end 12 | 13 | MPerceptron(T::Type, n_classes::Int, n_features::Int) = MPerceptron(rand(T, n_classes, n_features), 14 | zeros(T, n_classes), 15 | n_classes, 16 | n_features) 17 | 18 | function accuracy(y_true, y_hat) 19 | acc = 0. 20 | @inbounds for k = 1:length(y_true) 21 | acc += y_true[k] == y_hat[k] 22 | end 23 | return acc/length(y_hat) 24 | end 25 | 26 | function predict(h::MPerceptron, x, placeholder) 27 | placeholder .= A_mul_B!(placeholder, h.W,x) .+ h.b 28 | return indmax(placeholder) 29 | end 30 | 31 | function Base.show{T}(io::IO, p::MPerceptron{T}) 32 | n_classes = p.n_classes 33 | n_features = p.n_features 34 | print(io, "Perceptron{$T}(n_classes=$n_classes, n_features=$n_features)") 35 | end 36 | 37 | """ 38 | function to fit a Perceptron 39 | ' 40 | fit!(h::Perceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1) 41 | ' 42 | """ 43 | function fit!(h::MPerceptron, X_tr::Array, y_tr::Array, n_epochs::Int, learning_rate=0.1) 44 | 45 | T = eltype(X_tr) 46 | n_samples = size(X_tr, 2) 47 | y_signal_placeholder = zeros(T, h.n_classes) 48 | y_preds = zeros(Int64, n_samples) 49 | 50 | @inbounds for epoch in 1:n_epochs 51 | for m in 1:n_samples 52 | x = view(X_tr,:,m) 53 | y_hat = predict(h, x, y_signal_placeholder) 54 | if y_tr[m] != y_hat 55 | h.W[y_tr[m], :] .+= learning_rate .* x 56 | h.b[y_tr[m]] += learning_rate 57 | h.W[y_hat, :] .-= learning_rate .* x 58 | h.b[y_hat] -= learning_rate 59 | end 60 | end 61 | 62 | @inbounds for m in 1:n_samples 63 | y_preds[m] = predict(h, view(X_tr,:,m), y_signal_placeholder) 64 | end 65 | 66 | println("Accuracy epoch ", epoch, " is :", accuracy(y_tr, y_preds)) 67 | end 68 | end 69 | 70 | end 71 | -------------------------------------------------------------------------------- /MulticlassPerceptronMLJ/AmazonBookReviews.jl: -------------------------------------------------------------------------------- 1 | 2 | module AmazonBookReviews 3 | 4 | export load_data, decode_words 5 | 6 | # push!(LOAD_PATH, pwd()) 7 | 8 | function decode_words(x, pos_to_word) 9 | return [pos_to_word[position] for (position,counts) in enumerate(x) if counts>0] 10 | end 11 | 12 | function load_data(;path="",min_support=5, element_type=Float32) 13 | xneg = readlines(path * "negative.review") 14 | xpos = readlines(path * "positive.review") 15 | T = element_type 16 | word_counts_pos = Dict{String,T}([]) 17 | word_counts_neg = Dict{String,T}([]) 18 | 19 | for x in xneg 20 | x = split(x)[1:end-1] 21 | for w_c in x 22 | (w, c) = split(w_c, ':') 23 | c = parse(T, c) 24 | if haskey(word_counts_pos, w) 25 | word_counts_pos[w] = word_counts_pos[w] + c 26 | else 27 | word_counts_pos[w] = 1 28 | end 29 | end 30 | end 31 | 32 | for x in xpos 33 | x = split(x)[1:end-1] 34 | for w_c in x 35 | (w, c) = split(w_c, ':') 36 | c = parse(T, c) 37 | if haskey(word_counts_neg, w) 38 | word_counts_neg[w] = word_counts_neg[w] + c 39 | else 40 | word_counts_neg[w] = 1 41 | end 42 | end 43 | end 44 | 45 | print("min support:", min_support) 46 | total_counts = merge(+, word_counts_pos, word_counts_neg) 47 | supported_word_counts = Dict(w => c for (w,c) in total_counts if c >=min_support) 48 | word_to_pos = Dict(x => i for (i,x) in enumerate(keys(supported_word_counts))) 49 | pos_to_word = Dict(i => x for (i,x) in enumerate(keys(supported_word_counts))) 50 | 51 | n_samples = length(xneg) 52 | n_features = length(word_to_pos) 53 | data_encoded_neg = zeros(T, n_features , n_samples) 54 | 55 | for (m,x) in enumerate(xneg) 56 | x = split(x)[1:end-1] 57 | for w_c in x 58 | (w, c) = split(w_c, ':') 59 | c = parse(T, c) 60 | if haskey(word_to_pos,w) 61 | data_encoded_neg[word_to_pos[w],m] = c 62 | end 63 | end 64 | end 65 | 66 | n_samples = length(xpos) 67 | n_features = length(word_to_pos) 68 | data_encoded_pos = zeros(T, n_features , n_samples) 69 | 70 | for (m,x) in enumerate(xpos) 71 | x = split(x)[1:end-1] 72 | for w_c in x 73 | (w, c) = split(w_c, ':') 74 | c = parse(T, c) 75 | if haskey(word_to_pos,w) 76 | data_encoded_pos[word_to_pos[w],m] = c 77 | end 78 | end 79 | end 80 | 81 | X = hcat(data_encoded_neg , data_encoded_pos) 82 | y = vcat(-ones(length(xneg)), ones(length(xpos)) ) 83 | 84 | return (word_to_pos, pos_to_word, supported_word_counts, (X,y)) 85 | end 86 | 87 | 88 | 89 | end 90 | -------------------------------------------------------------------------------- /MulticlassPerceptronMLJ/README.md: -------------------------------------------------------------------------------- 1 | # Perceptron 2 | 3 | Package for training Multiclass Perceptron models. 4 | 5 | A library for those who feel deeply vanished from the (maybe non linearly separable) world. 6 | 7 | A library for the deepfolks who, maybe, stumble upon linearly separable problems. 8 | 9 | ### Installation 10 | 11 | You can clone the package 12 | ```julia 13 | using Pkg 14 | Pkg.clone("https://github.com/davidbp/MulticlassPerceptron.jl") 15 | ``` 16 | Or you can add the package. Remember to be in pkg mode inside Julia (type `]`). 17 | ``` 18 | (v1.1) pkg> add "https://github.com/davidbp/MulticlassPerceptron.jl" 19 | ``` 20 | 21 | 22 | ### Basic usage 23 | ```julia 24 | using MLDatasets 25 | 26 | train_x, train_y = MLDatasets.MNIST.traindata(); 27 | test_x, test_y = MLDatasets.MNIST.testdata(); 28 | train_x = Float32.(train_x); 29 | test_x = Float32.(test_x); 30 | train_y = train_y .+ 1; 31 | test_y = test_y .+ 1; 32 | train_x = reshape(train_x, 784, 60000); 33 | test_x = reshape(test_x, 784, 10000); 34 | ``` 35 | 36 | We can create a `PerceptronClassifer` type defining the type of the weights, the number of classes, 37 | and the number of features. 38 | 39 | The function `Perceptron.fit!` is used to train the model. 40 | 41 | ```julia 42 | using MulticlassPerceptron 43 | scores = [] 44 | n_features = size(train_x, 1); 45 | n_classes = length(unique(train_y)); 46 | perceptron = MulticlassPerceptronClassifier(Float32, n_classes, n_features); 47 | MulticlassPerceptron.fit!(perceptron, train_x[:,1:10], train_y[1:10], scores; print_flag=false, n_epochs=10); 48 | ``` 49 | 50 | #### Details of the `fit!` function 51 | 52 | > fit!(h::PerceptronClassifer, 53 | > X::Array, 54 | > y::Array; 55 | > n_epochs=50, 56 | > learning_rate=0.1, 57 | > print_flag=false, 58 | > compute_accuracy=true, 59 | > seed=srand(1234), 60 | > pocket=false, 61 | > shuffle_data=false) 62 | 63 | ##### Arguments 64 | 65 | - **`h`**, (PerceptronClassifer{T} type), Multiclass perceptron. 66 | - **`X`**, (Array{T,2} type), data contained in the columns of X. 67 | - **`y`**, (Vector{T} type), class labels (as integers from 1 to n_classes). 68 | 69 | ##### Keyword arguments 70 | 71 | - **`n_epochs`**, (Integer type), number of passes (epochs) through the data. 72 | - **`learning_rate`**, (Float type), learning rate (The standard perceptron is with learning_rate=1.) 73 | - **`compute_accuracy`**, (Bool type), if `true` the accuracy is computed at the end of every epoch. 74 | - **`print_flag`**, (Bool type), if `true` the accuracy is printed at the end of every epoch. 75 | - **`seed`**, (MersenneTwister type), seed for the permutation of the datapoints in case there the data is shuffled. 76 | - **`pocket`** , (Bool type), if `true` the best weights are saved (in the pocket) during learning. 77 | - **`shuffle_data`**, (Bool type), if `true` the data is shuffled at every epoch (in reality we only shuffle indicies for performance). 78 | 79 | 80 | 81 | #### Ascension from above: THe History of the profet who saw the light from the higher dimensions 82 | 83 | The savant circle, ruler of flatland, told the triangle that it was impossible to cross the line river. 84 | It was simply too long, far beyond the end of the realm the river went. One day, the stubborn triangle heard a voice: "the river can be crossed from above". What the hell is above? though the triangle. The triangle tried to explain to the other peasants what was the world from above but nobody listened. 85 | 86 | "This smartass thinks he can invent a word and sell us out on his dream. What on earth is `above` eh? show us" 87 | 88 | The poor triangle asked the others to have faith and investigate other ways to solve the problems they faced but nobody was there. Everybody was too deeply focused on other stuff. 89 | 90 | 91 | The story will continue ... 92 | -------------------------------------------------------------------------------- /MulticlassPerceptronMLJ/REQUIRE: -------------------------------------------------------------------------------- 1 | julia 1.0 2 | 3 | -------------------------------------------------------------------------------- /MulticlassPerceptronMLJ/basic_usage_train.jl: -------------------------------------------------------------------------------- 1 | 2 | using MLDatasets, Statistics 3 | using MulticlassPerceptron 4 | 5 | ## Prepare data 6 | train_x, train_y = MLDatasets.MNIST.traindata(); 7 | test_x, test_y = MLDatasets.MNIST.testdata(); 8 | train_x = Float32.(train_x); 9 | test_x = Float32.(test_x); 10 | train_y = train_y .+ 1; 11 | test_y = test_y .+ 1; 12 | train_x = reshape(train_x, 784, 60000); 13 | test_x = reshape(test_x, 784, 10000); 14 | 15 | ## Define model and train it 16 | scores = [] 17 | n_features = size(train_x, 1); 18 | n_classes = length(unique(train_y)); 19 | perceptron = MulticlassPerceptronClassifier(Float32, n_classes, n_features); 20 | println("\nStart Learning\n") 21 | MulticlassPerceptron.fit!(perceptron, train_x, train_y, scores; print_flag=true, n_epochs=100); 22 | y_hat_test = MulticlassPerceptron.predict(perceptron, test_x); 23 | y_hat_train = MulticlassPerceptron.predict(perceptron,train_x) 24 | println("\nLearning Finished\n") 25 | 26 | println("Results:") 27 | println("Train accuracy:", mean(y_hat_train .==train_y)) 28 | println("Test accuracy:", mean(y_hat_test .==test_y)) 29 | 30 | -------------------------------------------------------------------------------- /MulticlassPerceptronMLJ/src/MulticlassPerceptron.jl: -------------------------------------------------------------------------------- 1 | module MulticlassPerceptron 2 | 3 | #using MetadataTools, DocStringExtensions 4 | using Random: shuffle, MersenneTwister 5 | 6 | export MulticlassPerceptronClassifier, fit!, predict 7 | using LinearAlgebra: mul! 8 | 9 | #mutable struct MulticlassPerceptronClassifier{T} 10 | # W::AbstractMatrix{T} 11 | # b::AbstractVector{T} 12 | # n_classes::Int 13 | # n_features::Int 14 | #end 15 | 16 | mutable struct MulticlassPerceptronClassifier <: MLJBase.Deterministic 17 | weight_average::Bool 18 | end 19 | 20 | MLJBase.update(model::MulticlassPerceptronClassifier, verbosity, old_fitresult, old_cache, X, y) = 21 | MLJBase.fit(MulticlassPerceptronClassifier, verbosity, X, y) 22 | 23 | 24 | # We should be able to pass an argument stating whether we want to save the evolution 25 | # of the learning loss or not. 26 | MLJBase.input_scitype_union(::Type{<:MulticlassPerceptronClassifier}) = Found 27 | 28 | 29 | 30 | 31 | # fit returns coefficients minimizing a penalized rms loss function: 32 | function MLJBase.fit(model::MulticlassPerceptronClassifier, X, y) 33 | x = MLJBase.matrix(X) # convert table to matrix 34 | #fitresult = (x'x - model.lambda*I)\(x'y) # the coefficients 35 | 36 | fit!(model, X, y; model.n_epochs, model.learning_rate, model.print_flag, 37 | model.compute_accuracy, model.seed, model.pocket, 38 | model.shuffle_data) 39 | 40 | return fitresult 41 | end 42 | 43 | MulticlassPerceptronClassifier(; weight_average=false) = MulticlassPerceptronClassifier(weight_average) 44 | 45 | 46 | 47 | function Base.show(io::IO, p::MulticlassPerceptronClassifier{T}) where T<:Number 48 | n_classes = p.n_classes 49 | n_features = p.n_features 50 | 51 | print(io, "MulticlassPerceptronClassifier{$T}(n_classes=$n_classes, n_features=$n_features)") 52 | end 53 | 54 | MulticlassPerceptronClassifier(T::Type, n_classes::Int, n_features::Int) = MulticlassPerceptronClassifier{T}(rand(T, n_features, n_classes), 55 | zeros(T, n_classes), 56 | n_classes, 57 | n_features) 58 | 59 | """ 60 | Compute the accuracy betwwen `y` and `y_hat`. 61 | """ 62 | function accuracy(y::AbstractVector, y_hat::AbstractVector) 63 | acc = 0. 64 | @fastmath for k = 1:length(y) 65 | @inbounds acc += y[k] == y_hat[k] 66 | end 67 | return acc/length(y_hat) 68 | end 69 | 70 | """ 71 | Predicts the class for a given input in a `MulticlassPerceptronClassifier`. 72 | The placeholder is used to avoid allocating memory for each matrix-vector multiplication. 73 | 74 | - Returns the predicted class. 75 | """ 76 | function predict(h::MulticlassPerceptronClassifier, x::AbstractVector, class_placeholder::AbstractVector) 77 | #@fastmath class_placeholder .= At_mul_B!(class_placeholder, h.W, x) .+ h.b 78 | class_placeholder .= mul!(class_placeholder, transpose(h.W), x) .+ h.b 79 | return argmax(class_placeholder) 80 | end 81 | 82 | """ 83 | Function to predict the class for a given example. 84 | 85 | - Returns the predicted class. 86 | """ 87 | function predict(h::MulticlassPerceptronClassifier, x::AbstractVector) 88 | score = h.W' * x .+ h.b 89 | return argmax(score), score 90 | end 91 | 92 | """ 93 | Function to predict the class for a given input batch. 94 | - Returns the predicted class. 95 | """ 96 | function predict(h::MulticlassPerceptronClassifier, X::AbstractMatrix) 97 | predictions = zeros(Int64, size(X, 2)) 98 | class_placeholder = zeros(eltype(h.W), h.n_classes) 99 | 100 | @inbounds for m in 1:length(predictions) 101 | predictions[m] = predict(h, view(X,:,m), class_placeholder) 102 | end 103 | return predictions 104 | end 105 | 106 | """ 107 | > fit!(h::MulticlassPerceptronClassifier, 108 | > X::Array, 109 | > y::Array; 110 | > n_epochs=50, 111 | > learning_rate=0.1, 112 | > print_flag=false, 113 | > compute_accuracy=true, 114 | > seed=Random.seed!(1234), 115 | > pocket=false, 116 | > shuffle_data=false) 117 | 118 | ##### Arguments 119 | 120 | - **`h`**, (MulticlassPerceptronClassifier{T} type), Multiclass perceptron. 121 | - **`X`**, (Array{T,2} type), data contained in the columns of X. 122 | - **`y`**, (Vector{T} type), class labels (as integers from 1 to n_classes). 123 | 124 | ##### Keyword arguments 125 | 126 | - **`n_epochs`**, (Integer type), number of passes (epochs) through the data. 127 | - **`learning_rate`**, (Float type), learning rate (The standard perceptron is with learning_rate=1.) 128 | - **`compute_accuracy`**, (Bool type), if `true` the accuracy is computed at the end of every epoch. 129 | - **`print_flag`**, (Bool type), if `true` the accuracy is printed at the end of every epoch. 130 | - **`seed`**, (MersenneTwister type), seed for the permutation of the datapoints in case there the data is shuffled. 131 | - **`pocket`** , (Bool type), if `true` the best weights are saved (in the pocket) during learning. 132 | - **`shuffle_data`**, (Bool type), if `true` the data is shuffled at every epoch (in reality we only shuffle indicies for performance). 133 | """ 134 | function fit!(h::MulticlassPerceptronClassifier, X::AbstractArray, y::AbstractVector, scores::Array; 135 | n_epochs=50, learning_rate=1., print_flag=false, 136 | compute_accuracy=false, seed=MersenneTwister(1234), pocket=false, 137 | shuffle_data=false) 138 | 139 | n_features, n_samples = size(X) 140 | @assert length(y) == n_samples 141 | 142 | T = eltype(X) 143 | learning_rate = T(learning_rate) 144 | class_placeholder = zeros(T, h.n_classes) 145 | y_preds = zeros(Int64, n_samples) 146 | data_indices = Array(1:n_samples) 147 | max_acc = zero(T) 148 | 149 | if pocket 150 | W_hat = zeros(T, h.n_features, h.n_classes) 151 | b_hat = zeros(T, h.n_classes) 152 | end 153 | 154 | @fastmath for epoch in 1:n_epochs 155 | 156 | n_mistakes = 0 157 | if shuffle_data 158 | shuffle!(seed, data_indices) 159 | end 160 | #println("\nepoch ",epoch,"\n") 161 | @inbounds for m in data_indices 162 | #println("sample seen ", m ,"\n") 163 | x = view(X, :, m); 164 | y_hat = predict(h, x, class_placeholder) 165 | if y[m] != y_hat 166 | n_mistakes += 1 167 | #### wij ← wij − η (yj −tj) · xi 168 | h.W[:, y[m]] .= h.W[:, y[m]] .+ learning_rate .* x 169 | h.b[y[m]] = h.b[y[m]] + learning_rate 170 | h.W[:, y_hat] .= h.W[:, y_hat] .- learning_rate .* x 171 | h.b[y_hat] = h.b[y_hat] - learning_rate 172 | end 173 | end 174 | 175 | #println("FINISHED") 176 | 177 | if compute_accuracy 178 | @inbounds for m in data_indices 179 | y_preds[m] = predict(h, view(X, :, m), class_placeholder) 180 | end 181 | acc = accuracy(y, y_preds) 182 | push!(scores, acc) 183 | else 184 | acc = (n_samples - n_mistakes)/n_samples 185 | push!(scores, acc) 186 | end 187 | 188 | if pocket 189 | if acc > max_acc 190 | max_acc = acc 191 | copy!(W_hat, h.W) 192 | copy!(b_hat, h.b) 193 | end 194 | end 195 | 196 | if print_flag 197 | print("Epoch: $(epoch) \t Accuracy: $(round(acc; digits=3))\r") 198 | #flush(STDOUT) 199 | end 200 | end 201 | end 202 | 203 | 204 | end # module 205 | -------------------------------------------------------------------------------- /MulticlassPerceptronMLJ/src/MulticlassPerceptronOLD.jl: -------------------------------------------------------------------------------- 1 | module MulticlassPerceptron 2 | 3 | #using MetadataTools, DocStringExtensions 4 | using Random: shuffle, MersenneTwister 5 | 6 | export MulticlassPerceptronClassifier, fit!, predict 7 | using LinearAlgebra: mul! 8 | 9 | mutable struct MulticlassPerceptronClassifier{T} 10 | W::AbstractMatrix{T} 11 | b::AbstractVector{T} 12 | n_classes::Int 13 | n_features::Int 14 | end 15 | 16 | function Base.show(io::IO, p::MulticlassPerceptronClassifier{T}) where T<:Number 17 | n_classes = p.n_classes 18 | n_features = p.n_features 19 | 20 | print(io, "MulticlassPerceptronClassifier{$T}(n_classes=$n_classes, n_features=$n_features)") 21 | end 22 | 23 | MulticlassPerceptronClassifier(T::Type, n_classes::Int, n_features::Int) = MulticlassPerceptronClassifier{T}(rand(T, n_features, n_classes), 24 | zeros(T, n_classes), 25 | n_classes, 26 | n_features) 27 | 28 | """ 29 | Compute the accuracy betwwen `y` and `y_hat`. 30 | """ 31 | function accuracy(y::AbstractVector, y_hat::AbstractVector) 32 | acc = 0. 33 | @fastmath for k = 1:length(y) 34 | @inbounds acc += y[k] == y_hat[k] 35 | end 36 | return acc/length(y_hat) 37 | end 38 | 39 | """ 40 | Predicts the class for a given input in a `MulticlassPerceptronClassifier`. 41 | The placeholder is used to avoid allocating memory for each matrix-vector multiplication. 42 | 43 | - Returns the predicted class. 44 | """ 45 | function predict(h::MulticlassPerceptronClassifier, x::AbstractVector, class_placeholder::AbstractVector) 46 | #@fastmath class_placeholder .= At_mul_B!(class_placeholder, h.W, x) .+ h.b 47 | class_placeholder .= mul!(class_placeholder, transpose(h.W), x) .+ h.b 48 | return argmax(class_placeholder) 49 | end 50 | 51 | """ 52 | Function to predict the class for a given example. 53 | 54 | - Returns the predicted class. 55 | """ 56 | function predict(h::MulticlassPerceptronClassifier, x::AbstractVector) 57 | score = h.W' * x .+ h.b 58 | return argmax(score), score 59 | end 60 | 61 | """ 62 | Function to predict the class for a given input batch. 63 | - Returns the predicted class. 64 | """ 65 | function predict(h::MulticlassPerceptronClassifier, X::AbstractMatrix) 66 | predictions = zeros(Int64, size(X, 2)) 67 | class_placeholder = zeros(eltype(h.W), h.n_classes) 68 | 69 | @inbounds for m in 1:length(predictions) 70 | predictions[m] = predict(h, view(X,:,m), class_placeholder) 71 | end 72 | return predictions 73 | end 74 | 75 | """ 76 | > fit!(h::MulticlassPerceptronClassifier, 77 | > X::Array, 78 | > y::Array; 79 | > n_epochs=50, 80 | > learning_rate=0.1, 81 | > print_flag=false, 82 | > compute_accuracy=true, 83 | > seed=Random.seed!(1234), 84 | > pocket=false, 85 | > shuffle_data=false) 86 | 87 | ##### Arguments 88 | 89 | - **`h`**, (MulticlassPerceptronClassifier{T} type), Multiclass perceptron. 90 | - **`X`**, (Array{T,2} type), data contained in the columns of X. 91 | - **`y`**, (Vector{T} type), class labels (as integers from 1 to n_classes). 92 | 93 | ##### Keyword arguments 94 | 95 | - **`n_epochs`**, (Integer type), number of passes (epochs) through the data. 96 | - **`learning_rate`**, (Float type), learning rate (The standard perceptron is with learning_rate=1.) 97 | - **`compute_accuracy`**, (Bool type), if `true` the accuracy is computed at the end of every epoch. 98 | - **`print_flag`**, (Bool type), if `true` the accuracy is printed at the end of every epoch. 99 | - **`seed`**, (MersenneTwister type), seed for the permutation of the datapoints in case there the data is shuffled. 100 | - **`pocket`** , (Bool type), if `true` the best weights are saved (in the pocket) during learning. 101 | - **`shuffle_data`**, (Bool type), if `true` the data is shuffled at every epoch (in reality we only shuffle indicies for performance). 102 | """ 103 | function fit!(h::MulticlassPerceptronClassifier, X::AbstractArray, y::AbstractVector, scores::Array; 104 | n_epochs=50, learning_rate=1., print_flag=false, 105 | compute_accuracy=false, seed=MersenneTwister(1234), pocket=false, 106 | shuffle_data=false) 107 | println("HELOO TEST!!!") 108 | n_features, n_samples = size(X) 109 | @assert length(y) == n_samples 110 | 111 | T = eltype(X) 112 | learning_rate = T(learning_rate) 113 | class_placeholder = zeros(T, h.n_classes) 114 | y_preds = zeros(Int64, n_samples) 115 | data_indices = Array(1:n_samples) 116 | max_acc = zero(T) 117 | 118 | if pocket 119 | W_hat = zeros(T, h.n_features, h.n_classes) 120 | b_hat = zeros(T, h.n_classes) 121 | end 122 | 123 | @fastmath for epoch in 1:n_epochs 124 | 125 | n_mistakes = 0 126 | if shuffle_data 127 | shuffle!(seed, data_indices) 128 | end 129 | #println("\nepoch ",epoch,"\n") 130 | @inbounds for m in data_indices 131 | #println("sample seen ", m ,"\n") 132 | x = view(X, :, m); 133 | y_hat = predict(h, x, class_placeholder) 134 | if y[m] != y_hat 135 | n_mistakes += 1 136 | #### wij ← wij − η (yj −tj) · xi 137 | h.W[:, y[m]] .= h.W[:, y[m]] .+ learning_rate .* x 138 | h.b[y[m]] = h.b[y[m]] + learning_rate 139 | h.W[:, y_hat] .= h.W[:, y_hat] .- learning_rate .* x 140 | h.b[y_hat] = h.b[y_hat] - learning_rate 141 | end 142 | end 143 | 144 | #println("FINISHED") 145 | 146 | if compute_accuracy 147 | @inbounds for m in data_indices 148 | y_preds[m] = predict(h, view(X, :, m), class_placeholder) 149 | end 150 | acc = accuracy(y, y_preds) 151 | push!(scores, acc) 152 | else 153 | acc = (n_samples - n_mistakes)/n_samples 154 | push!(scores, acc) 155 | end 156 | 157 | if pocket 158 | if acc > max_acc 159 | max_acc = acc 160 | copy!(W_hat, h.W) 161 | copy!(b_hat, h.b) 162 | end 163 | end 164 | 165 | if print_flag 166 | print("Epoch: $(epoch) \t Accuracy: $(round(acc; digits=3))\r") 167 | #flush(STDOUT) 168 | end 169 | end 170 | end 171 | 172 | 173 | end # module 174 | -------------------------------------------------------------------------------- /RBM/notebooks/CRBM.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# CRBM Implementation \n", 10 | "\n", 11 | "Original model from \n", 12 | "\n", 13 | "- http://www.cs.nyu.edu/~gwtaylor/thesis/Taylor_Graham_W_200911_PhD_thesis.pdf\n", 14 | "\n", 15 | "There are a couple more available\n", 16 | "\n", 17 | "- https://github.com/dfdx/Boltzmann.jl/blob/master/src/conditional.jl\n", 18 | "\n", 19 | "- https://github.com/josepllberral/machine-learning-tools/blob/master/crbm.R" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "metadata": { 26 | "collapsed": false 27 | }, 28 | "outputs": [ 29 | { 30 | "name": "stderr", 31 | "output_type": "stream", 32 | "text": [ 33 | "WARNING: using BenchmarkTools.params in module Main conflicts with an existing identifier.\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "using Distributions\n", 39 | "using BenchmarkTools" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 3, 45 | "metadata": { 46 | "collapsed": false 47 | }, 48 | "outputs": [ 49 | { 50 | "data": { 51 | "text/plain": [ 52 | "sigmoid (generic function with 1 method)" 53 | ] 54 | }, 55 | "execution_count": 3, 56 | "metadata": {}, 57 | "output_type": "execute_result" 58 | } 59 | ], 60 | "source": [ 61 | "function sigmoid(M::Array)\n", 62 | " return 1./(1 .+ exp.(-M))\n", 63 | "end" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 11, 69 | "metadata": { 70 | "collapsed": false 71 | }, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "sample (generic function with 7 methods)" 77 | ] 78 | }, 79 | "execution_count": 11, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "Distributions.sample" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 13, 91 | "metadata": { 92 | "collapsed": false 93 | }, 94 | "outputs": [ 95 | { 96 | "data": { 97 | "text/plain": [ 98 | "Distributions.Normal{Float64}(μ=0.0, σ=1.0)" 99 | ] 100 | }, 101 | "execution_count": 13, 102 | "metadata": {}, 103 | "output_type": "execute_result" 104 | } 105 | ], 106 | "source": [ 107 | "Normal()" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": { 114 | "collapsed": true 115 | }, 116 | "outputs": [], 117 | "source": [] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": { 123 | "collapsed": true 124 | }, 125 | "outputs": [], 126 | "source": [] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": null, 131 | "metadata": { 132 | "collapsed": true 133 | }, 134 | "outputs": [], 135 | "source": [] 136 | } 137 | ], 138 | "metadata": { 139 | "kernelspec": { 140 | "display_name": "Julia 0.6.0-dev", 141 | "language": "julia", 142 | "name": "julia-0.6" 143 | }, 144 | "language_info": { 145 | "file_extension": ".jl", 146 | "mimetype": "application/julia", 147 | "name": "julia", 148 | "version": "0.6.0" 149 | } 150 | }, 151 | "nbformat": 4, 152 | "nbformat_minor": 0 153 | } 154 | -------------------------------------------------------------------------------- /RBM/notebooks/RBM_dfdx.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 62, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "using Boltzmann\n", 12 | "using MNIST\n", 13 | "using ImageView\n", 14 | "using Distributions\n", 15 | "\n", 16 | "## https://makandracards.com/mark/6817-bm-rbm-for-beginners\n", 17 | "## https://www.slideshare.net/sogo1127/learning-rbmrestricted-boltzmann-machine-in-practice" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 63, 23 | "metadata": { 24 | "collapsed": false 25 | }, 26 | "outputs": [ 27 | { 28 | "name": "stderr", 29 | "output_type": "stream", 30 | "text": [ 31 | "WARNING: Method definition plot_weights(Any, Any) in module Main at In[4]:3 overwritten at In[63]:3.\n", 32 | "WARNING: Method definition plot_weights(Any, Any, Any) in module Main at In[4]:3 overwritten at In[63]:3.\n" 33 | ] 34 | }, 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "plot_weights (generic function with 2 methods)" 39 | ] 40 | }, 41 | "execution_count": 63, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "\n", 48 | "function plot_weights(W, imsize, padding=10)\n", 49 | " h, w = imsize\n", 50 | " n = size(W, 1)\n", 51 | " rows = Int(floor(sqrt(n)))\n", 52 | " cols = Int(ceil(n / rows))\n", 53 | " halfpad = div(padding, 2)\n", 54 | " dat = zeros(rows * (h + padding), cols * (w + padding))\n", 55 | " for i=1:n\n", 56 | " wt = W[i, :]\n", 57 | " wim = reshape(wt, imsize)\n", 58 | " wim = wim ./ (maximum(wim) - minimum(wim))\n", 59 | " r = div(i - 1, cols) + 1\n", 60 | " c = rem(i - 1, cols) + 1\n", 61 | " dat[(r-1)*(h+padding)+halfpad+1 : r*(h+padding)-halfpad,\n", 62 | " (c-1)*(w+padding)+halfpad+1 : c*(w+padding)-halfpad] = wim\n", 63 | " end\n", 64 | " #ImageView.view(dat)\n", 65 | " return dat\n", 66 | "end\n", 67 | "\n", 68 | "\n" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 64, 74 | "metadata": { 75 | "collapsed": false 76 | }, 77 | "outputs": [], 78 | "source": [ 79 | "X, y = testdata() # test data is smaller, no need to downsample\n", 80 | "X = X ./ (maximum(X) - minimum(X));\n" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 65, 86 | "metadata": { 87 | "collapsed": false 88 | }, 89 | "outputs": [ 90 | { 91 | "data": { 92 | "text/markdown": [ 93 | "Construct RBM. Parameters:\n", 94 | "\n", 95 | " * T - type of RBM parameters (e.g. weights and biases; by default, Float64)\n", 96 | " * V - type of visible units\n", 97 | " * H - type of hidden units\n", 98 | " * n_vis - number of visible units\n", 99 | " * n_hid - number of hidden units\n", 100 | "\n", 101 | "Optional parameters:\n", 102 | "\n", 103 | " * sigma - variance to use during parameter initialization\n" 104 | ], 105 | "text/plain": [ 106 | "Construct RBM. Parameters:\n", 107 | "\n", 108 | " * T - type of RBM parameters (e.g. weights and biases; by default, Float64)\n", 109 | " * V - type of visible units\n", 110 | " * H - type of hidden units\n", 111 | " * n_vis - number of visible units\n", 112 | " * n_hid - number of hidden units\n", 113 | "\n", 114 | "Optional parameters:\n", 115 | "\n", 116 | " * sigma - variance to use during parameter initialization\n" 117 | ] 118 | }, 119 | "execution_count": 65, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "?Boltzmann.RBM" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 66, 131 | "metadata": { 132 | "collapsed": false 133 | }, 134 | "outputs": [], 135 | "source": [ 136 | "#RBM(T=Float32,V=Float32,H=Float32,n_vis=28*28, n_hid=300, 0.1)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 67, 142 | "metadata": { 143 | "collapsed": false 144 | }, 145 | "outputs": [ 146 | { 147 | "data": { 148 | "text/plain": [ 149 | "DataType" 150 | ] 151 | }, 152 | "execution_count": 67, 153 | "metadata": {}, 154 | "output_type": "execute_result" 155 | } 156 | ], 157 | "source": [ 158 | "typeof(RBM)" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 75, 164 | "metadata": { 165 | "collapsed": false 166 | }, 167 | "outputs": [ 168 | { 169 | "ename": "LoadError", 170 | "evalue": "MethodError: no method matching Boltzmann.RBM{T,V,H}(::Array{Float32,2}, ::Array{Float32,1}, ::Array{Float32,1})\u001b[0m\nClosest candidates are:\n Boltzmann.RBM{T,V,H}{T}(::Any) at sysimg.jl:66\u001b[0m", 171 | "output_type": "error", 172 | "traceback": [ 173 | "MethodError: no method matching Boltzmann.RBM{T,V,H}(::Array{Float32,2}, ::Array{Float32,1}, ::Array{Float32,1})\u001b[0m\nClosest candidates are:\n Boltzmann.RBM{T,V,H}{T}(::Any) at sysimg.jl:66\u001b[0m", 174 | "" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "T = Float32\n", 180 | "sigma=0.01\n", 181 | "n_vis=28*28\n", 182 | "n_hid = 100\n", 183 | "W=map(T, rand(Normal(0, sigma), n_hid, n_vis));\n", 184 | "\n", 185 | "rbm = RBM(map(T, rand(Normal(0, sigma), n_hid, n_vis)),\n", 186 | " map(T,zeros(784)), \n", 187 | " map(T,zeros(100)))" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 76, 193 | "metadata": { 194 | "collapsed": false 195 | }, 196 | "outputs": [], 197 | "source": [ 198 | "W=map(T, rand(Normal(0, sigma), n_hid, n_vis));" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 77, 204 | "metadata": { 205 | "collapsed": false 206 | }, 207 | "outputs": [], 208 | "source": [ 209 | "#fit(m, X, n_epochs=20, randomize=true)\n", 210 | "#plot_weights(m.W[1:64, :], (28, 28))" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 78, 216 | "metadata": { 217 | "collapsed": false 218 | }, 219 | "outputs": [ 220 | { 221 | "ename": "LoadError", 222 | "evalue": "error in method definition: function Boltzmann.BernoulliRBM must be explicitly imported to be extended", 223 | "output_type": "error", 224 | "traceback": [ 225 | "error in method definition: function Boltzmann.BernoulliRBM must be explicitly imported to be extended", 226 | "" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "BernoulliRBM(n_vis::Int, n_hid::Int; sigma=0.01) =\n", 232 | " RBM(Float64, Degenerate, Bernoulli, n_vis, n_hid; sigma=sigma)\n" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "metadata": { 239 | "collapsed": true 240 | }, 241 | "outputs": [], 242 | "source": [] 243 | } 244 | ], 245 | "metadata": { 246 | "kernelspec": { 247 | "display_name": "Julia 0.5.0", 248 | "language": "julia", 249 | "name": "julia-0.5" 250 | }, 251 | "language_info": { 252 | "file_extension": ".jl", 253 | "mimetype": "application/julia", 254 | "name": "julia", 255 | "version": "0.6.0" 256 | } 257 | }, 258 | "nbformat": 4, 259 | "nbformat_minor": 0 260 | } 261 | -------------------------------------------------------------------------------- /RBM/notebooks/rbm.jil: -------------------------------------------------------------------------------- 1 | 2 | 3 | type RBM{T,V,H}: 4 | W::Matrix{T} 5 | vis_bias::Vector{T} 6 | hid_bias::Vector{T} 7 | end 8 | 9 | 10 | function RBM(T::Type, V::Type, H::Type, 11 | n_vis::Int, n_hid::Int; sigma=0.01) 12 | RBM{T,V,H}(map(T, rand(Normal(0, sigma), (n_hid, n_vis))), 13 | zeros(n_vis), zeros(n_hid)) 14 | end -------------------------------------------------------------------------------- /RBM/src/RBM_models.jl: -------------------------------------------------------------------------------- 1 | 2 | module RBM_Models 3 | 4 | using Distributions 5 | import Base.show 6 | 7 | #### methods to export when this module is loaded 8 | export RBM, initialize_RBM, CDK, initialize_CDK, fit!, partial_fit! 9 | 10 | function sigmoid{T}(x::T) 11 | return 1/(1 + exp(-x)) 12 | end 13 | 14 | type RBM{T <: Real} 15 | n_vis::Int 16 | n_hid::Int 17 | W::Matrix{T} 18 | vis_bias::Vector{T} 19 | hid_bias::Vector{T} 20 | trained::Bool 21 | n_epochs_trained::Int 22 | end 23 | 24 | function initialize_RBM(n_vis, n_hid, sigma, T) 25 | 26 | return RBM{T}(n_vis, # num visible units 27 | n_hid, # num hidden unnits 28 | rand(Normal(0,sigma), n_hid, n_vis), # weight matrix 29 | zeros(n_vis), # visible vector 30 | zeros(n_hid), # Hidden vector 31 | false,0) # trained 32 | end 33 | 34 | function Base.show{T}(io::IO, rbm::RBM{T}) 35 | n_vis = size(rbm.vis_bias, 1) 36 | n_hid = size(rbm.hid_bias, 1) 37 | trained = rbm.trained 38 | print(io, "RBM{$T}(n_vis=$n_vis, n_hid=$n_hid, trained=$trained)") 39 | end 40 | 41 | 42 | type CDK{T} 43 | K::Int 44 | batch_size::Int 45 | 46 | # Placeholders needed for the gradients of the parameters of the RBM 47 | grad_W::Matrix{T} 48 | grad_vis_bias::Vector{T} 49 | grad_hid_bias::Vector{T} 50 | 51 | # Placeholders needed for performing CDK in a minibatch 52 | H::Matrix{T} 53 | V_hat::Matrix{T} 54 | H_hat::Matrix{T} 55 | rec_error::Float64 # This is probably irrelevant, allo 56 | 57 | # Placeholders needed for performing sampling in a minibatch 58 | V_sampling::Matrix{T} 59 | H_sampling::Matrix{T} 60 | H_aux::Matrix{T} 61 | V_aux::Matrix{T} 62 | 63 | end 64 | 65 | function initialize_CDK(rbm::RBM, K, batch_size) 66 | """ 67 | This function initializes a CDK type that will be used as placeholder for the 68 | memory needed for the gibbs sampling process needed at every minibatch update. 69 | """ 70 | T = eltype(rbm.vis_bias) 71 | grad_W = zeros(T, size(rbm.W)) 72 | grad_vis_bias = zeros(T, size(rbm.vis_bias)) 73 | grad_hid_bias = zeros(T, size(rbm.hid_bias)) 74 | V_hat = zeros(T, rbm.n_vis, batch_size) 75 | H_hat = zeros(T, rbm.n_hid, batch_size) 76 | H = zeros(T, rbm.n_hid, batch_size) 77 | V_sampling = zeros(T, rbm.n_vis, batch_size) 78 | H_sampling = zeros(T, rbm.n_hid, batch_size) 79 | H_aux = zeros(T, rbm.n_hid, batch_size) 80 | V_aux = zeros(T, rbm.n_vis, batch_size) 81 | 82 | cdk = CDK(K, batch_size, 83 | grad_W, grad_vis_bias,grad_hid_bias, 84 | H, V_hat, H_hat, 0., 85 | V_sampling, H_sampling, H_aux, V_aux) 86 | return cdk 87 | end 88 | 89 | function update_params!(rbm::RBM, opt::CDK, lr) 90 | rbm.W .+= lr .* opt.grad_W 91 | rbm.vis_bias .+= lr .* opt.grad_vis_bias 92 | rbm.hid_bias .+= lr .* opt.grad_hid_bias 93 | end 94 | 95 | function fit!(rbm::RBM, 96 | X::Matrix, 97 | batch_size::Integer, 98 | n_epochs::Integer, 99 | lr::Real, 100 | shuffle_data::Bool, 101 | opt) 102 | 103 | T = eltype(X) 104 | lr = T(lr) 105 | n_samples = size(X)[2] 106 | indicies = [x:min(x + batch_size-1, n_samples) for x in 1:batch_size:n_samples] 107 | sample_perm = Vector(1:n_samples) 108 | n_minibatches = T(length(indicies)) 109 | rec_errors = Vector{T}([]) 110 | 111 | for epoch in 1:n_epochs 112 | rec_error = Float32(0.) 113 | 114 | # should it be more efficient to Shuffle indicies not the whole data? 115 | # then access is not contiguous though 116 | if shuffle_data==true 117 | shuffle!(sample_perm) 118 | X .= X[:,sample_perm] 119 | end 120 | 121 | for minibatch_ind in indicies 122 | partial_fit!(rbm, X[:, minibatch_ind], lr, opt) 123 | rec_error += opt.rec_error 124 | end 125 | 126 | push!(rec_errors, rec_error/n_minibatches) 127 | rbm.n_epochs_trained +=1 128 | print(rec_errors[end], "\n") 129 | end 130 | rbm.trained = true 131 | return rec_errors 132 | end 133 | 134 | function partial_fit!(rbm::RBM, X, lr::Real, opt::CDK) 135 | compute_grad!(rbm, X, opt) 136 | update_params!(rbm, opt, lr) 137 | end 138 | 139 | function MSE(X, V_hat) 140 | aux = 0. 141 | for (x,y) in zip(X , V_hat) 142 | aux += (x - y)^2 143 | end 144 | return sqrt(aux) 145 | end 146 | 147 | function grad_bias!(grad_bias, X, Vhat::Matrix, batch_size) 148 | suma = fill!(grad_bias, 0.) 149 | aux = 0 150 | @inbounds for i in 1:size(X,1) 151 | @simd for j in 1:size(X,2) 152 | grad_bias[i] += X[i,j] - Vhat[i,j] 153 | end 154 | end 155 | grad_bias .= grad_bias./batch_size 156 | end 157 | 158 | function compute_grad!(rbm::RBM, X, opt::CDK) 159 | 160 | T = eltype(rbm.vis_bias) 161 | batch_size = size(X)[2] 162 | 163 | # Perform gibbs sampling to compute the negative phase 164 | for k in 1:opt.K 165 | rand!(opt.V_sampling) 166 | if k==1 167 | opt.H .= sigmoid.(A_mul_B!(opt.H_aux, rbm.W, X) .+ rbm.hid_bias) 168 | opt.V_hat .= sigmoid.(At_mul_B!(opt.V_hat, rbm.W, opt.H) .+ rbm.vis_bias) .> opt.V_sampling 169 | opt.H_hat .= sigmoid.(A_mul_B!(opt.H_aux, rbm.W, opt.V_hat) .+ rbm.hid_bias) 170 | else 171 | opt.V_hat .= sigmoid.(At_mul_B!(opt.V_hat, rbm.W, opt.H_hat) .+ rbm.vis_bias) .> opt.V_sampling 172 | opt.H_hat .= sigmoid.(A_mul_B!(opt.H_aux, rbm.W, opt.V_hat) .+ rbm.hid_bias) 173 | end 174 | end 175 | opt.grad_W .= (A_mul_Bt!(opt.grad_W, opt.H, X) .- A_mul_Bt!(opt.grad_W, opt.H_hat, opt.V_hat))./ batch_size; 176 | #opt.grad_vis_bias .= sum!(opt.grad_vis_bias, X .- opt.V_hat)./ batch_size; 177 | #opt.grad_vis_bias .= sum!(opt.grad_vis_bias, X .- opt.V_hat)./ batch_size; 178 | 179 | # Correct but generate allocations How can we rewrite it differently 180 | opt.grad_vis_bias .= squeeze(sum((X .- opt.V_hat), 2),2)./ batch_size; 181 | opt.grad_hid_bias .= squeeze(sum((opt.H .- opt.H_hat), 2),2)./ batch_size; 182 | 183 | #grad_bias!(opt.grad_vis_bias, X, opt.V_hat, batch_size); 184 | #grad_bias!(opt.grad_hid_bias, opt.H, opt.H_hat, batch_size); 185 | 186 | #opt.grad_hid_bias .= squeeze(sum((opt.H .- opt.H_hat), 2),2)./ batch_size; 187 | #opt.rec_error = MSE(X, (@view opt.V_hat)) 188 | #opt.rec_error = MSE(X, opt.V_hat) 189 | aux = 0. 190 | for (x,y) in zip(X , opt.V_hat) 191 | aux += (x - y)^2 192 | end 193 | opt.rec_error = sqrt(aux) 194 | 195 | end 196 | 197 | #function compute_grad_with_dot2!(rbm::RBM, X::Matrix, opt::CDK) 198 | # 199 | # T = eltype(rbm.vis_bias) 200 | # batch_size = size(X)[2] 201 | # 202 | # # Perform gibbs sampling to compute the negative phase 203 | # for k in 1:opt.K 204 | # rand!(opt.V_sampling) 205 | # if k==1 206 | # opt.H .= sigmoid.(A_mul_B!(opt.H_aux, rbm.W, X) .+ rbm.hid_bias) 207 | # opt.V_hat .= sigmoid.(rbm.W'* opt.H .+ rbm.vis_bias) .> opt.V_sampling 208 | # opt.H_hat .= sigmoid.(A_mul_B!(opt.H_aux, rbm.W, opt.V_hat) .+ rbm.hid_bias) 209 | # else 210 | # opt.V_hat .= sigmoid.(rbm.W'* opt.H_hat .+ rbm.vis_bias) .> opt.V_sampling 211 | # opt.H_hat .= sigmoid.(A_mul_B!(opt.H_aux, rbm.W, opt.V_hat) .+ rbm.hid_bias) 212 | # end 213 | # end 214 | # opt.grad_W .= (opt.H * X' .- opt.H_hat * opt.V_hat')./ batch_size; 215 | # opt.grad_vis_bias .= squeeze(sum((X .- opt.V_hat), 2),2)./ batch_size; 216 | # opt.grad_hid_bias .= squeeze(sum((opt.H .- opt.H_hat), 2),2)./ batch_size; 217 | # opt.rec_error = sqrt(sum((X .- opt.V_hat).^2)) 218 | #end 219 | 220 | end 221 | -------------------------------------------------------------------------------- /RBM/src/RBM_models_blas.jl: -------------------------------------------------------------------------------- 1 | 2 | # https://github.com/JuliaMath/Yeppp.jl 3 | 4 | module RBM_Models 5 | 6 | using Distributions 7 | using BLAS 8 | using Yeppp 9 | 10 | import Base.show 11 | export RBM, initialize_RBM, CDK, initialize_CDK, fit!, partial_fit! 12 | 13 | function sigmoid{T}(x::T) 14 | return 1/(1 + Yeppp.exp(-x)) 15 | end 16 | 17 | 18 | type RBM{T <: Real} 19 | n_vis::Int 20 | n_hid::Int 21 | W::Matrix{T} 22 | vis_bias::Vector{T} 23 | hid_bias::Vector{T} 24 | trained::Bool 25 | n_epochs_trained::Int 26 | end 27 | 28 | 29 | function initialize_RBM(n_vis, n_hid, sigma, T) 30 | 31 | return RBM{T}(n_vis, # num visible units 32 | n_hid, # num hidden unnits 33 | rand(Normal(0,sigma), n_hid, n_vis), # weight matrix 34 | zeros(n_vis), # visible vector 35 | zeros(n_hid), # Hidden vector 36 | false,0) # trained 37 | end 38 | 39 | 40 | function Base.show{T}(io::IO, rbm::RBM{T}) 41 | n_vis = size(rbm.vis_bias, 1) 42 | n_hid = size(rbm.hid_bias, 1) 43 | trained = rbm.trained 44 | print(io, "RBM{$T}(n_vis=$n_vis, n_hid=$n_hid, trained=$trained)") 45 | end 46 | 47 | 48 | type CDK{T} 49 | K::Int 50 | batch_size::Int 51 | 52 | # Placeholders needed for the gradients of the parameters of the RBM 53 | grad_W::Matrix{T} 54 | grad_vis_bias::Vector{T} 55 | grad_hid_bias::Vector{T} 56 | 57 | # Placeholders needed for performing CDK in a minibatch 58 | H::Matrix{T} 59 | V_hat::Matrix{T} 60 | H_hat::Matrix{T} 61 | rec_error::T 62 | 63 | # Placeholders needed for performing sampling in a minibatch 64 | V_sampling::Matrix{T} 65 | H_sampling::Matrix{T} 66 | H_aux::Matrix{T} 67 | V_aux::Matrix{T} 68 | end 69 | 70 | function initialize_CDK(rbm::RBM, K, batch_size) 71 | """ 72 | This function initializes a CDK type that will be used as placeholder for the 73 | memory needed for the gibbs sampling process needed at every minibatch update. 74 | """ 75 | T = eltype(rbm.vis_bias) 76 | grad_W = zeros(T, size(rbm.W)) 77 | grad_vis_bias = zeros(T, size(rbm.vis_bias)) 78 | grad_hid_bias = zeros(T, size(rbm.hid_bias)) 79 | V_hat = zeros(T, rbm.n_vis, batch_size) 80 | H_hat = zeros(T, rbm.n_hid, batch_size) 81 | H = zeros(T, rbm.n_hid, batch_size) 82 | V_sampling = zeros(T, rbm.n_vis, batch_size) 83 | H_sampling = zeros(T, rbm.n_hid, batch_size) 84 | H_aux = zeros(T, rbm.n_hid, batch_size) 85 | V_aux = zeros(T, rbm.n_vis, batch_size) 86 | 87 | cdk = CDK(K, batch_size, 88 | grad_W, grad_vis_bias,grad_hid_bias, 89 | H, V_hat, H_hat, 0., 90 | V_sampling, H_sampling, H_aux, V_aux) 91 | return cdk 92 | end 93 | 94 | function update_params!(rbm::RBM, opt::CDK, lr) 95 | rbm.W .+= lr .* opt.grad_W 96 | rbm.vis_bias .+= lr .* opt.grad_vis_bias 97 | rbm.hid_bias .+= lr .* opt.grad_hid_bias 98 | end 99 | 100 | function fit!(rbm::RBM, 101 | X::Matrix, 102 | batch_size::Integer, 103 | n_epochs::Integer, 104 | lr::Real, 105 | shuffle_data::Bool, 106 | opt) 107 | 108 | T = eltype(X) 109 | lr = T(lr) 110 | n_samples = size(X)[2] 111 | indicies = [x:min(x + batch_size-1, n_samples) for x in 1:batch_size:n_samples] 112 | sample_perm = Vector(1:n_samples) 113 | n_minibatches = T(length(indicies)) 114 | rec_errors = Vector{T}([]) 115 | 116 | for epoch in 1:n_epochs 117 | rec_error = zero(T) 118 | 119 | # should it be more efficient to Shuffle indicies not the whole data? 120 | # then access is not contiguous though 121 | if shuffle_data==true 122 | shuffle!(sample_perm) 123 | X .= X[:,sample_perm] 124 | end 125 | 126 | for minibatch_ind in indicies 127 | partial_fit!(rbm, X[:, minibatch_ind], lr, opt) 128 | rec_error += opt.rec_error 129 | end 130 | 131 | push!(rec_errors, rec_error/n_minibatches) 132 | rbm.n_epochs_trained +=1 133 | print(rec_errors[end], "\n") 134 | end 135 | rbm.trained = true 136 | return rec_errors 137 | end 138 | 139 | 140 | function partial_fit!(rbm::RBM, X::Matrix, lr::Real, opt::CDK) 141 | compute_grad!(rbm, X, opt) 142 | update_params!(rbm, opt, lr) 143 | end 144 | 145 | 146 | function mean_squared_error(X, V_hat) 147 | aux = zero(eltype(X)) 148 | @simd for (x,y) in zip(X , V_hat) 149 | aux += (x - y)^2 150 | end 151 | return sqrt(aux) 152 | end 153 | 154 | 155 | function A_mul_B_plus_C!(C,A,B) 156 | BLAS.gemm!('N', 'N', 1.0, A, B, 0.0, C); 157 | end 158 | 159 | 160 | function compute_grad!(rbm::RBM, X::Matrix, opt::CDK) 161 | 162 | T = eltype(rbm.vis_bias) 163 | batch_size = size(X)[2] 164 | 165 | # Perform gibbs sampling to compute the negative phase 166 | for k in 1:opt.K 167 | rand!(opt.V_sampling) 168 | if k==1 169 | opt.H .= sigmoid.(A_mul_B!(opt.H_aux, rbm.W, X) .+ rbm.hid_bias) 170 | opt.V_hat .= sigmoid.(At_mul_B!(opt.V_hat, rbm.W, opt.H) .+ rbm.vis_bias) .> opt.V_sampling 171 | opt.H_hat .= sigmoid.(A_mul_B!(opt.H_aux, rbm.W, opt.V_hat) .+ rbm.hid_bias) 172 | else 173 | opt.V_hat .= sigmoid.(At_mul_B!(opt.V_hat, rbm.W, opt.H_hat) .+ rbm.vis_bias) .> opt.V_sampling 174 | opt.H_hat .= sigmoid.(A_mul_B!(opt.H_aux, rbm.W, opt.V_hat) .+ rbm.hid_bias) 175 | end 176 | end 177 | 178 | opt.grad_W .= (A_mul_Bt!(opt.grad_W, opt.H, X) .- A_mul_Bt!(opt.grad_W, opt.H_hat, opt.V_hat))./ batch_size; 179 | opt.grad_vis_bias .= sum!(opt.grad_vis_bias, X .- opt.V_hat)./ batch_size; 180 | opt.grad_hid_bias .= squeeze(sum((opt.H .- opt.H_hat), 2),2)./ batch_size; 181 | 182 | opt.rec_error = mean_squared_error(X, opt.V_hat) 183 | end 184 | 185 | 186 | end 187 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # learn_julia 2 | Tutorials for the julia language 3 | -------------------------------------------------------------------------------- /code_optimization/basic_strategies.md: -------------------------------------------------------------------------------- 1 | 2 | # Basic code optimization techniques 3 | 4 | 5 | 6 | ## SIMD instrinsics 7 | 8 | http://kristofferc.github.io/post/intrinsics/ 9 | 10 | 11 | 12 | ## Loop unrolling 13 | 14 | ``` 15 | void vsum1(int n) { 16 | int i; 17 | for(i=0; i @show(loss(X, Y))\n", 83 | "opt = SGD(params(m))\n", 84 | "\n", 85 | "Flux.train!(loss, (X,Y) , opt, cb = throttle(evalcb, 1))\n", 86 | "accuracy(X, Y)\n", 87 | "\n", 88 | "# Test set accuracy\n", 89 | "tX = hcat(float.(reshape.(MNIST.images(:test), :))...)\n", 90 | "tY = onehotbatch(MNIST.labels(:test), 0:9)\n", 91 | "\n", 92 | "# If CuArrays\n", 93 | "# tX, tY = cu(tX), cu(tY)\n", 94 | "accuracy(tX, tY)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [ 103 | "Flux.train!(loss, dataset, opt, cb = throttle(evalcb, 10))\n", 104 | "accuracy(X, Y)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "teX = hcat(float.(reshape.(MNIST.images(:test), :))...)\n", 114 | "teY = onehotbatch(MNIST.labels(:test), 0:9)\n", 115 | "accuracy(teX, teY)" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "trX = hcat(float.(reshape.(MNIST.images(:train), :))...)\n", 125 | "trY = onehotbatch(MNIST.labels(:train), 0:9)\n", 126 | "accuracy(trX, trY)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "#### make iterations" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [ 142 | "# Flux.train!(loss, dataset, opt, cb = throttle(evalcb, 10))" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": null, 148 | "metadata": { 149 | "collapsed": true 150 | }, 151 | "outputs": [], 152 | "source": [] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "metadata": { 158 | "collapsed": true 159 | }, 160 | "outputs": [], 161 | "source": [] 162 | } 163 | ], 164 | "metadata": { 165 | "kernelspec": { 166 | "display_name": "Julia 0.6.0", 167 | "language": "julia", 168 | "name": "julia-0.6" 169 | }, 170 | "language_info": { 171 | "file_extension": ".jl", 172 | "mimetype": "application/julia", 173 | "name": "julia", 174 | "version": "0.6.0" 175 | } 176 | }, 177 | "nbformat": 4, 178 | "nbformat_minor": 1 179 | } 180 | -------------------------------------------------------------------------------- /install_common_packages.jl: -------------------------------------------------------------------------------- 1 | Pkg.update() 2 | Pkg.add("MNIST") 3 | Pkg.add("SIMD") 4 | Pkg.add("DataFrames") 5 | Pkg.add("BenchmarkTools") 6 | Pkg.add("IJulia") 7 | -------------------------------------------------------------------------------- /introduction_to_julia/10_plotting_pyplot.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Plotting julia " 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "cusing ImageMetadata" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": { 25 | "collapsed": true 26 | }, 27 | "outputs": [], 28 | "source": [] 29 | } 30 | ], 31 | "metadata": { 32 | "kernelspec": { 33 | "display_name": "Julia 0.6.0", 34 | "language": "julia", 35 | "name": "julia-0.6" 36 | }, 37 | "language_info": { 38 | "file_extension": ".jl", 39 | "mimetype": "application/julia", 40 | "name": "julia", 41 | "version": "0.6.0" 42 | } 43 | }, 44 | "nbformat": 4, 45 | "nbformat_minor": 1 46 | } 47 | -------------------------------------------------------------------------------- /introduction_to_julia/8_macros.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Macros\n", 8 | "\n", 9 | "## time macro" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 157, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stderr", 19 | "output_type": "stream", 20 | "text": [ 21 | "\u001b[1m\u001b[33mWARNING: \u001b[39m\u001b[22m\u001b[33mreplacing docs for '@timeit :: Tuple{Any}' in module 'Main'.\u001b[39m\n" 22 | ] 23 | }, 24 | { 25 | "data": { 26 | "text/plain": [ 27 | "@timeit" 28 | ] 29 | }, 30 | "execution_count": 157, 31 | "metadata": {}, 32 | "output_type": "execute_result" 33 | } 34 | ], 35 | "source": [ 36 | "\n", 37 | "\"\"\"\n", 38 | " @timeit expression\n", 39 | "Compute the time taken to execute `expression`\n", 40 | "#### Example\n", 41 | "\n", 42 | "```julia\n", 43 | "julia> @timeit 2+2\n", 44 | "Elapsed time in seconds: \n", 45 | "0.002308\n", 46 | "```\n", 47 | "\"\"\"\n", 48 | "macro timeit(expression)\n", 49 | " quote\n", 50 | " local t0 = time()\n", 51 | " local val = $(esc(expression))\n", 52 | " local t1 = time()\n", 53 | " println(\"Elapsed time in seconds: \" )\n", 54 | " @printf \"%3.f\\n\" t1-t0\n", 55 | " end\n", 56 | "end" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 156, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/markdown": [ 67 | "```\n", 68 | "@timeit expression\n", 69 | "```\n", 70 | "\n", 71 | "Compute the time taken to execute `expression`\n", 72 | "\n", 73 | "#### Example\n", 74 | "\n", 75 | "```julia\n", 76 | "julia> @timeit 2+2\n", 77 | "Elapsed time in seconds: \n", 78 | "0.002308\n", 79 | "```\n" 80 | ], 81 | "text/plain": [ 82 | "```\n", 83 | "@timeit expression\n", 84 | "```\n", 85 | "\n", 86 | "Compute the time taken to execute `expression`\n", 87 | "\n", 88 | "#### Example\n", 89 | "\n", 90 | "```julia\n", 91 | "julia> @timeit 2+2\n", 92 | "Elapsed time in seconds: \n", 93 | "0.002308\n", 94 | "```\n" 95 | ] 96 | }, 97 | "execution_count": 156, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "?@timeit" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 154, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "Elapsed time in seconds: \n", 116 | "0.000007\n" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "@timeit 2+232==23" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 141, 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "data": { 131 | "text/plain": [ 132 | "f (generic function with 1 method)" 133 | ] 134 | }, 135 | "execution_count": 141, 136 | "metadata": {}, 137 | "output_type": "execute_result" 138 | } 139 | ], 140 | "source": [ 141 | "f(x) = x +1" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 113, 147 | "metadata": {}, 148 | "outputs": [ 149 | { 150 | "name": "stdout", 151 | "output_type": "stream", 152 | "text": [ 153 | "Elapsed time in seconds: \n", 154 | "0.002308\n" 155 | ] 156 | }, 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "11" 161 | ] 162 | }, 163 | "execution_count": 113, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "@timeit f(10)" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "metadata": { 176 | "collapsed": true 177 | }, 178 | "outputs": [], 179 | "source": [ 180 | "#" 181 | ] 182 | } 183 | ], 184 | "metadata": { 185 | "kernelspec": { 186 | "display_name": "Julia 0.6.0-dev", 187 | "language": "julia", 188 | "name": "julia-0.6" 189 | }, 190 | "language_info": { 191 | "file_extension": ".jl", 192 | "mimetype": "application/julia", 193 | "name": "julia", 194 | "version": "0.6.0" 195 | } 196 | }, 197 | "nbformat": 4, 198 | "nbformat_minor": 1 199 | } 200 | -------------------------------------------------------------------------------- /introduction_to_julia/9_asyncronous_capabilities.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Asyncronous computing\n", 8 | "\n", 9 | "This notebooks shows how to create tasks and use them." 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 61, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "Julia Version 0.6.0-rc1.0\n", 22 | "Commit 6bdb3950bd (2017-05-07 00:00 UTC)\n", 23 | "Platform Info:\n", 24 | " OS: macOS (x86_64-apple-darwin13.4.0)\n", 25 | " CPU: Intel(R) Core(TM) i7-4650U CPU @ 1.70GHz\n", 26 | " WORD_SIZE: 64\n", 27 | " BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)\n", 28 | " LAPACK: libopenblas64_\n", 29 | " LIBM: libopenlibm\n", 30 | " LLVM: libLLVM-3.9.1 (ORCJIT, haswell)\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "versioninfo()" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 6, 41 | "metadata": { 42 | "collapsed": true 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "v = rand(1:3,100000);\n", 47 | "v2 = rand(1:3,100000);" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 108, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "predict (generic function with 2 methods)" 59 | ] 60 | }, 61 | "execution_count": 108, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "type LinearModel\n", 68 | " W::Vector\n", 69 | "end\n", 70 | "\n", 71 | "function predict(h::LinearModel, x::Vector)\n", 72 | " return sign(h.W'*x)\n", 73 | "end" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 64, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "using MLDatasets" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 69, 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "data": { 92 | "text/plain": [ 93 | ":(Ac_mul_B((Core.getfield)(h, :W), x))" 94 | ] 95 | }, 96 | "execution_count": 69, 97 | "metadata": {}, 98 | "output_type": "execute_result" 99 | } 100 | ], 101 | "source": [ 102 | "expand( :(h.W'*x))" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 39, 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [ 111 | "X,y = MLDatasets.MNIST.traindata();" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 40, 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "data": { 121 | "text/plain": [ 122 | "(28, 28, 60000)" 123 | ] 124 | }, 125 | "execution_count": 40, 126 | "metadata": {}, 127 | "output_type": "execute_result" 128 | } 129 | ], 130 | "source": [ 131 | "size(X)" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 157, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "X = reshape(X,(784,60000));\n", 141 | "h = LinearModel(rand(784));\n", 142 | "y_hat = zeros(size(X,2));" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 184, 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | " 0.000046 seconds (8 allocations: 6.375 KiB)\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "@time begin\n", 160 | " for m in size(X,2) \n", 161 | " y_hat[m] = predict(h, X[:,m])\n", 162 | " end\n", 163 | "end" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 182, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "name": "stdout", 173 | "output_type": "stream", 174 | "text": [ 175 | " 0.000059 seconds (12 allocations: 6.563 KiB)\n" 176 | ] 177 | } 178 | ], 179 | "source": [ 180 | "@time begin\n", 181 | " @sync for m in size(X,2) \n", 182 | " y_hat[m] = predict(h, X[:,m])\n", 183 | " end\n", 184 | "end" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 174, 190 | "metadata": {}, 191 | "outputs": [ 192 | { 193 | "name": "stdout", 194 | "output_type": "stream", 195 | "text": [ 196 | " 0.000093 seconds (39 allocations: 752 bytes)\n" 197 | ] 198 | } 199 | ], 200 | "source": [ 201 | "@time begin\n", 202 | " for i in 1:20\n", 203 | " @async for m in size(X,2) \n", 204 | " y_hat[m] = predict(h, view(X,:,m))\n", 205 | " end\n", 206 | " end\n", 207 | "end" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": null, 213 | "metadata": {}, 214 | "outputs": [], 215 | "source": [] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": null, 220 | "metadata": { 221 | "collapsed": true 222 | }, 223 | "outputs": [], 224 | "source": [] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": null, 229 | "metadata": { 230 | "collapsed": true 231 | }, 232 | "outputs": [], 233 | "source": [] 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": { 238 | "collapsed": true 239 | }, 240 | "source": [ 241 | "## More examples" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 82, 247 | "metadata": {}, 248 | "outputs": [ 249 | { 250 | "data": { 251 | "text/plain": [ 252 | "a2 (generic function with 1 method)" 253 | ] 254 | }, 255 | "execution_count": 82, 256 | "metadata": {}, 257 | "output_type": "execute_result" 258 | } 259 | ], 260 | "source": [ 261 | "a2() = det(rand(10000, 10000))" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": null, 267 | "metadata": { 268 | "collapsed": true 269 | }, 270 | "outputs": [], 271 | "source": [ 272 | "b = Task(a2);" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": null, 278 | "metadata": { 279 | "collapsed": true 280 | }, 281 | "outputs": [], 282 | "source": [ 283 | "typeof(b)" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": null, 289 | "metadata": { 290 | "collapsed": true 291 | }, 292 | "outputs": [], 293 | "source": [ 294 | "istaskdone(b)" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": null, 300 | "metadata": { 301 | "collapsed": true 302 | }, 303 | "outputs": [], 304 | "source": [ 305 | "schedule(b);\n", 306 | "wait()" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": null, 312 | "metadata": { 313 | "collapsed": true 314 | }, 315 | "outputs": [], 316 | "source": [ 317 | "istaskdone(b)" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": null, 323 | "metadata": { 324 | "collapsed": true 325 | }, 326 | "outputs": [], 327 | "source": [ 328 | "yield();" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": null, 334 | "metadata": { 335 | "collapsed": true 336 | }, 337 | "outputs": [], 338 | "source": [ 339 | "?schedule" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": null, 345 | "metadata": { 346 | "collapsed": true 347 | }, 348 | "outputs": [], 349 | "source": [] 350 | } 351 | ], 352 | "metadata": { 353 | "kernelspec": { 354 | "display_name": "Julia 0.6.0-dev", 355 | "language": "julia", 356 | "name": "julia-0.6" 357 | }, 358 | "language_info": { 359 | "file_extension": ".jl", 360 | "mimetype": "application/julia", 361 | "name": "julia", 362 | "version": "0.6.0" 363 | } 364 | }, 365 | "nbformat": 4, 366 | "nbformat_minor": 1 367 | } 368 | -------------------------------------------------------------------------------- /macros/macros.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Macros" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "```julia\n", 15 | "# we define a macro like this:\n", 16 | "macro mymacro(e1, e2, e3) # syntax-y stuff here\n", 17 | "end\n", 18 | "# mymacro can be called in two ways:\n", 19 | "@mymacro(expr_1, expr_2, expr_3) # <- no space before\n", 20 | "# or ‘(’ !!!\n", 21 | "@mymacro expr_1 expr_2 expr_3\n", 22 | "# We can also define a macro\n", 23 | "macro m_str(p) # <- p is now going to be a string\n", 24 | " # syntax-y stuff here\n", 25 | "end\n", 26 | "```\n" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 2, 32 | "metadata": { 33 | "collapsed": false 34 | }, 35 | "outputs": [ 36 | { 37 | "data": { 38 | "text/plain": [ 39 | "@sayhello (macro with 1 method)" 40 | ] 41 | }, 42 | "execution_count": 2, 43 | "metadata": {}, 44 | "output_type": "execute_result" 45 | } 46 | ], 47 | "source": [ 48 | "macro sayhello()\n", 49 | " return :( println(\"Hello, world!\") )\n", 50 | " end" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 3, 56 | "metadata": { 57 | "collapsed": false 58 | }, 59 | "outputs": [ 60 | { 61 | "name": "stdout", 62 | "output_type": "stream", 63 | "text": [ 64 | "Hello, world!\n" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "@sayhello()" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 4, 75 | "metadata": { 76 | "collapsed": false 77 | }, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "@sayhello (macro with 2 methods)" 83 | ] 84 | }, 85 | "execution_count": 4, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "macro sayhello(name)\n", 92 | " return :( println(\"Hello, \", $name) )\n", 93 | " end" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 5, 99 | "metadata": { 100 | "collapsed": false 101 | }, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "Hello, human\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "@sayhello(\"human\")" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 6, 118 | "metadata": { 119 | "collapsed": false 120 | }, 121 | "outputs": [ 122 | { 123 | "data": { 124 | "text/plain": [ 125 | ":((Main.println)(\"Hello, \", \"human\"))" 126 | ] 127 | }, 128 | "execution_count": 6, 129 | "metadata": {}, 130 | "output_type": "execute_result" 131 | } 132 | ], 133 | "source": [ 134 | "ex = macroexpand( :(@sayhello(\"human\")) )" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 8, 140 | "metadata": { 141 | "collapsed": false 142 | }, 143 | "outputs": [ 144 | { 145 | "data": { 146 | "text/plain": [ 147 | ":((Main.println)(\"Hello, \", \"human\"))" 148 | ] 149 | }, 150 | "execution_count": 8, 151 | "metadata": {}, 152 | "output_type": "execute_result" 153 | } 154 | ], 155 | "source": [ 156 | "@macroexpand @sayhello \"human\"" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 15, 162 | "metadata": { 163 | "collapsed": false 164 | }, 165 | "outputs": [ 166 | { 167 | "ename": "LoadError", 168 | "evalue": "\u001b[91msyntax: unexpected \"end\"\u001b[39m", 169 | "output_type": "error", 170 | "traceback": [ 171 | "\u001b[91msyntax: unexpected \"end\"\u001b[39m", 172 | "", 173 | "Stacktrace:", 174 | " [1] \u001b[1minclude_string\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::String, ::String\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./loading.jl:522\u001b[22m\u001b[22m" 175 | ] 176 | } 177 | ], 178 | "source": [] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "metadata": { 184 | "collapsed": true 185 | }, 186 | "outputs": [], 187 | "source": [] 188 | } 189 | ], 190 | "metadata": { 191 | "anaconda-cloud": {}, 192 | "kernelspec": { 193 | "display_name": "Julia 0.6.2", 194 | "language": "julia", 195 | "name": "julia-0.6" 196 | }, 197 | "language_info": { 198 | "file_extension": ".jl", 199 | "mimetype": "application/julia", 200 | "name": "julia", 201 | "version": "0.6.2" 202 | } 203 | }, 204 | "nbformat": 4, 205 | "nbformat_minor": 1 206 | } 207 | -------------------------------------------------------------------------------- /plotting_and_diagrams/Untitled.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": { 16 | "collapsed": true 17 | }, 18 | "outputs": [], 19 | "source": [ 20 | "using TikzGraphs" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 2, 26 | "metadata": { 27 | "collapsed": true 28 | }, 29 | "outputs": [], 30 | "source": [ 31 | "using LightGraphs\n" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 4, 37 | "metadata": { 38 | "collapsed": false 39 | }, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "! Package pgfkeys Error: I do not know the key '/tikz/hyper/.forward to' and I \n", 46 | "am going to ignore it. Perhaps you misspelled it.\n", 47 | "\n", 48 | "See the pgfkeys package documentation for explanation.\n", 49 | "Type H for immediate help.\n", 50 | " ... \n", 51 | " \n", 52 | "l.71 ...appendtoforwardinglist{/tikz/,/tikz/graphs/}\n", 53 | " \n", 54 | "Error saving as SVG\n" 55 | ] 56 | }, 57 | { 58 | "ename": "ErrorException", 59 | "evalue": "\u001b[91mLaTeX error\u001b[39m", 60 | "output_type": "error", 61 | "traceback": [ 62 | "\u001b[91mLaTeX error\u001b[39m", 63 | "" 64 | ] 65 | } 66 | ], 67 | "source": [ 68 | "g = DiGraph(4)\n", 69 | "add_edge!(g, 1, 2)\n", 70 | "add_edge!(g, 2, 3)\n" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 5, 76 | "metadata": { 77 | "collapsed": false 78 | }, 79 | "outputs": [ 80 | { 81 | "name": "stdout", 82 | "output_type": "stream", 83 | "text": [ 84 | "! Package pgfkeys Error: I do not know the key '/tikz/hyper/.forward to' and I \n", 85 | "am going to ignore it. Perhaps you misspelled it.\n", 86 | "\n", 87 | "See the pgfkeys package documentation for explanation.\n", 88 | "Type H for immediate help.\n", 89 | " ... \n", 90 | " \n", 91 | "l.71 ...appendtoforwardinglist{/tikz/,/tikz/graphs/}\n", 92 | " \n", 93 | "Error saving as SVG\n" 94 | ] 95 | }, 96 | { 97 | "ename": "ErrorException", 98 | "evalue": "\u001b[91mLaTeX error\u001b[39m", 99 | "output_type": "error", 100 | "traceback": [ 101 | "\u001b[91mLaTeX error\u001b[39m", 102 | "" 103 | ] 104 | } 105 | ], 106 | "source": [ 107 | "TikzGraphs.plot(g)" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": { 114 | "collapsed": true 115 | }, 116 | "outputs": [], 117 | "source": [] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": { 123 | "collapsed": true 124 | }, 125 | "outputs": [], 126 | "source": [ 127 | "add_edge!(g, 3, 4)\n", 128 | "add_edge!(g, 1, 4)\n", 129 | "TikzGraphs.plot(g)" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "metadata": { 136 | "collapsed": true 137 | }, 138 | "outputs": [], 139 | "source": [] 140 | } 141 | ], 142 | "metadata": { 143 | "kernelspec": { 144 | "display_name": "Julia 0.6.2", 145 | "language": "julia", 146 | "name": "julia-0.6" 147 | }, 148 | "language_info": { 149 | "file_extension": ".jl", 150 | "mimetype": "application/julia", 151 | "name": "julia", 152 | "version": "0.6.2" 153 | } 154 | }, 155 | "nbformat": 4, 156 | "nbformat_minor": 0 157 | } 158 | -------------------------------------------------------------------------------- /plotting_and_diagrams/tikzgraph/diagrams/graph.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /plotting_and_diagrams/tikzgraph/graph.tex: -------------------------------------------------------------------------------- 1 | \documentclass[tikz]{standalone} 2 | \usepackage{fontspec} 3 | \setmainfont{Latin Modern Math} 4 | \usetikzlibrary{graphs} 5 | \usetikzlibrary{graphdrawing} 6 | 7 | \usegdlibrary{layered} 8 | \begin{document} 9 | \begin{tikzpicture}[] 10 | \graph [layered layout, ] { 11 | 1/"1" [], 12 | 2/"2" [], 13 | 3/"3" [], 14 | 4/"4" [], 15 | ; 16 | 1 -> [,] 2; 17 | 1 -> [,] 4; 18 | 2 -> [,] 3; 19 | 3 -> [,] 4; 20 | }; 21 | 22 | \end{tikzpicture} 23 | \end{document} 24 | -------------------------------------------------------------------------------- /plotting_and_diagrams/tikzpicture.tex: -------------------------------------------------------------------------------- 1 | \documentclass[tikz]{standalone} 2 | \usepackage{fontspec} 3 | \setmainfont{Latin Modern Math} 4 | \usetikzlibrary{graphs} 5 | \usetikzlibrary{graphdrawing} 6 | 7 | \usegdlibrary{layered} 8 | \begin{document} 9 | \begin{tikzpicture}[] 10 | \graph [layered layout, ] { 11 | 1/"1" [], 12 | 2/"2" [], 13 | 3/"3" [], 14 | 4/"4" [], 15 | ; 16 | 1 -> [,] 2; 17 | 2 -> [,] 3; 18 | }; 19 | 20 | \end{tikzpicture} 21 | \end{document} 22 | -------------------------------------------------------------------------------- /python_vs_julia/compute_pi/compute_pi.jl: -------------------------------------------------------------------------------- 1 | function compute_pi(N::Int) 2 | """ 3 | Compute pi with a Monte Carlo simulation of N darts thrown in [-1,1]^2 4 | Returns estimate of pi 5 | """ 6 | n_landed_in_circle = 0 # counts number of points that have radial coordinate < 1, i.e. in circle 7 | for i = 1:N 8 | x = rand() * 2. - 1. # uniformly distributed number on x-axis 9 | y = rand() * 2. - 1. # uniformly distributed number on y-axis 10 | 11 | r2 = x*x + y*y # radius squared, in radial coordinates 12 | if r2 < 1.0 13 | n_landed_in_circle += 1 14 | end 15 | end 16 | 17 | return n_landed_in_circle / N * 4.0 18 | end 19 | 20 | N= 1000_000_000 21 | t0 = time() 22 | println("Start Computing") 23 | aprox = compute_pi(N) 24 | println("Approximate pi: ", aprox) 25 | println("Total time: ", abs(time()-t0), " seconds") 26 | 27 | -------------------------------------------------------------------------------- /python_vs_julia/compute_pi/compute_pi.py: -------------------------------------------------------------------------------- 1 | import time 2 | import random 3 | 4 | def compute_pi(N): 5 | """ 6 | Compute pi with a Monte Carlo simulation of N darts thrown in [-1,1]^2 7 | Returns estimate of pi 8 | """ 9 | n_landed_in_circle = 0 # counts number of points that have radial coordinate < 1, i.e. in circle 10 | for i in range(N): 11 | x = random.random() * 2 - 1 # uniformly distributed number on x-axis 12 | y = random.random() * 2 - 1 # uniformly distributed number on y-axis 13 | r2 = x*x + y*y # radius squared, in radial coordinates 14 | if r2 < 1.0: 15 | n_landed_in_circle += 1. 16 | return (n_landed_in_circle / N) * 4.0 17 | 18 | N= 1000_000_000 19 | t0 = time.time() 20 | print("Start Computing") 21 | pi = compute_pi(N) 22 | print("Approximate pi: {}".format(pi)) 23 | print( "Total time: {} seconds".format(abs(t0- time.time()))) 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /python_vs_julia/compute_pi/compute_pi_distributed.jl: -------------------------------------------------------------------------------- 1 | using Distributed 2 | 3 | @everywhere function get_sample() 4 | 5 | x = rand() * 2. - 1. # uniformly distributed number on x-axis 6 | y = rand() * 2. - 1. # uniformly distributed number on y-axis 7 | 8 | r2 = x*x + y*y # radius squared, in radial coordinates 9 | if r2 < 1.0 10 | return 1 11 | else 12 | return 0 13 | end 14 | end 15 | 16 | function compute_pi(N::Int) 17 | """ 18 | Compute pi with a Monte Carlo simulation of N darts thrown in [-1,1]^2 19 | Returns estimate of pi 20 | """ 21 | n_landed_in_circle = @distributed (+) for i = 1:N 22 | get_sample() 23 | end 24 | 25 | return n_landed_in_circle / N * 4.0 26 | end 27 | 28 | 29 | N= 1000_000_000 30 | t0 = time() 31 | println("Start Computing") 32 | aprox = compute_pi(N) 33 | println("Approximate pi: ", aprox) 34 | println("Total time: ", abs(time()-t0), " seconds") 35 | 36 | 37 | -------------------------------------------------------------------------------- /python_vs_julia/compute_pi/compute_pi_multithread.jl: -------------------------------------------------------------------------------- 1 | function compute_pi(N::Int) 2 | """ 3 | Compute pi with a Monte Carlo simulation of N darts thrown in [-1,1]^2 4 | Returns estimate of pi 5 | """ 6 | n_landed_in_circle = Threads.Atomic{Int}(0) # counts number of points that have radial coordinate < 1, i.e. in circle 7 | Threads.@threads for i = 1:N 8 | x = rand() * 2. - 1. # uniformly distributed number on x-axis 9 | y = rand() * 2. - 1. # uniformly distributed number on y-axis 10 | 11 | r2 = x*x + y*y # radius squared, in radial coordinates 12 | if r2 < 1.0 13 | Threads.atomic_add!(n_landed_in_circle, 1) 14 | end 15 | end 16 | 17 | return n_landed_in_circle.value / N * 4.0 18 | end 19 | 20 | N= 1000_000_000 21 | t0 = time() 22 | println("Start Computing") 23 | aprox = compute_pi(N) 24 | println("Approximate pi: ", aprox) 25 | println("Total time: ", abs(time()-t0), " seconds") 26 | 27 | 28 | -------------------------------------------------------------------------------- /python_vs_julia/compute_pi/compute_pi_parallel.jl: -------------------------------------------------------------------------------- 1 | @everywhere function compute_pi(N::Int) 2 | """ 3 | Compute pi with a Monte Carlo simulation of N darts thrown in [-1,1]^2 4 | Returns estimate of pi 5 | """ 6 | n_landed_in_circle = 0 # counts number of points that have radial coordinate < 1, i.e. in circle 7 | for i = 1:N 8 | x = rand() * 2 - 1 # uniformly distributed number on x-axis 9 | y = rand() * 2 - 1 # uniformly distributed number on y-axis 10 | 11 | r2 = x*x + y*y # radius squared, in radial coordinates 12 | if r2 < 1.0 13 | n_landed_in_circle += 1 14 | end 15 | end 16 | 17 | return n_landed_in_circle / N * 4.0 18 | end 19 | 20 | function compute_pi_parallel(N::Int; ncores::Int=8) 21 | """ 22 | Compute pi in parallel, over ncores cores, with a Monte Carlo simulation throwing N total darts 23 | """ 24 | 25 | # compute sum of pi's estimated among all cores in parallel 26 | sum_of_pis = @parallel (+) for i=1:ncores 27 | compute_pi(ceil(Int, N / ncores)) 28 | end 29 | 30 | return sum_of_pis / ncores # average value 31 | end 32 | 33 | compute_pi(2) 34 | N= 1_000_000_000 35 | t0 = time() 36 | println("Start Computing") 37 | aprox = compute_pi_parallel(N, ncores=4) 38 | println("Approximate pi: ", pi) 39 | println("Total time: ", abs(time()-t0), " seconds") 40 | 41 | -------------------------------------------------------------------------------- /python_vs_julia/count_triangles.jl: -------------------------------------------------------------------------------- 1 | ### Count triangles 2 | 3 | function generate_triangles(nodes) 4 | result = [] 5 | visited_ids = Set{Int}() 6 | for (node_a_id, nodes_node_a_id) in nodes 7 | temp_visited = Set() 8 | for node_b_id in nodes_node_a_id 9 | if node_b_id == node_a_id 10 | println("ERROR") 11 | break 12 | end 13 | if node_b_id in visited_ids 14 | continue 15 | end 16 | for node_c_id in nodes[node_b_id] 17 | if node_c_id in visited_ids 18 | continue 19 | end 20 | if node_c_id in temp_visited 21 | continue 22 | end 23 | if node_a_id in nodes[node_c_id] 24 | push!(result, [node_a_id, node_b_id, node_c_id]) 25 | else 26 | continue 27 | end 28 | end 29 | push!(temp_visited, node_b_id) 30 | end 31 | push!(visited_ids, node_a_id) 32 | end 33 | return result 34 | end 35 | 36 | 37 | function keys_to_ints(dict) 38 | aux = Dict{Int,Array{Int}}() 39 | for (k,v) in dict 40 | aux[parse(k)] = v 41 | end 42 | return aux 43 | end 44 | 45 | using JSON 46 | 47 | aux = readlines("graph.json") 48 | adj_dict = JSON.parse(aux[1]) 49 | adj_dict = keys_to_ints(adj_dict) 50 | 51 | t0 = time() 52 | println("Start Computing") 53 | 54 | triangles = 0 55 | for i in 1:100 56 | triangles = generate_triangles(adj_dict) 57 | end 58 | 59 | println("Number of triangles: ", length(triangles)) 60 | println("Total time: ", abs(time()-t0), " seconds") 61 | 62 | 63 | -------------------------------------------------------------------------------- /python_vs_julia/count_triangles.py: -------------------------------------------------------------------------------- 1 | 2 | import json 3 | 4 | #### function for counting undirected cycles 5 | def generate_triangles(nodes): 6 | visited_ids = set() # mark visited node 7 | for node_a_id in nodes: 8 | temp_visited = set() # to get undirected triangles 9 | for node_b_id in nodes[node_a_id]: 10 | if node_b_id == node_a_id: 11 | # to prevent self-loops, if your graph allows self-loops then you don't need this condition 12 | raise ValueError 13 | if node_b_id in visited_ids: 14 | continue 15 | for node_c_id in nodes[node_b_id]: 16 | if node_c_id in visited_ids: 17 | continue 18 | if node_c_id in temp_visited: 19 | continue 20 | if node_a_id in nodes[node_c_id]: 21 | yield(node_a_id, node_b_id, node_c_id) 22 | #visited_ids.add((node_a_id, node_b_id, node_c_id)) 23 | else: 24 | continue 25 | temp_visited.add(node_b_id) 26 | visited_ids.add(node_a_id) 27 | 28 | 29 | #### function for counting undirected cycles 30 | def generate_triangles_2(nodes): 31 | result = [] 32 | visited_ids = set() # mark visited node 33 | for node_a_id in nodes: 34 | temp_visited = set() # to get undirected triangles 35 | for node_b_id in nodes[node_a_id]: 36 | if node_b_id == node_a_id: 37 | # to prevent self-loops, if your graph allows self-loops then you don't need this condition 38 | raise ValueError 39 | if node_b_id in visited_ids: 40 | continue 41 | for node_c_id in nodes[node_b_id]: 42 | if node_c_id in visited_ids: 43 | continue 44 | if node_c_id in temp_visited: 45 | continue 46 | if node_a_id in nodes[node_c_id]: 47 | result.append((node_a_id, node_b_id, node_c_id)) 48 | else: 49 | continue 50 | temp_visited.add(node_b_id) 51 | visited_ids.add(node_a_id) 52 | 53 | return result 54 | 55 | with open('graph.json', 'r') as fp: 56 | adj_dict = json.load( fp) 57 | 58 | adj_dict_ = {} 59 | for key in adj_dict: 60 | adj_dict_[int(key)] = adj_dict[key] 61 | del adj_dict 62 | 63 | 64 | import time 65 | 66 | t0 = time.time() 67 | print("Start Computing") 68 | for i in range(1000): 69 | cycles = generate_triangles_2(adj_dict_) 70 | cycles = list(cycles) 71 | print("Number of triangles : {}".format(len(cycles))) 72 | print( "Total time: {} seconds".format(abs(t0- time.time()))) 73 | 74 | 75 | -------------------------------------------------------------------------------- /python_vs_julia/count_triangles/count_triangles.jl: -------------------------------------------------------------------------------- 1 | ### Count triangles 2 | 3 | function count_triangles(g, isDirected) 4 | nodes = length(g) 5 | count_Triangle = 0 #Initialize result 6 | # Consider every possible triplet of edges in graph 7 | for i in 1:nodes 8 | for j in 1:nodes 9 | for k in 1:nodes 10 | # check the triplet if it satisfies the condition 11 | if i!=j && i!=k && j!=k && g[i][j]==1 && g[j][k]==1 && g[k][i]==1 12 | count_Triangle += 1 13 | end 14 | end 15 | end 16 | end 17 | # if graph is directed , division is done by 3 else division by 6 is done 18 | if isDirected==true 19 | return count_Triangle/3 20 | else 21 | return count_Triangle/6 22 | end 23 | end 24 | 25 | 26 | # Load graph 27 | #using CSV 28 | #graph_table = CSV.read("graph.csv") 29 | #n_nodes = size(graph_table,1) 30 | #graph_list_of_lists = [Array(graph_table[i]) for i in 1:n_nodes ] 31 | 32 | graph = readlines("graph.csv") 33 | graph_list_of_lists = map(x-> parse.(Int,split(x,",")[2:end]), graph[2:end]) 34 | #graph = map(x-> split(x,",")[2:end], graph[2:end]) 35 | #graph = map(x-> parse.(Int,x), graph) 36 | 37 | # count trianges 38 | t0 = time() 39 | n_triangles = count_triangles(graph_list_of_lists[1:4], false) 40 | println("Start Computing") 41 | n_triangles = count_triangles(graph_list_of_lists, false) 42 | println("Number of triangles: ", n_triangles) 43 | println("Total time: ", abs(time()-t0), " seconds") 44 | 45 | -------------------------------------------------------------------------------- /python_vs_julia/count_triangles/count_triangles.py: -------------------------------------------------------------------------------- 1 | 2 | # https://www.geeksforgeeks.org/number-of-triangles-in-directed-and-undirected-graphs/ 3 | import time 4 | import networkx 5 | import numpy as np 6 | import pandas as pd 7 | 8 | def countTriangle(g, isDirected): 9 | nodes = len(g) 10 | count_Triangle = 0 #Initialize result 11 | # Consider every possible triplet of edges in graph 12 | for i in range(nodes): 13 | for j in range(nodes): 14 | for k in range(nodes): 15 | # check the triplet if it satisfies the condition 16 | if( i!=j and i !=k and j !=k and 17 | g[i][j] and g[j][k] and g[k][i]): 18 | count_Triangle += 1 19 | # if graph is directed , division is done by 3 20 | # else division by 6 is done 21 | return count_Triangle/3 if isDirected else count_Triangle/6 22 | 23 | # Create adjacency matrix of an undirected graph 24 | graph = [[0, 1, 1, 0], 25 | [1, 0, 1, 1], 26 | [1, 1, 0, 1], 27 | [0, 1, 1, 0]] 28 | 29 | 30 | n_nodes = 1000 31 | n_edges = 10 32 | 33 | print("The Number of nodes {}, number of expected edges per node: {} ".format(n_nodes, n_edges)) 34 | 35 | G = networkx.barabasi_albert_graph(n_nodes, n_edges, seed=1) 36 | adj_list = [(n, nbrdict) for n, nbrdict in G.adjacency()] 37 | 38 | def create_adjacency_matrix(adj_list): 39 | n_nodes = len(adj_list) 40 | graph = [[0]*n_nodes for node in range(n_nodes)] 41 | for i,conections in adj_list: 42 | positions_with_ones = list(conections.keys()) 43 | for k in positions_with_ones: 44 | graph[i][k] = np.int8(1) 45 | return graph 46 | 47 | graph = create_adjacency_matrix(adj_list) 48 | g_df = pd.DataFrame(graph) 49 | g_df.to_csv("graph.csv") 50 | 51 | t0 = time.time() 52 | print("Start computing") 53 | n_triangles = countTriangle(graph, False) 54 | print("The Number of triangles in undirected graph: ", n_triangles ) 55 | t = time.time()-t0 56 | print("Total time: {} seconds".format(t)) 57 | -------------------------------------------------------------------------------- /python_vs_julia/count_triangles/count_triangles_multithreaded.jl: -------------------------------------------------------------------------------- 1 | ### Count triangles 2 | using Base.Threads 3 | function count_triangles(g, isDirected) 4 | nodes = length(g) 5 | count_Triangle = Atomic{Int64}(0) 6 | # Consider every possible triplet of edges in graph 7 | @threads for i in 1:nodes 8 | for j in 1:nodes 9 | for k in 1:nodes 10 | # check the triplet if it satisfies the condition 11 | if i!=j && i!=k && j!=k && g[i][j]==1 && g[j][k]==1 && g[k][i]==1 12 | atomic_add!(count_Triangle, 1) 13 | end 14 | end 15 | end 16 | end 17 | # if graph is directed , division is done by 3 else division by 6 is done 18 | if isDirected==true 19 | return count_Triangle.value/3 20 | else 21 | return count_Triangle.value/6 22 | end 23 | end 24 | 25 | 26 | # Load graph 27 | #using CSV 28 | #graph_table = CSV.read("graph.csv") 29 | #n_nodes = size(graph_table,1) 30 | #graph_list_of_lists = [Array(graph_table[i]) for i in 1:n_nodes ] 31 | 32 | graph = readlines("graph.csv") 33 | graph_list_of_lists = map(x-> parse.(Int,split(x,",")[2:end]), graph[2:end]) 34 | #graph = map(x-> split(x,",")[2:end], graph[2:end]) 35 | #graph = map(x-> parse.(Int,x), graph) 36 | 37 | # count trianges 38 | t0 = time() 39 | n_triangles = count_triangles(graph_list_of_lists[1:4], false) 40 | println("Start Computing") 41 | n_triangles = count_triangles(graph_list_of_lists, false) 42 | println("Number of triangles: ", n_triangles) 43 | println("Total time: ", abs(time()-t0), " seconds") 44 | 45 | -------------------------------------------------------------------------------- /python_vs_julia/count_triangles/generate_graph.py: -------------------------------------------------------------------------------- 1 | ### generate graph and save it 2 | 3 | import networkx 4 | import json 5 | import argparse 6 | 7 | print("networkx.__version__:", networkx.__version__) 8 | 9 | parser = argparse.ArgumentParser(description="script generate_graph, it uses a Albert Barabasi random graph generator") 10 | parser.add_argument('-n', '--n_nodes', type=int, required=False, help='Number of nodes to generate') 11 | parser.add_argument('-e', '--n_edges', type=int, required=False, help='Number of edges to attach from a new node to existing nodes') 12 | args = parser.parse_args() 13 | 14 | #n_nodes = int(input("\n\nPlease enter Number of nodes in the random graph: ")) 15 | 16 | if __name__ == "__main__": 17 | 18 | 19 | ##### Argparsing ############################### 20 | if args.n_nodes : 21 | n_nodes = args.n_nodes 22 | print("\n\tUser introduced {} nodes".format(n_nodes)) 23 | else: 24 | n_nodes = 1000 25 | print("\n\tUser did not introduced n_nodes, using default value {}".format(n_nodes)) 26 | 27 | if args.n_edges: 28 | n_edges = args.n_edges 29 | print("\n\tUser introduced {edges} nodes".format(n_edges)) 30 | else: 31 | n_edges = 10 32 | print("\tUser did not introduced n_edges, using default value {}".format(n_edges)) 33 | 34 | 35 | ################################################ 36 | 37 | G = networkx.barabasi_albert_graph(n_nodes, n_edges, seed=1) 38 | 39 | #adj_list = G.adjacency_list() 40 | adj_list = list(G.adjacency()) 41 | n_nodes = len(adj_list) 42 | adj_dict = {i:adjacent_i for i,adjacent_i in zip(range(n_nodes), adj_list)} 43 | 44 | with open('graph.json', 'w') as fp: 45 | json.dump(adj_dict, fp) 46 | 47 | print("\tGraph: `graph.json` generated") 48 | print("\n") -------------------------------------------------------------------------------- /python_vs_julia/elementwise_functions/myfunc_py.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 5, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "\n", 11 | "def myfunc(a, b):\n", 12 | " if a > b:\n", 13 | " return a - b\n", 14 | " else:\n", 15 | " return a + b" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 6, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "vfunc = np.vectorize(myfunc)" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 7, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "x = np.random.rand(1_000_000)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 13, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "CPU times: user 386 ms, sys: 57.6 ms, total: 443 ms\n", 46 | "Wall time: 447 ms\n" 47 | ] 48 | }, 49 | { 50 | "data": { 51 | "text/plain": [ 52 | "array([2.41303539, 2.95970519, 2.42986298, ..., 2.10390795, 2.41215124,\n", 53 | " 2.59588939])" 54 | ] 55 | }, 56 | "execution_count": 13, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "%%time\n", 63 | "vfunc(x, 2)\n" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "### \"vectorized\" implementation" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 14, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "def myfunc_vec(x, value):\n", 80 | " result = np.empty(len(x))\n", 81 | " #x_bigger = np.where(x > value)\n", 82 | " x_bigger = x > value\n", 83 | " \n", 84 | " result[x_bigger] = result[x_bigger] - value\n", 85 | " result[~x_bigger] = result[~x_bigger] + value\n", 86 | " return result" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 15, 92 | "metadata": { 93 | "scrolled": true 94 | }, 95 | "outputs": [ 96 | { 97 | "name": "stdout", 98 | "output_type": "stream", 99 | "text": [ 100 | "CPU times: user 60.7 ms, sys: 14.2 ms, total: 75 ms\n", 101 | "Wall time: 52.7 ms\n" 102 | ] 103 | }, 104 | { 105 | "data": { 106 | "text/plain": [ 107 | "array([-0.1, -0.1, -0.1, ..., -0.1, -0.1, -0.1])" 108 | ] 109 | }, 110 | "execution_count": 15, 111 | "metadata": {}, 112 | "output_type": "execute_result" 113 | } 114 | ], 115 | "source": [ 116 | "%%time\n", 117 | "myfunc_vec(x,0.1)" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "### Don't use a loop to update elments in a numpy array " 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 16, 130 | "metadata": {}, 131 | "outputs": [], 132 | "source": [ 133 | "x = np.random.rand(1_000_000)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 17, 139 | "metadata": { 140 | "scrolled": true 141 | }, 142 | "outputs": [ 143 | { 144 | "name": "stdout", 145 | "output_type": "stream", 146 | "text": [ 147 | "CPU times: user 943 ms, sys: 25.5 ms, total: 969 ms\n", 148 | "Wall time: 980 ms\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "%%time \n", 154 | "result = np.zeros(len(x))\n", 155 | "value = 2.\n", 156 | "for k,x_k in enumerate(x):\n", 157 | " result[k] = myfunc(x_k, value)\n" 158 | ] 159 | } 160 | ], 161 | "metadata": { 162 | "kernelspec": { 163 | "display_name": "Python 3", 164 | "language": "python", 165 | "name": "python3" 166 | }, 167 | "language_info": { 168 | "codemirror_mode": { 169 | "name": "ipython", 170 | "version": 3 171 | }, 172 | "file_extension": ".py", 173 | "mimetype": "text/x-python", 174 | "name": "python", 175 | "nbconvert_exporter": "python", 176 | "pygments_lexer": "ipython3", 177 | "version": "3.7.3" 178 | }, 179 | "toc": { 180 | "base_numbering": 1, 181 | "nav_menu": {}, 182 | "number_sections": true, 183 | "sideBar": true, 184 | "skip_h1_title": false, 185 | "title_cell": "Table of Contents", 186 | "title_sidebar": "Contents", 187 | "toc_cell": false, 188 | "toc_position": {}, 189 | "toc_section_display": true, 190 | "toc_window_display": true 191 | } 192 | }, 193 | "nbformat": 4, 194 | "nbformat_minor": 2 195 | } 196 | -------------------------------------------------------------------------------- /python_vs_julia/generate_graph.py: -------------------------------------------------------------------------------- 1 | ### generate graph and save it 2 | 3 | import networkx 4 | import json 5 | 6 | 7 | var = input("\n\nPlease enter Number of nodes in the random graph: ") 8 | 9 | node_number = var 10 | initial_nodes = 2 11 | G = networkx.barabasi_albert_graph(node_number, initial_nodes, seed=1) 12 | 13 | adj_list = G.adjacency_list() 14 | n_nodes = len(adj_list) 15 | adj_dict = {i:adjacent_i for i,adjacent_i in zip(range(n_nodes), adj_list)} 16 | 17 | 18 | with open('graph.json', 'w') as fp: 19 | json.dump(adj_dict, fp) 20 | 21 | print("Graph: `graph.json` generated") 22 | print("\n") -------------------------------------------------------------------------------- /simd_examples/execute_this.jl: -------------------------------------------------------------------------------- 1 | include(joinpath(dirname(JULIA_HOME),"share","julia","build_sysimg.jl")) 2 | build_sysimg(force=true) 3 | 4 | -------------------------------------------------------------------------------- /speed_tests/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidbp/learn_julia/a624967aa95ece1912adb216fc652f45fe84d4b1/speed_tests/.DS_Store -------------------------------------------------------------------------------- /speed_tests/rbm.jl: -------------------------------------------------------------------------------- 1 | 2 | function sigmoid(vector::Array{Float64}) 3 | return 1./(1 + e.^(-vector)) 4 | end 5 | 6 | 7 | type RBM_col{T <: Real} 8 | W::Matrix{T} 9 | vis_bias::Vector{T} 10 | hid_bias::Vector{T} 11 | n_vis::Int64 12 | n_hid::Int64 13 | trained::Bool 14 | end 15 | 16 | function initializeRBM_col(n_vis::Int64, n_hid::Int64; sigma=0.01, T=Float64) 17 | 18 | return RBM_col{T}(sigma*randn(n_hid,n_vis), # weight matrix 19 | zeros(n_vis), # visible vector 20 | zeros(n_hid), # Hidden vector 21 | n_vis, # num visible units 22 | n_hid, # num hidden unnits 23 | false) # trained 24 | 25 | 26 | end 27 | 28 | function contrastive_divergence_col_K(Xbatch, rbm, K::Int64, lr::Float64) 29 | 30 | batch_size = size(Xbatch)[2] 31 | 32 | Delta_W = zeros(size(rbm.W)) 33 | Delta_b = zeros(size(rbm.vis_bias)) 34 | Delta_c = zeros(size(rbm.hid_bias)) 35 | 36 | for i in 1:batch_size 37 | x = Xbatch[:,i] 38 | xneg = Xbatch[:,i] 39 | 40 | for k in 1:K 41 | hneg = sigmoid( rbm.W * xneg .+ rbm.hid_bias) .> rand(rbm.n_hid) 42 | xneg = sigmoid( rbm.W' * hneg .+ rbm.vis_bias) .> rand(rbm.n_vis) 43 | end 44 | 45 | ehp = sigmoid(rbm.W * x + rbm.hid_bias) 46 | ehn = sigmoid(rbm.W * xneg + rbm.hid_bias) 47 | 48 | ### kron vs no kron??? 49 | Delta_W += lr * ( x * ehp' - xneg * ehn')' 50 | Delta_b += lr * (x - xneg) 51 | Delta_c += lr * (ehp - ehn) 52 | 53 | end 54 | 55 | rbm.W += Delta_W / batch_size; 56 | rbm.vis_bias += Delta_b / batch_size; 57 | rbm.hid_bias += Delta_c / batch_size; 58 | 59 | return 60 | end 61 | 62 | 63 | 64 | 65 | function contrastive_divergence_col_K_kron(Xbatch, rbm, K::Int64, lr::Float64) 66 | 67 | batch_size = size(Xbatch)[2] 68 | 69 | Delta_W = zeros(size(rbm.W)) 70 | Delta_b = zeros(size(rbm.vis_bias)) 71 | Delta_c = zeros(size(rbm.hid_bias)) 72 | 73 | for i in 1:batch_size 74 | x = Xbatch[:,i] 75 | xneg = Xbatch[:,i] 76 | 77 | for k in 1:K 78 | hneg = sigmoid( rbm.W * xneg .+ rbm.hid_bias) .> rand(rbm.n_hid) 79 | xneg = sigmoid( rbm.W' * hneg .+ rbm.vis_bias) .> rand(rbm.n_vis) 80 | end 81 | 82 | ehp = sigmoid(rbm.W * x + rbm.hid_bias) 83 | ehn = sigmoid(rbm.W * xneg + rbm.hid_bias) 84 | 85 | Delta_W += lr * (kron(x, ehp') - kron(xneg, ehn'))' 86 | Delta_b += lr * (x - xneg) 87 | Delta_c += lr * (ehp - ehn) 88 | 89 | end 90 | 91 | rbm.W += Delta_W / batch_size; 92 | rbm.vis_bias += Delta_b / batch_size; 93 | rbm.hid_bias += Delta_c / batch_size; 94 | 95 | return 96 | end 97 | 98 | X_train = rand(5000,784); 99 | X_batch_col = X_train'[:,1:200]; 100 | rbm = initializeRBM_col(784, 225) 101 | 102 | #warmup 103 | contrastive_divergence_col_K(X_batch_col, rbm, 1, 0.01) 104 | 105 | print("\n This RBM used scalar product\n\t") 106 | @time contrastive_divergence_col_K(X_batch_col, rbm, 1, 0.01) 107 | 108 | print("\n This RBM used kron product\n\t") 109 | @time contrastive_divergence_col_K_kron(X_batch_col, rbm, 1, 0.01) 110 | 111 | using BenchmarkTools 112 | print("\n Benchmark RBM used \n\t") 113 | print(@benchmark contrastive_divergence_col_K(X_batch_col, rbm, 1, 0.01)) 114 | print("\n") 115 | 116 | -------------------------------------------------------------------------------- /speed_tests/rbm.m: -------------------------------------------------------------------------------- 1 | 2 | function rbm; 3 | 4 | %using MNIST 5 | %X_train, y_train = MNIST.traindata() 6 | %X_train_rows = X_train'; 7 | %X_train_rows = X_train_rows[1:42000,:]; 8 | %X_train_cols = X_train[:,1:42000] 9 | 10 | X_train = rand(5000,784); 11 | 12 | X_batch_col = X_train(1:200,:)'; 13 | rbm = initializeRBM_col(784, 225, 0.01); 14 | tic; contrastive_divergence_col_K(X_batch_col, rbm, 1, 0.01); toc; 15 | 16 | X_batch_rows = X_train(1:200,:); 17 | rbm = initializeRBM_rows(784,225, 0.01); 18 | tic; contrastive_divergence_rows_K(X_batch_rows, rbm, 1, 0.01); toc; 19 | 20 | return; 21 | 22 | function x = sigmoid(v); 23 | x = 1./(1 + exp(-v)); 24 | return; 25 | 26 | function rbm = initializeRBM_col(n_vis, n_hid, sigma); 27 | rbm.W = sigma*randn(n_hid,n_vis); % weight matrix 28 | rbm.vis_bias = zeros(n_vis,1); % visible vector 29 | rbm.hid_bias = zeros(n_hid,1); % Hidden vector 30 | rbm.n_vis = n_vis; % num visible units 31 | rbm.n_hid = n_hid; % num hidden unnits 32 | return; 33 | 34 | function contrastive_divergence_col_K(Xbatch, rbm, K, lr); 35 | 36 | batch_size = size(Xbatch,2); 37 | 38 | Delta_W = zeros(size(rbm.W)); 39 | Delta_b = zeros(size(rbm.vis_bias)); 40 | Delta_c = zeros(size(rbm.hid_bias)); 41 | 42 | for i = 1:batch_size 43 | x = Xbatch(:,i); 44 | xneg = Xbatch(:,i); 45 | 46 | for k = 1:K 47 | hneg = sigmoid( rbm.W * xneg + rbm.hid_bias) > rand(size(rbm.n_hid)); 48 | xneg = sigmoid( rbm.W' * hneg + rbm.vis_bias) > rand(size(rbm.n_vis)); 49 | end 50 | 51 | ehp = sigmoid(rbm.W * x + rbm.hid_bias); 52 | ehn = sigmoid(rbm.W * xneg + rbm.hid_bias); 53 | 54 | Delta_W = Delta_W + lr * ( x * ehp' - xneg * ehn')'; 55 | Delta_b = Delta_b + lr * (x - xneg); 56 | Delta_c = Delta_c + lr * (ehp - ehn); 57 | 58 | end 59 | 60 | rbm.W = rbm.W + Delta_W / batch_size; 61 | rbm.vis_bias = rbm.vis_bias + Delta_b / batch_size; 62 | rbm.hid_bias = rbm.hid_bias + Delta_c / batch_size; 63 | 64 | return 65 | 66 | 67 | function contrastive_divergence_rows_K(Xbatch, rbm, K, lr) 68 | 69 | batch_size = size(Xbatch,1); 70 | 71 | Delta_W = zeros(size(rbm.W)); 72 | Delta_b = zeros(size(rbm.vis_bias)); 73 | Delta_c = zeros(size(rbm.hid_bias)); 74 | 75 | for i = 1:batch_size 76 | x = Xbatch(i:i,:); 77 | xneg = Xbatch(i:i,:); 78 | 79 | for k = 1:K 80 | hneg = sigmoid( xneg * rbm.W + rbm.hid_bias') > rand(1,rbm.n_hid); 81 | xneg = sigmoid( hneg * rbm.W' + rbm.vis_bias') > rand(1,rbm.n_vis); 82 | end 83 | 84 | ehp = sigmoid(x * rbm.W + rbm.hid_bias'); 85 | ehn = sigmoid(xneg * rbm.W + rbm.hid_bias'); 86 | 87 | Delta_W = Delta_W + lr * ( ehp' * x - ehn' * xneg)'; 88 | Delta_b = Delta_b + lr * (x - xneg)'; 89 | Delta_c = Delta_c + lr * (ehp - ehn)'; 90 | end 91 | 92 | rbm.W = rbm.W + Delta_W / batch_size; 93 | rbm.vis_bias = rbm.vis_bias + Delta_b / batch_size; 94 | rbm.hid_bias = rbm.hid_bias + Delta_c / batch_size; 95 | 96 | return 97 | 98 | 99 | function rbm = initializeRBM_rows(n_vis, n_hid, sigma) 100 | rbm.W = sigma*randn(n_vis,n_hid); % weight matrix 101 | rbm.vis_bias = zeros(n_vis,1); % visible vector 102 | rbm.hid_bias = zeros(n_hid,1); % Hidden vector 103 | rbm.n_vis = n_vis; % num visible units 104 | rbm.n_hid = n_hid; % num hidden unnits 105 | return; 106 | 107 | %%%%%%%%%% 108 | -------------------------------------------------------------------------------- /speed_tests/rbm.py: -------------------------------------------------------------------------------- 1 | 2 | import numpy as np 3 | import time 4 | 5 | class RBM(object): 6 | def __init__(self, n_vis, n_hid, sigma=0.01): 7 | self.W = sigma * np.random.randn(n_vis,n_hid); 8 | self.vis_bias = np.zeros([n_vis,1]) 9 | self.hid_bias = np.zeros([n_hid,1]) 10 | self.n_vis = n_vis 11 | self.n_hid = n_hid 12 | 13 | def sigmoid(vector): 14 | return 1 / (1 + np.exp(-vector)) 15 | 16 | def contrastive_divergence_rows_K(Xbatch, rbm, K, lr): 17 | 18 | batch_size = Xbatch.shape[0] 19 | 20 | Delta_W = np.zeros(rbm.W.shape) 21 | Delta_b = np.zeros([rbm.n_vis,1]) 22 | Delta_c = np.zeros([rbm.n_hid,1]) 23 | 24 | for i in range(batch_size): 25 | x = Xbatch[i:i+1,:] 26 | xneg = Xbatch[i:i+1,:] 27 | 28 | for k in range(K): 29 | hneg = sigmoid( np.dot(xneg,rbm.W) + rbm.hid_bias.T) > np.random.rand(1,rbm.n_hid) 30 | xneg = sigmoid( np.dot(hneg,rbm.W.T) + rbm.vis_bias.T) > np.random.rand(1,rbm.n_vis) 31 | 32 | ehp = sigmoid( np.dot(x,rbm.W) + rbm.hid_bias.T) 33 | ehn = sigmoid( np.dot(xneg,rbm.W) + rbm.hid_bias.T) 34 | 35 | Delta_W += lr * ( x * ehp.T - xneg * ehn.T).T 36 | Delta_b += lr * (x - xneg).T 37 | Delta_c += lr * (ehp - ehn).T 38 | 39 | rbm.W += Delta_W / batch_size; 40 | rbm.vis_bias += Delta_b / batch_size; 41 | rbm.hid_bias += Delta_c / batch_size; 42 | 43 | return 44 | 45 | 46 | X_train_rows = np.random.rand(5000,784); 47 | X_batch_rows = X_train_rows[:200,:]; 48 | rbm = RBM(784,225) 49 | start_time = time.time() 50 | contrastive_divergence_rows_K(X_batch_rows, rbm, 1, 0.01) 51 | print("--- %s seconds ---" % (time.time() - start_time)) 52 | -------------------------------------------------------------------------------- /speed_tests/rbm2.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import time 3 | 4 | class RBM(object): 5 | def __init__(self, n_vis, n_hid, sigma=0.01): 6 | self.W = sigma * np.random.randn(n_vis,n_hid); 7 | self.vis_bias = np.zeros([n_vis,1]) 8 | self.hid_bias = np.zeros([n_hid,1]) 9 | self.n_vis = n_vis 10 | self.n_hid = n_hid 11 | 12 | def sigmoid(vector): 13 | return 1 / (1 + np.exp(-vector)) 14 | 15 | def contrastive_divergence_rows_K(Xbatch, rbm, K, lr): 16 | 17 | batch_size = Xbatch.shape[0] 18 | 19 | Delta_W = np.zeros(rbm.W.shape) 20 | Delta_b = np.zeros([rbm.n_vis,1]) 21 | Delta_c = np.zeros([rbm.n_hid,1]) 22 | 23 | for i in range(batch_size): 24 | x = Xbatch[i:i+1,:] 25 | xneg = Xbatch[i:i+1,:] 26 | 27 | for k in range(K): 28 | hneg = sigmoid( np.dot(xneg,rbm.W) + rbm.hid_bias.T) > np.random.rand(1,rbm.n_hid) 29 | xneg = sigmoid( np.dot(hneg,rbm.W.T) + rbm.vis_bias.T) > np.random.rand(1,rbm.n_vis) 30 | 31 | ehp = sigmoid( np.dot(x,rbm.W) + rbm.hid_bias.T) 32 | ehn = sigmoid( np.dot(xneg,rbm.W) + rbm.hid_bias.T) 33 | 34 | Delta_W += lr * (x.T * ehp - (xneg.T * ehn)) 35 | Delta_b += lr * (x - xneg).T 36 | Delta_c += lr * (ehp - ehn).T 37 | 38 | rbm.W += Delta_W / batch_size; 39 | rbm.vis_bias += Delta_b / batch_size; 40 | rbm.hid_bias += Delta_c / batch_size; 41 | 42 | return 43 | 44 | 45 | X_train_rows = np.random.rand(5000,784); 46 | X_batch_rows = X_train_rows[:200,:]; 47 | rbm = RBM(784,225) 48 | start_time = time.time() 49 | contrastive_divergence_rows_K(X_batch_rows, rbm, 1, 0.01) 50 | print("--- %s seconds ---" % (time.time() - start_time)) -------------------------------------------------------------------------------- /speed_tests/rbm_evizero.jl: -------------------------------------------------------------------------------- 1 | 2 | sigmoid(x::Float64) = 1. / (1. + exp(-x)) 3 | 4 | type RBM_col{T <: Real} 5 | W::Matrix{T} 6 | vis_bias::Vector{T} 7 | hid_bias::Vector{T} 8 | n_vis::Int64 9 | n_hid::Int64 10 | trained::Bool 11 | end 12 | 13 | function initializeRBM_col(n_vis::Int64, n_hid::Int64; sigma=0.01, T=Float64) 14 | 15 | return RBM_col{T}(sigma*randn(n_hid,n_vis), # weight matrix 16 | zeros(n_vis), # visible vector 17 | zeros(n_hid), # Hidden vector 18 | n_vis, # num visible units 19 | n_hid, # num hidden unnits 20 | false) # trained 21 | 22 | 23 | end 24 | 25 | function contrastive_divergence_col_K(Xbatch, rbm, K::Int64, lr::Float64) 26 | 27 | batch_size = size(Xbatch)[2] 28 | 29 | Delta_W = zeros(size(rbm.W)) 30 | Delta_b = zeros(size(rbm.vis_bias)) 31 | Delta_c = zeros(size(rbm.hid_bias)) 32 | 33 | hneg = similar(rbm.hid_bias) 34 | b1 = similar(rbm.W * Xbatch[:,1]) 35 | b2 = similar(rbm.W' * hneg) 36 | ehp = similar(rbm.hid_bias) 37 | ehn = similar(rbm.hid_bias) 38 | xneg = similar(Xbatch[:,1]) 39 | @inbounds for i in 1:batch_size 40 | x = @view Xbatch[:,i] 41 | xneg .= @view Xbatch[:,i] 42 | 43 | for k in 1:K 44 | A_mul_B!(b1, rbm.W, xneg) 45 | hneg .= sigmoid.(b1 .+ rbm.hid_bias) .> rand.() 46 | At_mul_B!(b2, rbm.W, hneg) 47 | xneg .= sigmoid.(b2 .+ rbm.vis_bias) .> rand.() 48 | end 49 | 50 | A_mul_B!(b1, rbm.W, x) 51 | ehp .= sigmoid.(b1 .+ rbm.hid_bias) 52 | A_mul_B!(b1, rbm.W, xneg) 53 | ehn .= sigmoid.(b1 .+ rbm.hid_bias) 54 | 55 | ### kron vs no kron??? 56 | Delta_W .+= lr .* (ehp .* x' .- ehn .* xneg') 57 | Delta_b .+= lr .* (x .- xneg) 58 | Delta_c .+= lr .* (ehp .- ehn) 59 | 60 | end 61 | 62 | rbm.W .+= Delta_W ./ batch_size; 63 | rbm.vis_bias .+= Delta_b ./ batch_size; 64 | rbm.hid_bias .+= Delta_c ./ batch_size; 65 | 66 | return 67 | end 68 | 69 | 70 | using BenchmarkTools 71 | 72 | X_train = rand(5000,784); 73 | X_batch_col = X_train'[:,1:200]; 74 | rbm = initializeRBM_col(784, 225) 75 | 76 | print("\n This RBM took\n\t") 77 | print(@time contrastive_divergence_col_K(X_batch_col, rbm, 1, 0.01)) 78 | 79 | print("\n This RBM benchmark\n\t") 80 | print(@benchmark contrastive_divergence_col_K(X_batch_col, rbm, 1, 0.01)) 81 | print("\n\n") 82 | -------------------------------------------------------------------------------- /speed_tests/rbm_opt.jl: -------------------------------------------------------------------------------- 1 | 2 | function sigmoid(vector::Array{Float64}) 3 | return 1./(1 + e.^(-vector)) 4 | end 5 | 6 | type RBM_col{T <: Real} 7 | W::Matrix{T} 8 | vis_bias::Vector{T} 9 | hid_bias::Vector{T} 10 | n_vis::Int64 11 | n_hid::Int64 12 | trained::Bool 13 | end 14 | 15 | function initializeRBM_col(n_vis::Int64, n_hid::Int64; sigma=0.01, T=Float64) 16 | 17 | return RBM_col{T}(sigma*randn(n_hid,n_vis), # weight matrix 18 | zeros(n_vis), # visible vector 19 | zeros(n_hid), # Hidden vector 20 | n_vis, # num visible units 21 | n_hid, # num hidden unnits 22 | false) # trained 23 | end 24 | 25 | function contrastive_divergence_col_K(Xbatch, rbm, K::Int64, lr::Float64) 26 | 27 | batch_size = size(Xbatch)[2] 28 | 29 | Delta_W = zeros(size(rbm.W)) 30 | Delta_b = zeros(size(rbm.vis_bias)) 31 | Delta_c = zeros(size(rbm.hid_bias)) 32 | 33 | hneg = similar(rbm.hid_bias) 34 | b1 = similar(rbm.W * Xbatch[:, 1]) 35 | b2 = similar(rbm.W' * hneg) 36 | ehp = similar(rbm.hid_bias) 37 | ehn = similar(rbm.hid_bias) 38 | xneg = similar(Xbatch[:, 1]) 39 | 40 | @inbounds for i in 1:batch_size 41 | x = @view Xbatch[:,i] 42 | xneg = @view Xbatch[:,i] 43 | 44 | for k in 1:K 45 | hneg = sigmoid( rbm.W * xneg .+ rbm.hid_bias) .> rand(rbm.n_hid) 46 | xneg = sigmoid( rbm.W' * hneg .+ rbm.vis_bias) .> rand(rbm.n_vis) 47 | end 48 | 49 | ehp = sigmoid(rbm.W * x + rbm.hid_bias) 50 | ehn = sigmoid(rbm.W * xneg + rbm.hid_bias) 51 | 52 | Delta_W .+= lr .* ( x * ehp' .- xneg * ehn')' 53 | Delta_b .+= lr .* (x .- xneg) 54 | Delta_c .+= lr .* (ehp .- ehn) 55 | 56 | end 57 | 58 | rbm.W .+= Delta_W ./ batch_size; 59 | rbm.vis_bias .+= Delta_b ./ batch_size; 60 | rbm.hid_bias .+= Delta_c ./ batch_size; 61 | 62 | return 63 | end 64 | 65 | using BenchmarkTools 66 | 67 | X_train = rand(5000,784); 68 | X_batch_col = X_train'[:,1:200]; 69 | rbm = initializeRBM_col(784, 225) 70 | print("\n This RBM used scalar product\n\t") 71 | 72 | # warmup 73 | contrastive_divergence_col_K(X_batch_col[:,1:10], rbm, 1, 0.01) 74 | 75 | print(@time contrastive_divergence_col_K(X_batch_col, rbm, 1, 0.01)) 76 | print("\n") 77 | print(@benchmark contrastive_divergence_col_K(X_batch_col, rbm, 1, 0.01)) 78 | print("\n") 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /urls.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | https://medium.com/@Jernfrost/go-inspired-tour-of-the-julia-programming-language-5c5a1622f177 4 | 5 | 6 | https://eli.thegreenplace.net/2015/memory-layout-of-multi-dimensional-arrays 7 | --------------------------------------------------------------------------------