void softmax<T>::train(const Eigen::Ref<const EigenMat> &train, const std::vector<int> &labels) { #ifdef OCV_TEST_SOFTMAX gradient_check(); #endif auto const UniqueLabels = get_unique_labels(labels); auto const NumClass = UniqueLabels.size(); weight_ = EigenMat::Random(NumClass, train.rows()); grad_ = EigenMat::Zero(NumClass, train.rows()); auto const TrainCols = static_cast<int>(train.cols()); EigenMat const GroundTruth = get_ground_truth(static_cast<int>(NumClass), TrainCols, UniqueLabels, labels); std::random_device rd; std::default_random_engine re(rd()); int const Batch = (get_batch_size(TrainCols)); int const RandomSize = TrainCols != Batch ? TrainCols - Batch - 1 : 0; std::uniform_int_distribution<int> uni_int(0, RandomSize); for(size_t i = 0; i != params_.max_iter_; ++i){ auto const Cols = uni_int(re); auto const &TrainBlock = train.block(0, Cols, train.rows(), Batch); auto const >Block = GroundTruth.block(0, Cols, NumClass, Batch); auto const Cost = compute_cost(TrainBlock, weight_, GTBlock); if(std::abs(params_.cost_ - Cost) < params_.epsillon_ || Cost < 0){ break; } params_.cost_ = Cost; compute_gradient(TrainBlock, weight_, GTBlock); weight_.array() -= grad_.array() * params_.lrate_;//*/ } }
void train(Eigen::MatrixBase<Derived> const &input) { if(!reuse_layer_){ layers_.clear(); } std::random_device rd; std::default_random_engine re(rd()); int const Batch = get_batch_size(input.cols()); int const RandomSize = input.cols() != Batch ? input.cols() - Batch - 1 : 0; std::uniform_int_distribution<int> uni_int(0, RandomSize); #ifdef OCV_TEST_AUTOENCODER gradient_check(); #endif for(size_t i = 0; i < params_.hidden_size_.size(); ++i){ Eigen::MatrixBase<Derived> const &TmpInput = i == 0 ? input : eactivation_; if(!reuse_layer_){ layer es(TmpInput.rows(), params_.hidden_size_[i]); reduce_cost(uni_int, re, Batch, TmpInput, es); generate_activation(es, TmpInput, i==0?true:false); layers_.push_back(es); }else{ if(layers_.size() <= i){ layers_.emplace_back(static_cast<int>(TmpInput.rows()), params_.hidden_size_[i]); } reduce_cost(uni_int, re, Batch, TmpInput, layers_[i]); generate_activation(layers_[i], TmpInput, i==0?true:false); } } act_.clear(); buffer_.clear();//*/ }
void clustered_ba::connect(const vertices_size_type &idx) { pagmo_assert(get_number_of_vertices() > 0); const vertices_size_type prev_size = get_number_of_vertices() - 1; if (prev_size < m_m0) { // If we had not built the initial m0 nodes, do it. // We want to connect the newcomer island with high probability, and make sure that // at least one connection exists (otherwise the island stays isolated). // NOTE: is it worth to make it a user-tunable parameter? const double prob = 0.0; // Flag indicating if at least 1 connection was added. bool connection_added = false; // Main loop. for (std::pair<v_iterator,v_iterator> vertices = get_vertices(); vertices.first != vertices.second; ++vertices.first) { // Do not consider the new vertex itself. if (*vertices.first != idx) { if (m_drng() < prob) { connection_added = true; // Add the connections add_edge(*vertices.first,idx); add_edge(idx,*vertices.first); } } } // If no connections were established and this is not the first island being inserted, // establish at least one connection with a random island other than n. if ((!connection_added) && (prev_size != 0)) { // Get a random vertex index between 0 and n_vertices - 1. Keep on repeating the procedure if by // chance we end up on idx again. boost::uniform_int<vertices_size_type> uni_int(0,get_number_of_vertices() - 1); vertices_size_type rnd; do { rnd = uni_int(m_urng); } while (rnd == idx); // Add connections to the random vertex. add_edge(rnd,idx); add_edge(idx,rnd); } } else { // Now we need to add j edges, choosing the nodes with a probability // proportional to their number of connections. We keep track of the // connection established in order to avoid connecting twice to the same // node. // j is a random integer in the range 1 to m. boost::uniform_int<edges_size_type> uni_int2(1,m_m); std::size_t i = 0; std::size_t j = uni_int2(m_urng); std::pair<v_iterator,v_iterator> vertices; std::pair<a_iterator,a_iterator> adj_vertices; while (i < j) { // Let's find the current total number of edges. const edges_size_type n_edges = get_number_of_edges(); pagmo_assert(n_edges > 0); boost::uniform_int<edges_size_type> uni_int(0,n_edges - 1 - i); // Here we choose a random number between 0 and n_edges - 1 - i. const edges_size_type rn = uni_int(m_urng); edges_size_type n = 0; // Iterate over all vertices and accumulate the number of edges for each of them. Stop when the accumulated number of edges is greater // than rn. This is equivalent to giving a chance of connection to vertex v directly proportional to the number of edges departing from v. // You can think of this process as selecting a random edge among all the existing edges and connecting to the vertex from which the // selected edge departs. vertices = get_vertices(); for (; vertices.first != vertices.second; ++vertices.first) { // Do not consider it_n. if (*vertices.first != idx) { adj_vertices = get_adjacent_vertices(*vertices.first); n += boost::numeric_cast<edges_size_type>(std::distance(adj_vertices.first,adj_vertices.second)); if (n > rn) { break; } } } pagmo_assert(vertices.first != vertices.second); // If the candidate was not already connected, then add it. if (!are_adjacent(idx,*vertices.first)) { // Connect to nodes that are already adjacent to idx with probability p. // This step increases clustering in the network. adj_vertices = get_adjacent_vertices(idx); for(;adj_vertices.first != adj_vertices.second; ++adj_vertices.first) { if(m_drng() < m_p && *adj_vertices.first != *vertices.first && !are_adjacent(*adj_vertices.first,*vertices.first)) { add_edge(*adj_vertices.first, *vertices.first); add_edge(*vertices.first, *adj_vertices.first); } } // Connect to idx add_edge(*vertices.first,idx); add_edge(idx,*vertices.first); ++i; } } } }
void ihs::evolve(population &pop) const { // Let's store some useful variables. const problem::base &prob = pop.problem(); const problem::base::size_type prob_dimension = prob.get_dimension(), prob_i_dimension = prob.get_i_dimension(); const decision_vector &lb = prob.get_lb(), &ub = prob.get_ub(); const population::size_type pop_size = pop.size(); // Get out if there is nothing to do. if (pop_size == 0 || m_gen == 0) { return; } decision_vector lu_diff(prob_dimension); for (problem::base::size_type i = 0; i < prob_dimension; ++i) { lu_diff[i] = ub[i] - lb[i]; } // Int distribution to be used when picking random individuals. boost::uniform_int<population::size_type> uni_int(0,pop_size - 1); const double c = std::log(m_bw_min/m_bw_max) / m_gen; // Temporary individual used during evolution. population::individual_type tmp; tmp.cur_x.resize(prob_dimension); tmp.cur_f.resize(prob.get_f_dimension()); tmp.cur_c.resize(prob.get_c_dimension()); for (std::size_t g = 0; g < m_gen; ++g) { const double ppar_cur = m_ppar_min + ((m_ppar_max - m_ppar_min) * g) / m_gen, bw_cur = m_bw_max * std::exp(c * g); // Continuous part. for (problem::base::size_type i = 0; i < prob_dimension - prob_i_dimension; ++i) { if (m_drng() < m_phmcr) { // tmp's i-th chromosome element is the one from a randomly chosen individual. tmp.cur_x[i] = pop.get_individual(uni_int(m_urng)).cur_x[i]; // Do pitch adjustment with ppar_cur probability. if (m_drng() < ppar_cur) { // Randomly, add or subtract pitch from the current chromosome element. if (m_drng() > .5) { tmp.cur_x[i] += m_drng() * bw_cur * lu_diff[i]; } else { tmp.cur_x[i] -= m_drng() * bw_cur * lu_diff[i]; } // Handle the case in which we added or subtracted too much and ended up out // of boundaries. if (tmp.cur_x[i] > ub[i]) { tmp.cur_x[i] = boost::uniform_real<double>(lb[i],ub[i])(m_drng); } else if (tmp.cur_x[i] < lb[i]) { tmp.cur_x[i] = boost::uniform_real<double>(lb[i],ub[i])(m_drng); } } } else { // Pick randomly within the bounds. tmp.cur_x[i] = boost::uniform_real<double>(lb[i],ub[i])(m_drng); } } //Integer Part for (problem::base::size_type i = prob_dimension - prob_i_dimension; i < prob_dimension; ++i) { if (m_drng() < m_phmcr) { tmp.cur_x[i] = pop.get_individual(uni_int(m_urng)).cur_x[i]; if (m_drng() < ppar_cur) { if (m_drng() > .5) { tmp.cur_x[i] += double_to_int::convert(m_drng() * bw_cur * lu_diff[i]); } else { tmp.cur_x[i] -= double_to_int::convert(m_drng() * bw_cur * lu_diff[i]); } // Wrap over in case we went past the bounds. if (tmp.cur_x[i] > ub[i]) { tmp.cur_x[i] = lb[i] + double_to_int::convert(tmp.cur_x[i] - ub[i]) % static_cast<int>(lu_diff[i]); } else if (tmp.cur_x[i] < lb[i]) { tmp.cur_x[i] = ub[i] - double_to_int::convert(lb[i] - tmp.cur_x[i]) % static_cast<int>(lu_diff[i]); } } } else { // Pick randomly within the bounds. tmp.cur_x[i] = boost::uniform_int<int>(lb[i],ub[i])(m_urng); } } // And we push him back pop.push_back(tmp.cur_x); // We locate the worst individual. const population::size_type worst_idx = pop.get_worst_idx(); // And we get rid of him :) pop.erase(worst_idx); } }