Browse Source

abstract PrepareData interface for example

tags/v0.8.0
Oceania2018 6 years ago
parent
commit
5a2433c7ea
15 changed files with 70 additions and 27 deletions
  1. +1
    -0
      README.md
  2. +2
    -1
      src/TensorFlowNET.Core/TensorFlowNET.Core.csproj
  3. +5
    -0
      test/TensorFlowNET.Examples/BasicEagerApi.cs
  4. +4
    -0
      test/TensorFlowNET.Examples/BasicOperations.cs
  5. +4
    -0
      test/TensorFlowNET.Examples/HelloWorld.cs
  6. +1
    -0
      test/TensorFlowNET.Examples/IExample.cs
  7. +1
    -1
      test/TensorFlowNET.Examples/ImageRecognition.cs
  8. +1
    -1
      test/TensorFlowNET.Examples/InceptionArchGoogLeNet.cs
  9. +13
    -5
      test/TensorFlowNET.Examples/LinearRegression.cs
  10. +5
    -4
      test/TensorFlowNET.Examples/LogisticRegression.cs
  11. +4
    -0
      test/TensorFlowNET.Examples/MetaGraph.cs
  12. +8
    -3
      test/TensorFlowNET.Examples/NaiveBayesClassifier.cs
  13. +5
    -0
      test/TensorFlowNET.Examples/NamedEntityRecognition.cs
  14. +8
    -9
      test/TensorFlowNET.Examples/TextClassification/TextClassificationTrain.cs
  15. +8
    -3
      test/TensorFlowNET.Examples/TextClassification/TextClassificationWithMovieReviews.cs

+ 1
- 0
README.md View File

@@ -72,6 +72,7 @@ Read the docs & book [The Definitive Guide to Tensorflow.NET](https://tensorflow
* [Basic Operations](test/TensorFlowNET.Examples/BasicOperations.cs)
* [Image Recognition](test/TensorFlowNET.Examples/ImageRecognition.cs)
* [Linear Regression](test/TensorFlowNET.Examples/LinearRegression.cs)
* [Logistic Regression](test/TensorFlowNET.Examples/LogisticRegression.cs)
* [Text Classification](test/TensorFlowNET.Examples/TextClassificationWithMovieReviews.cs)
* [CNN Text Classification](test/TensorFlowNET.Examples/CnnTextClassification.cs)
* [Naive Bayes Classification](test/TensorFlowNET.Examples/NaiveBayesClassifier.cs)


+ 2
- 1
src/TensorFlowNET.Core/TensorFlowNET.Core.csproj View File

@@ -17,7 +17,8 @@
<Description>Google's TensorFlow binding in .NET Standard.
Docs: https://tensorflownet.readthedocs.io</Description>
<AssemblyVersion>0.5.0.0</AssemblyVersion>
<PackageReleaseNotes>Add a lot of APIs to build neural networks model</PackageReleaseNotes>
<PackageReleaseNotes>Add Logistic Regression to do MNIST.
Add a lot of APIs to build neural networks model</PackageReleaseNotes>
<LangVersion>7.2</LangVersion>
<FileVersion>0.5.0.0</FileVersion>
</PropertyGroup>


+ 5
- 0
test/TensorFlowNET.Examples/BasicEagerApi.cs View File

@@ -12,6 +12,7 @@ namespace TensorFlowNET.Examples
public class BasicEagerApi : IExample
{
private Tensor a, b, c, d;

public void Run()
{
// Set Eager API
@@ -34,5 +35,9 @@ namespace TensorFlowNET.Examples

// Full compatibility with Numpy
}

public void PrepareData()
{
}
}
}

+ 4
- 0
test/TensorFlowNET.Examples/BasicOperations.cs View File

@@ -96,5 +96,9 @@ namespace TensorFlowNET.Examples
}
}
}

public void PrepareData()
{
}
}
}

+ 4
- 0
test/TensorFlowNET.Examples/HelloWorld.cs View File

@@ -33,5 +33,9 @@ namespace TensorFlowNET.Examples
}
}
}

public void PrepareData()
{
}
}
}

