小编HWi*_*mer的帖子

实现随机梯度下降

我正在尝试使用多元线性回归和 L2 范数作为损失函数来实现随机梯度下降的基本方法。

结果可以在这张图中看到:

在此输入图像描述

它离理想的回归线还很远,但我真的不明白为什么会这样。我仔细检查了所有数组尺寸,它们似乎都适合。

下面是我的源代码。如果有人能看到我的错误或给我提示,我将不胜感激。

def SGD(x,y,learning_rate):
    theta = np.array([[0],[0]])
    
    for i in range(N):
        xi = x[i].reshape(1,-1)
        y_pre = xi@theta

        theta = theta + learning_rate*(y[i]-y_pre[0][0])*xi.T

    print(theta)

    return theta
    

N = 100
x = np.array(np.linspace(-2,2,N))
y = 4*x + 5 + np.random.uniform(-1,1,N)

X = np.array([x**0,x**1]).T

plt.scatter(x,y,s=6)

th = SGD(X,y,0.1)

y_reg = np.matmul(X,th)
print(y_reg)
print(x)
plt.plot(x,y_reg)

plt.show()
Run Code Online (Sandbox Code Playgroud)

编辑:另一种解决方案是用x = np.random.permutation(x)

python regression numpy

5
推荐指数
1
解决办法
671
查看次数

为什么这个变量不需要是可变的?

在此代码片段中,我将变量声明maxmut,但 rust-analyer 告诉我这是没有必要的。我删除了mut,代码仍然有效并运行。这是为什么?max我正在重新分配match 语句中的值。

fn validate_in_range_u32(value:u32, min:u32, max:u32) -> bool { 
    if value < min || value > max {
        return false;
    }
    true
}

fn main() { 
    let day = 24;
    let month = 8;

    if !validate_in_range_u32(month, 1, 12) {
        println!("Month is out of range (1-12)");
    }
    let max: u32;
    match month {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => max = 31,
        2 …
Run Code Online (Sandbox Code Playgroud)

rust

3
推荐指数
1
解决办法
93
查看次数

为什么我不能编译这个简单的线程测试?

I wanted to test some things with threads on my Macbook pro, but I can't get it to work.

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Run Code Online (Sandbox Code Playgroud)

this is the Version of clang installed on my machine. I tried to code some vector of threads but that didn't work so I went back and copied an example from SO.

#include <string>
#include <iostream>
#include <thread>

using namespace std;

// The function we …
Run Code Online (Sandbox Code Playgroud)

c++ macos multithreading

2
推荐指数
1
解决办法
62
查看次数

变量需要来自 parse() 的显式类型

我正在尝试创建一个从标准输入获取输入的方法。它应该循环,直到输入是我可以解析为整数的有效整数。我收到的错误是这样的:

error[E0282]: type annotations needed for `Result<F, _>`
 --> src/lib.rs:9:13
  |
9 |         let parsed_int_result = line.trim().parse(); // Getting the error here
  |             ^^^^^^^^^^^^^^^^^
  |
help: consider giving `parsed_int_result` an explicit type, where the placeholders `_` are specified
  |
9 |         let parsed_int_result: Result<F, _> = line.trim().parse(); // Getting the error here
  |                              ++++++++++++++
Run Code Online (Sandbox Code Playgroud)

我认为匹配语句有点错误,但我不知道应该如何解决。我只想捕获并重Err()试,直到结果值为Ok()

pub fn input_int_with_message(msg: &str) -> i32 {
    let mut parsed_int;
    println!("{msg}");
    loop {
        let mut line = String::new();
        std::io::stdin()
            .read_line(&mut line) …
Run Code Online (Sandbox Code Playgroud)

rust

2
推荐指数
1
解决办法
55
查看次数

标签 统计

rust ×2

c++ ×1

macos ×1

multithreading ×1

numpy ×1

python ×1

regression ×1