// Train a margin classifier with labels minus one or plus one and a squared margin deficit. use graph_io; pub fn fd_squared_hinge(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%x: tensor, %label: tensor) -> (tensor, tensor) {", rows, width); fd_line(" %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor", rows, width); fd_line(" %v1 = stablehlo.constant dense<1.00000000000000000e+00> : tensor", rows, width); fd_line(" %v2 = stablehlo.multiply %label, %x : tensor", rows, width); fd_line(" %v3 = stablehlo.subtract %v1, %v2 : tensor", rows, width); fd_line(" %v4 = stablehlo.maximum %v3, %v0 : tensor", rows, width); // d = maximum(1-label*x,0) fd_line(" %v5 = stablehlo.multiply %v4, %v4 : tensor", rows, width); // loss = d*d fd_line(" %v6 = stablehlo.constant dense<2.00000000000000000e+00> : tensor", rows, width); fd_line(" %v7 = stablehlo.negate %v6 : tensor", rows, width); fd_line(" %v8 = stablehlo.multiply %v7, %label : tensor", rows, width); fd_line(" %v9 = stablehlo.multiply %v8, %v4 : tensor", rows, width); // grad = -2*label*d fd_line(" return %v5, %v9 : tensor, tensor", rows, width); println!(" }"); println!("}"); }