Skip to content

Commit

Permalink
fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
Equim-chan committed Sep 23, 2023
1 parent fecb3a6 commit 58f7a7d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 63 deletions.
19 changes: 7 additions & 12 deletions convlog/tests/parse_and_convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ use testdata::{TestCase, TESTDATA};

#[test]
fn test_parse_and_convert() {
TESTDATA.iter().for_each(
|TestCase {
desc: description,
data,
}| {
let tenhou_log = tenhou::Log::from_json_str(data)
.unwrap_or_else(|_| panic!("failed to parse tenhou log (case: {description})"));
let mjai_log = tenhou_to_mjai(&tenhou_log)
.unwrap_or_else(|_| panic!("failed to transform tenhou log (case: {description})"));
TESTDATA.iter().for_each(|TestCase { desc, data }| {
let tenhou_log = tenhou::Log::from_json_str(data)
.unwrap_or_else(|_| panic!("failed to parse tenhou log (case: {desc})"));
let mjai_log = tenhou_to_mjai(&tenhou_log)
.unwrap_or_else(|_| panic!("failed to transform tenhou log (case: {desc})"));

assert!(mjai_log.len() >= 4);
},
);
assert!(mjai_log.len() >= 4);
});
}
4 changes: 2 additions & 2 deletions convlog/tests/split_raw_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ fn test_split_by_kyoku() {
TESTDATA.iter().for_each(|TestCase { desc, data }| {
let raw_log: RawLog = json::from_str(data)
.unwrap_or_else(|_| panic!("failed to parse tenhou log (case: {desc})"));
let splited_raw_logs = raw_log.split_by_kyoku();
let split_raw_logs = raw_log.split_by_kyoku();

let log =
Log::try_from(raw_log.clone()).unwrap_or_else(|_| panic!("invalid log (case: {desc})"));
let joined_kyokus: Vec<_> = splited_raw_logs
let joined_kyokus: Vec<_> = split_raw_logs
.into_iter()
.map(RawLog::from)
.map(|l| Log::try_from(l).unwrap_or_else(|_| panic!("invalid log (case: {desc})")))
Expand Down
6 changes: 3 additions & 3 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fluent_templates::static_loader! {
};
}

fn base_templates() -> Result<Tera> {
fn build_base_templates() -> Result<Tera> {
let mut tera = Tera::default();
tera.autoescape_on(vec![".tera", ".html"]);

Expand Down Expand Up @@ -69,7 +69,7 @@ impl View<'_> {
where
W: Write,
{
let mut templates = base_templates()?;
let mut templates = build_base_templates()?;
let lang_id = self.lang.parse()?;
templates.register_function(
"fluent",
Expand Down Expand Up @@ -140,6 +140,6 @@ mod test {
#[test]
fn template_compile() {
let _ = &*LOCALES;
base_templates().expect("failed to parse template");
build_base_templates().expect("failed to parse template");
}
}
92 changes: 46 additions & 46 deletions src/softmax.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
#[inline]
fn eq(l: f32, r: f32) -> bool {
(l - r).abs() < f32::EPSILON
}

pub fn softmax(arr: &mut [f32], temperature: f32) {
if arr.is_empty() {
return;
}

if !eq(temperature, 1.) {
arr.iter_mut().for_each(|x| *x /= temperature);
}

let max = arr
.iter()
.copied()
.max_by(|l, r| l.total_cmp(r))
.unwrap_or(f32::NEG_INFINITY);
let sum: f32 = arr.iter().copied().map(|x| (x - max).exp()).sum();

let offset = max + sum.ln();
arr.iter_mut()
.for_each(|x| *x = (*x - offset).exp().clamp(0., 1.));
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_softmax() {
let mut arr = [1., 4.2, 0.6, 1.23, 4.3, 1.2, 2.5];
softmax(&mut arr, 1.);
let expected = [
0.016590023,
0.40699518,
0.011120625,
0.020880206,
0.44979942,
0.020263102,
0.07435133,
];
assert!(arr.into_iter().zip(expected).all(|(l, r)| eq(l, r)));
}
}
#[inline]
fn eq(l: f32, r: f32) -> bool {
(l - r).abs() <= f32::EPSILON
}

pub fn softmax(arr: &mut [f32], temperature: f32) {
if arr.is_empty() {
return;
}

if !eq(temperature, 1.) {
arr.iter_mut().for_each(|x| *x /= temperature);
}

let max = arr
.iter()
.copied()
.max_by(|l, r| l.total_cmp(r))
.unwrap_or(f32::NEG_INFINITY);
let sum: f32 = arr.iter().copied().map(|x| (x - max).exp()).sum();

let offset = max + sum.ln();
arr.iter_mut()
.for_each(|x| *x = (*x - offset).exp().clamp(0., 1.));
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_softmax() {
let mut arr = [1., 4.2, 0.6, 1.23, 4.3, 1.2, 2.5];
softmax(&mut arr, 1.);
let expected = [
0.016590023,
0.40699518,
0.011120625,
0.020880206,
0.44979942,
0.020263102,
0.07435133,
];
assert!(arr.into_iter().zip(expected).all(|(l, r)| eq(l, r)));
}
}

0 comments on commit 58f7a7d

Please sign in to comment.