tree evaluate_not (tree t) { if (N(t)!=1) return evaluate_error ("bad not"); tree u= evaluate(t[0]); if (!is_bool (u)) return evaluate_error ("bad not"); return as_string_bool (!as_bool (u)); }
tree evaluate_eval_args (tree t) { #ifdef CLASSICAL_MACRO_EXPANSION if (macro_top_level (std_env) || !is_atomic (t[0])) return evaluate_error ("undefined", t[0]); basic_environment local= macro_arguments (std_env); int key= make_tree_label (t[0]->label); if (!local->contains (key)) return evaluate_error ("undefined", t[0]); tree u= local [key]; #else tree u= t[0]; #endif if (is_atomic (u)) return evaluate_error ("bad eval-args"); #ifdef CLASSICAL_MACRO_EXPANSION macro_up (std_env); #endif int i, n= N(u); tree r (u, n); for (i=0; i<n; i++) r[i]= evaluate (u[i]); #ifdef CLASSICAL_MACRO_EXPANSION macro_redown (std_env, local); #endif return r; }
tree evaluate_xor (tree t) { if (N(t)!=2) return evaluate_error ("bad xor"); tree t1= evaluate (t[0]); tree t2= evaluate (t[1]); if (!is_bool (t1) || !is_bool (t2)) return evaluate_error ("bad xor"); return as_string_bool (as_bool (t1) ^ as_bool (t2)); }
tree evaluate_length (tree t) { if (N(t)!=1) return evaluate_error ("bad length"); tree t1= evaluate (t[0]); if (is_compound (t1)) { if (is_tuple (t1)) return as_string (N (t1)); return evaluate_error ("bad length"); } return as_string (N (t1->label)); }
tree evaluate_and (tree t) { if (N(t)<2) return evaluate_error ("bad and"); for (int i=0; i<N(t); i++) { tree u= evaluate (t[i]); if (!is_bool (u)) return evaluate_error ("bad and"); if (!as_bool (u)) return as_string_bool (false); } return as_string_bool (true); }
tree evaluate_translate (tree t) { if (N(t)!=3) return evaluate_error ("bad translate"); tree t1= evaluate (t[0]); tree t2= evaluate (t[1]); tree t3= evaluate (t[2]); if (is_compound (t1) || is_compound (t2) || is_compound (t3)) return evaluate_error ("bad translate"); return translate (t1->label, t2->label, t3->label); }
tree evaluate_lookup (tree t) { if (N(t)!=2) return evaluate_error ("bad look up"); tree t1= evaluate (t[0]); tree t2= evaluate (t[1]); if (!(is_compound (t1) && is_int (t2))) return evaluate_error ("bad look up"); int i= as_int (t2); if (i < 0 || i >= N(t1)) return evaluate_error ("index out of range in look up"); return t1[i]; }
tree evaluate_lesseq (tree t) { if (N(t)!=2) return evaluate_error ("bad less or equal"); tree t1= evaluate (t[0]); tree t2= evaluate (t[1]); if (is_compound (t1) || is_compound (t2)) return evaluate_error ("bad less or equal"); string s1= t1->label; string s2= t2->label; if (is_double (s1) && (is_double (s2))) return as_string_bool (as_double (s1) <= as_double (s2)); if (is_length (s1) && is_length (s2)) return as_string_bool (as_length (s1) <= as_length (s2)); return evaluate_error ("bad less or equal"); }
tree evaluate_merge (tree t) { int i, n= N(t); if (n == 0) return ""; tree acc= evaluate (t[0]); if (is_concat (acc)) acc= tree_as_string (acc); for (i=1; i<n; i++) { tree add= evaluate (t[i]); if (is_atomic (acc) && (is_atomic (add) || is_concat (add))) acc= acc->label * tree_as_string (add); else if (is_tuple (acc) && is_tuple (add)) acc= acc * add; else if (is_func (acc, MACRO) && is_func (add, MACRO) && (N(acc) == N(add)) && (acc (0, N(acc)-1) == add (0, N(add)-1))) { tree r = copy (acc); tree u1= copy (acc[N(acc)-1]); tree u2= copy (add[N(add)-1]); tree u (CONCAT, u1, u2); if (u1 == "") u= u2; else if (u2 == "") u= u1; else if (is_atomic (u1) && is_atomic (u2)) u= u1->label * u2->label; r[N(r)-1]= u; acc= r; } else return evaluate_error ("bad merge"); } return acc; }
tree evaluate_date (tree t) { if (N(t)>2) return evaluate_error ("bad date"); string lan= as_string (std_env [LANGUAGE]); if (N(t) == 2) { tree u= evaluate (t[1]); if (is_compound (u)) return evaluate_error ("bad date"); lan= u->label; } string fm= ""; if (N(t) != 0) { tree u= evaluate (t[0]); if (is_compound (u)) return evaluate_error ("bad date"); fm= u->label; } return get_date (lan, fm); }
tree evaluate_number (tree t) { if (N(t)!=2) return evaluate_error ("bad number"); tree t1= evaluate (t[0]); tree t2= evaluate (t[1]); if (is_compound (t1) || is_compound (t2)) return evaluate_error ("bad number"); string s1= t1->label; string s2= t2->label; int nr= as_int (s1); if (s2 == "arabic") return as_string (nr); if (s2 == "roman") return roman_nr (nr); if (s2 == "Roman") return Roman_nr (nr); if (s2 == "alpha") return alpha_nr (nr); if (s2 == "Alpha") return Alpha_nr (nr); if (s2 == "fnsymbol") return tree (WITH, MODE, "math", tree (RIGID, fnsymbol_nr (nr))); return evaluate_error ("bad number"); }
tree evaluate_unequal (tree t) { if (N(t)!=2) return evaluate_error ("bad unequal"); tree t1= evaluate (t[0]); tree t2= evaluate (t[1]); if (is_atomic (t1) && is_atomic (t2) && is_length (t1->label) && is_length (t2->label)) return as_string_bool (as_length (t1) != as_length (t2)); return as_string_bool (t1 != t2); }
IMPALGEBRA_BEGIN_NAMESPACE ParabolicFit2D::ParabolicFit2D(const algebra::Vector2Ds& data) { // check that there are at least 3 points IMP_USAGE_CHECK(data.size() > 2, "At least 3 points are required for ParabolicFit2D " << data.size() << " given"); find_regression(data); evaluate_error(data); }
tree evaluate_range (tree t) { if (N(t)!=3) return evaluate_error ("bad range"); tree t1= evaluate (t[0]); tree t2= evaluate (t[1]); tree t3= evaluate (t[2]); if (!(is_int (t2) && is_int (t3))) return evaluate_error ("bad range"); if (is_compound (t1)) { if (is_tuple (t1)) { int i1= max (0, as_int (t2)); int i2= min (N (t1), as_int (t3)); i2 = max (i1, i2); return t1 (i1, i2); } return evaluate_error ("bad range"); } int i1= max (0, as_int (t2)); int i2= min (N(t1->label), as_int (t3)); i2 = max (i1, i2); return t1->label (i1, i2); }
IMPALGEBRA_BEGIN_NAMESPACE LinearFit2D::LinearFit2D(const algebra::Vector2Ds& data, const Floats &errors) { // check that there are at least 3 points IMP_USAGE_CHECK(data.size() > 1, "At least 2 points are required for LinearFit2D " << data.size() << " given"); IMP_USAGE_CHECK(errors.empty() || errors.size()==data.size(), "Either there must be no error bars given or one per" << " point."); find_regression(data, errors); evaluate_error(data, errors); }
tree evaluate_quasiquote (tree t) { if (is_atomic (t)) return t; else if (is_func (t, UNQUOTE, 1)) return evaluate (t[0]); else { int i, n= N(t); tree r (L(t)); for (i=0; i<n; i++) { if (is_func (t[i], VAR_UNQUOTE, 1)) { tree ins= evaluate (t[i]); if (is_compound (ins)) r << A(ins); else r << evaluate_error ("bad unquote*"); } else r << evaluate_quasiquote (t[i]); } return r; } }
void rsdecode(unsigned int *locators, unsigned int *received_codeword){ unsigned int syndrome[N - K] = {0}; unsigned int error_locator_poly[(N - K)/2 + 1] = {0}; unsigned int error_locator_poly_derivative[(N - K)/2 + 1] = {0}; unsigned int error_evaluator_poly[N] = {0}; unsigned int error_magnitudes[ERROR_NUM] = {0}; int i = 0; int error_location = 0; compute_syndrome(syndrome, received_codeword); get_error_locate_poly(locators, ERROR_NUM, error_locator_poly); get_evaluator_poly(syndrome, error_locator_poly, error_evaluator_poly); get_formal_derivation(error_locator_poly, error_locator_poly_derivative, (N - K)/2 + 1); evaluate_error(error_magnitudes, error_locator_poly_derivative, error_evaluator_poly, locators,ERROR_NUM); //add the error magnitudes to received codeword for(i = 0; i < ERROR_NUM; i++){ error_location = galois_log(locators[i], w); received_codeword[error_location] ^= error_magnitudes[i]; } }
tree evaluate_find_file (tree t) { int i, n=N(t); array<tree> r (n); for (i=0; i<n; i++) { r[i]= evaluate (t[i]); if (is_compound (r[i])) return evaluate_error ("bad find file"); } for (i=0; i<(n-1); i++) { url u= resolve (url (r[i]->label, r[n-1]->label)); if (!is_none (u)) { if (is_rooted (u, "default")) u= reroot (u, "file"); return as_string (u); } } url base_file_name (as_string (std_env["base-file-name"])); url u= resolve (base_file_name * url_parent () * r[n-1]->label); if (!is_none (u)) { if (is_rooted (u, "default")) u= reroot (u, "file"); return as_string (u); } return "false"; }
constexpr size_t hidden_units = 75; using network_t = dll::dyn_network_desc< dll::network_layers< dll::lstm_layer<time_steps, sequence_length, hidden_units, dll::last_only>, dll::recurrent_last_layer<time_steps, hidden_units>, dll::dense_layer<hidden_units, 10, dll::softmax> > , dll::updater<dll::updater_type::ADAM> // Adam , dll::batch_size<100> // The mini-batch size >::network_t; auto net = std::make_unique<network_t>(); REQUIRE(net->fine_tune(dataset.train(), 30) < 0.15); REQUIRE(net->evaluate_error(dataset.test()) < 0.25); } // Simple LSTM with truncation TEST_CASE("unit/lstm/2", "[unit][lstm]") { auto dataset = dll::make_mnist_dataset_nc_sub(0, 2000, dll::batch_size<100>{}, dll::scale_pre<255>{}); constexpr size_t time_steps = 28; constexpr size_t sequence_length = 28; constexpr size_t hidden_units = 75; using network_t = dll::dyn_network_desc< dll::network_layers< dll::lstm_layer<time_steps, sequence_length, hidden_units, dll::last_only, dll::truncate<20>>, dll::recurrent_last_layer<time_steps, hidden_units>, dll::dense_layer<hidden_units, 10, dll::softmax>
dataset.training_images, dataset.training_labels, dataset.training_images.size(), 10, train_generator_t{}); auto test_generator = dll::make_generator( dataset.test_images, dataset.test_labels, dataset.test_images.size(), 10, train_generator_t{}); auto dbn = std::make_unique<dbn_t>(); auto error = dbn->fine_tune(*train_generator, 50); std::cout << "error:" << error << std::endl; CHECK(error < 5e-2); auto test_error = dbn->evaluate_error(*test_generator); std::cout << "test_error:" << test_error << std::endl; CHECK(test_error < 0.3); } // Use a simple in-memory generator for pretraining and fine-tuning TEST_CASE("unit/augment/mnist/2", "[dbn][unit]") { typedef dll::dbn_desc< dll::dbn_layers< dll::rbm_desc<28 * 28, 200, dll::momentum, dll::batch_size<10>>::layer_t, dll::rbm_desc<200, 300, dll::momentum, dll::batch_size<10>>::layer_t, dll::rbm_desc<300, 10, dll::momentum, dll::batch_size<10>, dll::hidden<dll::unit_type::SOFTMAX>>::layer_t>, dll::batch_size<25>>::dbn_t dbn_t; auto dataset = mnist::read_dataset_direct<std::vector, etl::dyn_matrix<float, 1>>(500); REQUIRE(!dataset.training_images.empty());
tree rewrite_impl (tree t) { switch (L(t)) { case EXTERN: { int i, n= N(t); tree r (TUPLE, n); for (i=0; i<n; i++) r[i]= evaluate (t[i]); object expr= null_object (); for (i=n-1; i>0; i--) expr= cons (object (r[i]), expr); string fun= evaluate_string (t[0]); expr= cons (string_to_object (fun), expr); bool secure= as_bool (std_env ["secure"]); if (!secure && script_status < 2) { if (!as_bool (call ("secure?", expr))) return tree (ERROR, "insecure script"); } environment old_env= reenter_rewrite_env; reenter_rewrite_env= std_env; object o= eval (expr); reenter_rewrite_env= old_env; return content_to_tree (o); } #ifdef CLASSICAL_MACRO_EXPANSION case MAP_ARGS: { if (!(is_atomic (t[0]) && is_atomic (t[1]) && is_atomic (t[2]))) return evaluate_error ("invalid map-args"); if (macro_top_level (std_env)) return evaluate_error ("undefined", t[2]); basic_environment local= macro_arguments (std_env); int key= make_tree_label (t[2]->label); if (!local->contains (key)) return evaluate_error ("undefined", t[2]); tree v= local [key]; if (is_atomic (v)) return evaluate_error ("invalid-map-args"); macro_up (std_env); int start= 0, end= N(v); if (N(t)>=4) start= as_int (evaluate (t[3])); if (N(t)>=5) end = as_int (evaluate (t[4])); int i, n= max (0, end-start); tree r (make_tree_label (t[1]->label), n); for (i=0; i<n; i++) r[i]= tree (make_tree_label (t[0]->label), tree (ARG, copy (t[2]), as_string (start+i)), as_string (start+i)); macro_redown (std_env, local); return r; } #endif // CLASSICAL_MACRO_EXPANSION case VAR_INCLUDE: { url base_file_name (as_string (std_env ["base-file-name"])); url file_name= url_system (evaluate_string (t[0])); return load_inclusion (relative (base_file_name, file_name)); } case REWRITE_INACTIVE: { #ifdef CLASSICAL_MACRO_EXPANSION if ((!is_func (t[0], ARG)) || is_compound (t[0][0])) return evaluate_error ("invalid rewrite-inactive"); if (macro_top_level (std_env)) return evaluate_error ("undefined", t[0][0]); basic_environment local= macro_arguments (std_env); int key= make_tree_label (t[0][0]->label); if (!local->contains (key)) return evaluate_error ("undefined", t[0][0]); tree val= local [key]; int i, n= N(t[0]); for (i=1; i<n; i++) { int j= as_int (t[0][i]); if ((j>=0) && (j<N(val))) val= val[j]; else return evaluate_error ("invalid rewrite-inactive"); } #else tree val= t[0]; #endif int inactive_mode= INACTIVE_INLINE_RECURSE; if (t[1] == "recurse") inactive_mode= INACTIVE_INLINE_RECURSE; else if (t[1] == "recurse*") inactive_mode= INACTIVE_BLOCK_RECURSE; else if (t[1] == "once") inactive_mode= INACTIVE_INLINE_ONCE; else if (t[1] == "once*") inactive_mode= INACTIVE_BLOCK_ONCE; else if (t[1] == "error") inactive_mode= INACTIVE_INLINE_ERROR; else if (t[1] == "error*") inactive_mode= INACTIVE_BLOCK_ERROR; return rewrite_inactive (val, inactive_mode); } default: return t; } }
tree evaluate_change_case (tree t) { if (N(t) < 2) return evaluate_error ("bad change case"); return evaluate_change_case (t[0], evaluate (t[1]), false, true); }
tree evaluate_is_tuple (tree t) { if (N(t)!=1) return evaluate_error ("bad tuple query"); return as_string_bool(is_tuple (evaluate (t[0]))); }