< 返回版块

挺肥 发表于 2020-10-21 23:04

Rust 玩具神经网络

用 Rust 实现的玩具神经网络,灵感来自于 11行Python代码实现玩具神经网络

// Original python code
// https://iamtrask.github.io/2015/07/12/basic-python-network/
// Neural Network = 3 inputs, 4 hidden, 1 output with sigmoid activation

use ndarray::{array, Array};
use ndarray_rand::{rand::SeedableRng, rand_distr::Uniform, RandomExt}; // random matrix generation
use rand_isaac::isaac64::Isaac64Rng; // for setting seed

fn main() -> std::io::Result<()> {
    // set seeds
    let seed = 42;
    let mut rng = Isaac64Rng::seed_from_u64(seed);

    // dataset
    let x = array![[0., 0., 1.], [0., 1., 1.], [1., 0., 1.], [1., 1., 1.]];
    let y = array![[0., 1., 1., 0.]].reversed_axes();

    // initializing random weights
    let mut w0 = Array::random_using((3, 4), Uniform::new(0., 1.), &mut rng); // syn0
    let mut w1 = Array::random_using((4, 1), Uniform::new(0., 1.), &mut rng); // syn1

    for i in 0..1001 {
        // forward propagation
        let hidden = -x.dot(&w0); // l1
        let hidden = 1. / (1. + (hidden.mapv(f64::exp)));
        let output = -hidden.dot(&w1); // l2
        let output = 1. / (1. + (output.mapv(f64::exp)));
        
        // gradient calculation
        let output_delta = (&y - &output) * (output.clone() * (1. - &output)); // l2_delta
        let hidden_delta = output_delta.dot(&w1.t()) * (hidden.clone() * (1. - &hidden)); // l1_delta

        // update weights
        w1 = &w1 + &hidden.t().dot(&output_delta);
        w0 = &w0 + &x.t().dot(&hidden_delta);

        if i % 100 == 0 {
            println!("Epoch: {} \n {:?}\n", i,output);
        }
        
    }

    Ok(())
}

原文链接: https://www.reddit.com/r/rust/comments/jf6t5v/toy_neural_network_in_rust/

Reddit 讨论:为什么 Rust 没有自增(++)和自减(--)运算符?

https://www.reddit.com/r/rust/comments/jf66eu/why_are_there_no_increment_and_decrement/

加密货币监测平台 Cryptowatch 推出基于 Iced 的客户端

Iced:是跨平台的 Rust GUI 库

image.png

(效果看起来还是很不错的)

https://cryptowat.ch/apps/desktop

博客:Rust 生命周期

https://blog.thoughtram.io/lifetimes-in-rust/

视频:STM32 Rust 系列教程

https://www.youtube.com/playlist?list=PLDWmoWFf46givBRQmh5DyE27OsXMJPfag

Rust Careers : Rust 招聘网站

€49.99 发布一条招聘信息

https://rust.careers/


From 日报小组 @挺肥

社区学习交流平台订阅:

评论区

写评论

还没有评论

1 共 0 条评论, 1 页