Ejemplo n.º 1
0
VectorXd feedForwardNetwork::rpropTrain(MatrixXd x, MatrixXd y, int numepochs, int batchsize, double incScale, double decScale, double incScaleMax, double decScaleMin, bool verbose) {
	// initialize training parameters
	std::vector<MatrixXd> dW(layer_size.size()-1);
	std::vector<VectorXd> dB(layer_size.size()-1);
	std::vector<ArrayXXi> signDeltaW(layer_size.size()-1);
	std::vector<ArrayXi> signDeltaB(layer_size.size()-1);

	for(int i = 0; i < layer_size.size()-1; i++) {
		dW[i].setConstant(layer_size[i+1], layer_size[i], 0.1);
		dB[i].setConstant(layer_size[i+1], 0.1);
		signDeltaW[i].setZero(layer_size[i+1], layer_size[i]);
		signDeltaB[i].setZero(layer_size[i+1]);
		}

	long n_sample = x.cols();
	if (batchsize > n_sample) batchsize = n_sample;
	int n_batch = n_sample / batchsize; // truncated if not divided
	int remainder = n_sample - n_batch*batchsize;

	int n_batch2 = n_batch; // n_batch2 is the actual batch number
	if (remainder > 0) n_batch2++;

	int s = 0;  // update iteration, total iteration = numepoch x numbatch
	VectorXd loss(numepochs*n_batch2);  // mean sum of square error/loss
	MatrixXd error;  //raw error: per sample per output dimension
	error.setConstant(numepochs, n_batch2, -1);
	PermutationMatrix<Dynamic, Dynamic> perm(n_sample);

	MatrixXd x_perm(x);
	MatrixXd y_perm(y);

	for (int i = 0; i < numepochs; i++) {
		if (verbose) cout <<  "Epoch " << i + 1 << endl;
		perm.setIdentity();
		random_shuffle(perm.indices().data(), perm.indices().data() + perm.indices().size());
		x_perm = x_perm * perm;  // col = sample, shuffle samples
		y_perm = y_perm * perm;
		int this_batchsize = batchsize;

		for(int j = 0; j < n_sample; j +=batchsize) {
			if (j >= n_sample - remainder) this_batchsize = remainder;
			error = ff(x_perm.middleCols(j, this_batchsize), y_perm.middleCols(j,  this_batchsize));
			rprop(error, dW, dB, signDeltaW, signDeltaB, incScale, decScale, incScaleMax, decScaleMin);
			if (output == "softmax") {
				loss[s] = -(y_perm.middleCols(j, this_batchsize).array() * post[layer_size.size()-1].array().log()).colwise().sum().mean();
			} else {
				loss[s] = error.array().square().mean();
				}
			s++;
			}
		}
	return loss;
	}
Ejemplo n.º 2
0
	ReflectionComponent CollisionBody::Reflection(CollisionBody* val) {
		ReflectionComponent refcomp;
		Property fprop(Property::FLOAT);
		(refcomp.properties["Mass"] = fprop).Set<float>(val->mass);
		refcomp.properties["Mass"].update_func = [val] (Property& prop) { val->mass = prop.Get<float>(); };
		static std::vector<std::string> choices = {"BOX", "SPHERE", "CAPSULE"};
		std::string current_shape;
		switch (val->collision_shape) {
			case SPHERE:
				current_shape = "SPHERE";
				(refcomp.properties["radius"] = fprop).Set<float>(val->radius);
				refcomp.properties["radius"].update_func = [val] (Property& prop) {
					val->radius = prop.Get<float>();
					static_cast<btSphereShape*>(val->shape.get())->setUnscaledRadius(val->radius);
				};
				break;
			case BOX:
				current_shape = "BOX";
				(refcomp.properties["extent_x"] = fprop).Set<float>(val->half_extents.x());
				refcomp.properties["extent_x"].update_func = [val] (Property& prop) {
					val->half_extents.setX(prop.Get<float>());
					static_cast<btBoxShape*>(val->shape.get())->setImplicitShapeDimensions(val->half_extents);
				};
				(refcomp.properties["extent_y"] = fprop).Set<float>(val->half_extents.y());
				refcomp.properties["extent_y"].update_func = [val] (Property& prop) {
					val->half_extents.setY(prop.Get<float>());
					static_cast<btBoxShape*>(val->shape.get())->setImplicitShapeDimensions(val->half_extents);
				};
				(refcomp.properties["extent_z"] = fprop).Set<float>(val->half_extents.z());
				refcomp.properties["extent_z"].update_func = [val] (Property& prop) {
					val->half_extents.setZ(prop.Get<float>());
					static_cast<btBoxShape*>(val->shape.get())->setImplicitShapeDimensions(val->half_extents);
				};
				break;
			case CAPSULE:
				current_shape = "CAPSULE";
				(refcomp.properties["radius"] = fprop).Set<float>(val->radius);
				refcomp.properties["radius"].update_func = [val] (Property& prop) {
					val->radius = prop.Get<float>();
					static_cast<btCapsuleShape*>(val->shape.get())->setImplicitShapeDimensions(
						btVector3(val->radius, 0.5f * val->height, val->radius));
				};
				(refcomp.properties["height"] = fprop).Set<float>(val->height);
				refcomp.properties["height"].update_func = [val] (Property& prop) {
					val->height = prop.Get<float>();
					static_cast<btCapsuleShape*>(val->shape.get())->setImplicitShapeDimensions(
						btVector3(val->radius, 0.5f * val->height, val->radius));
				};
				break;
		}
		radio_t shape_choices = std::make_pair(std::ref(choices), current_shape);
		Property rprop(Property::RADIO);
		(refcomp.properties["Shape"] = rprop).Set<radio_t>(shape_choices);
		refcomp.properties["Shape"].update_func = [val] (Property& prop) { 
			radio_t shape_choices = prop.Get<radio_t>();
			if (shape_choices.second == "BOX") {
				val->new_collision_shape = BOX;
			}
			else if (shape_choices.second == "SPHERE") {
				val->new_collision_shape = SPHERE;
			}
			else if (shape_choices.second == "CAPSULE") {
				val->new_collision_shape = CAPSULE;
			}
		};
		Property prop(Property::BOOLEAN);
		(refcomp.properties["Disable Deactivation"] = prop).Set<bool>(val->disable_deactivation);
		refcomp.properties["Disable Deactivation"].update_func = [val] (Property& prop) { val->disable_deactivation = prop.Get<bool>(); };
		(refcomp.properties["Disable Rotation"] = prop).Set<bool>(val->disable_rotation);
		refcomp.properties["Disable Rotation"].update_func = [val] (Property& prop) { val->disable_rotation = prop.Get<bool>(); };
		return std::move(refcomp);
	}
Ejemplo n.º 3
0
	void rprop(tensor<__value_type,__memory_space_type, column_major>& W, tensor<__value_type,__memory_space_type, column_major>& dW, tensor<S,__memory_space_type, column_major>& dW_old, tensor<__value_type,__memory_space_type, column_major>& rate, const float& decay = 0.0f, const float& sparsedecay=0.0f, const float& eta_p=1.2f, const float& eta_m=0.5f){
            typedef tensor<__value_type, __memory_space_type> rm_tensor;
            typedef tensor<S, __memory_space_type> rm_tensor_S;
            rprop(*reinterpret_cast<rm_tensor*>(&W),*reinterpret_cast<rm_tensor*>(&dW),*reinterpret_cast<rm_tensor_S*>(&dW_old),*reinterpret_cast<rm_tensor*>(&rate),decay,sparsedecay,eta_p,eta_m);
        }