+ 1
- 0
test/TensorFlowNET.Examples/IExample.cs View File

@@ -11,5 +11,6 @@ namespace TensorFlowNET.Examples
public interface IExample
{
void Run();
void PrepareData();
}
}

+ 1
- 1
test/TensorFlowNET.Examples/ImageRecognition.cs View File

@@ -78,7 +78,7 @@ namespace TensorFlowNET.Examples
});
}

private void PrepareData()
public void PrepareData()
{
Directory.CreateDirectory(dir);



+ 1
- 1
test/TensorFlowNET.Examples/InceptionArchGoogLeNet.cs View File

@@ -83,7 +83,7 @@ namespace TensorFlowNET.Examples
});
}

private void PrepareData()
public void PrepareData()
{
Directory.CreateDirectory(dir);



+ 13
- 5
test/TensorFlowNET.Examples/LinearRegression.cs View File

@@ -19,14 +19,13 @@ namespace TensorFlowNET.Examples
int training_epochs = 1000;
int display_step = 50;

NDArray train_X, train_Y;
int n_samples;

public void Run()
{
// Training Data
var train_X = np.array(3.3f, 4.4f, 5.5f, 6.71f, 6.93f, 4.168f, 9.779f, 6.182f, 7.59f, 2.167f,
7.042f, 10.791f, 5.313f, 7.997f, 5.654f, 9.27f, 3.1f);
var train_Y = np.array(1.7f, 2.76f, 2.09f, 3.19f, 1.694f, 1.573f, 3.366f, 2.596f, 2.53f, 1.221f,
2.827f, 3.465f, 1.65f, 2.904f, 2.42f, 2.94f, 1.3f);
var n_samples = train_X.shape[0];
PrepareData();

// tf Graph Input
var X = tf.placeholder(tf.float32);
@@ -95,5 +94,14 @@ namespace TensorFlowNET.Examples
Console.WriteLine($"Absolute mean square loss difference: {Math.Abs((float)training_cost - (float)testing_cost)}");
});
}

public void PrepareData()
{
train_X = np.array(3.3f, 4.4f, 5.5f, 6.71f, 6.93f, 4.168f, 9.779f, 6.182f, 7.59f, 2.167f,
7.042f, 10.791f, 5.313f, 7.997f, 5.654f, 9.27f, 3.1f);
train_Y = np.array(1.7f, 2.76f, 2.09f, 3.19f, 1.694f, 1.573f, 3.366f, 2.596f, 2.53f, 1.221f,
2.827f, 3.465f, 1.65f, 2.904f, 2.42f, 2.94f, 1.3f);
n_samples = train_X.shape[0];
}
}
}

+ 5
- 4
test/TensorFlowNET.Examples/LogisticRegression.cs View File

@@ -21,9 +21,11 @@ namespace TensorFlowNET.Examples
private int batch_size = 100;
private int display_step = 1;

Datasets mnist;

public void Run()
{
var mnist = PrepareData();
PrepareData();

// tf Graph Input
var x = tf.placeholder(tf.float32, new TensorShape(-1, 784)); // mnist data image of shape 28*28=784
@@ -86,10 +88,9 @@ namespace TensorFlowNET.Examples
});
}

private Datasets PrepareData()
public void PrepareData()
{
var mnist = MnistDataSet.read_data_sets("logistic_regression", one_hot: true);
return mnist;
mnist = MnistDataSet.read_data_sets("logistic_regression", one_hot: true);
}
}
}

+ 4
- 0
test/TensorFlowNET.Examples/MetaGraph.cs View File

@@ -27,5 +27,9 @@ namespace TensorFlowNET.Examples
logits: logits);
});
}

public void PrepareData()
{
}
}
}

+ 8
- 3
test/TensorFlowNET.Examples/NaiveBayesClassifier.cs View File

