Browse Source

rename Text to TextProcess.

tags/v0.9
Oceania2018 6 years ago
parent
commit
9ab30660bc
20 changed files with 188 additions and 224 deletions
  1. +1
    -0
      test/TensorFlowNET.Examples/AudioProcess/README.md
  2. +0
    -43
      test/TensorFlowNET.Examples/MetaGraph.cs
  3. +0
    -0
      test/TensorFlowNET.Examples/Models/KMeansClustering.cs
  4. +0
    -0
      test/TensorFlowNET.Examples/Models/LinearRegression.cs
  5. +0
    -0
      test/TensorFlowNET.Examples/Models/LogisticRegression.cs
  6. +0
    -0
      test/TensorFlowNET.Examples/Models/NaiveBayesClassifier.cs
  7. +0
    -0
      test/TensorFlowNET.Examples/Models/NearestNeighbor.cs
  8. +156
    -156
      test/TensorFlowNET.Examples/Models/NeuralNetXor.cs
  9. +0
    -0
      test/TensorFlowNET.Examples/TextProcess/BinaryTextClassification.cs
  10. +2
    -2
      test/TensorFlowNET.Examples/TextProcess/DataHelpers.cs
  11. +0
    -0
      test/TensorFlowNET.Examples/TextProcess/NER/BiLstmCrfNer.cs
  12. +0
    -0
      test/TensorFlowNET.Examples/TextProcess/NER/CRF.cs
  13. +0
    -0
      test/TensorFlowNET.Examples/TextProcess/NER/LstmCrfNer.cs
  14. +0
    -0
      test/TensorFlowNET.Examples/TextProcess/NamedEntityRecognition.cs
  15. +0
    -0
      test/TensorFlowNET.Examples/TextProcess/TextClassificationTrain.cs
  16. +0
    -0
      test/TensorFlowNET.Examples/TextProcess/Word2Vec.cs
  17. +14
    -14
      test/TensorFlowNET.Examples/TextProcess/cnn_models/ITextClassificationModel.cs
  18. +0
    -0
      test/TensorFlowNET.Examples/TextProcess/cnn_models/VdCnn.cs
  19. +0
    -8
      test/TensorFlowNET.UnitTest/ExamplesTests/ExamplesTest.cs
  20. +15
    -1
      test/TensorFlowNET.UnitTest/GraphTest.cs

+ 1
- 0
test/TensorFlowNET.Examples/AudioProcess/README.md View File

@@ -0,0 +1 @@


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

@@ -1,43 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Tensorflow;
using static Tensorflow.Python;

namespace TensorFlowNET.Examples
{
public class MetaGraph : IExample
{
public int Priority => 100;
public bool Enabled { get; set; } = false;
public string Name => "Meta Graph";
public bool ImportGraph { get; set; } = true;


public bool Run()
{
ImportMetaGraph("my-save-dir/");
return false;
}

private void ImportMetaGraph(string dir)
{
with(tf.Session(), sess =>
{
var new_saver = tf.train.import_meta_graph(dir + "my-model-10000.meta");
new_saver.restore(sess, dir + "my-model-10000");
var labels = tf.constant(0, dtype: tf.int32, shape: new int[] { 100 }, name: "labels");
var batch_size = tf.size(labels);
var logits = (tf.get_collection("logits") as List<ITensorOrOperation>)[0] as Tensor;
var loss = tf.losses.sparse_softmax_cross_entropy(labels: labels,
logits: logits);
});
}

public void PrepareData()
{
}
}
}

test/TensorFlowNET.Examples/KMeansClustering.cs → test/TensorFlowNET.Examples/Models/KMeansClustering.cs View File


test/TensorFlowNET.Examples/LinearRegression.cs → test/TensorFlowNET.Examples/Models/LinearRegression.cs View File


test/TensorFlowNET.Examples/LogisticRegression.cs → test/TensorFlowNET.Examples/Models/LogisticRegression.cs View File


test/TensorFlowNET.Examples/NaiveBayesClassifier.cs → test/TensorFlowNET.Examples/Models/NaiveBayesClassifier.cs View File


test/TensorFlowNET.Examples/NearestNeighbor.cs → test/TensorFlowNET.Examples/Models/NearestNeighbor.cs View File


test/TensorFlowNET.Examples/NeuralNetXor.cs → test/TensorFlowNET.Examples/Models/NeuralNetXor.cs View File

