raskr / rust-autograd

Tensors and differentiable operations (like TensorFlow) in Rust
MIT License
484 stars 37 forks source link

dropout with train=false produces error #55

Closed fables-tales closed 2 years ago

fables-tales commented 2 years ago

Version: rc2.0.0-rc2

backtrace:

thread 'main' panicked at 'index out of bounds: the len is 0 but the index is 0', /home/penelope/.cargo/registry/src/github.com-1ecc6299db9ec823/autograd-2.0.0-rc2/src/evaluation.rs:446:54
stack backtrace:
   0: rust_begin_unwind
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:515:5
   1: core::panicking::panic_fmt
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/panicking.rs:92:14
   2: core::panicking::panic_bounds_check
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/panicking.rs:69:5
   3: <usize as core::slice::index::SliceIndex<[T]>>::index
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/slice/index.rs:184:10
   4: core::slice::index::<impl core::ops::index::Index<I> for [T]>::index
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/slice/index.rs:15:9
   5: <smallvec::SmallVec<A> as core::ops::index::Index<I>>::index
             at /home/penelope/.cargo/registry/src/github.com-1ecc6299db9ec823/smallvec-1.6.1/src/lib.rs:1624:10
   6: autograd::evaluation::<impl autograd::graph::Graph<F>>::eval
             at /home/penelope/.cargo/registry/src/github.com-1ecc6299db9ec823/autograd-2.0.0-rc2/src/evaluation.rs:446:54
   7: autograd::evaluation::Evaluator<F>::run
             at /home/penelope/.cargo/registry/src/github.com-1ecc6299db9ec823/autograd-2.0.0-rc2/src/evaluation.rs:147:9
   8: battlesnake::neuralscore::train_cogent::{{closure}}
             at ./battlesnake/src/neuralscore.rs:189:16
   9: autograd::variable::VariableEnvironment<F>::run
             at /home/penelope/.cargo/registry/src/github.com-1ecc6299db9ec823/autograd-2.0.0-rc2/src/variable.rs:688:9
  10: battlesnake::neuralscore::train_cogent
             at ./battlesnake/src/neuralscore.rs:166:3
  11: battlesnake::main
             at ./battlesnake/src/main.rs:314:16
  12: core::ops::function::FnOnce::call_once
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ops/function.rs:227:5

code:

env.run(|ctx| {
    let train_tensor = arr2(train_frames.iter().map(|x| Frame { values: *x }).collect_vec().as_slice());
    let train_y = arr2(train_you_won.as_slice());

    let test_tensor = arr2(test_frames.iter().map(|x| Frame { values: *x }).collect_vec().as_slice());
    let test_y = arr2(test_you_won.as_slice());

    let x = ctx.placeholder("x", &[-1, (N_SNAKES * DIM_PER_SNAKE) as isize]);
    let y = ctx.placeholder("y", &[-1, 1]);
    let w = ctx.variable("w1");
    let b = ctx.variable("b1");
    let w2 = ctx.variable("w2");
    let b2 = ctx.variable("b2");
    let z = tanh(matmul(x, w) + b);

    let dropout_1_test = dropout(z, 0.5, false);
    let z2_test = sigmoid(matmul(dropout_1_test, w2) + b2);
    let mean_loss_test = reduce_mean(mean_squared_error(z2_test, &y), &[0], false);

    //let dropout_1_train = dropout(z, 0.5, true);
    //let z2_train = sigmoid(matmul(dropout_1_train, w2) + b2);
    //let mean_loss_train = reduce_mean(mean_squared_error(z2_train, &y), &[0], false);

    let loss = ctx.evaluator().push(mean_loss_test)
    .feed(x, train_tensor.view())
    .feed(y, train_y.view() )
    .run();

    eprintln!("initial loss: {:?}", loss);
});

the same code works just fine with setting train to true

fables-tales commented 2 years ago

For anyone searching for this in the future: this was fixed in rc3