Sigmoid 曲线不适用于公式 C++
Sigmoid Curve not working with formula C++
我正在使用Microsoft Visual Studio 2010。
公式y = 1/(1+exp(-e))
在值范围内,其中 bih.biWidth 是要迭代的范围。
然而,当我尝试在代码中实现它不起作用时,为什么?任何专家可以指导我,谢谢。
for(int y=0; y<bih.biHeight; y++)
{
for(int x=0; x<bih.biWidth; x++)
{
SetPixel(hdc, (double)1/((double)1+exp(double(-x)))*bih.biWidth,
bih.biHeight-x, red);
}
}
线条几乎从图像的右下角开始,到图像右上角的轻微曲线结束。为什么会这样?
因为 0 是 S 形曲线的中心。 您的x
从 0 开始;如果要对称绘制曲线,则需要计算一个对称的参数,该参数在 0 左右对称:
for(int x=0; x<bih.biWidth; x++)
{
double a= x - 0.5*bih.biWidth;
SetPixel(hdc, bih.biWidth-x, 1.0/(1.0+exp(-a)) * bih.biHeight, red);
}
按常数因子缩放a
将调整 S 形函数的斜率。
(我也怀疑您的原始代码已经切换了 SetPixel(( 参数中使用的缩放因子,所以我已经修复了它。 当 x
的范围从 0 到 bih.biWidth
时,从bih.biHeight
中减去它是没有意义的......
[附加编辑:我还切换了参数,以便biWidth
和biHeight
分别位于x和y坐标中。 无论如何,这是绘制函数的传统方式 - 所以如果你想翻转绘图,你需要把它切换回来]
以下是您尝试执行的操作的惯用代码:
double f(double x) { return 1.0 / (1.0 + exp(-x)); }
void draw_graph(HDC hdc, BITMAPINFOHEADER bih, RECTF graph_bounds)
{
double graph_x, graph_y = f(graph_bounds.left);
MoveToEx(hdc, 0, bih.biHeight * (1 - (graph_y - graph_bounds.bottom) / (graph_bounds.top - graph_bounds.bottom), NULL);
for(int x=1; x<bih.biWidth; x++) {
graph_x = graph_bounds.left + (graph_bounds.right - graph_bounds.left) * x / bih.biWidth;
graph_y = f(graph_x);
LineTo(hdc, x, bih.biHeight * (1 - (graph_y - graph_bounds.bottom) / (graph_bounds.top - graph_bounds.bottom));
}
}
相关文章:
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 适用于 WebView2 旧版本的示例应用程序
- 在 NVIDIA GEFORCE GTX 1050 上下载适用于 Windows 10 的 openCL 1.2
- __attribute__(优化(0))) 是否适用于"recursively"?
- 为什么 std::erase(std::erase_if) 不是适用于<algorithm>任何容器的模板?
- 使用一个参数的模板函数时出错(适用于 2)
- 使用 适用于 Android 和 iOS 的 tf-lite C++ API
- 为什么这适用于 G++ 而不是 CLANG?
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 适用于 Linux 的 c++ 上的代理脚本
- 为什么我的 SFINAE 表达式不再适用于 GCC 8.2?
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 有没有适用于Windows.lib文件的GNU二进制文件描述符(BFD)
- 模板函数仅适用于VS
- 如何在cmake中包含适用于g++或viceversa的库
- 适用于win32、linux、mac的POSIX C包装器
- WinDBG适用于从Visual Studio 2015保存的转储,但不适用于任务管理器。显示异常代码"not found"
- 从uint8_t到NPY_UINT16 PyArray_SimpleNewFromData.适用于Linux,但不适用于