block_graph/builtin/
reinforcement.rs1impl AI<Vec<f32>,Vec<f32>> for AccQLayer{
2 fn forward(&self,mut input:Vec<f32>)->Vec<f32>{
3 let (dim,gamma)=(self.dim,self.gamma);
4 assert!(dim==0||dim==-1,"Dimension index was {dim} but a vec only has one tensor dimension");
5
6 input.iter_mut().rev().fold(0.0,|future,present|{
7 *present+=future*gamma;
8 *present
9 });
10 input
11 }
12}
13impl AccQLayer{
14 pub fn new(gamma:f32)->Self{Self::default().with_gamma(gamma)}
16 pub fn get_dim(&self)->i32{self.dim}
18 pub fn get_gamma(&self)->f32{self.gamma}
20 pub fn set_dim(&mut self,dim:i32){self.dim=dim}
22 pub fn set_gamma(&mut self,gamma:f32){self.gamma=gamma}
24 pub fn with_dim(mut self,dim:i32)->Self{
26 self.dim=dim;
27 self
28 }
29 pub fn with_gamma(mut self,gamma:f32)->Self{
31 self.gamma=gamma;
32 self
33 }
34}
35impl Decompose for AccQLayer{
36 fn compose((dim,gamma):Self::Decomposition)->Self{
37 Self{dim,gamma}
38 }
39 fn decompose(self)->Self::Decomposition{(self.dim,self.gamma)}
40 fn decompose_cloned(&self)->Self::Decomposition{(self.dim,self.gamma)}
41 type Decomposition=(i32,f32);
42}
43impl Default for AccQLayer{
44 fn default()->Self{
45 Self{dim:-1,gamma:0.9}
46 }
47}
48impl Op for AccQLayer{
49 type Output=Vec<f32>;
50}
51impl<A:AI<X,Y>,X,Y> AI<X,Y> for AccQ<A> where AccQLayer:AI<Y,Y>{
52 fn forward(&self,input:X)->Y{self.layer.forward(self.inner.forward(input))}
53 fn forward_mut(&mut self,input:X)->Y{self.layer.forward(self.inner.forward_mut(input))}
54}
55impl<A:Decompose> Decompose for AccQ<A>{
56 fn compose((inner,(dim,gamma)):Self::Decomposition)->Self{
57 Self{inner:A::compose(inner),layer:AccQLayer::compose((dim,gamma))}
58 }
59 fn decompose(self)->Self::Decomposition{(self.inner.decompose(),self.layer.decompose())}
60 fn decompose_cloned(&self)->Self::Decomposition{(self.inner.decompose_cloned(),self.layer.decompose_cloned())}
61 type Decomposition=(A::Decomposition,(i32,f32));
62}
63impl<A:IntoSequence<M>,M:AI<M::Output,M::Output>+Op> IntoSequence<M> for AccQ<A> where AccQLayer:Into<M>{
64 fn into_sequence(self)->Sequential<Vec<M>>{self.inner.into_sequence().with_next(self.layer)}
65}
66impl<A:Op<Output=Y>,Y> Op for AccQ<A> where AccQLayer:AI<Y,Y>{
67 type Output=Y;
68}
69impl<A:UnwrapInner> UnwrapInner for AccQ<A>{
70 fn unwrap_inner(self)->Self::Inner{self.into_inner().unwrap_inner()}
71 type Inner=A::Inner;
72}
73impl<A> AccQ<A>{
74 pub fn get_dim(&self)->i32{self.layer.dim}
76 pub fn get_gamma(&self)->f32{self.layer.gamma}
78 pub fn inner(&self)->&A{&self.inner}
80 pub fn inner_mut(&mut self)->&mut A{&mut self.inner}
82 pub fn into_inner(self)->A{self.inner}
84 pub fn new(gamma:f32,inner:A)->Self{
86 let layer=AccQLayer::new(gamma);
87 Self{inner,layer}
88 }
89 pub fn set_dim(&mut self,dim:i32){self.layer.dim=dim}
91 pub fn set_gamma(&mut self,gamma:f32){self.layer.gamma=gamma}
93 pub fn with_dim(mut self,dim:i32)->Self{
95 self.layer.dim=dim;
96 self
97 }
98 pub fn with_gamma(mut self,gamma:f32)->Self{
100 self.layer.gamma=gamma;
101 self
102 }
103 pub fn with_inner<B>(&self,inner:B)->AccQ<B> where AccQ<B>:Op{AccQ::new(self.get_gamma(),inner).with_dim(self.get_dim())}
105}
106impl<M:AI<M::Output,M::Output>+Op> IntoSequence<M> for AccQLayer where AccQLayer:Into<M>{
107 fn into_sequence(self)->Sequential<Vec<M>>{vec![self.into()].sequential()}
108}
109#[derive(Clone,Copy,Debug,Deserialize,Default,PartialEq,Serialize)]
110pub struct AccQ<A>{inner:A,layer:AccQLayer}
112#[derive(Clone,Copy,Debug,Deserialize,PartialEq,Serialize)]
113pub struct AccQLayer{dim:i32,gamma:f32}
115use crate::{
116 AI,Decompose,IntoSequence,Op,UnwrapInner
117};
118use serde::{Deserialize,Serialize};
119use super::Sequential;