@@ -15,9 +15,9 @@ namespace TensorFlowNET.Examples
public Normal dist { get; set; }
public void Run()
{
np.array<float>(1.0f, 1.0f);
var X = np.array<float>(new float[][] { new float[] { 1.0f, 1.0f }, new float[] { 2.0f, 2.0f }, new float[] { -1.0f, -1.0f }, new float[] { -2.0f, -2.0f }, new float[] { 1.0f, -1.0f }, new float[] { 2.0f, -2.0f }, });
var y = np.array<int>(0,0,1,1,2,2);
np.array(1.0f, 1.0f);
var X = np.array(new float[][] { new float[] { 1.0f, 1.0f }, new float[] { 2.0f, 2.0f }, new float[] { -1.0f, -1.0f }, new float[] { -2.0f, -2.0f }, new float[] { 1.0f, -1.0f }, new float[] { 2.0f, -2.0f }, });
var y = np.array(0,0,1,1,2,2);
fit(X, y);
// Create a regular grid and classify each point
}
@@ -102,5 +102,10 @@ namespace TensorFlowNET.Examples
// exp to get the actual probabilities
return tf.exp(log_prob);
}

public void PrepareData()
{
}
}
}

+ 5
- 0
test/TensorFlowNET.Examples/NamedEntityRecognition.cs View File

@@ -14,5 +14,10 @@ namespace TensorFlowNET.Examples
{
throw new NotImplementedException();
}

public void PrepareData()
{
throw new NotImplementedException();
}
}
}

+ 8
- 9
test/TensorFlowNET.Examples/TextClassification/TextClassificationTrain.cs View File

@@ -23,7 +23,7 @@ namespace TensorFlowNET.Examples.CnnTextClassification

public void Run()
{
download_dbpedia();
PrepareData();
Console.WriteLine("Building dataset...");
var (x, y, alphabet_size) = DataHelpers.build_char_dataset("train", "vdcnn", CHAR_MAX_LEN);

@@ -32,17 +32,9 @@ namespace TensorFlowNET.Examples.CnnTextClassification
with(tf.Session(), sess =>
{
new VdCnn(alphabet_size, CHAR_MAX_LEN, NUM_CLASS);

});
}

public void download_dbpedia()
{
string url = "https://github.com/le-scientifique/torchDatasets/raw/master/dbpedia_csv.tar.gz";
Web.Download(url, dataDir, dataFileName);
Compress.ExtractTGZ(Path.Join(dataDir, dataFileName), dataDir);
}

private (int[][], int[][], int[], int[]) train_test_split(int[][] x, int[] y, float test_size = 0.3f)
{
int len = x.Length;
@@ -75,5 +67,12 @@ namespace TensorFlowNET.Examples.CnnTextClassification

return (train_x.ToArray(), valid_x.ToArray(), train_y.ToArray(), valid_y.ToArray());
}

public void PrepareData()
{
string url = "https://github.com/le-scientifique/torchDatasets/raw/master/dbpedia_csv.tar.gz";
Web.Download(url, dataDir, dataFileName);
Compress.ExtractTGZ(Path.Join(dataDir, dataFileName), dataDir);
}
}
}

+ 8
- 3
test/TensorFlowNET.Examples/TextClassification/TextClassificationWithMovieReviews.cs View File

@@ -13,10 +13,11 @@ namespace TensorFlowNET.Examples
{
string dir = "text_classification_with_movie_reviews";
string dataFile = "imdb.zip";
NDArray train_data, train_labels, test_data, test_labels;

public void Run()
{
var((train_data, train_labels), (test_data, test_labels)) = PrepareData();
PrepareData();

Console.WriteLine($"Training entries: {train_data.size}, labels: {train_labels.size}");

@@ -40,7 +41,7 @@ namespace TensorFlowNET.Examples
model.add(keras.layers.Embedding(vocab_size, 16));
}

private ((NDArray, NDArray), (NDArray, NDArray)) PrepareData()
public void PrepareData()
{
Directory.CreateDirectory(dir);

@@ -71,7 +72,11 @@ namespace TensorFlowNET.Examples
var y_train = labels_train;
var y_test = labels_test;

return ((x_train, y_train), (x_test, y_test));
x_train = train_data;
train_labels = y_train;

test_data = x_test;
test_labels = y_test;
}

private NDArray ReadData(string file)


Loading…
Cancel
Save