@@ -1,156 +1,156 @@
using System;
using System.Collections.Generic;
using System.Text;
using NumSharp;
using Tensorflow;
using TensorFlowNET.Examples.Utility;
using static Tensorflow.Python;
namespace TensorFlowNET.Examples
{
/// <summary>
/// Simple vanilla neural net solving the famous XOR problem
/// https://github.com/amygdala/tensorflow-workshop/blob/master/workshop_sections/getting_started/xor/README.md
/// </summary>
public class NeuralNetXor : IExample
{
public int Priority => 10;
public bool Enabled { get; set; } = true;
public string Name => "NN XOR";
public bool ImportGraph { get; set; } = false;
public int num_steps = 10000;
private NDArray data;
private (Operation, Tensor, Tensor) make_graph(Tensor features,Tensor labels, int num_hidden = 8)
{
var stddev = 1 / Math.Sqrt(2);
var hidden_weights = tf.Variable(tf.truncated_normal(new int []{2, num_hidden}, seed:1, stddev: (float) stddev ));
// Shape [4, num_hidden]
var hidden_activations = tf.nn.relu(tf.matmul(features, hidden_weights));
var output_weights = tf.Variable(tf.truncated_normal(
new[] {num_hidden, 1},
seed: 17,
stddev: (float) (1 / Math.Sqrt(num_hidden))
));
// Shape [4, 1]
var logits = tf.matmul(hidden_activations, output_weights);
// Shape [4]
var predictions = tf.sigmoid(tf.squeeze(logits));
var loss = tf.reduce_mean(tf.square(predictions - tf.cast(labels, tf.float32)), name:"loss");
var gs = tf.Variable(0, trainable: false, name: "global_step");
var train_op = tf.train.GradientDescentOptimizer(0.2f).minimize(loss, global_step: gs);
return (train_op, loss, gs);
}
public bool Run()
{
PrepareData();
float loss_value = 0;
if (ImportGraph)
loss_value = RunWithImportedGraph();
else
loss_value = RunWithBuiltGraph();
return loss_value < 0.0628;
}
private float RunWithImportedGraph()
{
var graph = tf.Graph().as_default();
tf.train.import_meta_graph("graph/xor.meta");
Tensor features = graph.get_operation_by_name("Placeholder");
Tensor labels = graph.get_operation_by_name("Placeholder_1");
Tensor loss = graph.get_operation_by_name("loss");
Tensor train_op = graph.get_operation_by_name("train_op");
Tensor global_step = graph.get_operation_by_name("global_step");
var init = tf.global_variables_initializer();
float loss_value = 0;
// Start tf session
with(tf.Session(graph), sess =>
{
sess.run(init);
var step = 0;
var y_ = np.array(new int[] { 1, 0, 0, 1 }, dtype: np.int32);
while (step < num_steps)
{
// original python:
//_, step, loss_value = sess.run(
// [train_op, gs, loss],
// feed_dict={features: xy, labels: y_}
// )
var result = sess.run(new ITensorOrOperation[] { train_op, global_step, loss }, new FeedItem(features, data), new FeedItem(labels, y_));
loss_value = result[2];
step = result[1];
if (step % 1000 == 0)
Console.WriteLine($"Step {step} loss: {loss_value}");
}
Console.WriteLine($"Final loss: {loss_value}");
});
return loss_value;
}
private float RunWithBuiltGraph()
{
var graph = tf.Graph().as_default();
var features = tf.placeholder(tf.float32, new TensorShape(4, 2));
var labels = tf.placeholder(tf.int32, new TensorShape(4));
var (train_op, loss, gs) = make_graph(features, labels);
var init = tf.global_variables_initializer();
float loss_value = 0;
// Start tf session
with(tf.Session(graph), sess =>
{
sess.run(init);
var step = 0;
var y_ = np.array(new int[] { 1, 0, 0, 1 }, dtype: np.int32);
while (step < num_steps)
{
var result = sess.run(new ITensorOrOperation[] { train_op, gs, loss }, new FeedItem(features, data), new FeedItem(labels, y_));
loss_value = result[2];
step = result[1];
if (step % 1000 == 0)
Console.WriteLine($"Step {step} loss: {loss_value}");
}
Console.WriteLine($"Final loss: {loss_value}");
});
return loss_value;
}
public void PrepareData()
{
data = new float[,]
{
{1, 0 },
{1, 1 },
{0, 0 },
{0, 1 }
};
if (ImportGraph)
{
// download graph meta data
string url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/xor.meta";
Web.Download(url, "graph", "xor.meta");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using NumSharp;
using Tensorflow;
using TensorFlowNET.Examples.Utility;
using static Tensorflow.Python;
namespace TensorFlowNET.Examples
{
/// <summary>
/// Simple vanilla neural net solving the famous XOR problem
/// https://github.com/amygdala/tensorflow-workshop/blob/master/workshop_sections/getting_started/xor/README.md
/// </summary>
public class NeuralNetXor : IExample
{
public int Priority => 10;
public bool Enabled { get; set; } = true;
public string Name => "NN XOR";
public bool ImportGraph { get; set; } = false;
public int num_steps = 10000;
private NDArray data;
private (Operation, Tensor, Tensor) make_graph(Tensor features,Tensor labels, int num_hidden = 8)
{
var stddev = 1 / Math.Sqrt(2);
var hidden_weights = tf.Variable(tf.truncated_normal(new int []{2, num_hidden}, seed:1, stddev: (float) stddev ));
// Shape [4, num_hidden]
var hidden_activations = tf.nn.relu(tf.matmul(features, hidden_weights));
var output_weights = tf.Variable(tf.truncated_normal(
new[] {num_hidden, 1},
seed: 17,
stddev: (float) (1 / Math.Sqrt(num_hidden))
));
// Shape [4, 1]
var logits = tf.matmul(hidden_activations, output_weights);
// Shape [4]
var predictions = tf.sigmoid(tf.squeeze(logits));
var loss = tf.reduce_mean(tf.square(predictions - tf.cast(labels, tf.float32)), name:"loss");
var gs = tf.Variable(0, trainable: false, name: "global_step");
var train_op = tf.train.GradientDescentOptimizer(0.2f).minimize(loss, global_step: gs);
return (train_op, loss, gs);
}
public bool Run()
{
PrepareData();
float loss_value = 0;
if (ImportGraph)
loss_value = RunWithImportedGraph();
else
loss_value = RunWithBuiltGraph();
return loss_value < 0.0628;
}
private float RunWithImportedGraph()
{
var graph = tf.Graph().as_default();
tf.train.import_meta_graph("graph/xor.meta");
Tensor features = graph.get_operation_by_name("Placeholder");
Tensor labels = graph.get_operation_by_name("Placeholder_1");
Tensor loss = graph.get_operation_by_name("loss");
Tensor train_op = graph.get_operation_by_name("train_op");
Tensor global_step = graph.get_operation_by_name("global_step");
var init = tf.global_variables_initializer();
float loss_value = 0;
// Start tf session
with(tf.Session(graph), sess =>
{
sess.run(init);
var step = 0;
var y_ = np.array(new int[] { 1, 0, 0, 1 }, dtype: np.int32);
while (step < num_steps)
{
// original python:
//_, step, loss_value = sess.run(
// [train_op, gs, loss],
// feed_dict={features: xy, labels: y_}
// )
var result = sess.run(new ITensorOrOperation[] { train_op, global_step, loss }, new FeedItem(features, data), new FeedItem(labels, y_));
loss_value = result[2];
step = result[1];
if (step % 1000 == 0)
Console.WriteLine($"Step {step} loss: {loss_value}");
}
Console.WriteLine($"Final loss: {loss_value}");
});
return loss_value;
}
private float RunWithBuiltGraph()
{
var graph = tf.Graph().as_default();
var features = tf.placeholder(tf.float32, new TensorShape(4, 2));
var labels = tf.placeholder(tf.int32, new TensorShape(4));
var (train_op, loss, gs) = make_graph(features, labels);
var init = tf.global_variables_initializer();
float loss_value = 0;
// Start tf session
with(tf.Session(graph), sess =>
{
sess.run(init);
var step = 0;
var y_ = np.array(new int[] { 1, 0, 0, 1 }, dtype: np.int32);
while (step < num_steps)
{
var result = sess.run(new ITensorOrOperation[] { train_op, gs, loss }, new FeedItem(features, data), new FeedItem(labels, y_));
loss_value = result[2];
step = result[1];
if (step % 1000 == 0)
Console.WriteLine($"Step {step} loss: {loss_value}");
}
Console.WriteLine($"Final loss: {loss_value}");
});
return loss_value;
}
public void PrepareData()
{
data = new float[,]
{
{1, 0 },
{1, 1 },
{0, 0 },
{0, 1 }
};
if (ImportGraph)
{
// download graph meta data
string url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/xor.meta";
Web.Download(url, "graph", "xor.meta");
}
}
}
}

test/TensorFlowNET.Examples/Text/BinaryTextClassification.cs → test/TensorFlowNET.Examples/TextProcess/BinaryTextClassification.cs View File


test/TensorFlowNET.Examples/Text/DataHelpers.cs → test/TensorFlowNET.Examples/TextProcess/DataHelpers.cs View File

@@ -43,8 +43,8 @@ namespace TensorFlowNET.Examples.CnnTextClassification
x[i][j] = char_dict["<pad>"];
else
x[i][j] = char_dict.ContainsKey(content[j].ToString()) ? char_dict[content[j].ToString()] : char_dict["<unk>"];
}
}
y[i] = int.Parse(parts[0]);
}


test/TensorFlowNET.Examples/Text/NER/BiLstmCrfNer.cs → test/TensorFlowNET.Examples/TextProcess/NER/BiLstmCrfNer.cs View File


test/TensorFlowNET.Examples/Text/NER/CRF.cs → test/TensorFlowNET.Examples/TextProcess/NER/CRF.cs View File


test/TensorFlowNET.Examples/Text/NER/LstmCrfNer.cs → test/TensorFlowNET.Examples/TextProcess/NER/LstmCrfNer.cs View File


test/TensorFlowNET.Examples/NamedEntityRecognition.cs → test/TensorFlowNET.Examples/TextProcess/NamedEntityRecognition.cs View File


test/TensorFlowNET.Examples/Text/TextClassificationTrain.cs → test/TensorFlowNET.Examples/TextProcess/TextClassificationTrain.cs View File


test/TensorFlowNET.Examples/Text/Word2Vec.cs → test/TensorFlowNET.Examples/TextProcess/Word2Vec.cs View File


test/TensorFlowNET.Examples/Text/cnn_models/ITextClassificationModel.cs → test/TensorFlowNET.Examples/TextProcess/cnn_models/ITextClassificationModel.cs View File

@@ -1,14 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
using Tensorflow;
namespace TensorFlowNET.Examples.Text.cnn_models
{
interface ITextClassificationModel
{
Tensor is_training { get; }
Tensor x { get;}
Tensor y { get; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Tensorflow;
namespace TensorFlowNET.Examples.Text.cnn_models
{
interface ITextClassificationModel
{
Tensor is_training { get; }
Tensor x { get;}
Tensor y { get; }
}
}

test/TensorFlowNET.Examples/Text/cnn_models/VdCnn.cs → test/TensorFlowNET.Examples/TextProcess/cnn_models/VdCnn.cs View File


+ 0
- 8
test/TensorFlowNET.UnitTest/ExamplesTests/ExamplesTest.cs View File

@@ -61,14 +61,6 @@ namespace TensorFlowNET.ExamplesTests
new LogisticRegression() { Enabled = true, training_epochs=10, train_size = 500, validation_size = 100, test_size = 100 }.Run();
}
[Ignore]
[TestMethod]
public void MetaGraph()
{
tf.Graph().as_default();
new MetaGraph() { Enabled = true }.Run();
}
[Ignore]
[TestMethod]
public void NaiveBayesClassifier()


+ 15
- 1
test/TensorFlowNET.UnitTest/GraphTest.cs View File

@@ -5,6 +5,7 @@ using System.Runtime.InteropServices;
using System.Text;
using Tensorflow;
using Buffer = Tensorflow.Buffer;
using static Tensorflow.Python;

namespace TensorFlowNET.UnitTest
{
@@ -417,6 +418,19 @@ namespace TensorFlowNET.UnitTest

}


public void ImportGraphMeta()
{
var dir = "my-save-dir/";
with(tf.Session(), sess =>
{
var new_saver = tf.train.import_meta_graph(dir + "my-model-10000.meta");
new_saver.restore(sess, dir + "my-model-10000");
var labels = tf.constant(0, dtype: tf.int32, shape: new int[] { 100 }, name: "labels");
var batch_size = tf.size(labels);
var logits = (tf.get_collection("logits") as List<ITensorOrOperation>)[0] as Tensor;
var loss = tf.losses.sparse_softmax_cross_entropy(labels: labels,
logits: logits);
});
}
}
}

Loading…
Cancel
Save