我试图在具有单个特征和多个训练样例(m)的数据集上实现批量梯度下降.
当我尝试使用正规方程时,我得到了正确的答案但错误的答案下面的代码在MATLAB中执行批量梯度下降.
function [theta] = gradientDescent(X, y, theta, alpha, iterations)
m = length(y);
delta=zeros(2,1);
for iter =1:1:iterations
for i=1:1:m
delta(1,1)= delta(1,1)+( X(i,:)*theta - y(i,1)) ;
delta(2,1)=delta(2,1)+ (( X(i,:)*theta - y(i,1))*X(i,2)) ;
end
theta= theta-( delta*(alpha/m) );
computeCost(X,y,theta)
end
end
Run Code Online (Sandbox Code Playgroud)
y是具有目标值的向量,X是一个矩阵,第一列中包含一列和第二列值(变量).
我使用矢量化实现了这一点,即
theta = theta - (alpha/m)*delta
Run Code Online (Sandbox Code Playgroud)
... delta是一个初始化为零的2元素列向量.
成本函数J(Theta)是1/(2m)*(sum from i=1 to m [(h(theta)-y)^2]).
matlab gradient machine-learning linear-regression gradient-descent
mutex m;
void thread_function()
{
static int i = 0;
for(int j =0; j<1000; j++)
{
{ //this scope should make this ineffective according to my understanding
lock_guard<mutex> lock(m);
}
i++;
cout<<i<<endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我从2个线程调用此函数。因此,如果函数以线程安全的方式运行,则int i的期望值为1000 * 2 = 2000。
如果没有互斥锁,则打印i时的结果将在1990到2000之间变化(由于非原子int)。在没有作用域块的情况下插入锁保护装置可以防止这种情况。
但是,据我所知,围绕作用域的块应该使其立即获取并释放锁,因此在写入int i时不再具有线程安全性。但是,我注意到我总是2000。我误会了吗?
我使用vec4i在4元素向量整数数组中使用凸性缺陷存储缺陷.
我的凸壳阵列采用船体元素和等高线轮廓; 我想要做的是从凸起缺陷的起点到一个终点绘制一条线.为此,我需要访问缺陷向量的vec4i中存在的元素起始索引!
我该怎么做呢??
#include <opencv\cv.h>
#include <opencv2\highgui\highgui.hpp>
#include<opencv\cvaux.h>
#include<opencv\cxcore.h>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include<conio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
int main(){
Mat img, frame, img2, img3;
VideoCapture cam(0);
while (true){
cam.read(frame);
cvtColor(frame, img, CV_BGR2HSV);
//thresholding
inRange(img, Scalar(0, 143, 86), Scalar(39, 255, 241), img2);
imshow("hi", img2);
//finding contours
vector<vector<Point>> Contours;
vector<Vec4i> hier;
//morphological transformations
erode(img2, img2, getStructuringElement(MORPH_RECT, Size(3, 3)));
erode(img2, img2, getStructuringElement(MORPH_RECT, Size(3, 3)));
dilate(img2, img2, getStructuringElement(MORPH_RECT, Size(8, 8)));
dilate(img2, img2, getStructuringElement(MORPH_RECT, Size(8, 8)));
//finding the contours …Run Code Online (Sandbox Code Playgroud) 我目前有一个基类Env,它是一个接口,并且我有几个派生类。
class Env
{
public:
//method in question
virtual std::tuple<Eigen::VectorXf,float,bool,std::string> step(const //what goes here?//)=0;
virtual ~Env(); //virtual destructor
};
Run Code Online (Sandbox Code Playgroud)
派生类的示例如下(标题)
class MountainCar: public Env
{
public:
MountainCar();
std::tuple<VectorXf,float,bool,std::string> step(const int) override;
};
Run Code Online (Sandbox Code Playgroud)
现在,设计是所有环境都必须从Env继承。但是,我想强制所有环境来实现step()方法,这就是为什么基本Env中的step方法是纯虚拟方法的原因。
但是,每个派生的Env在step方法中可以采用不同的参数类型,并且这应该是有效的替代(这些有效类型来自固定的已知集合)例如,mountain car用int参数定义了它。另一个环境CartPole使用VectorXf作为step参数。
最初,我使基类成为带有参数U的模板类,然后将U传递给step方法。然后,派生类用于继承模板实例,例如从Env继承的MountainCar示例。但是,问题在于所有派生类都继承自基类的不同实例,并且我不能再将公共基指针用于多态。
如何设计具有C ++ 11功能的系统?
如何找到矩阵给定行中最大的所有索引.max(a(1,:))仅返回第一个最大值!
我想要所有最大元素的索引(多个)
c++ ×3
c++11 ×2
matlab ×2
concurrency ×1
gradient ×1
inheritance ×1
matrix ×1
max ×1
opencv ×1
polymorphism ×1