Koç University deep learning framework.

Overview

Knet

Knet (pronounced "kay-net") is the Koç University deep learning framework implemented in Julia by Deniz Yuret and collaborators. It supports GPU operation and automatic differentiation using dynamic computational graphs for models defined in plain Julia. You can install Knet with the following at the julia prompt: using Pkg; Pkg.add("Knet"). Some starting points:

  • Tutorial: introduces Julia and Knet via examples.
  • Documentation: installation, introduction, design, implementation, full reference and deep learning chapters.
  • Examples: more tutorials and example models.
  • Benchmarks: comparison of Knet's speed with TensorFlow, PyTorch, DyNet etc.
  • Paper: Yuret, D. "Knet: beginning deep learning with 100 lines of julia." In Machine Learning Systems Workshop at NIPS 2016.
  • KnetML: github organization with Knet repos of models, tutorials, layer collections and other resources.
  • Images: Knet machine images are available for AWS, Singularity and Docker.
  • Issues: if you find a bug, please open a github issue.
  • knet-users: if you need help or would like to request a feature, please join this mailing list.
  • knet-dev: if you would like to contribute to Knet development, please join this mailing list and check out these tips.
  • knet-slack: Slack channel for Knet.
  • Related work: Please check out Flux, Mocha, JuliaML, JuliaDiff, JuliaGPU, JuliaOpt for related packages.

Example

Here is a simple example where we define, train and test the LeNet model for the MNIST handwritten digit recognition dataset from scratch using 15 lines of code and 10 seconds of GPU computation.

# Install packages before first run: using Pkg; pkg"add Knet IterTools MLDatasets"
using Knet, IterTools, MLDatasets

# Define convolutional layer:
struct Conv; w; b; end
Conv(w1,w2,nx,ny) = Conv(param(w1,w2,nx,ny), param0(1,1,ny,1))
(c::Conv)(x) = relu.(pool(conv4(c.w, x) .+ c.b))

# Define dense layer:
struct Dense; w; b; f; end
Dense(i,o; f=identity) = Dense(param(o,i), param0(o), f)
(d::Dense)(x) = d.f.(d.w * mat(x) .+ d.b)

# Define a chain of layers and a loss function:
struct Chain; layers; end
(c::Chain)(x) = (for l in c.layers; x = l(x); end; x)
(c::Chain)(x,y) = nll(c(x),y)

# Load MNIST data:
xtrn,ytrn = MNIST.traindata(Float32); ytrn[ytrn.==0] .= 10
xtst,ytst = MNIST.testdata(Float32);  ytst[ytst.==0] .= 10
dtrn = minibatch(xtrn, ytrn, 100; xsize = (28,28,1,:))
dtst = minibatch(xtst, ytst, 100; xsize = (28,28,1,:))

# Define and train LeNet (~10 secs on a GPU or ~3 mins on a CPU to reach ~99% accuracy)
LeNet = Chain((Conv(5,5,1,20), Conv(5,5,20,50), Dense(800,500,f=relu), Dense(500,10)))
progress!(adam(LeNet, ncycle(dtrn,3)))
accuracy(LeNet,data=dtst)

Contributing

Knet is an open-source project and we are always open to new contributions: bug reports and fixes, feature requests and contributions, new machine learning models and operators, inspiring examples, benchmarking results are all welcome. See Tips for Developers for instructions.

Contributors: Can Gümeli, Carlo Lucibello, Ege Onat, Ekin Akyürek, Ekrem Emre Yurdakul, Emre Ünal, Emre Yolcu, Enis Berk, Erenay Dayanık, İlker Kesen, Kai Xu, Meriç Melike Softa, Mike Innes, Onur Kuru, Ozan Arkan Can, Ömer Kırnap, Phuoc Nguyen, Rene Donner, Tim Besard, Zhang Shiwei.

