├── .gitignore ├── CSharpOrNot ├── App.xaml ├── App.xaml.cs ├── BitmapTools.cs ├── CSharpOrNot.cs ├── CSharpOrNot.csproj ├── CSharpOrNotProgram.cs ├── CSharpOrNotWindow.xaml ├── CSharpOrNotWindow.xaml.cs ├── DataTools.cs ├── README.md ├── TrainCommand.cs └── UICommand.cs ├── FSharp └── FashionMnistF │ ├── FashionMnist.fs │ └── FashionMnistF.fsproj ├── FashionMnistClassification ├── FashionMnistClassification.cs ├── FashionMnistClassification.csproj ├── FashionMnistClassification.ruleset └── fashionMNIST.py ├── Gradient-Samples.sln ├── LICENSE ├── README.md ├── ResNetBlock ├── ResNetBlock.cs ├── ResNetBlock.csproj └── ResNetSampleProgram.cs ├── SimpleApproximation ├── SimpleApproximation.csproj ├── SimpleApproximationProgram.cs └── simple_approx.py ├── VB └── FashionMnistVB │ ├── FashionMnist.vb │ └── FashionMnistVB.vbproj └── v1 ├── BasicMath ├── BasicMath.csproj └── BasicMathProgram.cs ├── CharRNN └── README.md ├── FSharp └── BasicMathF │ ├── BasicMath.fs │ └── BasicMathF.fsproj ├── GPT-2 └── README.md ├── LinearSVM ├── LinearSVM.csproj ├── LinearSvmCommand.cs └── LinearSvmProgram.cs ├── RL-MLAgents ├── IEnvironment.cs ├── Program.cs ├── PythonListExtensions.cs ├── README.md ├── RL-MLAgents.csproj ├── RepeatObservationEnvironment.cs ├── SoftActorCritic │ ├── ActorCritic.cs │ ├── Policies.cs │ ├── Policy.cs │ ├── ReplayBuffer.cs │ ├── SoftActorCritic.cs │ └── Tools.cs └── UnityEnvironmentProxy.cs └── VB └── BasicMathVB ├── BasicMath.vb └── BasicMathVB.vbproj /.gitignore: -------------------------------------------------------------------------------- 1 | v1/GPT-2/checkpoint 2 | v1/GPT-2/models 3 | v1/GPT-2/samples 4 | 5 | ## Ignore Visual Studio temporary files, build results, and 6 | ## files generated by popular Visual Studio add-ons. 7 | ## 8 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 9 | 10 | # User-specific files 11 | *.suo 12 | *.user 13 | *.userosscache 14 | *.sln.docstates 15 | 16 | # User-specific files (MonoDevelop/Xamarin Studio) 17 | *.userprefs 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | bld/ 27 | [Bb]in/ 28 | [Oo]bj/ 29 | [Ll]og/ 30 | 31 | # Visual Studio 2015/2017 cache/options directory 32 | .vs/ 33 | # Uncomment if you have tasks that create the project's static files in wwwroot 34 | #wwwroot/ 35 | 36 | # Visual Studio 2017 auto generated files 37 | Generated\ Files/ 38 | 39 | # MSTest test Results 40 | [Tt]est[Rr]esult*/ 41 | [Bb]uild[Ll]og.* 42 | 43 | # NUNIT 44 | *.VisualState.xml 45 | TestResult.xml 46 | 47 | # Build Results of an ATL Project 48 | [Dd]ebugPS/ 49 | [Rr]eleasePS/ 50 | dlldata.c 51 | 52 | # Benchmark Results 53 | BenchmarkDotNet.Artifacts/ 54 | 55 | # .NET Core 56 | project.lock.json 57 | project.fragment.lock.json 58 | artifacts/ 59 | **/Properties/launchSettings.json 60 | 61 | # StyleCop 62 | StyleCopReport.xml 63 | 64 | # Files built by Visual Studio 65 | *_i.c 66 | *_p.c 67 | *_i.h 68 | *.ilk 69 | *.meta 70 | *.obj 71 | *.iobj 72 | *.pch 73 | *.pdb 74 | *.ipdb 75 | *.pgc 76 | *.pgd 77 | *.rsp 78 | *.sbr 79 | *.tlb 80 | *.tli 81 | *.tlh 82 | *.tmp 83 | *.tmp_proj 84 | *.log 85 | *.vspscc 86 | *.vssscc 87 | .builds 88 | *.pidb 89 | *.svclog 90 | *.scc 91 | 92 | # Chutzpah Test files 93 | _Chutzpah* 94 | 95 | # Visual C++ cache files 96 | ipch/ 97 | *.aps 98 | *.ncb 99 | *.opendb 100 | *.opensdf 101 | *.sdf 102 | *.cachefile 103 | *.VC.db 104 | *.VC.VC.opendb 105 | 106 | # Visual Studio profiler 107 | *.psess 108 | *.vsp 109 | *.vspx 110 | *.sap 111 | 112 | # Visual Studio Trace Files 113 | *.e2e 114 | 115 | # TFS 2012 Local Workspace 116 | $tf/ 117 | 118 | # Guidance Automation Toolkit 119 | *.gpState 120 | 121 | # ReSharper is a .NET coding add-in 122 | _ReSharper*/ 123 | *.[Rr]e[Ss]harper 124 | *.DotSettings.user 125 | 126 | # JustCode is a .NET coding add-in 127 | .JustCode 128 | 129 | # TeamCity is a build add-in 130 | _TeamCity* 131 | 132 | # DotCover is a Code Coverage Tool 133 | *.dotCover 134 | 135 | # AxoCover is a Code Coverage Tool 136 | .axoCover/* 137 | !.axoCover/settings.json 138 | 139 | # Visual Studio code coverage results 140 | *.coverage 141 | *.coveragexml 142 | 143 | # NCrunch 144 | _NCrunch_* 145 | .*crunch*.local.xml 146 | nCrunchTemp_* 147 | 148 | # MightyMoose 149 | *.mm.* 150 | AutoTest.Net/ 151 | 152 | # Web workbench (sass) 153 | .sass-cache/ 154 | 155 | # Installshield output folder 156 | [Ee]xpress/ 157 | 158 | # DocProject is a documentation generator add-in 159 | DocProject/buildhelp/ 160 | DocProject/Help/*.HxT 161 | DocProject/Help/*.HxC 162 | DocProject/Help/*.hhc 163 | DocProject/Help/*.hhk 164 | DocProject/Help/*.hhp 165 | DocProject/Help/Html2 166 | DocProject/Help/html 167 | 168 | # Click-Once directory 169 | publish/ 170 | 171 | # Publish Web Output 172 | *.[Pp]ublish.xml 173 | *.azurePubxml 174 | # Note: Comment the next line if you want to checkin your web deploy settings, 175 | # but database connection strings (with potential passwords) will be unencrypted 176 | *.pubxml 177 | *.publishproj 178 | 179 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 180 | # checkin your Azure Web App publish settings, but sensitive information contained 181 | # in these scripts will be unencrypted 182 | PublishScripts/ 183 | 184 | # NuGet Packages 185 | *.nupkg 186 | # The packages folder can be ignored because of Package Restore 187 | **/[Pp]ackages/* 188 | # except build/, which is used as an MSBuild target. 189 | !**/[Pp]ackages/build/ 190 | # Uncomment if necessary however generally it will be regenerated when needed 191 | #!**/[Pp]ackages/repositories.config 192 | # NuGet v3's project.json files produces more ignorable files 193 | *.nuget.props 194 | *.nuget.targets 195 | 196 | # Microsoft Azure Build Output 197 | csx/ 198 | *.build.csdef 199 | 200 | # Microsoft Azure Emulator 201 | ecf/ 202 | rcf/ 203 | 204 | # Windows Store app package directories and files 205 | AppPackages/ 206 | BundleArtifacts/ 207 | Package.StoreAssociation.xml 208 | _pkginfo.txt 209 | *.appx 210 | 211 | # Visual Studio cache files 212 | # files ending in .cache can be ignored 213 | *.[Cc]ache 214 | # but keep track of directories ending in .cache 215 | !*.[Cc]ache/ 216 | 217 | # Others 218 | ClientBin/ 219 | ~$* 220 | *~ 221 | *.dbmdl 222 | *.dbproj.schemaview 223 | *.jfm 224 | *.pfx 225 | *.publishsettings 226 | orleans.codegen.cs 227 | 228 | # Including strong name files can present a security risk 229 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 230 | #*.snk 231 | 232 | # Since there are multiple workflows, uncomment next line to ignore bower_components 233 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 234 | #bower_components/ 235 | 236 | # RIA/Silverlight projects 237 | Generated_Code/ 238 | 239 | # Backup & report files from converting an old project file 240 | # to a newer Visual Studio version. Backup files are not needed, 241 | # because we have git ;-) 242 | _UpgradeReport_Files/ 243 | Backup*/ 244 | UpgradeLog*.XML 245 | UpgradeLog*.htm 246 | ServiceFabricBackup/ 247 | *.rptproj.bak 248 | 249 | # SQL Server files 250 | *.mdf 251 | *.ldf 252 | *.ndf 253 | 254 | # Business Intelligence projects 255 | *.rdl.data 256 | *.bim.layout 257 | *.bim_*.settings 258 | *.rptproj.rsuser 259 | 260 | # Microsoft Fakes 261 | FakesAssemblies/ 262 | 263 | # GhostDoc plugin setting file 264 | *.GhostDoc.xml 265 | 266 | # Node.js Tools for Visual Studio 267 | .ntvs_analysis.dat 268 | node_modules/ 269 | 270 | # Visual Studio 6 build log 271 | *.plg 272 | 273 | # Visual Studio 6 workspace options file 274 | *.opt 275 | 276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 277 | *.vbw 278 | 279 | # Visual Studio LightSwitch build output 280 | **/*.HTMLClient/GeneratedArtifacts 281 | **/*.DesktopClient/GeneratedArtifacts 282 | **/*.DesktopClient/ModelManifest.xml 283 | **/*.Server/GeneratedArtifacts 284 | **/*.Server/ModelManifest.xml 285 | _Pvt_Extensions 286 | 287 | # Paket dependency manager 288 | .paket/paket.exe 289 | paket-files/ 290 | 291 | # FAKE - F# Make 292 | .fake/ 293 | 294 | # JetBrains Rider 295 | .idea/ 296 | *.sln.iml 297 | 298 | # CodeRush 299 | .cr/ 300 | 301 | # Python Tools for Visual Studio (PTVS) 302 | __pycache__/ 303 | *.pyc 304 | 305 | # Cake - Uncomment if you are using it 306 | # tools/** 307 | # !tools/packages.config 308 | 309 | # Tabs Studio 310 | *.tss 311 | 312 | # Telerik's JustMock configuration file 313 | *.jmconfig 314 | 315 | # BizTalk build output 316 | *.btp.cs 317 | *.btm.cs 318 | *.odx.cs 319 | *.xsd.cs 320 | 321 | # OpenCover UI analysis results 322 | OpenCover/ 323 | 324 | # Azure Stream Analytics local run output 325 | ASALocalRun/ 326 | 327 | # MSBuild Binary and Structured Log 328 | *.binlog 329 | 330 | # NVidia Nsight GPU debugger configuration file 331 | *.nvuser 332 | 333 | # MFractors (Xamarin productivity tool) working folder 334 | .mfractor/ 335 | 336 | # Visual Studio Code 337 | .vscode/ 338 | -------------------------------------------------------------------------------- /CSharpOrNot/App.xaml: -------------------------------------------------------------------------------- 1 |  4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CSharpOrNot/App.xaml.cs: -------------------------------------------------------------------------------- 1 | namespace LostTech.Gradient.Samples { 2 | using Avalonia; 3 | using Avalonia.Markup.Xaml; 4 | 5 | public class App : Application { 6 | public override void Initialize() { 7 | AvaloniaXamlLoader.Load(this); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharpOrNot/BitmapTools.cs: -------------------------------------------------------------------------------- 1 | namespace LostTech.Gradient.Samples { 2 | using System; 3 | using System.Drawing; 4 | using System.Drawing.Imaging; 5 | using System.Runtime.InteropServices; 6 | 7 | static class BitmapTools { 8 | public static void ToBitmap(byte[] brightness, Bitmap target) { 9 | if (target.PixelFormat != PixelFormat.Format8bppIndexed) 10 | throw new NotSupportedException("The only supported pixel format is " + PixelFormat.Format8bppIndexed); 11 | 12 | var bitmapData = target.LockBits(new Rectangle(new Point(), target.Size), 13 | ImageLockMode.WriteOnly, 14 | PixelFormat.Format8bppIndexed); 15 | 16 | try { 17 | Marshal.Copy(source: brightness, 18 | startIndex: 0, length: bitmapData.Width * bitmapData.Height, 19 | destination: bitmapData.Scan0); 20 | } finally { 21 | target.UnlockBits(bitmapData); 22 | } 23 | } 24 | 25 | // default .NET upscaling tries to interpolate, which we avoid here 26 | public static void Upscale(Bitmap source, Bitmap target) { 27 | if (target.Width % source.Width != 0 || target.Height % source.Height != 0) 28 | throw new ArgumentException(); 29 | 30 | int scaleY = target.Height / source.Height; 31 | int scaleX = target.Width / source.Width; 32 | 33 | var sourceData = source.LockBits(new Rectangle(new Point(), source.Size), 34 | ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed); 35 | try { 36 | var targetData = target.LockBits(new Rectangle(new Point(), target.Size), 37 | ImageLockMode.WriteOnly, 38 | PixelFormat.Format8bppIndexed); 39 | 40 | try { 41 | for (int sourceY = 0; sourceY < sourceData.Height; sourceY++) 42 | for (int sourceX = 0; sourceX < sourceData.Width; sourceX++) { 43 | byte brightness = Marshal.ReadByte(sourceData.Scan0, 44 | sourceY * sourceData.Width + sourceX); 45 | for (int targetY = sourceY * scaleY; 46 | targetY < (sourceY + 1) * scaleY; 47 | targetY++) 48 | for (int targetX = sourceX * scaleX; 49 | targetX < (sourceX + 1) * scaleX; 50 | targetX++) 51 | Marshal.WriteByte(targetData.Scan0, 52 | targetY * targetData.Width + targetX, 53 | brightness); 54 | } 55 | } finally { 56 | target.UnlockBits(targetData); 57 | } 58 | } finally { 59 | source.UnlockBits(sourceData); 60 | } 61 | } 62 | 63 | public static void SetGreyscalePalette(Bitmap bitmap) { 64 | ColorPalette pal = bitmap.Palette; 65 | 66 | for (int i = 0; i < 256; i++) { 67 | pal.Entries[i] = Color.FromArgb(255, i, i, i); 68 | } 69 | 70 | bitmap.Palette = pal; 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /CSharpOrNot/CSharpOrNot.cs: -------------------------------------------------------------------------------- 1 | namespace LostTech.Gradient.Samples { 2 | using System; 3 | using System.Drawing; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Text; 7 | using numpy; 8 | using tensorflow; 9 | using tensorflow.keras; 10 | using tensorflow.keras.layers; 11 | 12 | static class CSharpOrNot 13 | { 14 | public static Model CreateModel(int classCount) { 15 | var activation = tf.keras.activations.elu_fn; 16 | const int filterCount = 8; 17 | int[] resNetFilters = { filterCount, filterCount, filterCount }; 18 | return new Sequential(new Layer[] { 19 | new Dropout(rate: 0.05), 20 | new Conv2D(filters: filterCount, kernel_size: 5, strides: (1,1), padding: "same"), 21 | new Activation(activation), 22 | new MaxPool2D(pool_size: 2), 23 | new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation), 24 | new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation), 25 | new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation), 26 | new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation), 27 | new MaxPool2D(), 28 | new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation), 29 | new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation), 30 | new MaxPool2D(), 31 | new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation), 32 | new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation), 33 | new AvgPool2D(pool_size: new []{2,2}), 34 | new Flatten(), 35 | new Dense(units: classCount, activation: tf.keras.activations.softmax_fn), 36 | }); 37 | } 38 | 39 | public const int Width = 64, Height = 64; 40 | public static readonly Size Size = new Size(Width, Height); 41 | // being opinionated here 42 | const string Tab = " "; 43 | const char Whitespace = '\u00FF'; 44 | public static readonly string[] IncludeExtensions = { 45 | ".cs", 46 | ".py", 47 | ".h", 48 | ".cc", 49 | ".c", 50 | ".tcl", 51 | ".java", 52 | ".sh", 53 | }; 54 | 55 | public static ndarray GreyscaleImageBytesToNumPy(byte[] inputs, int imageCount, int width, int height) 56 | => (ndarray)inputs.Select(b => b/255.0f).ToArray().ToNumPyArray() 57 | .reshape(new[] { imageCount, height, width, 1 }); 58 | 59 | public static string[] ReadCode(string filePath) 60 | => File.ReadAllLines(filePath) 61 | .Select(line => line.Replace("\t", Tab)) 62 | .Select(line => { 63 | var result = new StringBuilder(line.Length); 64 | // replace non-ASCII characters with underscore 65 | // also make all whitespace stand out 66 | foreach (char c in line) { 67 | result.Append( 68 | c <= 32 ? Whitespace 69 | : c >= 255 ? '_' 70 | : c); 71 | } 72 | return result.ToString(); 73 | }) 74 | .ToArray(); 75 | 76 | /// 77 | /// Copies a rectangular block of text into a byte array 78 | /// 79 | public static void RenderTextBlockToGreyscaleBytes(string[] lines, 80 | Point startingPoint, Size size, 81 | byte[] destination) 82 | { 83 | if (size.IsEmpty) throw new ArgumentException(); 84 | if (destination.Length < size.Width * size.Height) throw new ArgumentException(); 85 | if (startingPoint.Y == lines.Length) { 86 | Array.Fill(destination, (byte)Whitespace); 87 | return; 88 | } 89 | 90 | for (int y = 0; y < size.Height; y++) { 91 | int sourceY = y + startingPoint.Y; 92 | int destOffset = y * size.Width; 93 | if (sourceY >= lines.Length) { 94 | Array.Fill(destination, (byte)255, 95 | startIndex: destOffset, 96 | count: size.Width*size.Height - destOffset); 97 | return; 98 | } 99 | 100 | for (int x = 0; x < size.Width; x++) { 101 | int sourceX = x + startingPoint.X; 102 | if (sourceX >= lines[sourceY].Length) { 103 | Array.Fill(destination, (byte)255, 104 | startIndex: destOffset, 105 | count: size.Width - x); 106 | break; 107 | } 108 | 109 | destination[destOffset] = (byte)lines[sourceY][sourceX]; 110 | destOffset++; 111 | } 112 | } 113 | } 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /CSharpOrNot/CSharpOrNot.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | Exe 4 | net6.0 5 | 6 | 7 | LostTech.Gradient.Samples 8 | 9 | 10 | 11 | %(Filename) 12 | 13 | 14 | Designer 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /CSharpOrNot/CSharpOrNotProgram.cs: -------------------------------------------------------------------------------- 1 | namespace LostTech.Gradient.Samples { 2 | using System; 3 | using System.Linq; 4 | using Avalonia; 5 | using Avalonia.Logging.Serilog; 6 | using LostTech.Gradient; 7 | using LostTech.TensorFlow; 8 | using ManyConsole.CommandLineUtils; 9 | using tensorflow; 10 | using tensorflow.core.protobuf.config_pb2; 11 | 12 | static class CSharpOrNotProgram { 13 | public static int Main(string[] args) { 14 | TensorFlowSetup.Instance.OptInToUsageDataCollection(); 15 | GradientEngine.UseEnvironmentFromVariable(); 16 | 17 | var gpus = tf.config.list_physical_devices("GPU"); 18 | foreach(var gpu in gpus) 19 | tf.config.experimental.set_memory_growth(gpu, enable: true); 20 | 21 | return ConsoleCommandDispatcher.DispatchCommand( 22 | ConsoleCommandDispatcher.FindCommandsInSameAssemblyAs(typeof(CSharpOrNotProgram)), 23 | args, Console.Out); 24 | } 25 | 26 | // Avalonia configuration, don't remove; also used by visual designer. 27 | public static AppBuilder BuildAvaloniaApp() 28 | => AppBuilder.Configure() 29 | .UsePlatformDetect() 30 | .LogToDebug(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /CSharpOrNot/CSharpOrNotWindow.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 15 | 16 | 17 | 20 | 21 | 23 | 24 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /CSharpOrNot/CSharpOrNotWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | namespace LostTech.Gradient.Samples { 2 | using System; 3 | using System.Drawing.Imaging; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | using Avalonia; 8 | using Avalonia.Controls; 9 | using Avalonia.Interactivity; 10 | using Avalonia.Markup.Xaml; 11 | using Avalonia.Media; 12 | using LostTech.Gradient; 13 | using LostTech.Gradient.Exceptions; 14 | using LostTech.TensorFlow; 15 | using MoreLinq; 16 | using numpy; 17 | using tensorflow; 18 | using tensorflow.keras; 19 | using static LostTech.Gradient.Samples.CSharpOrNot; 20 | using Image = Avalonia.Controls.Image; 21 | using Point = System.Drawing.Point; 22 | using Bitmap = System.Drawing.Bitmap; 23 | using PixelFormat = System.Drawing.Imaging.PixelFormat; 24 | 25 | public class CSharpOrNotWindow : Window 26 | { 27 | readonly ContentControl languageBox; 28 | readonly TextBox codeDisplay; 29 | readonly TextBlock codeWindow; 30 | readonly TextBlock language; 31 | readonly Image codeImage; 32 | readonly Button openFileButton; 33 | string[] code; 34 | readonly Model model; 35 | bool loaded = false; 36 | public CSharpOrNotWindow() { 37 | this.InitializeComponent(); 38 | #if DEBUG 39 | this.AttachDevTools(); 40 | #endif 41 | this.codeDisplay = this.Get("CodeDisplay"); 42 | this.codeDisplay.PropertyChanged += this.CodeDisplayOnPropertyChanged; 43 | 44 | this.codeWindow = this.Get("CodeWindow"); 45 | this.language = this.Get("Language"); 46 | this.languageBox = this.Get("LanguageBox"); 47 | this.codeImage = this.Get("CodeImage"); 48 | this.openFileButton = this.Get