<jupyter_start><jupyter_text>Preparing the workspace for CNTK in jupyter notebook<jupyter_code>#r "netstandard"
#load "Paket.fsx"<jupyter_output><empty_output><jupyter_text>* Unless you reference *netstandard* this way anything CNTK related will be marked as errors and will be ignored by autocomplete.* *Paket.fsx* is neccessary to enable IfSharp's paket extensions such as Paket.Dependencies.Install. Downloading paket.exe for each notebook specifically is thus unnessecary.  1. Install neccessary packages<jupyter_code>Paket.Dependencies.Install """
framework: netstandard2.0
generate_load_scripts: true
storage: none
source https://nuget.org/api/v2
nuget CNTK.CPUOnly
nuget MathNet.Numerics
nuget MathNet.Numerics.FSharp
nuget FSharp.Control.AsyncSeq
"""<jupyter_output><empty_output><jupyter_text>Usually in F notebooks we tend to see paket.dependencies created this way:> Paket.Package ["CNTK.CPUOnly"; "FsLab"; "FSharp.Charting"]I used Paket.Dependencies.Install instead of Paket.Package as it allows us to use the current folder for paket files instead piling them up in the directory where *IfSharp.exe* is located. It also allows us to set **storage: none** and save some disk space and bandwidth by not re-downloading the same dependencies every time a project needs CNTK support, some of the required DLLs being rather immense as it is.This is slightly ironic, considering that in order to use CNTK in jupyter we end up having to copy all dependencies in a common bin path anyway. This [seems to be an issue](http://christoph.ruegg.name/blog/loading-native-dlls-in-fsharp-interactive.html) with the way F interactive handles native dependencies in general.  2. Reference Cntk.Core.Managed-\*.\*.dll from the nuget packages folder<jupyter_code>#load @"../CNTK100 - Preparing workspace/.paket/load/main.group.fsx"

System.Reflection.Assembly.GetAssembly(typeof<CNTK.CNTKLib>).CodeBase
|> printfn "CNTK managed binary referenced from:\n\t%s"<jupyter_output><empty_output><jupyter_text>In Windows, if you get an error the likes of:>The namespace or module 'CNTK' is not defined.after Paket.Dependencies.Install above succeeds, it could be because the path where *IfSharp.exe* is located seems to have precedence as far as the load directive is concerned. This means that once *main.group.fsx* has been generated in the *IfSharp* folder (for instance after downloading the visualization tools) you will no longer have access to the local generated scripts until you delete those in the *IfSharp* folder, or rename either.If you ommit **storage: none** from Paket.Dependencies.Install, the IfSharp folder is also where any nuget packages will physically end up, as long as you call *paket* from within the notebook.  3. Copy all CNTK dependencies to the same folderAs far as I can tell this is a managed CNTK issue, not a jupyter or IfSharp issue.Currently all CNTK dlls need to be copied in the same folder with _Cntk.Core.Managed-..dll_ else they can't be resolved at runtime. The included [script](https://github.com/SpaceAntelope/IfCntk/blob/master/notebooks/cntk-tutorials/fsx/PrepareWorkspace.fsx) finds the path of the presently referenced Cntk DLL and uses it to locate any dependencies. Should work for both GPU and CPUOnly.Again, this is only neccessary for working with CNTK outside Visual Studio.If you are interested in F scripting with CNTK make sure it's through a 64bit version of fsi.exe, such as fsiAnyCPU.exe, or you will run into bad image exceptions.<jupyter_code>#load "../fsx/PrepareWorkspace.fsx"
open PrepareWorkspace

CreateOrCleanLocalBinFolder "../bin"
CopyDependenciesToLocalFolder "../bin" Release<jupyter_output><empty_output><jupyter_text>So far I haven't been able to reference debug DLLs successfully in a notebook, i.e. without hitting the dreaded DllNotFoundException: Cntk.Core.CSBinding-..dll, so I suggest you also stick to the release builds.  4. Reference CNTK from the newly created folderIn order to successfully reference CNTK from the new location you will have to restart the kernel, otherwise the reference set in main.group.fsx will persist.<jupyter_code>#r @"..\bin\Cntk.Core.Managed-2.6.dll"
#load @"../CNTK100 - Preparing workspace/.paket/load/main.group.fsx"
open CNTK

DeviceDescriptor.UseDefaultDevice().Type
|> printfn "Congratulations, you are using CNTK for: %A"

System.Reflection.Assembly.GetAssembly(typeof<CNTK.CNTKLib>).CodeBase
|> printfn "\nCNTK managed binary referenced from:\n\t%s"<jupyter_output><empty_output><jupyter_text>5. Update path variable Finally, we need to update the path variable to inlcude the newly created /bin folder, otherwise we get an error when instantiating the learner before training.>CNTK.CNTKLibPINVOKE.SGDLearner__SWIG_1 exceptionThis only affects the current process, so it is something you will need to do whenever you run a cntk notebook.<jupyter_code>open System
open System.IO

Environment.GetEnvironmentVariable("PATH")
|> fun path -> sprintf "%s%c%s" path (Path.PathSeparator) (Path.GetFullPath("bin"))
|> fun path -> Environment.SetEnvironmentVariable("PATH", path)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07817398119122257, 'fraction_numerical': 0.0037225705329153604, 'mean_word_length': 5.4702154626109, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 37, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '180337', 'license': 'MIT', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1446, 'n_words': 594, 'path': 'notebooks/cntk-tutorials/CNTK100 - Preparing workspace/Preparing workspace.ipynb', 'repo_name': 'SpaceAntelope/IfCntk', 'size': '5104'}