Skip to content

Commit 8c0f45f

Browse files
committed
added FashionMNIST F# edition
1 parent 81fe79a commit 8c0f45f

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
open System
2+
open FSharp.Interop.Dynamic
3+
open FSharp.Interop.Dynamic.Operators
4+
5+
open Gradient
6+
open numpy
7+
open tensorflow
8+
open tensorflow.keras
9+
open tensorflow.keras.layers
10+
open tensorflow.keras.optimizers
11+
open SharPy.Runtime
12+
13+
[<EntryPoint>]
14+
let main argv =
15+
GradientSetup.OptInToUsageDataCollection()
16+
GradientSetup.UseEnvironmentFromVariable() |> ignore
17+
18+
GradientLog.OutputWriter <- Console.Out
19+
20+
let struct (train, test) = tf.keras.datasets.fashion_mnist.load_data()
21+
let trainImages: ndarray = train?Item1 ?/? 255.0f
22+
let trainLabels = train?Item2
23+
let testImages: ndarray = test?Item1 ?/? 255.0f
24+
let testLabels: ndarray = test?Item2
25+
26+
let inputArgs = PythonDict<_,obj>()
27+
inputArgs.["input_shape"] <- struct (28, 28)
28+
let model = Sequential([|
29+
Flatten(kwargs = inputArgs) :> obj;
30+
Dense(units = 128, activation = tf.nn.relu_fn) :> obj;
31+
Dense(units = 10, activation = tf.nn.softmax_fn) :> obj;
32+
|])
33+
34+
model.compile(
35+
optimizer = Adam(),
36+
loss = tf.keras.losses.sparse_categorical_crossentropy_fn,
37+
metrics = ["accuracy"])
38+
39+
model.fit(trainImages, trainLabels, epochs = 5) |> ignore
40+
41+
let evalResult = model.evaluate(testImages, testLabels)
42+
let accuracy = Core.Operators.float (Dyn.getIndex evalResult [1] : numpy.float64)
43+
printfn "Test accuracy: %f" accuracy
44+
45+
0 // return an integer exit code
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.2</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="FashionMnist.fs" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="FSharp.Interop.Dynamic" Version="4.0.3.130" />
14+
<PackageReference Include="Gradient" Version="0.1.10-tech-preview5.1.1" />
15+
</ItemGroup>
16+
</Project>

Gradient-Samples.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "VB", "VB", "{875975E4-796F-
2929
EndProject
3030
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "BasicMathVB", "VB\BasicMathVB\BasicMathVB.vbproj", "{453E1365-83C2-46B6-B8E0-728B778CA1A7}"
3131
EndProject
32-
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "FashionMnistVB", "VB\FashionMnistVB\FashionMnistVB.vbproj", "{1077A08A-AB90-4286-BB0C-0F2E9D620595}"
32+
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "FashionMnistVB", "VB\FashionMnistVB\FashionMnistVB.vbproj", "{1077A08A-AB90-4286-BB0C-0F2E9D620595}"
33+
EndProject
34+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FashionMnistF", "FSharp\FashionMnistF\FashionMnistF.fsproj", "{670A666C-1A56-40B0-874D-72673623112E}"
3335
EndProject
3436
Global
3537
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -73,6 +75,10 @@ Global
7375
{1077A08A-AB90-4286-BB0C-0F2E9D620595}.Debug|Any CPU.Build.0 = Debug|Any CPU
7476
{1077A08A-AB90-4286-BB0C-0F2E9D620595}.Release|Any CPU.ActiveCfg = Release|Any CPU
7577
{1077A08A-AB90-4286-BB0C-0F2E9D620595}.Release|Any CPU.Build.0 = Release|Any CPU
78+
{670A666C-1A56-40B0-874D-72673623112E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
79+
{670A666C-1A56-40B0-874D-72673623112E}.Debug|Any CPU.Build.0 = Debug|Any CPU
80+
{670A666C-1A56-40B0-874D-72673623112E}.Release|Any CPU.ActiveCfg = Release|Any CPU
81+
{670A666C-1A56-40B0-874D-72673623112E}.Release|Any CPU.Build.0 = Release|Any CPU
7682
EndGlobalSection
7783
GlobalSection(SolutionProperties) = preSolution
7884
HideSolutionNode = FALSE
@@ -81,6 +87,7 @@ Global
8187
{CEF0739E-BD49-466E-9F8D-226C9C474DB4} = {B18FC4F9-E13F-437F-A8DB-4783E9C0F356}
8288
{453E1365-83C2-46B6-B8E0-728B778CA1A7} = {875975E4-796F-4181-BACE-7DC71CE35D1F}
8389
{1077A08A-AB90-4286-BB0C-0F2E9D620595} = {875975E4-796F-4181-BACE-7DC71CE35D1F}
90+
{670A666C-1A56-40B0-874D-72673623112E} = {B18FC4F9-E13F-437F-A8DB-4783E9C0F356}
8491
EndGlobalSection
8592
GlobalSection(ExtensibilityGlobals) = postSolution
8693
SolutionGuid = {052B6168-274F-43B0-B3F0-24F5C5D4BE58}

0 commit comments

Comments
 (0)