rust编程_在Rust中编程感知器

rust编程

Two years ago, I wrote a blogpost about implementing a perceptron in Python that quite a few people liked. Nowadays I’m getting started with the Rust programming language, so after the big Rust 1.0 release today I thought it would be a good moment to do another post about implementing a simple perceptron, this time in Rust.

两年前,我写了一篇有关在Python中实现感知器 的博客文章,很多人都喜欢。 如今,我开始使用Rust编程语言,因此,在今天发布了重要的Rust 1.0之后 ,我认为这是撰写关于实现简单感知器的另一篇文章的好时机,这次是在Rust中。

If you need any background information about perceptrons, please refer to my last post about this topic. I won’t discuss that topic again here.

如果您需要有关感知器的任何背景信息,请参阅我关于该主题的最新文章 。 我在这里不再讨论这个话题。

创建一个新项目 (Creating a new project)

First, let’s create a new Rust project with Cargo:

首先,让我们使用Cargo创建一个新的Rust项目:


$ cargo new perceptron-rs
$ cd perceptron-rs

Then add the rand crate as a dependency in Cargo.toml:

然后将rand板条箱添加为Cargo.toml中的依赖


[dependencies]
rand = "0.3"

辅助功能 (Helper functions)

For our implementation, we need some helper functions:

对于我们的实现,我们需要一些帮助函数:

For this example we won’t bother writing generic code, so the implementations are quite straightforward:

对于此示例,我们不会费心编写通用代码,因此实现非常简单:

/// Heaviside Step Function
/// Heaviside Step Function
fnfn    heavisideheaviside (( valval ::    f64f64 ))    ->->    i8i8    {
{
         (( valval    >=>=    0.00.0 ))    asas    i8
i8
}

}

/// Dot product of input and weights
/// Dot product of input and weights
fnfn    dotdot (( inputinput ::    (( i8i8 ,,    i8i8 ,,    i8i8 ),),    weightsweights ::    (( f64f64 ,,    f64f64 ,,    f64f64 ))))    ->->    f64f64    {
{
         inputinput .. 00    asas    f64f64    **    weightsweights .. 0
0
         ++    inputinput .. 11    asas    f64f64    **    weightsweights .. 1
1
         ++    inputinput .. 22    asas    f64f64    **    weightsweights .. 2
2
}
}

结构 (Structs)

Let’s define a simple struct to hold the training data and the expected output. As in the original blogpost, we’ll train the perceptron to do the boolean OR function.

让我们定义一个简单的结构来保存训练数据和预期的输出。 与原始博客一样,我们将训练感知器来执行布尔OR功能。

A TrainingDatum can hold an input value (implemented as 3-tuple) and an expected output value (a simple integer).

TrainingDatum可以保存一个输入值(实现为三元组)和一个预期的输出值(一个简单的整数)。

The first two values of the input field are the two inputs for the OR function. The third value is the bias.

输入字段的前两个值是“或”功能的两个输入。 第三个值是偏差。

初始化 (Initialization)

First we need to load the rand library and initialize a random number generator instance:

首先,我们需要加载rand库并初始化一个随机数生成器实例:

externextern    cratecrate    randrand ;

;

useuse    randrand :::: RngRng ;
;
useuse    randrand :::: distributionsdistributions :::: {{ RangeRange ,,    IndependentSampleIndependentSample };

};

// ...

// ...

letlet    mutmut    rngrng    ==    randrand :::: thread_rngthread_rng ();
();

The training data is provided as an array of TrainingDatum instances:

训练数据作为TrainingDatum实例的数组提供:

We then initialize the weight vector with random data between 0 and 1:

然后,我们使用0到1之间的随机数据初始化权重向量。

letlet    rangerange    ==    RangeRange :::: newnew (( 0.00.0 ,,    1.01.0 );
);
letlet    mutmut    ww    ==    (
(
         rangerange .. ind_sampleind_sample (( && mutmut    rngrng ),
),
         rangerange .. ind_sampleind_sample (( && mutmut    rngrng ),
),
         rangerange .. ind_sampleind_sample (( && mutmut    rngrng ),
),
);
);

The learning rate is set to 0.2 and the iteration count to 100.

学习率设置为0.2 ,迭代次数设置为100

Now we can start the training process!

现在我们可以开始培训过程了!

// Training
// Training
printlnprintln !! (( "Starting training phase with {} iterations...""Starting training phase with {} iterations..." ,,    nn );
);
forfor    __    inin    00 .... nn    {

{

         // Choose a random training sample
// Choose a random training sample
         letlet    && TrainingDatumTrainingDatum    {{    inputinput ::    xx ,,    expectedexpected    }}    ==    rngrng .. choosechoose (( && training_datatraining_data ).). unwrapunwrap ();

();

         // Calculate the dot product
// Calculate the dot product
         letlet    resultresult    ==    dotdot (( xx ,,    ww );

);

         // Calculate the error
// Calculate the error
         letlet    errorerror    ==    expectedexpected    --    heavisideheaviside (( resultresult );

);

         // Update the weights
// Update the weights
         ww .. 00    +=+=    etaeta    **    errorerror    asas    f64f64    **    xx .. 00    asas    f64f64 ;
;
         ww .. 11    +=+=    etaeta    **    errorerror    asas    f64f64    **    xx .. 11    asas    f64f64 ;
;
         ww .. 22    +=+=    etaeta    **    errorerror    asas    f64f64    **    xx .. 22    asas    f64f64 ;
;
}
}

After 100 iterations, our perceptron should have learned how to behave like an OR function.

经过100次迭代后,我们的感知器应该已经学会了如何像OR函数那样工作。

测试中 (Testing)

Let’s run the resulting program!

让我们运行结果程序!


$ cargo build
$ target/debug/perceptron
Starting training phase with 100 iterations...
0 OR 0: -0.07467118 -> 0
0 OR 1: 0.69958573 -> 1
1 OR 0: 0.82801196 -> 1
1 OR 1: 1.60226888 -> 1

Yay for Rust!

是的,Rust!

反馈 (Feedback)

I’m still quite new to Rust and not an expert on AI either. So if you have any feedback, suggestion or improvement, please leave it below or on Hacker News!

我对Rust还是很陌生,也不是AI专家。 因此,如果您有任何反馈,建议或改进,请将其保留在下方或Hacker News上

The code is on Github. If you have improvements concerning the code itself, you may also create a pull request.

代码在Github上 。 如果您对代码本身有改进,则还可以创建一个请求请求。

翻译自: https://www.pybloggers.com/2015/05/programming-a-perceptron-in-rust/

rust编程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值