FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

change TextClassificationWithMovieReviews to BinaryTextClassification. · attackgithub/TensorFlow.NET@c0760ff · GitHub

Repository navigation

Commit c0760ff

Browse files
committed
change TextClassificationWithMovieReviews to BinaryTextClassification.
1 parent 0cb07ec commit c0760ff

7 files changed

Lines changed: 87 additions & 40 deletions

File tree

‎README.md‎

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Import TF.NET.
3737
using Tensorflow;
3838
```
3939

40-
Add two constants.
40+
Add two constants:
4141
```cs
4242
// Create a Constant op
4343
var a = tf.constant(4.0f);
@@ -50,7 +50,7 @@ using (var sess = tf.Session())
5050
}
5151
```
5252

53-
Feed placeholder.
53+
Feed placeholder:
5454
```cs
5555
// Create a placeholder op
5656
var a = tf.placeholder(tf.float32);
@@ -59,27 +59,86 @@ var c = tf.add(a, b);
5959

6060
using(var sess = tf.Session())
6161
{
62-
var feed_dict = new Dictionary<Tensor, object>();
63-
feed_dict.Add(a, 3.0f);
64-
feed_dict.Add(b, 2.0f);
65-
66-
var o = sess.run(c, feed_dict);
62+
var o = sess.run(c, new FeedItem(a, 3.0f), new FeedItem(b, 2.0f));
6763
}
6864
```
6965

66+
Linear Regression:
67+
68+
```c#
69+
// We can set a fixed init value in order to debug
70+
var W = tf.Variable(-0.06f, name: "weight");
71+
var b = tf.Variable(-0.73f, name: "bias");
72+
73+
// Construct a linear model
74+
var pred = tf.add(tf.multiply(X, W), b);
75+
76+
// Mean squared error
77+
var cost = tf.reduce_sum(tf.pow(pred - Y, 2.0f)) / (2.0f * n_samples);
78+
79+
// Gradient descent
80+
// Note, minimize() knows to modify W and b because Variable objects are trainable=True by default
81+
var optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost);
82+
83+
// Initialize the variables (i.e. assign their default value)
84+
var init = tf.global_variables_initializer();
85+
86+
// Start training
87+
with(tf.Session(), sess =>
88+
{
89+
// Run the initializer
90+
sess.run(init);
91+
92+
// Fit all training data
93+
for (int epoch = 0; epoch < training_epochs; epoch++)
94+
{
95+
foreach (var (x, y) in zip<float>(train_X, train_Y))
96+
sess.run(optimizer, new FeedItem(X, x), new FeedItem(Y, y));
97+
98+
// Display logs per epoch step
99+
if ((epoch + 1) % display_step == 0)
100+
{
101+
var c = sess.run(cost, new FeedItem(X, train_X), new FeedItem(Y, train_Y));
102+
Console.WriteLine($"Epoch: {epoch + 1} cost={c} " + $"W={sess.run(W)} b={sess.run(b)}");
103+
}
104+
105+
Console.WriteLine("Optimization Finished!");
106+
var training_cost = sess.run(cost, new FeedItem(X, train_X), new FeedItem(Y, train_Y));
107+
Console.WriteLine($"Training cost={training_cost} W={sess.run(W)} b={sess.run(b)}");
108+
109+
// Testing example
110+
var test_X = np.array(6.83f, 4.668f, 8.9f, 7.91f, 5.7f, 8.7f, 3.1f, 2.1f);
111+
var test_Y = np.array(1.84f, 2.273f, 3.2f, 2.831f, 2.92f, 3.24f, 1.35f, 1.03f);
112+
Console.WriteLine("Testing... (Mean square loss Comparison)");
113+
114+
var testing_cost = sess.run(tf.reduce_sum(tf.pow(pred - Y, 2.0f)) / (2.0f * test_X.shape[0]), new FeedItem(X, test_X), new FeedItem(Y, test_Y));
115+
Console.WriteLine($"Testing cost={testing_cost}");
116+
117+
var diff = Math.Abs((float)training_cost - (float)testing_cost);
118+
Console.WriteLine($"Absolute mean square loss difference: {diff}");
119+
}
120+
});
121+
```
122+
123+
124+
70125
Read the docs & book [The Definitive Guide to Tensorflow.NET](https://tensorflownet.readthedocs.io/en/latest/FrontCover.html).
71126

72127
### More examples:
73128

74129
* [Hello World](test/TensorFlowNET.Examples/HelloWorld.cs)
75130
* [Basic Operations](test/TensorFlowNET.Examples/BasicOperations.cs)
76-
* [Image Recognition](test/TensorFlowNET.Examples/ImageRecognition.cs)
77131
* [Linear Regression](test/TensorFlowNET.Examples/LinearRegression.cs)
78132
* [Logistic Regression](test/TensorFlowNET.Examples/LogisticRegression.cs)
79133
* [Nearest Neighbor](test/TensorFlowNET.Examples/NearestNeighbor.cs)
134+
* [Naive Bayes Classification](test/TensorFlowNET.Examples/NaiveBayesClassifier.cs)
135+
* [Image Recognition](test/TensorFlowNET.Examples/ImageRecognition.cs)
136+
* [K-means Clustering](test/TensorFlowNET.Examples/KMeansClustering.cs)
137+
* [NN XOR](test/TensorFlowNET.Examples/NeuralNetXor.cs)
138+
* [Object Detection](test/TensorFlowNET.Examples/ObjectDetection.cs)
80139
* [Text Classification](test/TensorFlowNET.Examples/TextClassificationWithMovieReviews.cs)
81140
* [CNN Text Classification](test/TensorFlowNET.Examples/CnnTextClassification.cs)
82-
* [Naive Bayes Classification](test/TensorFlowNET.Examples/NaiveBayesClassifier.cs)
141+
83142
* [Named Entity Recognition](test/TensorFlowNET.Examples/NamedEntityRecognition.cs)
84143

85144
### Contribute:
@@ -94,7 +153,7 @@ You can:
94153
* Debug one of the unit tests that is marked as Ignored to get it to work (can be challenging)
95154
* Debug one of the not yet working examples and get it to work (hard)
96155

97-
How to debug unit tests:
156+
### How to debug unit tests:
98157

99158
The best way to find out why a unit test is failing is to single step it in C# and its pendant Python at the same time to see where the flow of execution digresses or where variables exhibit different values. Good Python IDEs like PyCharm let you single step into the tensorflow library code.
100159

‎docs/TIM.jpg‎

3.71 KB
LoadingViewer requires iframe.

‎src/TensorFlowNET.Core/TensorFlowNET.Core.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ More math/ linalg APIs.</PackageReleaseNotes>
4444

4545
<ItemGroup>
4646
<PackageReference Include="Google.Protobuf" Version="3.7.0" />
47-
<PackageReference Include="NumSharp" Version="0.10.0-alpha" />
47+
<PackageReference Include="NumSharp" Version="0.10.0-beta" />
4848
</ItemGroup>
4949

5050
<ItemGroup>

‎test/TensorFlowNET.Examples/NeuralNetXor.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private float RunWithImportedGraph()
7676
var init = tf.global_variables_initializer();
7777
float loss_value = 0;
7878
// Start tf session
79-
with<Session>(tf.Session(graph), sess =>
79+
with(tf.Session(graph), sess =>
8080
{
8181
sess.run(init);
8282
var step = 0;

test/TensorFlowNET.Examples/Text/TextClassificationWithMovieReviews.cs renamed to test/TensorFlowNET.Examples/Text/BinaryTextClassification.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@
99

1010
namespace TensorFlowNET.Examples
1111
{
12-
public class TextClassificationWithMovieReviews : Python, IExample
12+
/// <summary>
13+
/// This example classifies movie reviews as positive or negative using the text of the review.
14+
/// This is a binary—or two-class—classification, an important and widely applicable kind of machine learning problem.
15+
/// https://github.com/tensorflow/docs/blob/master/site/en/tutorials/keras/basic_text_classification.ipynb
16+
/// </summary>
17+
public class BinaryTextClassification : Python, IExample
1318
{
1419
public int Priority => 9;
15-
public bool Enabled { get; set; } = false;
16-
public string Name => "Movie Reviews";
17-
public bool ImportGraph { get; set; } = true;
20+
public bool Enabled { get; set; } = true;
21+
public string Name => "Binary Text Classification";
22+
public bool ImportGraph { get; set; } = true;
1823

19-
string dir = "text_classification_with_movie_reviews";
24+
string dir = "binary_text_classification";
2025
string dataFile = "imdb.zip";
2126
NDArray train_data, train_labels, test_data, test_labels;
2227

@@ -89,17 +94,18 @@ public void PrepareData()
8994
private NDArray ReadData(string file)
9095
{
9196
var lines = File.ReadAllLines(file);
92-
var nd = new NDArray(lines[0].StartsWith("[") ? typeof(object) : np.int32, new Shape(lines.Length));
97+
var nd = new NDArray(lines[0].StartsWith("[") ? typeof(string) : np.int32, new Shape(lines.Length));
9398

9499
if (lines[0].StartsWith("["))
95100
{
96101
for (int i = 0; i < lines.Length; i++)
97102
{
98-
var matches = Regex.Matches(lines[i], @"\d+\s*");
103+
/*var matches = Regex.Matches(lines[i], @"\d+\s*");
99104
var data = new int[matches.Count];
100105
for (int j = 0; j < data.Length; j++)
101106
data[j] = Convert.ToInt32(matches[j].Value);
102-
nd[i] = data.ToArray();
107+
nd[i] = data.ToArray();*/
108+
nd[i] = lines[i].Substring(1, lines[i].Length - 2).Replace(" ", string.Empty);
103109
}
104110
}
105111
else

test/TensorFlowNET.Examples/python/text_classification_with_movie_reviews.py renamed to test/TensorFlowNET.Examples/python/binary_text_classification.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,30 +71,12 @@ def decode_review(text):
7171

7272
history = model.fit(partial_x_train,
7373
partial_y_train,
74-
epochs=40,
74+
epochs=20,
7575
batch_size=512,
7676
validation_data=(x_val, y_val),
7777
verbose=1)
7878

7979
results = model.evaluate(test_data, test_labels)
80-
81-
# serialize model to JSON
82-
model_json = model.to_json()
83-
with open("model.json", "w") as json_file:
84-
json_file.write(model_json)
85-
# serialize weights to HDF5
86-
model.save_weights("model.h5")
87-
print("Saved model to disk")
88-
89-
# load json and create model
90-
json_file = open('model.json', 'r')
91-
loaded_model_json = json_file.read()
92-
json_file.close()
93-
loaded_model = model_from_json(loaded_model_json)
94-
# load weights into new model
95-
loaded_model.load_weights("model.h5")
96-
print("Loaded model from disk")
97-
9880
print(results)
9981

10082
history_dict = history.history

‎test/TensorFlowNET.UnitTest/ExamplesTests/ExamplesTest.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void TextClassificationTrain()
105105
public void TextClassificationWithMovieReviews()
106106
{
107107
tf.Graph().as_default();
108-
new TextClassificationWithMovieReviews() { Enabled = true }.Run();
108+
new BinaryTextClassification() { Enabled = true }.Run();
109109
}
110110

111111
[TestMethod]

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL