Нейронные сети. Описание. Обучение. Реализация.

Общие сведения и виды искусственного интеллекта | Методы обучения нейронных сетей | Реализация

Для реализации работы нейронной сети необходимо подготовить базу для работы с матрицами и векторами. Естесственно во многих средах программирования уже есть соответствующие классы. Но мы Не будем лезть в дебри и создадим свои классы, имеющие необходимые функции.

Перед компиляцией стоит добавить в соответствующие места кода символ "<" так как html код "съедает" его.

Класс работы с векторами



Vector.h
template
class Vector
{
	int elements;
	T*vec;
public:
	int getElements(); // возвращает число элементов
	void Init(); // инициализация
	void Print(); // вывод вектора на экран
	void setElements(int); // инициализация определенного элемента
	void EVector(); // перевод в еденичный вектор
	void F(T,T,int); // инициализация по функции Ферми
	T operator[](int);
	T IElement(int); // возвращает i-ый элемент
	bool operator<=(Vector&);
	bool operator<(Vector&);
	bool operator==(Vector&);
	Vector&operator=(Matrix&);
	Vector&operator=(Vector&);
	Vector&operator+(Vector&);
	Vector&operator-(Vector&);
	Vector&operator*(Matrix&);
	Vector&operator*(T);
	Vector();
	Vector(int);
	Vector(const Vector&);
	~Vector();
};

// Vector.cpp
template
T Vector::operator[](int idx)
{
	return vec[idx];
}
template
Vector::Vector()
{
	this->elements=1;
	vec=new T[1];
	vec[0]=0;
}
template
Vector::Vector(int n)
{
	this->elements=n;
	vec=new T[n];
	for(int i=0;i
Vector::Vector(const Vector&tmp)
{
	this->elements=tmp.elements;
	this->vec=new T[elements];
	for(int i=0;ivec[i]=tmp.vec[i];
}
template
Vector::~Vector()
{
	delete[] vec;
}
template
int Vector::getElements()
{
	return this->elements;
}
template
void Vector::setElements(int n)
{
	delete[] vec;
	this->elements=n;
	vec=new T[n];
}
template
void Vector::Print()
{
	for(int i=0;ielements;i++)
		printf("%4.3f ", vec[i]);
	cout<
void Vector::Init()
{
	for(int i=0;ielements;i++)
	{
		cout<<"Enter vector ["<>vec[i];
	}
}
template
void Vector::EVector()
{
	for(int i=0;ielements;i++)
	vec[i]=1;
}
template
T Vector::IElement(int idx)
{
	return this->vec[idx];
}
template
void Vector::F(T alpha,T beta,int type)
{
/*
Types:
	0 - Fermi;
*/
	if(type==0)
	{
		for(int i=0;ielements;i++)
			this->vec[i]=beta+(1/(1+exp(-alpha*this->vec[i])));
	}
}
template
Vector&Vector::operator=(Vector&b)
{
	if(vec)
		delete[] vec;
	this->elements=b.elements;
	vec=new T[elements];
	for(int i=0;i
bool Vector::operator<=(Vector&b)
{
	int flag=0;
	if(this->elements!=b.elements)
	{
		cout<<"Error! '<=' is bad operation for Vectors!\n";
		return false;
	}
	for(int i=0;i
bool Vector::operator<(Vector&b)
{
	int flag=0;
	if(this->elements!=b.elements)
	{
		cout<<"Error! '<=' is bad operation for Vector!\n";
		return false;
	}
	for(int i=0;i
bool Vector::operator==(Vector&b)
{
	int flag=0;
	if(this->elements!=b.elements)
	{
		cout<<"Error! '<=' is bad operation for Vector!\n";
		return false;
	}
	for(int i=0;i
Vector&Vector::operator*(Matrix&b)
{
	Vector*tmp=new Vector(b.getStolbec());
	if(this->elements!=b.getStroka())
	{
		cout<<"Error '*' operation!!!\n";
		return*tmp;
	}
	for(int i=0;ivec[i]+=(*this)[j]*b[j][i];
	return*tmp;
}
template
Vector&Vector::operator*(T a)
{
	Vector*tmp=new Vector(this->elements);
	for(int i=0;ielements;i++)
		tmp->vec[i]=this->vec[i]*a;
	return*tmp;
}
template
Vector&Vector::operator+(Vector&b)
{
	Vector*tmp=new Vector(b.elements);
	if(this->elements!=b.elements)
	{
		cout<<"Error! '+' is bad operation for Vector!\n";
		return*tmp;
	}
	for(int i=0;ivec[i]=(*this)[i]+b[i];
	return*tmp;
}
template
Vector&Vector::operator-(Vector&b)
{
	Vector*tmp=new Vector(b.elements);
	if(this->elements!=b.elements)
	{
		cout<<"Error! '-' is bad operation for Vector!\n";
		return*tmp;
	}
	for(int i=0;ivec[i]=(*this)[i]-b[i];
	return*tmp;
}
  

Страницы: 1 | 2 | 3 |

СГУ.clan - в помощь студентам
Хостинг от uCoz