I couldn't find a list in code and this seems like the right place for it.
for a list of activation function look here.
starting with the translation of the functions in this link.
here is my attempt:
(same here not sure if all the math is correct so not posting as answer)
using UnityEngine;
using System.Collections;
using System;
///<summary>
///Evaluation Functions from:
///https://stats.stackexchange.com/questions/154879/a-list-of-cost-functions-used-in-neural-networks-alongside-applications/154880#154880
///D infront means the Deravitive of the function
///error is the result for one perceptron.
///d_error is the derivative needed for backpropagation,
///to calculate the delta weights for the network
///</summary>
[System.Serializable]
public class Evaluation
{
public ErrorFunction errorFunction;
double error;
double d_error;
double td;
double od;
public Evaluation(ErrorFunction _errorFunction)
{
errorFunction = _errorFunction;
}
public double CalculateError(double t, double o)
{
switch(errorFunction)
{
case ErrorFunction.Quadratic:
return Quadratic(t,o);
case ErrorFunction.CrossEntropy:
return CrossEntropy(t,o);
case ErrorFunction.Exponentional:
return Exponentional(t,o);
case ErrorFunction.Hellinger:
return Hellinger(t,o);
case ErrorFunction.KullbackLeibler:
return KullbackLeibler(t,o);
case ErrorFunction.GeneralizedKL:
return GeneralizedKL(t,o);
case ErrorFunction.ItakuraSaito:
return ItakuraSaito(t,o);
}
return 0;
}
public double DerivativeError(double t, double o)
{
switch(errorFunction)
{
case ErrorFunction.Quadratic:
return DQuadratic(t,o);
case ErrorFunction.CrossEntropy:
return DCrossEntropy(t,o);
case ErrorFunction.Exponentional:
return DExponentional(t,o);
case ErrorFunction.Hellinger:
return DHellinger(t,o);
case ErrorFunction.KullbackLeibler:
return DKullbackLeibler(t,o);
case ErrorFunction.GeneralizedKL:
return DGeneralizedKL(t,o);
case ErrorFunction.ItakuraSaito:
return DItakuraSaito(t,o);
}
return 0;
}
public double Quadratic(double t, double o)
{
error = 0.5*Math.Pow((o-t),2);
return error;
}
public double DQuadratic(double t, double o)
{
d_error = t-o;
return d_error;
}
public double CrossEntropy(double t, double o)
{
error = t*Math.Log(o)+(1-t)*Math.Log(1-o);
return -error;
}
public double DCrossEntropy(double t, double o)
{
d_error = (o-t)/((o+1)*o);
return d_error;
}
public double Exponentional(double t, double o)
{
error = Math.Pow((o-t),2);
return Math.PI*Math.Exp(1/Math.PI*error);
}
public double DExponentional(double t, double o)
{
d_error = (2/Math.PI)*(o-t)*Exponentional(t,o);
return d_error;
}
public double Hellinger(double t, double o)
{
error = Math.Pow((Math.Sqrt(o)-Math.Sqrt(t)),2);
return (1/Math.Sqrt(2))*error;
}
public double DHellinger(double t, double o)
{
d_error = (Math.Sqrt(o)-Math.Sqrt(t))/(Math.Sqrt(2)*Math.Sqrt(o));
return d_error;
}
public double KullbackLeibler(double t, double o)
{
error = t*Math.Log(t/o);
return error;
}
public double DKullbackLeibler(double t, double o)
{
d_error = t/o;
return error;
}
public double GeneralizedKL(double t, double o)
{
error = KullbackLeibler(t,o) - t + o;
return error;
}
public double DGeneralizedKL(double t, double o)
{
d_error = (t + o)/o;
return d_error;
}
public double ItakuraSaito(double t, double o)
{
error = t/o - Math.Log(t/o) - 1;
return error;
}
public double DItakuraSaito(double t, double o)
{
d_error = (t+Math.Pow(o,2))/Math.Pow(o,2);
return d_error;
}
}
public enum ErrorFunction
{
none,
Quadratic,
CrossEntropy,
Exponentional,
Hellinger,
KullbackLeibler,
GeneralizedKL,
ItakuraSaito
}
Related
(the following is an example to simplify the explanation, but it reflects pretty well my real problem)
I have these classes and function, which i can't change or modify:
CannotChangeThis.h
class baseRectangle
{
public:
baseRectangle() : x(0),y(0),w(0),h(0) {}
baseRectangle(int xx, int yy, int ww, int hh) : x(xx),y(yy),w(ww),h(hh) {}
int x,y,w,h; // left, top, width, height
void SetRectangle(int xx, int yy, int ww, int hh)
{
x=xx;y=yy;w=ww;h=hh;
}
int GetRight() {return w-x-1;}
int GetBottom() {return h-y-1;}
[other methods]
}
baseRectangle GetABaseRectangle();
void PassABaseRectangle(baseRectangle br);
I have a derived class that do some computation when you change the base class data:
MyNewClass.h
class DerivedRect : public BaseRectangle
{
private:
DoPreComputation()
{
r=w-x-1;b=h-y-1;
cx=ww/2;cy=hh/2;
}
public:
int r,b,cx,cy; // right, bottom, centerX, centerY
DerivedRect () : r(0),b(0),cx(0),cy(0) {}
void SetRectangle(int xx,yy,ww,hh)
{
BaseRectangle::SetRectangle(int xx,yy,ww,hh);
DoPreComputation();
}
int GetRight() {return r;}
int GetBottom() {return b;}
DerivedRect &operator=(const BaseRectangle &r1 )
{
if (&r1 == this) { return *this; } // prevent assigning to self
BaseRectangle ::operator=(r1);
DoPreComputation();
return *this;
}
}
DerivedRect GetADerivedRect();
void PassADerivedRect(DerivedRect dr);
My problem:
AdvRect rr;
rr = hRect; // this works
AdvRect ar = hRect; // this cause error "conversion from 'BaseRectangle ' to non-scalar type 'DerivedRect' requested"
PassADerivedRect( GetABaseRectangle() ); // Error "no known conversion for.."
PassABaseRectangle( GetADerivedRect() ); // Error "no known conversion for.."
I think i'm missing something very basic about converting or casting between base and derived classes.
I've seen here in stackoverflow what Object slicing is, but since my derived class just do "pre-computation" over the same data, i don't think that should be a problem.
What am i doing wrong?
Instead of a converting assignment operator, make it a conversion constructor.
class DerivedRect : public baseRectangle {
public:
DerivedRect(const baseRectangle &r1 ) : baseRectangle(r1) {
DoPreComputation();
}
// DerivedRect &operator=(const BaseRectangle &r1 ) // no need for this
int r = 0, b = 0, cx = 0, cy = 0; // right, bottom, centerX, centerY
};
So I'm supposed to create a program that sets temperatures to celsius and farenheit. In it I create an equals method. Then I'm to create a driver that runs it and tests temperatures to see if they are equal, or not, using the equals method. I cannot get the correct boolean values, it just keeps spitting out false. Hitting a wall here. Code is as follows:
public class Temperature
{
public enum Scale{ CELSIUS, FARENHEIT};
public static final double DEFAULT_DEGREES = 0.0;
private double temperature;
private Scale scale;
public Temperature()
{
this(Temperature.DEFAULT_DEGREES);
}//default
public Temperature(double newTemperature)
{
this(newTemperature, Scale.CELSIUS);
}//ending bracket of constructor double
public Temperature(Scale newScale)
{
this(0, newScale);
}//end of constructor scale
public Temperature(double newTemperature, Scale newScale)
{
this.setTemperature(newTemperature);
this.setScale(newScale);
}//end bracket of constructor degrees and scale
public Scale getScale()
{
return this.scale;
}//end of method getScale
public void setScale(Scale newScale)
{
this.scale=newScale;
}//end of method setScale
public double getTemperature()
{
return this.temperature;
}//ending bracket of metho getTemperature
public void setTemperature(double newTemperature)
{
if(newTemperature < Temperature.DEFAULT_DEGREES)
{
this.temperature=Temperature.DEFAULT_DEGREES;
}
else
{
this.temperature = newTemperature;
}//ending of if
}//ending bracket of method setTemperature
public double getTemperatureInFarenheit()
{
double rv;
if(this.getScale() == Scale.CELSIUS)
{
rv= this.getTemperature();
}
else
{
rv = ((this.getTemperature()* 1.8) + 32);
}//end of if
return rv;
}//end of bracket of method getweightinfarenheit
public double getTemperatureInCelsius()
{
double rv;
if(this.getScale() == Scale.FARENHEIT)
{
rv = this.getTemperature();
}
else
{
rv= ((this.getTemperature()- 32) * 0.5556);
}
return rv;
}//end of method gettemperatureincelsius
public boolean equals(Object otherObject)
{
boolean rv = false;
if(otherObject instanceof Temperature)
{
Temperature otherTemperature = (Temperature)otherObject;
if((this.getTemperature()== otherTemperature.getTemperature())
&&(this.getScale()== otherTemperature.getScale()))
{
rv = true;
}//end of nested if
}//end of if
return rv;
}//end of bracket method equals
}//ending of class
This is my driver:
public class TemperatureDriver
{
public static void main(String[]args)
{
Temperature w1 = new Temperature();
w1.setTemperature(32);
w1.setScale(Temperature.Scale.FARENHEIT);
Temperature w2 = new Temperature();
w2.setTemperature(0);
w2.setScale(Temperature.Scale.CELSIUS);
System.out.println(w1.equals(w2));
Temperature w3 = new Temperature();
w3.setTemperature(-40.0);
w3.setScale(Temperature.Scale.FARENHEIT);
Temperature w4 = new Temperature();
w4.setTemperature(-40.0);
w4.setScale(Temperature.Scale.CELSIUS);
System.out.println(w3.equals(w4));
Temperature w5 = new Temperature();
w5.setTemperature(212);
w5.setScale(Temperature.Scale.FARENHEIT);
Temperature w6 = new Temperature();
w6.setTemperature(100);
w6.setScale(Temperature.Scale.CELSIUS);
System.out.println(w5.equals(w6));
}//end of method main
}//end of class Temperaturedriver
So there are a couple of issues I see. First, in your implementation of
getTemperatureInCelsius()
if(this.getScale() == Scale.FARENHEIT)
should be, I think,
if(this.getScale() == Scale.CELSIUS)
and vice versa for your implementation of getTemperatureInFarenheit.
Once those are fixed, I think the primary issue with your equals is it insists scales be the same. Where you have
if((this.getTemperature()== otherTemperature.getTemperature())
&&(this.getScale()== otherTemperature.getScale()))
instead should be
if(this.getTemperatureInFarenheit()== otherTemperature.getTemperatureInFahrenheit())
where I am deliberately comparing in Farenheit to avoid the rounding in your celsius conversion factor. That will probably make equals work (once the fix I mentioned for getTemperatureInFarenheit is made).
I still think I would feel better about saying something like
if(Math.abs(this.getTemperatureInFarenheit()-otherTemperature.getTemperatureInFahrenheit())<0.001)
than what I suggested above, and I would try that if the above fails.
I need to do this for one of my subject to get passed.This is the code and my problem is at PDA::class pda_ipari : public pda_basic where i need to create a namespace which has a derived class from an other namespace. Could you help me ?
#include <iostream>
using namespace std;
namespace PDA{
class pda_basic{
private :
float cpu_ghz;
int cpu_core;
int memory;
int storage;
public :
float getGhz()
{
return cpu_ghz;
}
void setGhz(float m)
{
cpu_ghz=m;
}
int getCore()
{
return cpu_core;
}
void setCore(int m)
{
cpu_core=m;
}
int getMemory()
{
return memory;
}
void setMemory(int m)
{
memory=m;
}
int getStorage()
{
return storage;
}
void setStorage(int m)
{
storage=m;
}
};
}
namespace abtu9p_pda{
PDA::class pda_ipari : public pda_basic {
private:
float display;
string vonalkodTipus;
int ip_vedelem;
public:
float getDisplay()
{
return display;
}
void setDisplay(float m)
{
display=m;
}
string getVonalkod()
{
return vonalkodTipus;
}
void setVonalkod(string m)
{
vonalkodTipus=m;
}
int getIP()
{
return ip_vedelem;
}
void setIP(int m)
{
ip_vedelem=m;
}
};
}
int main()
{
PDA::pda_basic tablet;
tablet.setGhz(2.7);
cout <<tablet.getGhz()<<endl;
return 0;
}
I am coding a Fibonacci sequence in Eclipse and this is my code-
public class FibonacciAlgorithm {
private int a = 0;
private int b = 1;
public FibonacciAlgorithm() {
}
public int increment() {
int temp = b;
b = a + b;
a = temp;
return value;
}
public int getValue() {
return b;
}
}
It is showing an error in the return value; line saying value cannot be resolved to a variable. I don't see any other errors.
Where is value defined? You return something that was not defined anywhere.
You don't have a "value" defined, this is your error. I don't remember the thing exactly, but I think you don't need a and b, I found this in my code archive, hope it helps.
public class Fibonacci
{
public static long fibo(int n)
{
if (n <= 1) return n;
else return fibo(n - 1) + fibo(n - 2);
}
public static void main() {
int count = 5; // change accordingly, bind to input etc.
int N = Integer.parseInt(count);
for (int i = 1; i <= N; i++)
System.out.println(i + ": " + fibo(i));
}
}
In case you want to stay with your own code, try returning "b" as value.
Your method is returning an int variable so you would have to define and return value as an int
I am not sure what you trying to do.
If you have "getValue" method I think "increment" method should be void.
When you want current Fibonacci value use "getValue" method.
public class FibonacciAlgorithm {
private int a = 0;
private int b = 1;
public FibonacciAlgorithm() {
}
public void increment() {
int temp = b;
b = a + b;
a = temp;
}
public int getValue() {
return b;
}
I'm trying to code some stuff for a game but I keep getting error messages on 8 lines that keep saying "Expected '=', ',', ';', 'asm' or 'attribute' before "insert what it's talking about here""
Its annoying and I can't figure out why. Here is the code:
class Vec2 **"error here before Vec2"**
{
public:
float X, Y;
Vec2() {}
Vec2(const float &x, const float &y) :
X(x),
Y(y)
{
};
float &operator[] (const int &index)
{
switch (index)
{
case 0:
return X;
case 1:
return Y;
}
throw Exceptions::IndexOutOfRange();
};
float *operator & ()
{
return &X;
};
};
template<> class TypeInfo<Vec2> : public TypeInfo_Atomic<Vec2> {}; **"error here before <"**
class Vec3 **"error here before Vec3"**
{
public:
float X, Y, Z;
Vec3() {}
Vec3(const float &x, const float &y, const float &z) :
X(x),
Y(y),
Z(z)
{
};
float &operator[] (const int &index)
{
switch (index)
{
case 0:
return X;
case 1:
return Y;
case 2:
return Z;
}
throw Exceptions::IndexOutOfRange();
};
float *operator & ()
{
return &X;
};
};
template<> class TypeInfo<Vec3> : public TypeInfo_Atomic<Vec3> {}; **"error here before <"**
class Vec4 **"error here before Vec4"**
{
public:
float X, Y, Z, W;
Vec4() {}
Vec4(const float &x, const float &y, const float &z, const float &w) :
X(x),
Y(y),
Z(z),
W(w)
{
};
float &operator[] (const int &index)
{
switch (index)
{
case 0:
return X;
case 1:
return Y;
case 2:
return Z;
case 3:
return W;
}
throw Exceptions::IndexOutOfRange();
};
float *operator & ()
{
return &X;
};
};
template<> class TypeInfo<Vec4> : public TypeInfo_Atomic<Vec4> {}; **"error here before <"**
class Color **"error here before Color"**
{
public:
byte R, G, B, A;
Color() {}
Color(byte r, byte g, byte b, byte a) :
R(r),
G(g),
B(b),
A(a)
{
};
byte *operator & ()
{
return &R;
};
static const Color Red,
Green,
Blue,
Yellow,
White,
Black;
};
template<> class TypeInfo<Color> : public TypeInfo_Atomic<Color> {}; **"flag here before <"**
there are 8 errors total. Help would be hugely appreciated!
If you're putting objective-c and c++ code into the same file, you need to use a .mm file extension.
http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/_index.html
It doesn't think your module is C++. What suffix did you give the file?