<jupyter_start><jupyter_text>Vector to Torch Tensor<jupyter_code>:dep tch = { path = "../../tch-rs" }

extern crate tch;
use tch::{Tensor};

let xs = vec![0.0 as f32; 10];
let ys = Tensor::of_slice(xs.as_slice());

ys.print();<jupyter_output>0
 0
 0
 0
 0
 0
 0
 0
 0
 0
[ Variable[CPUType]{10} ]<jupyter_text>Torch Tensor to vector<jupyter_code>:dep tch = { path = "../../tch-rs" }

extern crate tch;
use tch::{Tensor, kind};

let ys = Tensor::arange2(0.0, 10.0, 1.0, kind::FLOAT_CPU).reshape(&[2, 5]);
ys.print();
let mut zs = vec![0.0 as f32; 10];
println!("{:?}", &zs);
ys.copy_data(zs.as_mut_slice(), 10);
println!("{:?}", &zs);
:dep tch = { path = "../../tch-rs" }

extern crate tch;
use tch::{Tensor, kind};

let ys = Tensor::arange2(0.0, 10.0, 1.0, kind::FLOAT_CPU).reshape(&[2, 5]);
let zs = ys.max().double_value(&[]);
println!("{}", zs);<jupyter_output>9<jupyter_text>Slice<jupyter_code>:dep tch = { path = "../../tch-rs" }

extern crate tch;
use tch::{Tensor};

let xs = Tensor::of_slice(&[1.0 as f32, 2.0, 3.0, 4.0, 5.0, 6.0]).reshape(&[2, 3]);

let thetas = xs.slice(1, 2, 3, 1);
thetas.print();

let xys = xs.slice(1, 0, 2, 1);
xys.print();<jupyter_output>3
 6
[ Variable[CPUType]{2,1} ]
 1  2
 4  5
[ Variable[CPUType]{2,2} ]<jupyter_text>Broadcast<jupyter_code>:dep tch = { path = "../../tch-rs" }

extern crate tch;
use tch::{Tensor};

let x = Tensor::of_slice(&[1.0 as f32, 2.0, 3.0]).reshape(&[-1, 1]);
let y = Tensor::of_slice(&[10.0 as f32, 20.0]).reshape(&[1, -1]);
let z = x + 
y;
z.print();<jupyter_output>11  21
 12  22
 13  23
[ Variable[CPUType]{3,2} ]<jupyter_text>Stack<jupyter_code>:dep tch = { path = "../../tch-rs" }

extern crate tch;
use tch::{Tensor};

let dirs = Tensor::of_slice(&[-0.1 as f32, 0.0, 1.0, -0.2, -0.1, 0.0]).reshape(&[2, 3]);
let dxs = dirs.cos();
let dys = dirs.sin();
let dxys = Tensor::stack(&[dxs, dys], -1);
println!("{:?}", dxys.size());
dxys.print();<jupyter_output>[2, 3, 2]
(1,.,.) = 
  0.9950 -0.0998
  1.0000  0.0000
  0.5403  0.8415

(2,.,.) = 
  0.9801 -0.1987
  0.9950 -0.0998
  1.0000  0.0000
[ Variable[CPUType]{2,3,2} ]<jupyter_text>Scalar<jupyter_code>:dep tch = { path = "../../tch-rs" }

extern crate tch;
use tch::{Tensor, Scalar};

let xs = Scalar::float(0.5);
println!("{}", xs.to_string().unwrap());<jupyter_output>0.5<jupyter_text>Compare<jupyter_code>:dep tch = { path = "../../tch-rs" }

extern crate tch;
use tch::{Tensor, Scalar};

let xs = Tensor::of_slice(&[0.0 as f32, 0.0, 0.0, 1.0, 2.0, 3.0]);
let ys = xs.gt(Scalar::float(0.5));
ys.print();<jupyter_output>0
 0
 0
 1
 1
 1
[ Variable[CPUType]{6} ]<jupyter_text>nonzero (where)<jupyter_code>:dep tch = { path = "../../tch-rs" }

extern crate tch;
use tch::{Tensor, Scalar};

let xs = Tensor::of_slice(&[0.0 as f32, 0.0, 0.0, 1.0, 2.0, 3.0]);
let ys = xs.gt(Scalar::float(0.5)).nonzero();
ys.print();
:dep tch = { path = "../../tch-rs" }

extern crate tch;
use tch::{Tensor, Scalar};

let xs = Tensor::of_slice(
    &[0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 
      0.0, 0.0, 1.0, 0.0, 4.0, 0.0 as f32]).reshape(&[2, 6]);
let ys = xs.gt(Scalar::float(0.5)).nonzero();
ys.print();
:dep tch = { path = "../../tch-rs" }

extern crate tch;
use tch::{Tensor, Scalar};

let xs = Tensor::of_slice(
    &[0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 
      0.0, 0.0, 1.0, 0.0, 4.0, 0.0 as f32]).reshape(&[2, 6]);

for i in 0..2 {
    let ys = xs.slice(0, i, i + 1, 1).squeeze();
//     ys.print();
    let zs = ys.ge(Scalar::float(0.5)).nonzero().min();
    zs.print();
};<jupyter_output>3
[ Variable[CPUType]{} ]
2
[ Variable[CPUType]{} ]
{'fraction_non_alphanumeric': 0.2519774011299435, 'fraction_numerical': 0.08757062146892655, 'mean_word_length': 3.5927367055771726, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '366946', 'license': 'MIT', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1693, 'n_words': 363, 'path': 'learn-tch-rs/tch-rs-operations.ipynb', 'repo_name': 'taku-y/learn-pp-rust', 'size': '3540'}