Keras.NET 0.5.0

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.

There is a newer version of this package available.
See the version list below for details.
Install-Package Keras.NET -Version 0.5.0
dotnet add package Keras.NET --version 0.5.0
<PackageReference Include="Keras.NET" Version="0.5.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Keras.NET --version 0.5.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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.7
  • Install keras and numpy

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:

Another example with Prima Indians Diabetic Dataset

Python example taken from: https://machinelearningmastery.com/save-load-keras-deep-learning-models/

//Load train data
NDarray dataset = np.loadtxt(fname: "pima-indians-diabetes.data.csv", delimiter: ",");
var X = dataset[":,0: 8"];
var Y = dataset[":, 8"];

//Build sequential model
var model = new Sequential();
model.Add(new Dense(12, input_dim: 8, kernel_initializer: "uniform", activation: "relu"));
model.Add(new Dense(8, kernel_initializer: "uniform", activation: "relu"));
model.Add(new Dense(1, activation: "sigmoid"));

//Compile and train
model.Compile(optimizer:"adam", loss:"binary_crossentropy", metrics: new string[] { "accuracy" });
model.Fit(X, Y, batch_size: 10, epochs: 150, verbose: 1);

//Evaluate model
var scores = model.Evaluate(X, Y, verbose: 1);
Console.WriteLine("Accuracy: {0}", scores[1] * 100);

//Save model and weights
string json = model.ToJson();
File.WriteAllText("model.json", json);
model.SaveWeight("model.h5");
Console.WriteLine("Saved model to disk");
//Load model and weight
var loaded_model = Sequential.ModelFromJson(File.ReadAllText("model.json"));
loaded_model.LoadWeight("model.h5");
Console.WriteLine("Loaded model from disk");

loaded_model.Compile(optimizer: "rmsprop", loss: "binary_crossentropy", metrics: new string[] { "accuracy" });
scores = model.Evaluate(X, Y, verbose: 1);
Console.WriteLine("Accuracy: {0}", scores[1] * 100);

Output

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.7
  • Install keras and numpy

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:

Another example with Prima Indians Diabetic Dataset

Python example taken from: https://machinelearningmastery.com/save-load-keras-deep-learning-models/

//Load train data
NDarray dataset = np.loadtxt(fname: "pima-indians-diabetes.data.csv", delimiter: ",");
var X = dataset[":,0: 8"];
var Y = dataset[":, 8"];

//Build sequential model
var model = new Sequential();
model.Add(new Dense(12, input_dim: 8, kernel_initializer: "uniform", activation: "relu"));
model.Add(new Dense(8, kernel_initializer: "uniform", activation: "relu"));
model.Add(new Dense(1, activation: "sigmoid"));

//Compile and train
model.Compile(optimizer:"adam", loss:"binary_crossentropy", metrics: new string[] { "accuracy" });
model.Fit(X, Y, batch_size: 10, epochs: 150, verbose: 1);

//Evaluate model
var scores = model.Evaluate(X, Y, verbose: 1);
Console.WriteLine("Accuracy: {0}", scores[1] * 100);

//Save model and weights
string json = model.ToJson();
File.WriteAllText("model.json", json);
model.SaveWeight("model.h5");
Console.WriteLine("Saved model to disk");
//Load model and weight
var loaded_model = Sequential.ModelFromJson(File.ReadAllText("model.json"));
loaded_model.LoadWeight("model.h5");
Console.WriteLine("Loaded model from disk");

loaded_model.Compile(optimizer: "rmsprop", loss: "binary_crossentropy", metrics: new string[] { "accuracy" });
scores = model.Evaluate(X, Y, verbose: 1);
Console.WriteLine("Accuracy: {0}", scores[1] * 100);

Output

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
3.8.4.4 914 9/22/2020
3.7.4.4 205 9/22/2020
3.7.4.2 2,256 5/1/2020
3.7.4.1 812 3/23/2020
3.7.3 1,737 1/10/2020
3.6.4.2 329 5/1/2020
3.6.4.1 228 3/23/2020
3.6.3 417 1/10/2020
3.6.2.4 478 12/28/2019
3.6.2.3 273 12/28/2019
3.6.2.2 271 12/28/2019
3.6.2.1 158 12/28/2019
3.6.1.12 996 11/8/2019
3.6.1.11 560 10/6/2019
3.6.1.10 240 10/6/2019
3.6.1.9 279 9/27/2019
3.6.1.8 2,752 8/22/2019
3.6.1.6 320 8/14/2019
3.6.1.5 330 7/30/2019
3.6.1.4 304 7/22/2019
3.6.1.1 227 7/22/2019
3.5.4.2 119 5/1/2020
3.5.4.1 172 3/23/2020
3.5.3 229 1/10/2020
3.5.2.4 246 12/28/2019
3.5.2.3 253 12/28/2019
3.5.2.2 252 12/28/2019
3.5.2.1 133 12/28/2019
3.5.1.12 149 11/8/2019
3.5.1.11 157 10/6/2019
3.5.1.10 146 10/6/2019
3.5.1.9 166 9/27/2019
3.5.1.8 173 8/22/2019
3.5.1.6 166 8/14/2019
3.5.1.5 173 7/30/2019
3.5.1.4 243 7/22/2019
2.7.4.4 98 9/22/2020
2.7.4.2 115 5/1/2020
2.7.4.1 172 3/23/2020
2.7.3 225 1/10/2020
2.7.2.4 238 12/28/2019
2.7.2.3 246 12/28/2019
2.7.2.2 258 12/28/2019
2.7.2.1 264 12/28/2019
2.7.1.12 142 11/8/2019
2.7.1.11 155 10/6/2019
2.7.1.10 147 10/6/2019
2.7.1.9 147 9/27/2019
2.7.1.8 166 8/22/2019
2.7.1.6 166 8/14/2019
2.7.1.5 171 7/30/2019
2.7.1.4 186 7/22/2019
2.7.1.2 176 7/22/2019
2.7.1.1 208 7/22/2019
0.6.4 272 7/16/2019
0.6.3 300 6/26/2019
0.6.2 193 6/25/2019
0.6.0 215 6/21/2019
0.5.2 207 6/18/2019
0.5.0 228 6/18/2019
Show less