本文整理汇总了C#中Range.AddAttribute方法的典型用法代码示例。如果您正苦于以下问题:C# Range.AddAttribute方法的具体用法?C# Range.AddAttribute怎么用?C# Range.AddAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Range
的用法示例。
在下文中一共展示了Range.AddAttribute方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Regression
//public int[] Classification(Vector[] features, int[] labels)
//{
//}
public double[] Regression(Vector[] features, double[] values)
{
var wMeans = Variable.Vector(Vector.Zero(features[0].Count).ToArray());
var wPrecision = Variable.WishartFromShapeAndRate(100, PositiveDefiniteMatrix.IdentityScaledBy(features[0].Count, 0.01));
var w = Variable.VectorGaussianFromMeanAndPrecision(wMeans, wPrecision).Named("w");
var numItems = Variable.New<int>().Named("numItems");
var i = new Range(numItems).Named("i");
i.AddAttribute(new Sequential());
var noisePrecision = Variable.New<double>().Named("noisePrecision");
var x = Variable.Array<Vector>(i).Named("x");
var y = Variable.Array<double>(i).Named("y");
using (Variable.ForEach(i))
{
y[i] = Variable.GaussianFromMeanAndPrecision(Variable.InnerProduct(w, x[i]), noisePrecision);
}
numItems.ObservedValue = features.Length;
x.ObservedValue = features;
y.ObservedValue = values;
var engine = new InferenceEngine();
engine.Compiler.UseSerialSchedules = true;
engine.ShowProgress = false;
var wPosterior = engine.Infer<VectorGaussian>(w);
y.ClearObservedValue();
w.ObservedValue = wPosterior.GetMean();
var inferredValues = engine.Infer<IList<Gaussian>>(y);
return inferredValues.Select(v => v.GetMean()).ToArray();
}
示例2: Model
public Model()
{
// Classes.
numClasses = Variable.New<int>().Named("numClasses");
c = new Range(numClasses).Named("c");
// Items.
numItems = Variable.New<int>().Named("numItems");
i = new Range(numItems).Named("i");
i.AddAttribute(new Sequential());
// The prior distribution for weight vector for each class. When
// <see cref="Test"/> is called, this is set to the posterior weight
// distributions from <see cref="Train"/>.
wPrior = Variable.Array<VectorGaussian>(c).Named("wPrior");
// The weight vector for each class.
w = Variable.Array<Vector>(c).Named("w");
w[c] = Variable<Vector>.Random(wPrior[c]);
noisePrecision = Variable.New<double>().Named("noisePrecision");
// Arrays of <see cref="Vector"/>-valued items (feature vectors) and integer labels.
x = Variable.Array<Vector>(i).Named("x");
y = Variable.Array<int>(i).Named("y");
// For all items...
using (Variable.ForEach(i))
{
// ...compute the score of this item across all classes...
score = BPMUtils.ComputeClassScores(w, x[i], noisePrecision);
y[i] = Variable.DiscreteUniform(c);
// ... and constrain the output.
BPMUtils.ConstrainMaximum(y[i], score);
}
// Inference engine settings (EP).
engine.Compiler.UseSerialSchedules = true;
engine.ShowProgress = false;
}
示例3: Run
public void Run()
{
InferenceEngine engine = new InferenceEngine();
if (!(engine.Algorithm is ExpectationPropagation))
{
Console.WriteLine("This example only runs with Expectation Propagation");
return;
}
Rand.Restart(0);
int nQuestions = 100;
int nSubjects = 40;
int nChoices = 4;
Gaussian abilityPrior = new Gaussian(0, 1);
Gaussian difficultyPrior = new Gaussian(0, 1);
Gamma discriminationPrior = Gamma.FromShapeAndScale(5, 1);
double[] trueAbility, trueDifficulty, trueDiscrimination;
int[] trueTrueAnswer;
int[][] data = Sample(nSubjects, nQuestions, nChoices, abilityPrior, difficultyPrior, discriminationPrior,
out trueAbility, out trueDifficulty, out trueDiscrimination, out trueTrueAnswer);
Range question = new Range(nQuestions).Named("question");
Range subject = new Range(nSubjects).Named("subject");
Range choice = new Range(nChoices).Named("choice");
var response = Variable.Array(Variable.Array<int>(question), subject).Named("response");
response.ObservedValue = data;
var ability = Variable.Array<double>(subject).Named("ability");
ability[subject] = Variable.Random(abilityPrior).ForEach(subject);
var difficulty = Variable.Array<double>(question).Named("difficulty");
difficulty[question] = Variable.Random(difficultyPrior).ForEach(question);
var discrimination = Variable.Array<double>(question).Named("discrimination");
discrimination[question] = Variable.Random(discriminationPrior).ForEach(question);
var trueAnswer = Variable.Array<int>(question).Named("trueAnswer");
trueAnswer[question] = Variable.DiscreteUniform(nChoices).ForEach(question);
using (Variable.ForEach(subject)) {
using (Variable.ForEach(question)) {
var advantage = (ability[subject] - difficulty[question]).Named("advantage");
var advantageNoisy = Variable.GaussianFromMeanAndPrecision(advantage, discrimination[question]).Named("advantageNoisy");
var correct = (advantageNoisy > 0).Named("correct");
using (Variable.If(correct))
response[subject][question] = trueAnswer[question];
using (Variable.IfNot(correct))
response[subject][question] = Variable.DiscreteUniform(nChoices);
}
}
engine.NumberOfIterations = 5;
subject.AddAttribute(new Sequential()); // needed to get stable convergence
engine.Compiler.UseSerialSchedules = true;
if (false) {
// set this to do majority voting
ability.ObservedValue = Util.ArrayInit(nSubjects, i => 0.0);
difficulty.ObservedValue = Util.ArrayInit(nQuestions, i => 0.0);
discrimination.ObservedValue = Util.ArrayInit(nQuestions, i => 1.0);
}
var trueAnswerPosterior = engine.Infer<IList<Discrete>>(trueAnswer);
int numCorrect = 0;
for (int q = 0; q < nQuestions; q++) {
int bestGuess = trueAnswerPosterior[q].GetMode();
if (bestGuess == trueTrueAnswer[q]) numCorrect++;
}
double pctCorrect = 100.0*numCorrect/nQuestions;
Console.WriteLine("{0}% TrueAnswers correct", pctCorrect.ToString("f0"));
var difficultyPosterior = engine.Infer<IList<Gaussian>>(difficulty);
for (int q = 0; q < Math.Min(nQuestions, 4); q++) {
Console.WriteLine("difficulty[{0}] = {1} (sampled from {2})", q, difficultyPosterior[q], trueDifficulty[q].ToString("g2"));
}
var discriminationPosterior = engine.Infer<IList<Gamma>>(discrimination);
for (int q = 0; q < Math.Min(nQuestions, 4); q++) {
Console.WriteLine("discrimination[{0}] = {1} (sampled from {2})", q, discriminationPosterior[q], trueDiscrimination[q].ToString("g2"));
}
var abilityPosterior = engine.Infer<IList<Gaussian>>(ability);
for (int s = 0; s < Math.Min(nSubjects, 4); s++) {
Console.WriteLine("ability[{0}] = {1} (sampled from {2})", s, abilityPosterior[s], trueAbility[s].ToString("g2"));
}
}
示例4: Model
public Model(SharedVariableArray<Vector> w, Range c, int numChunks)
{
// Items.
numItems = Variable.New<int>().Named("numItems");
i = new Range(numItems).Named("i");
i.AddAttribute(new Sequential());
// The model identifier for the shared variables.
model = new MicrosoftResearch.Infer.Models.Model(numChunks).Named("model");
// The weight vector for each submodel.
wModel = w.GetCopyFor(model).Named("wModel");
noisePrecision = Variable.New<double>().Named("noisePrecision");
// Arrays of <see cref="Vector"/>-valued items (feature vectors) and integer labels.
x = Variable.Array<Vector>(i).Named("x");
y = Variable.Array<int>(i).Named("y");
// For all items...
using (Variable.ForEach(i))
{
// ...compute the score of this item across all classes...
score = BPMUtils.ComputeClassScores(wModel, x[i], noisePrecision);
y[i] = Variable.DiscreteUniform(c);
// ... and constrain the output.
BPMUtils.ConstrainMaximum(y[i], score);
}
// Inference engine settings (EP).
engine.Compiler.UseSerialSchedules = true;
engine.ShowProgress = false;
}
示例5: Model
public Model()
{
// Classes.
numClasses = Variable.New<int>().Named("numClasses");
c = new Range(numClasses).Named("c");
// Features.
numFeatures = Variable.New<int>().Named("numFeatures");
f = new Range(numFeatures).Named("f");
// Items.
numItems = Variable.New<int>().Named("numItems");
i = new Range(numItems).Named("i");
i.AddAttribute(new Sequential());
// Features per item.
numFeaturesPerItem = Variable.Array<int>(i).Named("numFeaturesPerItem");
fItem = new Range(numFeaturesPerItem[i]).Named("fItem");
// The prior distribution for weight vector for each class. When
// <see cref="Test"/> is called, this is set to the posterior weight
// distributions from <see cref="Train"/>.
wPrior = Variable.Array(Variable.Array<Gaussian>(f), c).Named("wPrior");
// The weight vector for each class.
w = Variable.Array(Variable.Array<double>(f), c).Named("w");
w[c][f] = Variable<double>.Random(wPrior[c][f]);
noisePrecision = Variable.New<double>().Named("noisePrecision");
// Jagged array of feature values - each item is an array of data values
// whose indices are given by the corresponding indices[i].
values = Variable.Array(Variable.Array<double>(fItem), i).Named("values");
// Jagged array of indices for the items.
indices = Variable.Array(Variable.Array<int>(fItem), i).Named("indices");
// Labels.
y = Variable.Array<int>(i).Named("y");
// For all items...
using (Variable.ForEach(i))
{
// ...compute the score of this item across all classes...
score = BPMUtils.ComputeClassScores(w, values[i], indices[i], fItem, noisePrecision);
y[i] = Variable.DiscreteUniform(c);
// ...and constrain the output.
BPMUtils.ConstrainMaximum(y[i], score);
}
// Inference engine settings (EP).
engine.Compiler.UseSerialSchedules = true;
engine.ShowProgress = false;
}
示例6: Model
public Model(ISharedVariableArray<VariableArray<double>, double[][]> w, Range c, int numChunks)
{
// Items.
numItems = Variable.New<int>().Named("numItems");
i = new Range(numItems).Named("i");
i.AddAttribute(new Sequential());
// Features per item.
numFeaturesPerItem = Variable.Array<int>(i).Named("numFeaturesPerItem");
fItem = new Range(numFeaturesPerItem[i]).Named("fItem");
// The model identifier for the shared variables.
model = new MicrosoftResearch.Infer.Models.Model(numChunks).Named("model");
// The weight vector for each submodel.
wModel = w.GetCopyFor(model).Named("wModel");
noisePrecision = Variable.New<double>().Named("noisePrecision");
// Jagged array of feature values - each item is an array of data values
// whose indices are given by the corresponding indices[i].
values = Variable.Array(Variable.Array<double>(fItem), i).Named("values");
// Jagged array of indices for the items.
indices = Variable.Array(Variable.Array<int>(fItem), i).Named("indices");
// Labels.
y = Variable.Array<int>(i).Named("y");
// For all items...
using (Variable.ForEach(i))
{
// ...compute the score of this item across all classes...
score = BPMUtils.ComputeClassScores(wModel, values[i], indices[i], fItem, noisePrecision);
y[i] = Variable.DiscreteUniform(c);
// ... and constrain the output.
BPMUtils.ConstrainMaximum(y[i], score);
}
// Inference engine settings (EP).
engine.Compiler.UseSerialSchedules = true;
engine.ShowProgress = false;
}