-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathProgram.cs
More file actions
72 lines (54 loc) · 2.93 KB
/
Copy pathProgram.cs
File metadata and controls
72 lines (54 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using Microsoft.ML;
using Microsoft.ML.Data;
using System;
using System.Collections.Generic;
using System.IO;
using TextTransferLearning.DataStructures;
namespace TextTransferLearning
{
class Program
{
static readonly string _modelPath = Path.Combine(Environment.CurrentDirectory, "Model");
static void Main(string[] args)
{
var context = new MLContext();
var tensorFlowModel = context.Model.LoadTensorFlowModel(_modelPath);
DataViewSchema schema = tensorFlowModel.GetModelSchema();
Console.WriteLine(" =============== TensorFlow Model Schema =============== ");
var featuresType = (VectorDataViewType)schema["Features"].Type;
Console.WriteLine($"Name: Features, Type: {featuresType.ItemType.RawType}, Size: ({featuresType.Dimensions[0]})");
var predictionType = (VectorDataViewType)schema["Prediction/Softmax"].Type;
Console.WriteLine($"Name: Prediction/Softmax, Type: {predictionType.ItemType.RawType}, Size: ({predictionType.Dimensions[0]})");
// Run here to show tensorflow error
var wordLookup = context.Data.LoadFromTextFile(Path.Combine(_modelPath, "imdb_word_index.csv"),
columns: new[]
{
new TextLoader.Column("Words", DataKind.String, 0),
new TextLoader.Column("Ids", DataKind.Int32, 1),
},
separatorChar: ',');
Action<VariableLengthVector, FixedLengthVector> ResizeFeaturesAction = (s, f) =>
{
var features = s.VariableLengthFeatures;
Array.Resize(ref features, 600);
f.Features = features;
};
var pipeline = context.Transforms.Text.TokenizeIntoWords("Tokens", "Review")
.Append(context.Transforms.Conversion.MapValue("VariableLengthFeatures", wordLookup, wordLookup.Schema["Words"], wordLookup.Schema["Ids"], "Tokens"))
.Append(context.Transforms.CustomMapping(ResizeFeaturesAction, "Resize"))
.Append(tensorFlowModel.ScoreTensorFlowModel("Prediction/Softmax", "Features"))
.Append(context.Transforms.CopyColumns("Prediction", "Prediction/Softmax"));
var data = context.Data.LoadFromEnumerable(new List<MovieReview>());
var model = pipeline.Fit(data);
var predictionEngine = context.Model.CreatePredictionEngine<MovieReview, MovieReviewSentiment>(model);
var review = new MovieReview
{
Review = "I like the action"
};
var prediction = predictionEngine.Predict(review);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Number of classes: {0}", prediction.Prediction.Length);
Console.WriteLine("Sentiment? {0}", prediction.Prediction[1] > 0.5 ? "Positive" : "Negative");
}
}
}