Comments
  • using CuArrays

    using CuArrays

    All tests now pass, save for the JLD code which we need to replace.

    Tests fail on multi-GPU setups, ~~and segfault at the end due to a stream destructor~~. I think we need to rework all the handles and runtime stuff to do things the CuArrays way, e.g. through CUDAdrv. I'm not sure what our multi-gpu or stream story is in terms of cublas handles etc though.

    In any case we mainly want to do benchmarking to begin with, and this should be ready enough for that.

    opened by MikeInnes 38
  • Feature request: more KnetArray indexing

    Feature request: more KnetArray indexing

    The following would be useful:

    getindex(::Knet.KnetArray{Float32,3}, ::Colon, ::Int64, ::Int64)
    getindex(::Knet.KnetArray{Float32,3}, ::Colon, ::Array{Int64,1}, ::Int64)
    getindex(::Knet.KnetArray{Float32,3}, ::Colon, ::Array{Int64,1},  ::Array{Int64,1})
    
    enhancement 0.8.6 indexing KnetArray project 
    opened by ngphuoc 33
  • TopNode not defined

    TopNode not defined

    julia> using Knet WARNING: Base.SparseMatrix is deprecated. likely near /home/rluser/.julia/v0.5/CUSPARSE/src/CUSPARSE.jl:2 WARNING: Method definition (::Type{Knet._CudaArray})(CUDArt.CudaArray{#T<:Any, #N<:Any}) in module Knet at /home/rluser/.julia/v0.5/Knet/src/util/cudart.jl:127 overwritten at /home/rluser/.julia/v0.5/Knet/src/util/cudart.jl:128. WARNING: Base.writemime is deprecated. likely near /home/rluser/.julia/v0.5/Knet/src/util/cudart.jl:133 WARNING: Base.writemime is deprecated. likely near /home/rluser/.julia/v0.5/Knet/src/util/cudart.jl:133 WARNING: Base.LambdaStaticData is deprecated, use LambdaInfo instead. likely near /home/rluser/.julia/v0.5/Knet/src/util/deepcopy.jl:21 ERROR: LoadError: LoadError: UndefVarError: TopNode not defined in include_from_node1(::String) at ./loading.jl:426 (repeats 2 times) in eval(::Module, ::Any) at ./boot.jl:234 in require(::Symbol) at ./loading.jl:357 while loading /home/rluser/.julia/v0.5/Knet/src/util/deepcopy.jl, in expression starting on line 21 while loading /home/rluser/.julia/v0.5/Knet/src/Knet.jl, in expression starting on line 17

    opened by AStupidBear 28
  • abnormal KnetArray error

    abnormal KnetArray error

    After using Pkg.update() yesterday, Knet.jl no longer works anymore for KnetArray. Pkg.test("Knet") failed, and

    julia> a = KnetArray(ones(10));                
                                                   
    julia> a + a                                    
    10-element Knet.KnetArray{Float64,1}:          
     0.00707721                                    
     0.000453814                                   
     0.000180473                                   
     0.0                                           
     0.0                                           
     0.0                                           
     0.0                                           
     0.0                                           
     0.0                                           
     0.0                                           
    

    My configurations is:
    Ubuntu 14.04 Julia 0.5.0 AutoGrad.jl master Knet.jl master

    If you don't have this issue, then it may be a coincidence that my gpu crashed at the same time of updating.

    opened by AStupidBear 25
  • gamma, lgamma, digamma work with GPU

    gamma, lgamma, digamma work with GPU

    Related issues: https://github.com/denizyuret/Knet.jl/issues/290 https://github.com/denizyuret/Knet.jl/issues/292

    Related PR: https://github.com/denizyuret/Knet.jl/pull/291

    enhancement 
    opened by xukai92 23
  • Julia 0.7 compatibility

    Julia 0.7 compatibility

    What is the situation of porting Knet to Julia 0.7? Is there work going on but not visible in this repository? Are there important dependencies that need to be ported first before it's worth trying Knet itself? I'm willing to help out if there's something an outsider can do.

    opened by GunnarFarneback 22
  • Knet's build assumes Julia is accessed in the path by `julia`

    Knet's build assumes Julia is accessed in the path by `julia`

    Knet was working, but then I upgraded to v0.6, then re-installed v0.5.2 as just the generic binary (my packages were wiped though). When re-adding Knet, I get the following:

    INFO: Building Knet
    INFO: Compiling CUDA kernels.
    nvcc -c -O3 --use_fast_math -Wno-deprecated-gpu-targets -arch=compute_30 -code=sm_30 --compiler-options "-O3 -Wall -fPIC -fopenmp" cuda1.cu -o cuda1.o
    cuda1.cu(1117): error: extra text after expected end of number
    
    cuda1.cu(1117): error: attribute "global" does not apply here
    
    cuda1.cu(1117): error: incomplete type is not allowed
    
    cuda1.cu(1117): error: expected a ";"
    
    cuda1.cu(1130): error: extra text after expected end of number
    
    cuda1.cu(1131): error: extra text after expected end of number
    
    cuda1.cu(1134): error: extra text after expected end of number
    
    cuda1.cu(1147): error: extra text after expected end of number
    
    cuda1.cu(1148): error: extra text after expected end of number
    
    cuda1.cu(1151): error: extra text after expected end of number
    
    cuda1.cu(1164): error: extra text after expected end of number
    
    cuda1.cu(1165): error: extra text after expected end of number
    
    cuda1.cu(1168): error: extra text after expected end of number
    
    cuda1.cu(1181): error: extra text after expected end of number
    
    cuda1.cu(1182): error: extra text after expected end of number
    
    cuda1.cu(1185): error: extra text after expected end of number
    
    cuda1.cu(1185): error: extra text after expected end of number
    
    cuda1.cu(1199): error: extra text after expected end of number
    
    cuda1.cu(1199): error: extra text after expected end of number
    
    cuda1.cu(1200): error: extra text after expected end of number
    
    cuda1.cu(1200): error: extra text after expected end of number
    
    cuda1.cu(1203): error: extra text after expected end of number
    
    cuda1.cu(1203): error: extra text after expected end of number
    
    cuda1.cu(1217): error: extra text after expected end of number
    
    cuda1.cu(1217): error: extra text after expected end of number
    
    cuda1.cu(1218): error: extra text after expected end of number
    
    cuda1.cu(1218): error: extra text after expected end of number
    
    cuda1.cu(1221): error: extra text after expected end of number
    
    cuda1.cu(1221): error: extra text after expected end of number
    
    cuda1.cu(1235): error: extra text after expected end of number
    
    cuda1.cu(1235): error: extra text after expected end of number
    
    cuda1.cu(1236): error: extra text after expected end of number
    
    cuda1.cu(1236): error: extra text after expected end of number
    
    cuda1.cu(1239): error: extra text after expected end of number
    
    cuda1.cu(1239): error: extra text after expected end of number
    
    cuda1.cu(1253): error: extra text after expected end of number
    
    cuda1.cu(1253): error: extra text after expected end of number
    
    cuda1.cu(1254): error: extra text after expected end of number
    
    cuda1.cu(1254): error: extra text after expected end of number
    
    cuda1.cu(1257): error: extra text after expected end of number
    
    cuda1.cu(1257): error: extra text after expected end of number
    
    cuda1.cu(1271): error: extra text after expected end of number
    
    cuda1.cu(1271): error: extra text after expected end of number
    
    cuda1.cu(1272): error: extra text after expected end of number
    
    cuda1.cu(1272): error: extra text after expected end of number
    
    cuda1.cu(1275): error: extra text after expected end of number
    
    cuda1.cu(1275): error: extra text after expected end of number
    
    cuda1.cu(1289): error: extra text after expected end of number
    
    cuda1.cu(1289): error: extra text after expected end of number
    
    cuda1.cu(1290): error: extra text after expected end of number
    
    cuda1.cu(1290): error: extra text after expected end of number
    
    cuda1.cu(1293): error: extra text after expected end of number
    
    cuda1.cu(1293): error: extra text after expected end of number
    
    cuda1.cu(1307): error: extra text after expected end of number
    
    cuda1.cu(1307): error: extra text after expected end of number
    
    cuda1.cu(1308): error: extra text after expected end of number
    
    cuda1.cu(1308): error: extra text after expected end of number
    
    cuda1.cu(1311): error: extra text after expected end of number
    
    cuda1.cu(1311): error: extra text after expected end of number
    
    cuda1.cu(1325): error: extra text after expected end of number
    
    cuda1.cu(1325): error: extra text after expected end of number
    
    cuda1.cu(1326): error: extra text after expected end of number
    
    cuda1.cu(1326): error: extra text after expected end of number
    
    cuda1.cu(1329): error: extra text after expected end of number
    
    cuda1.cu(1329): error: extra text after expected end of number
    
    cuda1.cu(1343): error: extra text after expected end of number
    
    cuda1.cu(1343): error: extra text after expected end of number
    
    cuda1.cu(1344): error: extra text after expected end of number
    
    cuda1.cu(1344): error: extra text after expected end of number
    
    cuda1.cu(1347): error: extra text after expected end of number
    
    cuda1.cu(1347): error: extra text after expected end of number
    
    cuda1.cu(1361): error: extra text after expected end of number
    
    cuda1.cu(1361): error: extra text after expected end of number
    
    cuda1.cu(1362): error: extra text after expected end of number
    
    cuda1.cu(1362): error: extra text after expected end of number
    
    cuda1.cu(1365): error: extra text after expected end of number
    
    cuda1.cu(1365): error: extra text after expected end of number
    
    cuda1.cu(1379): error: extra text after expected end of number
    
    cuda1.cu(1379): error: extra text after expected end of number
    
    cuda1.cu(1380): error: extra text after expected end of number
    
    cuda1.cu(1380): error: extra text after expected end of number
    
    cuda1.cu(1383): error: extra text after expected end of number
    
    cuda1.cu(1383): error: extra text after expected end of number
    
    cuda1.cu(1397): error: extra text after expected end of number
    
    cuda1.cu(1397): error: extra text after expected end of number
    
    cuda1.cu(1398): error: extra text after expected end of number
    
    cuda1.cu(1398): error: extra text after expected end of number
    
    cuda1.cu(1401): error: extra text after expected end of number
    
    cuda1.cu(1401): error: extra text after expected end of number
    
    cuda1.cu(1401): error: extra text after expected end of number
    
    cuda1.cu(1416): error: extra text after expected end of number
    
    cuda1.cu(1416): error: extra text after expected end of number
    
    cuda1.cu(1416): error: extra text after expected end of number
    
    cuda1.cu(1417): error: extra text after expected end of number
    
    cuda1.cu(1417): error: extra text after expected end of number
    
    cuda1.cu(1417): error: extra text after expected end of number
    
    cuda1.cu(1420): error: extra text after expected end of number
    
    cuda1.cu(1420): error: extra text after expected end of number
    
    cuda1.cu(1420): error: extra text after expected end of number
    
    cuda1.cu(1435): error: extra text after expected end of number
    
    Error limit reached.
    100 errors detected in the compilation of "/tmp/tmpxft_00005ebc_00000000-7_cuda1.cpp1.ii".
    Compilation terminated.
    make: *** [cuda1.o] Error 4
    ================================[ ERROR: Knet ]=================================
    
    LoadError: failed process: Process(`make`, ProcessExited(2)) [2]
    while loading /home/crackauc/.julia/v0.5/Knet/deps/build.jl, in expression starting on line 6
    
    ================================================================================
    
    ================================[ BUILD ERRORS ]================================
    
    WARNING: Knet had build errors.
    
     - packages with build errors remain installed in /home/crackauc/.julia/v0.5
     - build the package(s) and all dependencies with `Pkg.build("Knet")`
     - build a single package by running its `deps/build.jl` script
    
    ================================================================================
    
    opened by ChrisRackauckas 21
  • Using conv and pool without CUDA

    Using conv and pool without CUDA

    Just wondering, but is there a way to use conv and pool without a GPU? I'm running a windows machine and even though I have a nvidia card installed, I failed to install CUDA. If any of you have tips on how to get this working that would be appreciated.

    Thanks!

    speed KnetArray project 
    opened by niczky12 20
  • error in gcnode (KeyError: key ... not found)

    error in gcnode (KeyError: key ... not found)

    I am seeing this error below in Knet (presumable Autograd garbage collectors) about 1 in 4 times. In my case I use the python module skopt to optimize some hyperparameter (called with PyCall). In the same Julia program, I run thus multiple training loops sequentially (with different numbers of layers, L2 regularization,...). It works for 3 iterations but at the 4th iteration I get this error during the 1st epoch.

    In a different case, where I use the same code but slightly more input features this issue does not show up.

    This error appears in about 6 to 7 hours of computing (which does not help to create a minimal reproducer).

    I call these commands after every time a neural network is trained and validated.

    GC.gc()
    

    Are there any ideas what I could try to make the training more stable?

    Thank you for your hard work on Knet which is very helpful in my work! On a different computer (with only 8 GB GPU RAM) , Knet runs rock solid for more than 10 days (and is still running)!

    ERROR: LoadError: (in a Julia function called from Python)
    JULIA: KeyError: key 0x1818ad5e70b5b27c not found
    Stacktrace:
     [1] getindex at ./dict.jl:467 [inlined]
     [2] gcnode(::AutoGrad.Node, ::AutoGrad.Tape) at /home/ulg/gher/abarth/.julia/packages/Knet/rgT4R/src/autograd_gpu/gcnode.jl:79
     [3] differentiate(::Function; o::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /home/ulg/gher/abarth/.julia/packages/AutoGrad/VFrAv/src/co
    re.jl:168
     [4] differentiate at /home/ulg/gher/abarth/.julia/packages/AutoGrad/VFrAv/src/core.jl:135 [inlined]
     [5] iterate at /home/ulg/gher/abarth/.julia/packages/Knet/rgT4R/src/train20/train.jl:26 [inlined]
     [6] iterate(::Base.Iterators.Enumerate{Knet.Train20.Minimize{DINCAE.PointCloud{KnetArray{Float32,N} where N,Float32,2}}}, ::Tuple{Int64,Int64}) at ./iterators.jl:139
     [7] macro expansion at /home/ulg/gher/abarth/.julia/dev/DINCAE/src/points.jl:574 [inlined]
     [8] macro expansion at ./timing.jl:174 [inlined]
     [9] reconstruct_points(::Type{T} where T, ::Type{T} where T, ::String, ::String, ::Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float6
    4}},StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}, ::Array{String,1}; epochs::Int64, batch_size::Int64, truth_uncertain::Bool, enc_
    nfilter_internal::StepRange{Int64,Int64}, skipconnections::UnitRange{Int64}, clip_grad::Float64, regularization_L1_beta::Int64, regularization_L2_beta::Float64, save_
    epochs::Array{Int64,1}, upsampling_method::Symbol, probability_skip_for_training::Float64, jitter_std_pos::Tuple{Float32,Float32}, ntime_win::Int64, learning_rate::Fl
    oat64, learning_rate_decay_epoch::Float64, loss_weights_refine::Tuple{Float64}, auxdata_files::Array{Any,1}, savesnapshot::Bool) at /home/ulg/gher/abarth/.julia/dev/D
    INCAE/src/points.jl:563
     [10] func(::Array{Any,1}) at /home/users/a/b/abarth/.julia/dev/DINCAE/examples/DINCAE_altimetry_optim.jl:142
     [11] #invokelatest#1 at ./essentials.jl:710 [inlined]
     [12] invokelatest(::Any, ::Any) at ./essentials.jl:709
     [13] _pyjlwrap_call(::Function, ::Ptr{PyCall.PyObject_struct}, ::Ptr{PyCall.PyObject_struct}) at /home/ulg/gher/abarth/.julia/packages/PyCall/BcTLp/src/callback.jl:28
     [14] pyjlwrap_call(::Ptr{PyCall.PyObject_struct}, ::Ptr{PyCall.PyObject_struct}, ::Ptr{PyCall.PyObject_struct}) at /home/ulg/gher/abarth/.julia/packages/PyCall/BcTLp/src/callback.jl:49
     [15] macro expansion at /home/ulg/gher/abarth/.julia/packages/PyCall/BcTLp/src/exception.jl:95 [inlined]
     [16] #110 at /home/ulg/gher/abarth/.julia/packages/PyCall/BcTLp/src/pyfncall.jl:43 [inlined]
     [17] disable_sigint at ./c.jl:446 [inlined]
     [18] __pycall! at /home/ulg/gher/abarth/.julia/packages/PyCall/BcTLp/src/pyfncall.jl:42 [inlined]
     [19] _pycall!(::PyObject, ::PyObject, ::Tuple{typeof(func),Array{PyObject,1}}, ::Int64, ::PyObject) at /home/ulg/gher/abarth/.julia/packages/PyCall/BcTLp/src/pyfncall.jl:29
     [20] _pycall!(::PyObject, ::PyObject, ::Tuple{typeof(func),Array{PyObject,1}}, ::Base.Iterators.Pairs{Symbol,Any,Tuple{Symbol,Symbol},NamedTuple{(:n_calls, :x0),Tuple{Int64,PyVector{Any}}}}) at /home/ulg/gher/abarth/.julia/packages/PyCall/BcTLp/src/pyfncall.jl:11
     [21] (::PyObject)(::Function, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Symbol,Any,Tuple{Symbol,Symbol},NamedTuple{(:n_calls, :x0),Tuple{Int64,PyVector{Any}}}}) at /home/ulg/gher/abarth/.julia/packages/PyCall/BcTLp/src/pyfncall.jl:86
     [22] top-level scope at /home/users/a/b/abarth/.julia/dev/DINCAE/examples/DINCAE_altimetry_optim.jl:253
     [23] include(::Function, ::Module, ::String) at ./Base.jl:380
     [24] include(::Module, ::String) at ./Base.jl:368
     [25] exec_options(::Base.JLOptions) at ./client.jl:296
    

    These are the versions that I use:

    Julia: 1.5.2 Knet v1.4.2 CUDA v1.3.3 (with CUDA: 11.0) AutoGrad v1.2.3 GPU: Tesla V100-PCIE OS: Linux

    opened by Alexander-Barth 18
  • Seg fault after upgrading to Julia 1.5

    Seg fault after upgrading to Julia 1.5

    Code was working fine until Julia 1.5. Looks like there's something not right with autograd and unpool? Stack trace below:

    Illegal inttoptr
    	  %12 = ptrtoint %jl_value_t addrspace(10)* addrspace(13)* %32 to i64, !dbg !28
    Illegal inttoptr
    	  %13 = inttoptr i64 %12 to %jl_value_t addrspace(10)*, !dbg !28
    
    signal (6): Aborted
    in expression starting at xxx
    gsignal at /usr/bin/../lib/libc.so.6 (unknown line)
    abort at /usr/bin/../lib/libc.so.6 (unknown line)
    unknown function (ip: 0x7f4930c412c9)
    _ZN4llvm13FPPassManager13runOnFunctionERNS_8FunctionE at /usr/bin/../lib/libLLVM-10.so (unknown line)
    _ZN4llvm13FPPassManager11runOnModuleERNS_6ModuleE at /usr/bin/../lib/libLLVM-10.so (unknown line)
    _ZN4llvm6legacy15PassManagerImpl3runERNS_6ModuleE at /usr/bin/../lib/libLLVM-10.so (unknown line)
    unknown function (ip: 0x7f4930d480fb)
    unknown function (ip: 0x7f4930d4affb)
    unknown function (ip: 0x7f4930d4ccf8)
    unknown function (ip: 0x7f4930d4d9e2)
    unknown function (ip: 0x7f4930d4eb88)
    unknown function (ip: 0x7f4930ccf77f)
    jl_invoke at /usr/bin/../lib/libjulia.so.1 (unknown line)
    forw##kw at /home/xxx/.julia/packages/AutoGrad/VFrAv/src/core.jl:65 [inlined]
    #unpool#440 at ./none:0
    unknown function (ip: 0x7f48da0ea425)
    unpool##kw at ./none:0
    unknown function (ip: 0x7f48da0ea205)
    Sampling at /home/xxx/.julia/packages/KnetLayers/zfhNR/src/cnn.jl:12
    applychain at /home/xxx/.julia/packages/KnetLayers/zfhNR/src/chain.jl:25
    Chain at /home/andevellicus/.julia/packages/KnetLayers/zfhNR/src/chain.jl:27
    unknown function (ip: 0x7f48da0e9fff)
    
    #2 at /home/xxx/.julia/packages/AutoGrad/VFrAv/src/core.jl:205
    unknown function (ip: 0x7f48da0c032c)
    #differentiate#3 at /home/xxx/.julia/packages/AutoGrad/VFrAv/src/core.jl:144
    differentiate at /home/xxx/.julia/packages/AutoGrad/VFrAv/src/core.jl:135 [inlined]
    
    unknown function (ip: 0x7f48da0bea0c)
    
    unknown function (ip: 0x7f48da76ad0c)
    unknown function (ip: 0x7f4930ce65c5)
    unknown function (ip: 0x7f4930ce624e)
    unknown function (ip: 0x7f4930ce6d90)
    unknown function (ip: 0x7f4930ce7840)
    unknown function (ip: 0x7f4930d03bd1)
    unknown function (ip: 0x7f4930cd9bc2)
    jl_load_rewrite at /usr/bin/../lib/libjulia.so.1 (unknown line)
    unknown function (ip: 0x7f492044db98)
    unknown function (ip: 0x7f492044d652)
    unknown function (ip: 0x7f492002e339)
    unknown function (ip: 0x7f492003b69e)
    unknown function (ip: 0x7f492003b7f5)
    unknown function (ip: 0x55bab43ab4fe)
    unknown function (ip: 0x55bab43ab0a7)
    __libc_start_main at /usr/bin/../lib/libc.so.6 (unknown line)
    unknown function (ip: 0x55bab43ab15d)
    Allocations: 122169744 (Pool: 122133980; Big: 35764); GC: 291
    
    opened by andevellicus 18
  • fix logp for karray

    fix logp for karray

    Previous version was focused on matrices, now it is generally improved both for matrices and tensors. This is done exploiting the mode=1 (cumulating only on the channel dimension) of the cuda softmax call.

    # PRE
    
    julia> k = KnetArray(rand(10,100)) # 2d tensor
    
    julia> @btime logp($k,1);
      4.111 μs (23 allocations: 1.08 KiB)
    
    julia> @btime logp($k,2);
      9.929 μs (39 allocations: 1.42 KiB)
    
    
    julia> k = KnetArray(rand(Float32,10,10,100)); # 3d tensor 
    
    julia> @btime logp($k,1);
      4.195 μs (21 allocations: 1.00 KiB)
    
    julia> @btime logp($k,2);
    ERROR: Transpose is supported only for 2D KnetArrays
    Stacktrace:
     [1] transpose(::Knet.KnetArray{Float32,3}) at /home/carlo/Git/Knet.jl/src/linalg.jl:89
     [2] logp(::Knet.KnetArray{Float32,3}, ::Int64, ::Vararg{Int64,N} where N) at /home/carlo/Git/Knet.jl/src/loss.jl:19
     [3] ##core#1420(::Knet.KnetArray{Float32,3}) at /home/carlo/.julia/v0.6/BenchmarkTools/src/execution.jl:312
     [4] ##sample#1421(::BenchmarkTools.Parameters) at /home/carlo/.julia/v0.6/BenchmarkTools/src/execution.jl:318
     [5] #_run#7(::Bool, ::String, ::Array{Any,1}, ::Function, ::BenchmarkTools.Benchmark{Symbol("##benchmark#1419")}, ::BenchmarkTools.Parameters) at /home/carlo/.julia/v0.6/BenchmarkTools/src/execution.jl:346
     [6] (::BenchmarkTools.#kw##_run)(::Array{Any,1}, ::BenchmarkTools.#_run, ::BenchmarkTools.Benchmark{Symbol("##benchmark#1419")}, ::BenchmarkTools.Parameters) at ./<missing>:0
     [7] anonymous at ./<missing>:?
     [8] #run_result#19(::Array{Any,1}, ::Function, ::BenchmarkTools.Benchmark{Symbol("##benchmark#1419")}, ::BenchmarkTools.Parameters) at /home/carlo/.julia/v0.6/BenchmarkTools/src/execution.jl:40
     [9] (::BenchmarkTools.#kw##run_result)(::Array{Any,1}, ::BenchmarkTools.#run_result, ::BenchmarkTools.Benchmark{Symbol("##benchmark#1419")}, ::BenchmarkTools.Parameters) at ./<missing>:0
     [10] #run#21(::Array{Any,1}, ::Function, ::BenchmarkTools.Benchmark{Symbol("##benchmark#1419")}, ::BenchmarkTools.Parameters) at /home/carlo/.julia/v0.6/BenchmarkTools/src/execution.jl:63
     [11] (::Base.#kw##run)(::Array{Any,1}, ::Base.#run, ::BenchmarkTools.Benchmark{Symbol("##benchmark#1419")}, ::BenchmarkTools.Parameters) at ./<missing>:0
     [12] warmup(::BenchmarkTools.Benchmark{Symbol("##benchmark#1419")}) at /home/carlo/.julia/v0.6/BenchmarkTools/src/execution.jl:96
     [13] macro expansion at ./REPL.jl:97 [inlined]
     [14] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73
    
    julia> @btime logp($k,3);
      20.335 μs (78 allocations: 2.69 KiB)
    
    # AFTER
    
    julia> k = KnetArray(rand(10,100)) # 2d tensor
    
    julia> @btime logp($k,1);
      3.997 μs (24 allocations: 1.13 KiB)
    
    julia> @btime logp($k,2);
      3.987 μs (24 allocations: 1.13 KiB)
    
    julia> k = KnetArray(rand(Float32,10,10,100)); # 3d tensor 
    
    julia> @btime logp($k,1);
      4.018 μs (25 allocations: 1.17 KiB)
    
    julia> @btime logp($k,2);
      4.037 μs (25 allocations: 1.16 KiB)
    
    julia> @btime logp($k,3);
      4.013 μs (24 allocations: 1.16 KiB)
    
    enhancement 
    opened by CarloLucibello 16
  • how to use the resnet modules in example folder

    how to use the resnet modules in example folder

    I am confused by how to use the resent modules on new data.

    From the annotation of the "resnet.jl", mode=0 means train on new data. However, optimization steps are not included in the module. No optimization steps are includes in the "resnetlib.jl" as well.

    An explanatory pipeline is much appreciated. Many thank!

    opened by ccshao 0
  • Problem with conv4 on gpu

    Problem with conv4 on gpu

    Dear Deniz,

    We are facing a strange problem with conv4 on gpu. The code

    > using Knet
    > x = rand(Float32, 224,224,3,4) |> CuArray
    > w = param(5,5,3,8)
    > conv4(w,x)
    
    

    generates the error:

    MethodError: no method matching similar(::CuArray{Float32, 4, CUDA.Mem.DeviceBuffer}, ::Missing)
    
    Stacktrace:
      [1] conv4_algo(w::CuArray{Float32, 4, CUDA.Mem.DeviceBuffer}, x::CuArray{Float32, 4, CUDA.Mem.DeviceBuffer}, y::CuArray{Float32, 4, CUDA.Mem.DeviceBuffer}; handle::Ptr{Nothing}, o::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
        @ Knet.Ops20_gpu ~/.julia/packages/Knet/YIFWC/src/ops20_gpu/conv.jl:166
      [2] conv4(w::CuArray{Float32, 4, CUDA.Mem.DeviceBuffer}, x::CuArray{Float32, 4, CUDA.Mem.DeviceBuffer}; handle::Ptr{Nothing}, alpha::Int64, o::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
        @ Knet.Ops20_gpu ~/.julia/packages/Knet/YIFWC/src/ops20_gpu/conv.jl:9
      [3] conv4(w::CuArray{Float32, 4, CUDA.Mem.DeviceBuffer}, x::CuArray{Float32, 4, CUDA.Mem.DeviceBuffer})
        @ Knet.Ops20_gpu ~/.julia/packages/Knet/YIFWC/src/ops20_gpu/conv.jl:7
      [4] forw(::Function, ::Param{CuArray{Float32, 4, CUDA.Mem.DeviceBuffer}}, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
        @ AutoGrad ~/.julia/packages/AutoGrad/1QZxP/src/core.jl:66
      [5] forw
        @ ~/.julia/packages/AutoGrad/1QZxP/src/core.jl:65 [inlined]
      [6] #conv4#28
        @ ./none:0 [inlined]
      [7] conv4(w::Param{CuArray{Float32, 4, CUDA.Mem.DeviceBuffer}}, x::CuArray{Float32, 4, CUDA.Mem.DeviceBuffer})
        @ Knet.Ops20 ./none:0
    

    Strange, because this happens on our Nvidia-Server but NOT on my local computer with identical installations of Julia and Knet. Only difference is the Cuda-driver, which is

    Driver Version: 460.91.03 CUDA Version: 11.2

    on the nvidia-machine (with error) and Driver Version: 510.73.05 CUDA Version: 11.6 on my computer. Unfortunately it is not so easy to change the driver on the server, because of people are using a multitude of frameworks there. Maybe (hopefully) you have an idea...

    cordially (a)do

    opened by andreasdominik 4
  • Fix deprecations from MLDatasets in tutorial notebooks

    Fix deprecations from MLDatasets in tutorial notebooks

    https://github.com/JuliaML/MLDatasets.jl/issues/120

    From [email protected], data should be accessed as

    trainset = MNIST(:train)
    

    instead of

    xtrn,ytrn = MNIST.traindata(Float32)
    
    opened by islent 0
  • Cannot locate artifact 'libknet8' for aarch64-linux-gnu-libgfortran5-cxx11-libstdcxx29-julia_version+1.7.1 in Docker Container on Apple Silicon

    Cannot locate artifact 'libknet8' for aarch64-linux-gnu-libgfortran5-cxx11-libstdcxx29-julia_version+1.7.1 in Docker Container on Apple Silicon

    Hello, I am trying to get Knet working in a docker container on my apple silicon Mac. however the following problem occurred

    (@v1.7) pkg> add Knet
      Installing known registries into `~/.julia`
        Updating registry at `~/.julia/registries/General.toml`
       Resolving package versions...
       Installed IrrationalConstants ─ v0.1.1
       Installed JpegTurbo_jll ─────── v2.1.0+0
       Installed Adapt ─────────────── v3.3.2
       Installed ColorTypes ────────── v0.11.0
       Installed Preferences ───────── v1.2.3
       Installed OffsetArrays ──────── v1.10.8
       Installed JLD2 ──────────────── v0.4.17
       Installed ImageCore ─────────── v0.9.3
       Installed Zstd_jll ──────────── v1.5.0+0
       Installed Libtiff_jll ───────── v4.3.0+0
       Installed FixedPointNumbers ─── v0.8.4
       Installed TensorCore ────────── v0.1.1
       Installed ImageMagick ───────── v1.2.1
       Installed GPUCompiler ───────── v0.13.10
       Installed SpecialFunctions ──── v1.8.1
       Installed NNlib ─────────────── v0.7.31
       Installed TimerOutputs ──────── v0.5.13
       Installed AbstractFFTs ──────── v1.0.1
       Installed LLVMExtra_jll ─────── v0.0.13+0
       Installed NaNMath ───────────── v0.3.6
       Installed ColorVectorSpace ──── v0.9.8
       Installed OrderedCollections ── v1.4.1
       Installed AutoGrad ──────────── v1.2.4
       Installed JLLWrappers ───────── v1.3.0
       Installed TranscodingStreams ── v0.9.6
       Installed ChainRulesCore ────── v1.11.3
       Installed CEnum ─────────────── v0.4.1
       Installed BFloat16s ─────────── v0.2.0
       Installed Reexport ──────────── v1.2.2
       Installed Random123 ─────────── v1.4.2
       Installed Graphics ──────────── v1.1.1
       Installed GPUArrays ─────────── v8.1.3
       Installed FileIO ────────────── v1.12.0
       Installed RandomNumbers ─────── v1.5.3
       Installed ImageMagick_jll ───── v6.9.12+0
       Installed Requires ──────────── v1.2.0
       Installed Colors ────────────── v0.12.8
       Installed MappedArrays ──────── v0.4.1
       Installed StackViews ────────── v0.1.1
       Installed DataStructures ────── v0.18.11
       Installed PaddedViews ───────── v0.5.11
       Installed OpenSpecFun_jll ───── v0.5.5+0
       Installed Compat ────────────── v3.41.0
       Installed ExprTools ─────────── v0.1.6
       Installed MacroTools ────────── v0.5.9
       Installed InverseFunctions ──── v0.1.2
       Installed LogExpFunctions ───── v0.3.6
       Installed libpng_jll ────────── v1.6.38+0
       Installed MosaicViews ───────── v0.3.3
       Installed ChangesOfVariables ── v0.1.2
       Installed DocStringExtensions ─ v0.8.6
       Installed LLVM ──────────────── v4.7.0
       Installed Knet ──────────────── v1.4.9
       Installed CUDA ──────────────── v3.6.2
      Downloaded artifact: JpegTurbo
      Downloaded artifact: Zstd
      Downloaded artifact: Libtiff
      Downloaded artifact: LLVMExtra
      Downloaded artifact: ImageMagick
      Downloaded artifact: OpenSpecFun
      Downloaded artifact: libpng
        Updating `~/.julia/environments/v1.7/Project.toml`
      [1902f260] + Knet v1.4.9
        Updating `~/.julia/environments/v1.7/Manifest.toml`
      [621f4979] + AbstractFFTs v1.0.1
      [79e6a3ab] + Adapt v3.3.2
      [6710c13c] + AutoGrad v1.2.4
      [ab4f0b2a] + BFloat16s v0.2.0
      [fa961155] + CEnum v0.4.1
      [052768ef] + CUDA v3.6.2
      [d360d2e6] + ChainRulesCore v1.11.3
      [9e997f8a] + ChangesOfVariables v0.1.2
      [3da002f7] + ColorTypes v0.11.0
      [c3611d14] + ColorVectorSpace v0.9.8
      [5ae59095] + Colors v0.12.8
      [34da2185] + Compat v3.41.0
      [864edb3b] + DataStructures v0.18.11
      [ffbed154] + DocStringExtensions v0.8.6
      [e2ba6199] + ExprTools v0.1.6
      [5789e2e9] + FileIO v1.12.0
      [53c48c17] + FixedPointNumbers v0.8.4
      [0c68f7d7] + GPUArrays v8.1.3
      [61eb1bfa] + GPUCompiler v0.13.10
      [a2bd30eb] + Graphics v1.1.1
      [a09fc81d] + ImageCore v0.9.3
      [6218d12a] + ImageMagick v1.2.1
      [3587e190] + InverseFunctions v0.1.2
      [92d709cd] + IrrationalConstants v0.1.1
      [033835bb] + JLD2 v0.4.17
      [692b3bcd] + JLLWrappers v1.3.0
      [1902f260] + Knet v1.4.9
      [929cbde3] + LLVM v4.7.0
      [2ab3a3ac] + LogExpFunctions v0.3.6
      [1914dd2f] + MacroTools v0.5.9
      [dbb5928d] + MappedArrays v0.4.1
      [e94cdb99] + MosaicViews v0.3.3
      [872c559c] + NNlib v0.7.31
      [77ba4419] + NaNMath v0.3.6
      [6fe1bfb0] + OffsetArrays v1.10.8
      [bac558e1] + OrderedCollections v1.4.1
      [5432bcbf] + PaddedViews v0.5.11
      [21216c6a] + Preferences v1.2.3
      [74087812] + Random123 v1.4.2
      [e6cf234a] + RandomNumbers v1.5.3
      [189a3867] + Reexport v1.2.2
      [ae029012] + Requires v1.2.0
      [276daf66] + SpecialFunctions v1.8.1
      [cae243ae] + StackViews v0.1.1
      [62fd8b95] + TensorCore v0.1.1
      [a759f4b9] + TimerOutputs v0.5.13
      [3bb67fe8] + TranscodingStreams v0.9.6
      [c73af94c] + ImageMagick_jll v6.9.12+0
      [aacddb02] + JpegTurbo_jll v2.1.0+0
      [dad2f222] + LLVMExtra_jll v0.0.13+0
      [89763e89] + Libtiff_jll v4.3.0+0
      [efe28fd5] + OpenSpecFun_jll v0.5.5+0
      [3161d3a3] + Zstd_jll v1.5.0+0
      [b53b4c65] + libpng_jll v1.6.38+0
      [0dad84c5] + ArgTools
      [56f22d72] + Artifacts
      [2a0f44e3] + Base64
      [ade2ca70] + Dates
      [8bb1440f] + DelimitedFiles
      [8ba89e20] + Distributed
      [f43a241f] + Downloads
      [b77e0a4c] + InteractiveUtils
      [4af54fe1] + LazyArtifacts
      [b27032c2] + LibCURL
      [76f85450] + LibGit2
      [8f399da3] + Libdl
      [37e2e46d] + LinearAlgebra
      [56ddb016] + Logging
      [d6f4376e] + Markdown
      [a63ad114] + Mmap
      [ca575930] + NetworkOptions
      [44cfe95a] + Pkg
      [de0858da] + Printf
      [3fa0cd96] + REPL
      [9a3f8284] + Random
      [ea8e919c] + SHA
      [9e88b42a] + Serialization
      [1a1011a3] + SharedArrays
      [6462fe0b] + Sockets
      [2f01184e] + SparseArrays
      [10745b16] + Statistics
      [fa267f1f] + TOML
      [a4e569a6] + Tar
      [8dfed614] + Test
      [cf7118a7] + UUIDs
      [4ec0a83e] + Unicode
      [e66e0078] + CompilerSupportLibraries_jll
      [deac9b47] + LibCURL_jll
      [29816b5a] + LibSSH2_jll
      [c8ffd9c3] + MbedTLS_jll
      [14a3606d] + MozillaCACerts_jll
      [4536629a] + OpenBLAS_jll
      [05823500] + OpenLibm_jll
      [83775a58] + Zlib_jll
      [8e850b90] + libblastrampoline_jll
      [8e850ede] + nghttp2_jll
      [3f19e933] + p7zip_jll
        Building Random123 → `~/.julia/scratchspaces/44cfe95a-1eb2-52ea-b672-e2afdf69b78f/0e8b146557ad1c6deb1367655e052276690e71a3/build.log`
    Precompiling project...
      ✗ Knet
      60 dependencies successfully precompiled in 101 seconds
      1 dependency errored. To see a full report either run `import Pkg; Pkg.precompile()` or load the package
    
    (@v1.7) pkg> precompile
    Precompiling project...
      ✗ Knet
      0 dependencies successfully precompiled in 3 seconds (60 already precompiled)
    
    ERROR: The following 1 direct dependency failed to precompile:
    
    Knet [1902f260-5fb4-5aff-8c31-6271790ab950]
    
    Failed to precompile Knet [1902f260-5fb4-5aff-8c31-6271790ab950] to /root/.julia/compiled/v1.7/Knet/jl_nZfCMi.
    ERROR: LoadError: Cannot locate artifact 'libknet8' for aarch64-linux-gnu-libgfortran5-cxx11-libstdcxx29-julia_version+1.7.1 in '/root/.julia/packages/Knet/RCkV0/Artifacts.toml'
    Stacktrace:
      [1] error(s::String)
        @ Base ./error.jl:33
      [2] artifact_slash_lookup(name::String, artifact_dict::Dict{String, Any}, artifacts_toml::String, platform::Base.BinaryPlatforms.Platform)
        @ Artifacts /usr/local/julia/share/julia/stdlib/v1.7/Artifacts/src/Artifacts.jl:608
      [3] var"@artifact_str"(__source__::LineNumberNode, __module__::Module, name::Any, platform::Nothing)
        @ Artifacts /usr/local/julia/share/julia/stdlib/v1.7/Artifacts/src/Artifacts.jl:678
      [4] var"@artifact_str"(__source__::LineNumberNode, __module__::Module, name::Any)
        @ Artifacts /usr/local/julia/share/julia/stdlib/v1.7/Artifacts/src/Artifacts.jl:641
      [5] include(mod::Module, _path::String)
        @ Base ./Base.jl:418
      [6] include(x::String)
        @ Knet ~/.julia/packages/Knet/RCkV0/src/Knet.jl:1
      [7] top-level scope
        @ ~/.julia/packages/Knet/RCkV0/src/Knet.jl:17
      [8] include
        @ ./Base.jl:418 [inlined]
      [9] include_package_for_output(pkg::Base.PkgId, input::String, depot_path::Vector{String}, dl_load_path::Vector{String}, load_path::Vector{String}, concrete_deps::Vector{Pair{Base.PkgId, UInt64}}, source::Nothing)
        @ Base ./loading.jl:1318
     [10] top-level scope
        @ none:1
     [11] eval
        @ ./boot.jl:373 [inlined]
     [12] eval(x::Expr)
        @ Base.MainInclude ./client.jl:453
     [13] top-level scope
        @ none:1
    in expression starting at /root/.julia/packages/Knet/RCkV0/src/libknet8/LibKnet8.jl:7
    in expression starting at /root/.julia/packages/Knet/RCkV0/src/libknet8/LibKnet8.jl:3
    in expression starting at /root/.julia/packages/Knet/RCkV0/src/Knet.jl:1
    
    (@v1.7) pkg> 
    

    I am using the official Julia Docker image with Julia v1.7.1 I am not using CUDA On My Ubuntu machine it runs perfectly fine.

    I am not sure if this is related to Knet, Docker or Apple...
    But I hope someone could help me find a Solution:)

    opened by manuEbg 9
  • R1 Regularization

    R1 Regularization

    @Kausta found a bug in the cat/uncat higher order gradients implementing R1 regularization. I am moving from email to this github issue to follow up. Here is his error description:

    The current implementation is on the Github page (https://github.com/Kausta/HiSD.jl) (with the error in dis_loss_real function in core/networks.py), and the main error I am getting is the following:

    ERROR: LoadError: MethodError: no method matching back(::typeof(AutoGrad.uncat), ::Type{AutoGrad.Arg{4}}, ::Knet.KnetArrays.KnetMatrix{Float32}, ::AutoGrad.Result{Knet.KnetArrays.KnetMatrix{Float32}}, ::AutoGrad.Result{Knet.KnetArrays.KnetMatrix{Float32}}, ::Int64, ::Int64, ::AutoGrad.Result{Knet.KnetArrays.KnetMatrix{Float32}}, ::Knet.KnetArrays.KnetMatrix{Float32})
    

    Here is his references on PyTorch/TF implementations:

    I am adding the papers, documentations and the implementations we discussed. The R1 regularization was defined in the paper https://arxiv.org/pdf/1801.04406.pdf (Which Training Methods for GANs do actually Converge?), which simplifies the gradient regularization from https://arxiv.org/pdf/1705.09367.pdf (Stabilizing Training of Generative Adversarial Networks through Regularization). The original R1 implementation can be found at https://github.com/ChristophReich1996/Dirac-GAN/blob/decb8283d919640057c50ff5a1ba01b93ed86332/dirac_gan/loss.py#L292, and the paper I am implementing uses the following implementation https://github.com/imlixinyang/HiSD/blob/main/core/networks.py#L80 (paper link: https://arxiv.org/pdf/2103.01456.pdf).

    There had been a Variable interface (like Autograd.jl Param) in PyTorch previously, but it is deprecated in favor of a more unified interface using only Tensors. (PyTorch Autograd automatically supports Tensors with requires_grad set to True, and both gradients and saved forward values are kept directly on the tensors. During the forward pass, an operation is only recorded in the backward graph if at least one of its input tensors require grad. During the backward pass (.backward()), only leaf tensors with requires_grad=True will have gradients accumulated into their .grad fields. Internally, autograd represents this graph as a graph of Function objects (really expressions), and stores the entry points to the graph on the .grad_fn attribute of each torch.Tensor.) These are documented in https://pytorch.org/docs/stable/notes/autograd.html, with example based explanations in https://pytorch.org/tutorials/beginner/basics/autogradqs_tutorial.html. The torch.autograd.grad function is documented at https://pytorch.org/docs/stable/generated/torch.autograd.grad.html, and the remaining autograd related functions are documented in https://pytorch.org/docs/stable/autograd.html?highlight=variable, including the functional higher level API for computing jacobians, hessians and jacobian/hessian dot products with input vectors. Moreover, the pytorch documentation contains a gradient penalty example (WGAN-GP gradient penalty, similar to the Autograd.jl issue https://github.com/denizyuret/AutoGrad.jl/issues/120), however, it is inside the documentation for AMP (automatic mixed precision): https://pytorch.org/docs/stable/notes/amp_examples.html#gradient-penalty.

    Tensorflow also contains a documentation for higher order gradients with nested tapes in https://www.tensorflow.org/guide/advanced_autodiff#higher-order_gradients, and it is followed (in the same link) by an input gradient penalty example (The gradient of (the magnitude of the gradient with respect to the inputs) with respect to the model), which is similar to R1 regularization. I am adding this example, together with the small hypothetical example I wrote for R1 implementation as an attachment. (Here is a gist: https://gist.github.com/denizyuret/1af3577afbe6a53d61bc75f86fed4ac4)

    Meanwhile, I will start by writing a minimal reproducible example for the uncat bug, and checking the current implementation/unit tests. Also, pytorch contains a gradgradcheck method (https://pytorch.org/docs/stable/generated/torch.autograd.gradgradcheck.html#torch.autograd.gradgradcheck) for gradients of gradients. I think a similar one for Autograd.jl could be a nice addition for easier testing/bug-fixing.

    opened by denizyuret 9
  • Derivative of a Function That Includes @diff Macro

    Derivative of a Function That Includes @diff Macro

    Hello.

    I am currently using Julia Versio 1.6.3 on a Platform "OS: Linux (x86_64-pc-linux-gnu) CPU: Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz, GPU : CuDevice(0): Tesla T4". I am trying to implement a variational autoencoder called Gradient Origin Networks (GONs). GONs are introduced as a generative model which does not require encoders or hypernetworks. Assume Variational GON model called F. First, a zero vector z_0 is passed through the model F, and then the latent vector initialized as the minus gradient of the loss with respect to this zero vector. Therefore, the latent space is determined by only one gradient step. Let us call this latent vector as z. Then, the network parameters are optimized by using the loss with the reconstruction F(z).

    I am currently performing my experiments on MNIST dataset where I linearly interpolated the images to the size of 32x32. The decoding and reparametrization functions are as follows. theta is a vector of model weights.

    function reparametrize(mu, logvar)
        
        std = exp.(0.5 .* logvar)
        epsilon = convert(Atype, randn(F, size(mu)))
        z = mu .+ epsilon .* std
        
        return z
    end
    
    function decode(theta, z; batch_size = 64, training = true)
        
        mu = theta[1] * z .+ theta[2]
        logvar = theta[3] * z .+ theta[4]
        
        z = reparametrize(mu, logvar)
        
        z = reshape(z, (1, 1, nz, batch_size))
        z = deconv4(theta[5], z, mode = 1) .+ theta[6]
        z = batchnorm(z, bnmoments(), theta[7]; training = training)
        z = Knet.elu.(z)
        
        z = deconv4(theta[8], z, stride = 2, padding = 1, mode = 1) .+ theta[9]
        z = batchnorm(z, bnmoments(), theta[10]; training = training)
        z = Knet.elu.(z)
        
        z = deconv4(theta[11], z, stride = 2, padding = 1, mode = 1) .+ theta[12]
        z = batchnorm(z, bnmoments(), theta[13]; training = training)
        z = Knet.elu.(z)
        
        z = deconv4(theta[14], z, stride = 2, padding = 1, mode = 1) .+ theta[15]
        x_hat = Knet.sigm.(z)
        
        return x_hat, mu, logvar
        
    end
    

    For the loss, it is used binary cross-entropy and KL-divergence. The code is given as follows.

    function BCE(x_tensor,x_hat_tensor)
        x = mat(x_tensor)
        x_hat = mat(x_hat_tensor)
        return -mean(sum((x .* log.(x_hat .+ F(1e-10)) + (1 .- x) .* log.(1 .- x_hat .+ F(1e-10))), dims = 1))
    end
    
    function KLD(mu, logvar)
        var = exp.(logvar)
        std = sqrt.(var)
        KL = -0.5 * mean(sum(1 .+ logvar .- (mu .* mu) - exp.(logvar), dims = 1))
        return KL
    end
    
    function loss(theta, x, z)
        x_hat, mu, logvar = decode(theta, z)
        L = BCE(x, x_hat) + KLD(mu, logvar)
        return L
    end
    

    Since there are two steps for GON (1-) Use the gradient w.r.t. origin to determine the latent space z, 2-) Use latent space for reconstruction) I need to track all the gradient w.r.t. model weights from the steps (1) and (2). Therefore, I wrote the following decoding function and loss function for training purpose.

    function decode_train(theta, x; batch_size = 64,training = true)
        origin = param(Atype(zeros(nz, batch_size)))
    
        derivative_origin = @diff loss(value.(theta), x, origin)
        dz = grad(derivative_origin, origin)
    
        z = -value(dz)
    
        x_hat, mu, logvar = decode(theta, origin);
        return x_hat, mu, logvar
    end
    
    function loss_train(theta, x)
        x_hat, mu, logvar = decode_train(theta, x)
        L = BCE(x, x_hat) + KLD(mu, logvar)
        return L
    end
    

    However, I am not able to take the gradient of the " loss_train(theta, x)" function. I am getting the following error when I use the @diff macro of AutoGrad package. How can I handle to train this model which requires a second order derivative (I need the derivative of the function decode_train)? To reproduce this result, you can run the following notebook : https://github.com/BariscanBozkurt/Gradient-Origin-Networks/blob/main/GON_Implementation_Issue.ipynb My code: @diff loss_train(theta, x) The error is:

    Stacktrace: [1] copyto!(a::KnetArray{Float32, 4}, b::Base.Broadcast.Broadcasted{Base.Broadcast.Style{AutoGrad.Value}, NTuple{4, Base.OneTo{Int64}}, typeof(identity), Tuple{AutoGrad.Result{KnetArray{Float32, 4}}}}) @ Knet.KnetArrays ~/.julia/packages/Knet/RCkV0/src/knetarrays/broadcast.jl:35 [2] copyto!(x::AutoGrad.Result{KnetArray{Float32, 4}}, y::Base.Broadcast.Broadcasted{Base.Broadcast.Style{AutoGrad.Value}, NTuple{4, Base.OneTo{Int64}}, typeof(identity), Tuple{AutoGrad.Result{KnetArray{Float32, 4}}}}) @ AutoGrad ~/.julia/packages/AutoGrad/TTpeo/src/core.jl:55 [3] materialize! @ ./broadcast.jl:894 [inlined] [4] materialize! @ ./broadcast.jl:891 [inlined] [5] materialize!(dest::AutoGrad.Result{KnetArray{Float32, 4}}, x::AutoGrad.Result{KnetArray{Float32, 4}}) @ Base.Broadcast ./broadcast.jl:887 [6] batchnorm4_back(g::KnetArray{Float32, 4}, x::AutoGrad.Result{KnetArray{Float32, 4}}, dy::AutoGrad.Result{KnetArray{Float32, 4}}; eps::Float64, training::Bool, cache::Knet.Ops20.BNCache, moments::Knet.Ops20.BNMoments, o::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ Knet.Ops20 ~/.julia/packages/Knet/RCkV0/src/ops20/batchnorm.jl:262 [7] #batchnorm4x#191 @ ~/.julia/packages/Knet/RCkV0/src/ops20/batchnorm.jl:317 [inlined] [8] #back#210 @ ./none:0 [inlined] [9] differentiate(::Function; o::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ AutoGrad ~/.julia/packages/AutoGrad/TTpeo/src/core.jl:165 [10] differentiate @ ~/.julia/packages/AutoGrad/TTpeo/src/core.jl:135 [inlined] [11] decode_train(theta::Vector{Any}, x::KnetArray{Float32, 4}; batch_size::Int64, training::Bool) @ Main ./In[14]:4 [12] decode_train @ ./In[14]:2 [inlined] [13] loss_train(theta::Vector{Any}, x::KnetArray{Float32, 4}) @ Main ./In[16]:2 [14] (::var"#16#17")() @ Main ~/.julia/packages/AutoGrad/TTpeo/src/core.jl:205 [15] differentiate(::Function; o::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ AutoGrad ~/.julia/packages/AutoGrad/TTpeo/src/core.jl:144 [16] differentiate(::Function) @ AutoGrad ~/.julia/packages/AutoGrad/TTpeo/src/core.jl:135 [17] top-level scope @ In[18]:1 [18] eval @ ./boot.jl:360 [inlined] [19] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String) @ Base ./loading.jl:1116 [20] softscope_include_string(m::Module, code::String, filename::String) @ SoftGlobalScope ~/.julia/packages/SoftGlobalScope/u4UzH/src/SoftGlobalScope.jl:65 [21] execute_request(socket::ZMQ.Socket, msg::IJulia.Msg) @ IJulia ~/.julia/packages/IJulia/e8kqU/src/execute_request.jl:67 [22] #invokelatest#2 @ ./essentials.jl:708 [inlined] [23] invokelatest @ ./essentials.jl:706 [inlined] [24] eventloop(socket::ZMQ.Socket) @ IJulia ~/.julia/packages/IJulia/e8kqU/src/eventloop.jl:8 [25] (::IJulia.var"#15#18")() @ IJulia ./task.jl:411 MethodError: no method matching copyto!(::KnetArray{Float32, 4}, ::AutoGrad.Result{KnetArray{Float32, 4}}) Closest candidates are: copyto!(::KnetArray{T, N} where N, ::Array{T, N} where N) where T at /kuacc/users/bbozkurt15/.julia/packages/Knet/RCkV0/src/knetarrays/copy.jl:10 copyto!(::KnetArray{T, N} where N, ::Array{S, N} where N) where {T, S} at /kuacc/users/bbozkurt15/.julia/packages/Knet/RCkV0/src/knetarrays/copy.jl:18 copyto!(::KnetArray{T, N} where N, ::KnetArray{T, N} where N) where T at /kuacc/users/bbozkurt15/.julia/packages/Knet/RCkV0/src/knetarrays/copy.jl:9 ...

    Stacktrace: [1] differentiate(::Function; o::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ AutoGrad ~/.julia/packages/AutoGrad/TTpeo/src/core.jl:148 [2] differentiate(::Function) @ AutoGrad ~/.julia/packages/AutoGrad/TTpeo/src/core.jl:135 [3] top-level scope @ In[18]:1 [4] eval @ ./boot.jl:360 [inlined] [5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String) @ Base ./loading.jl:1116

    opened by BariscanBozkurt 4
Releases(v1.4.10)
Ivy is a templated deep learning framework which maximizes the portability of deep learning codebases.

Ivy is a templated deep learning framework which maximizes the portability of deep learning codebases. Ivy wraps the functional APIs of existing frameworks. Framework-agnostic functions, libraries and layers can then be written using Ivy, with simultaneous support for all frameworks. Ivy currently supports Jax, TensorFlow, PyTorch, MXNet and Numpy. Check out the docs for more info!

Ivy 8.2k Jan 2, 2023
A template repository for submitting a job to the Slurm Cluster installed at the DISI - University of Bologna

Cluster di HPC con GPU per esperimenti di calcolo (draft version 1.0) Per poter utilizzare il cluster il primo passo è abilitare l'account istituziona

null 20 Dec 16, 2022
Jittor Medical Segmentation Lib -- The assignment of Pattern Recognition course (2021 Spring) in Tsinghua University

THU模式识别2021春 -- Jittor 医学图像分割 模型列表 本仓库收录了课程作业中同学们采用jittor框架实现的如下模型: UNet SegNet DeepLab V2 DANet EANet HarDNet及其改动HarDNet_alter PSPNet OCNet OCRNet DL

null 48 Dec 26, 2022
Introduction to AI assignment 1 HCM University of Technology, term 211

Sokoban Bot Introduction to AI assignment 1 HCM University of Technology, term 211 Abstract This is basically a solver for Sokoban game using Breadth-

Quang Minh 4 Dec 12, 2022
Code for the AI lab course 2021/2022 of the University of Verona

AI-Lab Code for the AI lab course 2021/2022 of the University of Verona Set-Up the environment for the curse Download Anaconda for your System. Instal

Davide Corsi 5 Oct 19, 2022
It is the assignment for COMP 576 in Rice University

COMP-576 It is the assignment for COMP 576 in Rice University There are two programming assignments and one Final Project. Assignment 1: It is a MLP a

Maojie Tang 1 Nov 25, 2021
Final project code: Implementing MAE with downscaled encoders and datasets, for ESE546 FA21 at University of Pennsylvania

546 Final Project: Masked Autoencoder Haoran Tang, Qirui Wu 1. Training To train the network, please run mae_pretraining.py. Please modify folder path

Haoran Tang 0 Apr 22, 2022
Final project code: Implementing BicycleGAN, for CIS680 FA21 at University of Pennsylvania

680 Final Project: BicycleGAN Haoran Tang Instructions 1. Training To train the network, please run train.py. Change hyper-parameters and folder paths

Haoran Tang 0 Apr 22, 2022
Final Project for the CS238: Decision Making Under Uncertainty course at Stanford University in Autumn '21.

Final Project for the CS238: Decision Making Under Uncertainty course at Stanford University in Autumn '21. We optimized wind turbine placement in a wind farm, subject to wake effects, using Q-learning.

Manasi Sharma 2 Sep 27, 2022
CS50x-AI - Artificial Intelligence with Python from Harvard University

CS50x-AI Artificial Intelligence with Python from Harvard University ?? Table of

Hosein Damavandi 6 Aug 22, 2022
CS506-Spring2022 - Code and Slides for Boston University CS 506

CS 506 - Computational Tools for Data Science Code, slides, and notes for Boston

Lance Galletti 17 May 6, 2022
Aalto-cs-msc-theses - Listing of M.Sc. Theses of the Department of Computer Science at Aalto University

Aalto-CS-MSc-Theses Listing of M.Sc. Theses of the Department of Computer Scienc

Jorma Laaksonen 3 Jan 27, 2022
An University Project of Quera Web Crawling.

WebCrawlerProject An University Project of Quera Web Crawling. خزشگر اینستاگرام در این پروژه شما باید با استفاده از کتابخانه های زیر یک خزشگر اینستاگر

Mahdi 3 Aug 12, 2022
🔥 Cogitare - A Modern, Fast, and Modular Deep Learning and Machine Learning framework for Python

Cogitare is a Modern, Fast, and Modular Deep Learning and Machine Learning framework for Python. A friendly interface for beginners and a powerful too

Cogitare - Modern and Easy Deep Learning with Python 76 Sep 30, 2022
PArallel Distributed Deep LEarning: Machine Learning Framework from Industrial Practice (『飞桨』核心框架,深度学习&机器学习高性能单机、分布式训练和跨平台部署)

English | 简体中文 Welcome to the PaddlePaddle GitHub. PaddlePaddle, as the only independent R&D deep learning platform in China, has been officially open

null 19.4k Jan 4, 2023
Machine learning framework for both deep learning and traditional algorithms

NeoML is an end-to-end machine learning framework that allows you to build, train, and deploy ML models. This framework is used by ABBYY engineers for

NeoML 704 Dec 27, 2022
Deep learning (neural network) based remote photoplethysmography: how to extract pulse signal from video using deep learning tools

Deep-rPPG: Camera-based pulse estimation using deep learning tools Deep learning (neural network) based remote photoplethysmography: how to extract pu

Terbe Dániel 138 Dec 17, 2022
deep-table implements various state-of-the-art deep learning and self-supervised learning algorithms for tabular data using PyTorch.

deep-table implements various state-of-the-art deep learning and self-supervised learning algorithms for tabular data using PyTorch.

null 63 Oct 17, 2022