Skip to content

Instantly share code, notes, and snippets.

@eiel
Last active August 6, 2017 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eiel/0afd9833ea875ed5f27511e2527ee573 to your computer and use it in GitHub Desktop.
Save eiel/0afd9833ea875ed5f27511e2527ee573 to your computer and use it in GitHub Desktop.
Rustで「ゼロからつくるDeep Learning」 p26 2.3.2 重みとバイアスの導入
[package]
name = "rust-nn"
version = "0.1.0"
authors = ["Tomohiko Himura <eiel.hal@gmail.com>"]
[dependencies]
nalgebra = "0.12.3"
extern crate nalgebra as na;
use na::{Matrix1x2, Vector2, Vector1};
// p26 2.3.2 重みとバイアスの導入
// 行列同士の内積
fn main() {
   // Vector2 は Matrix2x1 のエイリアスとなっているので内積をとるためには transposeされた値を用意しないといけない。
    // let x = Vector2::new(0.0, 1.0).transpose(); // としてもよい
let x = Matrix1x2::new(0.0, 1.0);
let w = Vector2::new(0.5, 0.5);
let b = Vector1::new(-0.7); // x * w の結果はVector1になる。プリミティブ型を直接演算はできないので、Vector1に持ち上げておく。
let sum = x * w + b;
println!("{} * {} + {} = {}", x, w, b, sum);
}
┌ ┐
│ 0.000 1.000 │
└ ┘
* ┌ ┐
│ 0.500 │
│ 0.500 │
└ ┘
+ ┌ ┐
│ -0.700 │
└ ┘
= ┌ ┐
│ -0.200 │
└ ┘
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment