-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0316fde
commit 4c814d6
Showing
11 changed files
with
124 additions
and
88 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/TensorFlowNET.Examples/TextProcessing/CnnTextClassificationKeras.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using Tensorflow.Keras.Utils; | ||
using static Tensorflow.KerasApi; | ||
|
||
namespace TensorFlowNET.Examples | ||
{ | ||
/// <summary> | ||
/// https://colab.research.google.com/github/keras-team/keras-io/blob/master/examples/nlp/ipynb/text_classification_from_scratch.ipynb#scrollTo=qqTCrB7SmJv9 | ||
/// </summary> | ||
public class CnnTextClassificationKeras : SciSharpExample, IExample | ||
{ | ||
public ExampleConfig InitConfig() | ||
=> Config = new ExampleConfig | ||
{ | ||
Name = "CNN Text Classification (Keras)", | ||
Enabled = false | ||
}; | ||
|
||
public bool Run() | ||
{ | ||
return true; | ||
} | ||
|
||
public override void PrepareData() | ||
{ | ||
string fileName = "aclImdb_v1.tar.gz"; | ||
string url = $"https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"; | ||
string data_dir = Path.GetTempPath(); | ||
Web.Download(url, data_dir, fileName); | ||
Compress.ExtractGZip(Path.Join(data_dir, fileName), data_dir); | ||
data_dir = Path.Combine(data_dir, "aclImdb_v1"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import numpy as np | ||
import tensorflow as tf | ||
from tensorflow import keras | ||
from tensorflow.keras import layers | ||
|
||
from tensorflow.python.platform import gfile | ||
# GRAPH_PB_PATH = 'D:/tmp/TensorflowIssue/TensorflowIssue/model/saved_model.pb' | ||
GRAPH_PB_PATH = 'D:/tmp/TensorFlow.NET/data/saved_model.pb' | ||
with tf.compat.v1.Session() as sess: | ||
print("load graph") | ||
with tf.io.gfile.GFile(GRAPH_PB_PATH,'rb') as f: | ||
graph_def = tf.compat.v1.GraphDef() | ||
graph_def.ParseFromString(f.read()) | ||
sess.graph.as_default() | ||
tf.import_graph_def(graph_def, name='') | ||
graph_nodes=[n for n in graph_def.node] | ||
names = [] | ||
for t in graph_nodes: | ||
names.append(t.name) | ||
print(names) | ||
|
||
inputs = keras.Input(shape=(784,)) | ||
|
||
dense = layers.Dense(64, activation="relu") | ||
x = dense(inputs) | ||
|
||
dense = layers.Dense(64, activation="relu") | ||
x = dense(x) | ||
|
||
dense = layers.Dense(10) | ||
outputs = dense(x) | ||
|
||
model = keras.Model(inputs=inputs, outputs=outputs, name="mnist_model") | ||
model.summary(); | ||
|
||
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() | ||
|
||
x_train = x_train.reshape(60000, 784).astype("float32") / 255 | ||
x_test = x_test.reshape(10000, 784).astype("float32") / 255 | ||
|
||
model.compile( | ||
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), | ||
optimizer=keras.optimizers.RMSprop(), | ||
metrics=["accuracy"], | ||
) | ||
|
||
history = model.fit(x_train, y_train, batch_size=64, epochs=2, validation_split=0.2) | ||
|
||
test_scores = model.evaluate(x_test, y_test, verbose=2) | ||
print("Test loss:", test_scores[0]) | ||
print("Test accuracy:", test_scores[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import tensorflow as tf | ||
from tensorflow import keras | ||
from tensorflow.keras import layers | ||
|
||
physical_devices = tf.config.list_physical_devices('CPU') | ||
tf.config.experimental.set_memory_growth(physical_devices[0], True) | ||
|
||
tf.config.run_functions_eagerly(True) | ||
tf.debugging.set_log_device_placement(True) | ||
|
||
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) | ||
print(a) | ||
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]) | ||
print(b) | ||
c = tf.matmul(a, b) | ||
print(c) | ||
|
||
vocab_size = 20000 # Only consider the top 20k words | ||
maxlen = 200 # Only consider the first 200 words of each movie review | ||
(x_train, y_train), (x_val, y_val) = keras.datasets.imdb.load_data(num_words=vocab_size) | ||
print(len(x_train), "Training sequences") | ||
print(len(x_val), "Validation sequences") | ||
x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=maxlen) | ||
x_val = keras.preprocessing.sequence.pad_sequences(x_val, maxlen=maxlen) |