Keras.NET
0.6.0
See the version list below for details.
dotnet add package Keras.NET --version 0.6.0
NuGet\Install-Package Keras.NET -Version 0.6.0
<PackageReference Include="Keras.NET" Version="0.6.0" />
paket add Keras.NET --version 0.6.0
#r "nuget: Keras.NET, 0.6.0"
// Install Keras.NET as a Cake Addin #addin nuget:?package=Keras.NET&version=0.6.0 // Install Keras.NET as a Cake Tool #tool nuget:?package=Keras.NET&version=0.6.0
Keras.NET
Keras.NET is a high-level neural networks API, written in C# with Python Binding and capable of running on top of TensorFlow, CNTK, or Theano. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result with the least possible delay is key to doing good research.
Use Keras if you need a deep learning library that:
Allows for easy and fast prototyping (through user friendliness, modularity, and extensibility). Supports both convolutional networks and recurrent networks, as well as combinations of the two. Runs seamlessly on CPU and GPU.
Keras.NET is using:
Prerequisite
- Python 3.6, Link: https://www.python.org/downloads/
- Install keras, numpy and one of the backend (Tensorflow/CNTK/Theano). Please see on how to configure: https://keras.io/backend/
Nuget
Install from nuget: https://www.nuget.org/packages/Keras.NET
Install-Package Keras.NET
dotnet add package Keras.NET
Example with XOR sample
//Load train data
NDarray x = np.array(new float[,] { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } });
NDarray y = np.array(new float[] { 0, 1, 1, 0 });
//Build sequential model
var model = new Sequential();
model.Add(new Dense(32, activation: "relu", input_shape: new Shape(2)));
model.Add(new Dense(64, activation: "relu"));
model.Add(new Dense(1, activation: "sigmoid"));
//Compile and train
model.Compile(optimizer:"sgd", loss:"binary_crossentropy", metrics: new string[] { "accuracy" });
model.Fit(x, y, batch_size: 2, epochs: 1000, verbose: 1);
//Save model and weights
string json = model.ToJson();
File.WriteAllText("model.json", json);
model.SaveWeight("model.h5");
//Load model and weight
var loaded_model = Sequential.ModelFromJson(File.ReadAllText("model.json"));
loaded_model.LoadWeight("model.h5");
Output:
MNIST CNN Example
Python example taken from: https://keras.io/examples/mnist_cnn/
int batch_size = 128;
int num_classes = 10;
int epochs = 12;
// input image dimensions
int img_rows = 28, img_cols = 28;
Shape input_shape = null;
// the data, split between train and test sets
var ((x_train, y_train), (x_test, y_test)) = MNIST.LoadData();
if(K.ImageDataFormat() == "channels_first")
{
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols);
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols);
input_shape = (1, img_rows, img_cols);
}
else
{
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1);
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1);
input_shape = (img_rows, img_cols, 1);
}
x_train = x_train.astype(np.float32);
x_test = x_test.astype(np.float32);
x_train /= 255;
x_test /= 255;
Console.WriteLine("x_train shape: " + x_train.shape);
Console.WriteLine(x_train.shape[0] + " train samples");
Console.WriteLine(x_test.shape[0] + " test samples");
// convert class vectors to binary class matrices
y_train = Utils.ToCategorical(y_train, num_classes);
y_test = Utils.ToCategorical(y_test, num_classes);
// Build CNN model
var model = new Sequential();
model.Add(new Conv2D(32, kernel_size: (3, 3).ToTuple(),
activation: "relu",
input_shape: input_shape));
model.Add(new Conv2D(64, (3, 3).ToTuple(), activation: "relu"));
model.Add(new MaxPooling2D(pool_size: (2, 2).ToTuple()));
model.Add(new Dropout(0.25));
model.Add(new Flatten());
model.Add(new Dense(128, activation: "relu"));
model.Add(new Dropout(0.5));
model.Add(new Dense(num_classes, activation: "softmax"));
model.Compile(loss: "categorical_crossentropy",
optimizer: new Adadelta(), metrics: new string[] { "accuracy" });
model.Fit(x_train, y_train,
batch_size: batch_size,
epochs: epochs,
verbose: 1,
validation_data: new NDarray[] { x_test, y_test });
var score = model.Evaluate(x_test, y_test, verbose: 0);
Console.WriteLine("Test loss:", score[0]);
Console.WriteLine("Test accuracy:", score[1]);
Output
Reached 98% accuracy within 3 epoches.
Documentation
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Numpy.Bare (>= 3.6.1.1)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Keras.NET:
Package | Downloads |
---|---|
Laraue.Core.Keras
Utils to launch Keras models in .NET |
|
BasicSamples
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.8.5 | 22,045 | 12/7/2020 |
3.8.4.4 | 2,604 | 9/22/2020 |
3.7.5 | 1,373 | 12/7/2020 |
3.7.4.4 | 1,080 | 9/22/2020 |
3.7.4.2 | 8,683 | 5/1/2020 |
3.7.4.1 | 1,456 | 3/23/2020 |
3.7.3 | 2,423 | 1/10/2020 |
3.6.4.2 | 1,048 | 5/1/2020 |
3.6.4.1 | 631 | 3/23/2020 |
3.6.3 | 880 | 1/10/2020 |
3.6.2.4 | 897 | 12/28/2019 |
3.6.2.3 | 627 | 12/28/2019 |
3.6.2.2 | 665 | 12/28/2019 |
3.6.2.1 | 539 | 12/28/2019 |
3.6.1.12 | 1,452 | 11/8/2019 |
3.6.1.11 | 980 | 10/6/2019 |
3.6.1.10 | 635 | 10/6/2019 |
3.6.1.9 | 658 | 9/27/2019 |
3.6.1.8 | 3,476 | 8/22/2019 |
3.6.1.6 | 709 | 8/14/2019 |
3.6.1.5 | 747 | 7/30/2019 |
3.6.1.4 | 666 | 7/22/2019 |
3.6.1.1 | 651 | 7/22/2019 |
3.5.4.2 | 558 | 5/1/2020 |
3.5.4.1 | 556 | 3/23/2020 |
3.5.3 | 676 | 1/10/2020 |
3.5.2.4 | 591 | 12/28/2019 |
3.5.2.3 | 613 | 12/28/2019 |
3.5.2.2 | 631 | 12/28/2019 |
3.5.2.1 | 511 | 12/28/2019 |
3.5.1.12 | 533 | 11/8/2019 |
3.5.1.11 | 520 | 10/6/2019 |
3.5.1.10 | 537 | 10/6/2019 |
3.5.1.9 | 547 | 9/27/2019 |
3.5.1.8 | 561 | 8/22/2019 |
3.5.1.6 | 540 | 8/14/2019 |
3.5.1.5 | 551 | 7/30/2019 |
3.5.1.4 | 684 | 7/22/2019 |
2.7.5 | 512 | 12/7/2020 |
2.7.4.4 | 463 | 9/22/2020 |
2.7.4.2 | 510 | 5/1/2020 |
2.7.4.1 | 546 | 3/23/2020 |
2.7.3 | 612 | 1/10/2020 |
2.7.2.4 | 596 | 12/28/2019 |
2.7.2.3 | 629 | 12/28/2019 |
2.7.2.2 | 650 | 12/28/2019 |
2.7.2.1 | 626 | 12/28/2019 |
2.7.1.12 | 516 | 11/8/2019 |
2.7.1.11 | 544 | 10/6/2019 |
2.7.1.10 | 511 | 10/6/2019 |
2.7.1.9 | 506 | 9/27/2019 |
2.7.1.8 | 533 | 8/22/2019 |
2.7.1.6 | 533 | 8/14/2019 |
2.7.1.5 | 533 | 7/30/2019 |
2.7.1.4 | 547 | 7/22/2019 |
2.7.1.2 | 570 | 7/22/2019 |
2.7.1.1 | 652 | 7/22/2019 |
0.6.4 | 729 | 7/16/2019 |
0.6.3 | 682 | 6/26/2019 |
0.6.2 | 562 | 6/25/2019 |
0.6.0 | 579 | 6/21/2019 |
0.5.2 | 575 | 6/18/2019 |
0.5.0 | 634 | 6/18/2019 |