I'm currently learning neural nets and stumbled across different sources and different codes all good however i found one code which found interesting and want to adapt it for various things such as OCR and that stuff. I am relatively new to C sharp and i would like some help on what i can do to make this code to my use. Basically this code adopts one output and I want that i can add several output neurons. My main problem is that i managed to adapt it to work with multiple outputs however then i found it impossible for me to test the network i.e First im training giving the network with the inputs and expected outputs. Then i just give the network an input string . The problem is that the Class Pattern is only accepts 3 parameters. How can i tell it/ work so that i can train with 3 parameters but then test only giving the string which i need to identify after that generalization is done ?? This is parts of the code which i am using for training -- this code is not mine just that its clear i am just using it for testing..
So testing-- this is being called
Activate(new Pattern(values, _inputDims)));
and Pattern method is this one --
private double[] _inputs;
private double _output;
public Pattern(string value, int inputSize)
{
string[] line = value.Split(',');
if (line.Length - 1 != inputSize)
throw new Exception("Input does not match network configuration");
_inputs = new double[inputSize];
for (int i = 0; i < inputSize; i++)
{
_inputs[i] = double.Parse(line[i]);
}
_output = double.Parse(line[inputSize]);
}
so i want that the Pattern method accepts also outputSize however it must be able to do the testing when i just pass the value and input dimensions. Honestly i don't know what i can do I've already spent many time looking for all available options.
You can add an optional parameter to the pattern constructor.
Pattern ( string value, int inputSize, int outputSize = 1)
Related
This question already has answers here:
Rule of thumb to test the equality of two doubles in C#?
(6 answers)
Closed 11 months ago.
I want to analyse a polynomial and only output the real roots. I use the Systems.Numerics to get the complex number and then compare their imaginary component to a set value to find out if they are real. To analyse the polynom, I use MathNet.Numerics for the analysis. The outputs should later be processed and printed out, but for testing reasons, I also print out the full complex number.
This is my code:
Complex[] roots = func.Roots();
List<double> realroots = new List<double>();
// Complex[] rootsDis = roots.Distinct().ToArray();
foreach (var root in roots)
{
if (root.Imaginary == 0)
{
string Root = root.Real.ToString();
double.TryParse(Root, out double RealRoot);
realroots.Add(RealRoot);
Ausgabe.Items.Add(root.ToString());
}
else
{
Ausgabe.Items.Add("no real roots");
return;
}
}
My problem is, that even if the imaginary component is 0 when printed out, the if statement is not fulfilled an also inserting conditions like >0,001 does not help either (I did the same in python once and
used numpy, there it helped). What can I do to correct that?
Edit: I solved it by deleting the return in the else statement. What i didnt grasp was, that a foreach loop goes through the array one by one and my programm failed becaus it broke the loop when it found the first non real root. But, thanks for your help nonetheless, I learend some things from it.
Could you compare with some calibrated epsilon?
For example:
if (Math.Abs(root.Imaginary) < 0.0000001)
{
// This is real!
}
this is a problem which I been facing for quite some time, ive searched all over the internet with many people asking simular questions to myself but never actually applicable in my case.
the goal of my task is to use a string array and generate all permutations and combinations of a certain length. for example (handwritten so may not be 100% accurate)
string[] values = new string[] { "ye", "ok", "nice", "5");
int length = 3;
:
yeoknice
yeok5
yeniceok
yenice5
ye5ok
ye5nice
okyenice
okye5
okniceye
oknice5
ok5nice
ok5ye
niceokye
niceok5
niceyeok
niceye5
nice5ok
nice5ye
5niceok
5niceye
5okye
5oknice
5yeok
5yenice
also more values in the string array could be added like 100+ values in the string array and the length set to around 100 too.
ive written some pheudocode:
string generatedvalue;
int64 progress = 0;//so that if the generating takes along time the code can read from file and see the progress and start generating where it left off. i think if the amount of numbers its generating is big then the progress value could be big too maybe it would be good to support something higher like int512
string[] values = new string[] { "ye", "ok", "nice", "5", "ohyeah", "rate", "best", "a", "b", "c", "f");
int length = 7;
//generate all combinations and perumtations
loop(conditions{
progress++;
//magic
? :(
//
generatedvalue = resultsfrommagic // put one of the combinations into the generatedvalue and write it to file so all the combinations and permutations are stored in a file on a new line
//write to file
writetofileappendnewline(progress + generatedvalue)
}
}
results from my online reserach:
ive found a page which is very very close if not the answer to the question(except i dont know if its efficant and it doesnt accept string array only numbers) but its not in c# and the syntax is very strange so i cant begin to translate it or begin to include it in my work( since i dont understand that language it is possible that its not acutally the asnwer to the question anyway) https://elixirforum.com/t/generate-all-combinations-having-a-fixed-array-size/26196 .
what ive tried:
ive created code which generates results from a string array using random numbers to randomly select an index from the string array and make a result from there eventually in theoery generating all the possible combinations through the random selecting however this is really really slow and potentially bad when it comes to generating the same values.
the specific requirments of the string array, the permutations and the context makes this really difficult to find help online which can be used to help. i appreciate any help for this complex task!
I am trying to implement my own multi-layer perceptron, unfortunately i make some mistake i can't find. Link to full program is here (it is light, simple c# console application). I am learning from this book , the code I am trying rewrite from batch to sequential form is at this github.
Link to my my project is here (github).
The perceptron itself is here.
My test inputs are Xor function, and function, or function and some random function with a little noise.
My questions are:
1)
Before I covered all my code with infinity checks (for double overflow) all my results (and weights) very quickly (100+ iterations) converged to some super high values and results became NaN. After adding checks i just got double.MaxValue. Interesting part is that if i run the same program about 5 times i will get correct results (depending on number of iterations). The only random variable there are weights that are initialized using random numbers (in range -1/sqrt(n) < x < 1/sqrt(n) where n is number of neurons in hidden layer). What might be the cause of this?
2)
I am training and validating on the same data set (because it does not matter now) and because it is sequential algorithm I am shuffling training inputs and targets INSIDE my class.
public void Train(int iterations, double eta)
{
_lastHiddenUpdates = new double[_hiddenWeights.RowLength(), _hiddenWeights.ColumnLength() + 1];
_lastOutputUpdates = new double[_outputWeights.Length];
for (int i = 0; i < iterations; i++)
{
ShuffleRows(); // <---- ShuffleRows is a private method without any ref parameter!
this._currentIteration = i;
var result = ForwardPhase(_trainingInput);
BackwardsPhase(result.OutputResult, result.HiddenValues, eta);
}
}
This is inside the MultiLayerPerceptron class. The thing is that after training the original array double[] also is shuffled! array of doubles is struct and structs are passed by value, not by reference and original array is in program.cs. Why is it changed outside of scope? Am i missing something? Now i am just cloning target arrays.
3)
This is super ugly
var infinity = deltasHs[i, j];
if (double.IsNegativeInfinity(infinity))
{
deltasHs[i, j] = double.MinValue;
}
else if (double.IsPositiveInfinity(infinity))
{
deltasHs[i, j] = double.MaxValue;
}
How can I simply this?
Note: During writing this program i was not paying attention to performance, sometimes i loop many times through one array just to keep readability at reasonable level.
I also know that you should not train and validate on the same data set but that is not my goal here, i will be perfectly happy if my perceptron will learn the noise as well. I just want this stupid goose to work (and understand).
I've had quite a bit of experience with programming (three semesters teaching VBasic, C++, and Java), and now I'm in college and I'm taking a C# class, which is quite boring (the teacher knows less than I do).
Anyways, for one of our exercises, we're creating a number guessing/lottery game. It works kind of like this:
User inputs three integers from 1-4 and clicks submit (I have them storing into an array)
Program generates three numbers from 1-4 (also in an array)
Function that checks matching runs and checks the two arrays
If all three match in order (i.e. 1,2,3 = 1,2,3 and NOT 1,2,3 = 1,3,2), matching = 4
If all three match NOT in order, matching = 3
If only two match, matching = 2
I want to make sure that only one match counts as one (i.e. [1,1,2][1,2,3] only gives one match to the user.
If only one matches, matching = 1
If no matches, matching stays at 0 (it's instantiated at submit_click)
I've got all of the code and GUI working except for the matching logic. I know I could do it with a LARGE amount of if statements, and I know cases would probably work, but I'm not as experienced with cases.
I'm not expecting my 'homework' to be done here, but I just want to know what method would be most effective to get this to correctly work (if it's easier to exclude the one match per item, then that's fine), and to possibly see some working code.
Thanks!
EDIT
I apologize if I come across as arrogant, I didn't mean to come across as a know-it-all (I definitely do not).
I have NOT taught classes, I've just taken classes from a teacher who's primarily a programming in and I'm at a community college and my professor isn't primarily a programming teacher.
I didn't take time to write a ton of if statements because I know that it would just get shot down as ineffective. I currently don't have the resources to test the answers, but as soon as I can I'll check them out and post back.
Again, I apologize for coming across as rude and arrogant, and I appreciate your answers more than you know.
Thanks again!
You can use a loop to achieve this functionality. I've used a list simply for ease of use, performing remove operations and the like. Something like this should work:
public static int getNumberOfMatches(List<int> userGuesses, List<int> machineGuesses) {
// Determine list equality.
bool matchedAll = true;
for (int i = 0; i < userGuesses.Count; i++) {
if (userGuesses[i] != machineGuesses[i]) {
matchedAll = false;
break;
}
}
// The lists were equal; return numberOfGuesses + 1 [which equals 4 in this case].
if (matchedAll) {
return userGuesses.Count + 1;
}
// Remove all matches from machineGuesses.
foreach (int userGuess in userGuesses) {
if (machineGuesses.Contains(userGuess)) {
machineGuesses.Remove(userGuess);
}
}
// Determine number of matches made.
return userGuesses.Count - machineGuesses.Count;
}
I think for the first case, for all matches in order you would scan the arrays together and maybe increment a counter. Since you mentioned you know c++, this would be
int userGuesses[3];
int randomGen[3];
int matches = 0;
for(int i=0; i < 3; i++) if(userGuesses[i] == randoGen[i]) matches++;
if(matches == 3) //set highest score here.
if(matches == 2) // next score for ordered matches etc.
For the not-in-order case, you will need to lookup the generated array for each user guess to see if it has that value.
A while back a post by Jon Skeet planted the idea in my head of building a CompiledFormatter class, for using in a loop instead of String.Format().
The idea is the portion of a call to String.Format() spent parsing the format string is overhead; we should be able to improve performance by moving that code outside of the loop. The trick, of course, is the new code should exactly match the String.Format() behavior.
This week I finally did it. I went through using the .Net framework source provided by Microsoft to do a direct adaption of their parser (it turns out String.Format() actually farms the work to StringBuilder.AppendFormat()). The code I came up with works, in that my results are accurate within my (admittedly limited) test data.
Unfortunately, I still have one problem: performance. In my initial tests the performance of my code closely matches that of the normal String.Format(). There's no improvement at all; it's even consistently a few milliseconds slower. At least it's still in the same order (ie: the amount slower doesn't increase; it stays within a few milliseconds even as the test set grows), but I was hoping for something better.
It's possible that the internal calls to StringBuilder.Append() are what actually drive the performance, but I'd like to see if the smart people here can help improve things.
Here is the relevant portion:
private class FormatItem
{
public int index; //index of item in the argument list. -1 means it's a literal from the original format string
public char[] value; //literal data from original format string
public string format; //simple format to use with supplied argument (ie: {0:X} for Hex
// for fixed-width format (examples below)
public int width; // {0,7} means it should be at least 7 characters
public bool justify; // {0,-7} would use opposite alignment
}
//this data is all populated by the constructor
private List<FormatItem> parts = new List<FormatItem>();
private int baseSize = 0;
private string format;
private IFormatProvider formatProvider = null;
private ICustomFormatter customFormatter = null;
// the code in here very closely matches the code in the String.Format/StringBuilder.AppendFormat methods.
// Could it be faster?
public String Format(params Object[] args)
{
if (format == null || args == null)
throw new ArgumentNullException((format == null) ? "format" : "args");
var sb = new StringBuilder(baseSize);
foreach (FormatItem fi in parts)
{
if (fi.index < 0)
sb.Append(fi.value);
else
{
//if (fi.index >= args.Length) throw new FormatException(Environment.GetResourceString("Format_IndexOutOfRange"));
if (fi.index >= args.Length) throw new FormatException("Format_IndexOutOfRange");
object arg = args[fi.index];
string s = null;
if (customFormatter != null)
{
s = customFormatter.Format(fi.format, arg, formatProvider);
}
if (s == null)
{
if (arg is IFormattable)
{
s = ((IFormattable)arg).ToString(fi.format, formatProvider);
}
else if (arg != null)
{
s = arg.ToString();
}
}
if (s == null) s = String.Empty;
int pad = fi.width - s.Length;
if (!fi.justify && pad > 0) sb.Append(' ', pad);
sb.Append(s);
if (fi.justify && pad > 0) sb.Append(' ', pad);
}
}
return sb.ToString();
}
//alternate implementation (for comparative testing)
// my own test call String.Format() separately: I don't use this. But it's useful to see
// how my format method fits.
public string OriginalFormat(params Object[] args)
{
return String.Format(formatProvider, format, args);
}
Additional notes:
I'm wary of providing the source code for my constructor, because I'm not sure of the licensing implications from my reliance on the original .Net implementation. However, anyone who wants to test this can just make the relevant private data public and assign values that mimic a particular format string.
Also, I'm very open to changing the FormatInfo class and even the parts List if anyone has a suggestion that could improve the build time. Since my primary concern is sequential iteration time from front to end maybe a LinkedList would fare better?
[Update]:
Hmm... something else I can try is adjusting my tests. My benchmarks were fairly simple: composing names to a "{lastname}, {firstname}" format and composing formatted phone numbers from the area code, prefix, number, and extension components. Neither of those have much in the way of literal segments within the string. As I think about how the original state machine parser worked, I think those literal segments are exactly where my code has the best chance to do well, because I no longer have to examine each character in the string.
Another thought:
This class is still useful, even if I can't make it go faster. As long as performance is no worse than the base String.Format(), I've still created a strongly-typed interface which allows a program to assemble it's own "format string" at run time. All I need to do is provide public access to the parts list.
Here's the final result:
I changed the format string in a benchmark trial to something that should favor my code a little more:
The quick brown {0} jumped over the lazy {1}.
As I expected, this fares much better compared to the original; 2 million iterations in 5.3 seconds for this code vs 6.1 seconds for String.Format. This is an undeniable improvement. You might even be tempted to start using this as a no-brainer replacement for many String.Format situations. After all, you'll do no worse and you might even get a small performance boost: as much 14%, and that's nothing to sneeze at.
Except that it is. Keep in mind, we're still talking less than half a second difference for 2 million attempts, under a situation specifically designed to favor this code. Not even busy ASP.Net pages are likely to create that much load, unless you're lucky enough to work on a top 100 web site.
Most of all, this omits one important alternative: you can create a new StringBuilder each time and manually handle your own formatting using raw Append() calls. With that technique my benchmark finished in only 3.9 seconds. That's a much greater improvement.
In summary, if performance doesn't matter as much, you should stick with the clarity and simplicity of the built-in option. But when in a situation where profiling shows this really is driving your performance, there is a better alternative available via StringBuilder.Append().
Don't stop now!
Your custom formatter might only be slightly more efficient than the built-in API, but you can add more features to your own implementation that would make it more useful.
I did a similar thing in Java, and here are some of the features I added (besides just pre-compiled format strings):
1) The format() method accepts either a varargs array or a Map (in .NET, it'd be a dictionary). So my format strings can look like this:
StringFormatter f = StringFormatter.parse(
"the quick brown {animal} jumped over the {attitude} dog"
);
Then, if I already have my objects in a map (which is pretty common), I can call the format method like this:
String s = f.format(myMap);
2) I have a special syntax for performing regular expression replacements on strings during the formatting process:
// After calling obj.toString(), all space characters in the formatted
// object string are converted to underscores.
StringFormatter f = StringFormatter.parse(
"blah blah blah {0:/\\s+/_/} blah blah blah"
);
3) I have a special syntax that allows the formatted to check the argument for null-ness, applying a different formatter depending on whether the object is null or non-null.
StringFormatter f = StringFormatter.parse(
"blah blah blah {0:?'NULL'|'NOT NULL'} blah blah blah"
);
There are a zillion other things you can do. One of the tasks on my todo list is to add a new syntax where you can automatically format Lists, Sets, and other Collections by specifying a formatter to apply to each element as well as a string to insert between all elements. Something like this...
// Wraps each elements in single-quote charts, separating
// adjacent elements with a comma.
StringFormatter f = StringFormatter.parse(
"blah blah blah {0:#['$'][,]} blah blah blah"
);
But the syntax is a little awkward and I'm not in love with it yet.
Anyhow, the point is that your existing class might not be much more efficient than the framework API, but if you extend it to satisfy all of your personal string-formatting needs, you might end up with a very convenient library in the end. Personally, I use my own version of this library for dynamically constructing all SQL strings, error messages, and localization strings. It's enormously useful.
It seems to me that in order to get actual performance improvement, you'd need to factor out any format analysis done by your customFormatter and formattable arguments into a function that returns some data structure that tells a later formatting call what to do. Then you pull those data structures in your constructor and store them for later use. Presumably this would involve extending ICustomFormatter and IFormattable. Seems kinda unlikely.
Have you accounted for the time to do the JIT compile as well? After all, the framework will be ngen'd which could account for the differences?
The framework provides explicit overrides to the format methods that take fixed-sized parameter lists instead of the params object[] approach to remove the overhead of allocating and collecting all of the temporary object arrays. You might want to consider that for your code as well. Also, providing strongly-typed overloads for common value types would reduce boxing overhead.
I gotta believe that spending as much time optimizing data IO would earn exponentially bigger returns!
This is surely a kissin' cousin to YAGNI for this. Avoid Premature Optimization. APO.