守护进程与Clojure / JVM(Daemon with Clojure/JVM)
我想在一个小服务器上运行一个小的(不是太多太多)守护进程,看一个目录添加到它的新文件(以及主程序中的任何目录),并调用另一个Clojure程序来处理那个新文件。
理想情况下,每个文件都会被添加到一个队列(由Clojure中的
ref
表示的列表?),并且主进程将以FIFO为基础处理队列中的那些文件。我的问题是:有一个JVM一直在运行这个小程序太多资源吗? 你有什么建议去做这个吗?
非常感谢你!
编辑:我应该问的另一个问题:我应该将它作为自己的实例运行(使用更少的内存)并让它在看到文件时启动新的JVM,或者在同一个JVM上运行将处理文件的Clojure代码?
I'd like to have a small (not doing too damn much) daemon running on a little server, watching a directory for new files being added to it (and any directories in the main one), and calling another Clojure program to deal with that new file.
Ideally, each file would be added to a queue (a list represented by a
ref
in Clojure?) and the main process would take care of those files in the queue on a FIFO basis.My question is: is having a JVM up running this little program all the time too much a resource hog? And do you have any suggestions as to how go about doing this?
Thank you very much!
EDIT: Another question I should ask: should I run this as its own instance (using less memory) and have it launch a new JVM when a file is seen, or have it on the same JVM the Clojure code that will process the file?
原文:https://stackoverflow.com/questions/2933280
最满意答案
拦截部分有一个错误(paramsA)。 其他一切都很好。 我已经实施了Alexey在评论中提出的建议。 这是解决方案:
pow <- function(x,y) { return(x^y) } #### posterior distribution posteriorDistribution <- function(x, y, a, b,s2,N) { sumSqError <- 0.0 for(i in 1:N) { sumSqError <- sumSqError + pow(y[i] - (a + b*x[i]),2) } return((-((N/2)+1) * log(s2)) + ((-0.5/s2) * sumSqError)) } # x <- x values # y <- actual datapoints # N <- sample size # m <- length of chain # sigmaProposalWidth <- width of uniform proposal dist for sigma squared # paramAProposalWidth <- width of uniform proposal dist for intercept # paramBProposalWidth <- width of uniform proposal dist for slope mcmcSampling <- function(x,y,N,m,sigmaProposalWidth,paramAProposalWidth,paramBProposalWidth) { desiredAcc <- 0.44 paramsA <- vector("numeric",length=m) # intercept paramsB <- vector("numeric",length=m) # slope s2 <- vector("numeric",length=m) # sigma squared paramsA[1] <- 0 paramsB[1] <- 0 s2[1] <- 1 accATot <- 0 accBTot <- 0 accS2Tot <- 0 for(i in 2:m) { paramsA[i] <- paramsA[i-1] + runif(1,-paramAProposalWidth,paramAProposalWidth) accA <- 1 if((posteriorDistribution(x,y,paramsA[i],paramsB[i-1],s2[i-1],N) - posteriorDistribution(x,y,paramsA[i-1],paramsB[i-1],s2[i-1],N)) < log(runif(1))) { paramsA[i] <- paramsA[i-1] accA <- 0 } accATot <- accATot + accA paramsB[i] <- paramsB[i-1] + runif(1,-paramBProposalWidth,paramBProposalWidth) accB <- 1 if((posteriorDistribution(x,y,paramsA[i],paramsB[i],s2[i-1],N) - posteriorDistribution(x,y,paramsA[i-1],paramsB[i-1],s2[i-1],N)) < log(runif(1))) { paramsB[i] <- paramsB[i-1] accB <- 0 } accBTot <- accBTot + accB s2[i] <- s2[i-1] + runif(1,-sigmaProposalWidth,sigmaProposalWidth) accS2 <- 1 if((s2[i] < 0) || (posteriorDistribution(x,y,paramsA[i],paramsB[i],s2[i],N) - posteriorDistribution(x,y,paramsA[i],paramsB[i],s2[i-1],N)) < log(runif(1))) { s2[i] <- s2[i-1] accS2 <- 0 } accS2Tot <- accS2Tot + accS2 if(i%%100==0) { paramAProposalWidth <- paramAProposalWidth * ((accATot/100)/desiredAcc) paramBProposalWidth <- paramBProposalWidth * ((accBTot/100)/desiredAcc) sigmaProposalWidth <- sigmaProposalWidth * ((accS2Tot/100)/desiredAcc) accATot <- 0 accBTot <- 0 accS2Tot <- 0 } } res <- data.frame(paramsA,paramsB,s2) return(res) }
There was one mistake in the intercept section (paramsA). Everything else was fine. I've implemented what Alexey suggested in his comments. Here's the solution:
pow <- function(x,y) { return(x^y) } #### posterior distribution posteriorDistribution <- function(x, y, a, b,s2,N) { sumSqError <- 0.0 for(i in 1:N) { sumSqError <- sumSqError + pow(y[i] - (a + b*x[i]),2) } return((-((N/2)+1) * log(s2)) + ((-0.5/s2) * sumSqError)) } # x <- x values # y <- actual datapoints # N <- sample size # m <- length of chain # sigmaProposalWidth <- width of uniform proposal dist for sigma squared # paramAProposalWidth <- width of uniform proposal dist for intercept # paramBProposalWidth <- width of uniform proposal dist for slope mcmcSampling <- function(x,y,N,m,sigmaProposalWidth,paramAProposalWidth,paramBProposalWidth) { desiredAcc <- 0.44 paramsA <- vector("numeric",length=m) # intercept paramsB <- vector("numeric",length=m) # slope s2 <- vector("numeric",length=m) # sigma squared paramsA[1] <- 0 paramsB[1] <- 0 s2[1] <- 1 accATot <- 0 accBTot <- 0 accS2Tot <- 0 for(i in 2:m) { paramsA[i] <- paramsA[i-1] + runif(1,-paramAProposalWidth,paramAProposalWidth) accA <- 1 if((posteriorDistribution(x,y,paramsA[i],paramsB[i-1],s2[i-1],N) - posteriorDistribution(x,y,paramsA[i-1],paramsB[i-1],s2[i-1],N)) < log(runif(1))) { paramsA[i] <- paramsA[i-1] accA <- 0 } accATot <- accATot + accA paramsB[i] <- paramsB[i-1] + runif(1,-paramBProposalWidth,paramBProposalWidth) accB <- 1 if((posteriorDistribution(x,y,paramsA[i],paramsB[i],s2[i-1],N) - posteriorDistribution(x,y,paramsA[i-1],paramsB[i-1],s2[i-1],N)) < log(runif(1))) { paramsB[i] <- paramsB[i-1] accB <- 0 } accBTot <- accBTot + accB s2[i] <- s2[i-1] + runif(1,-sigmaProposalWidth,sigmaProposalWidth) accS2 <- 1 if((s2[i] < 0) || (posteriorDistribution(x,y,paramsA[i],paramsB[i],s2[i],N) - posteriorDistribution(x,y,paramsA[i],paramsB[i],s2[i-1],N)) < log(runif(1))) { s2[i] <- s2[i-1] accS2 <- 0 } accS2Tot <- accS2Tot + accS2 if(i%%100==0) { paramAProposalWidth <- paramAProposalWidth * ((accATot/100)/desiredAcc) paramBProposalWidth <- paramBProposalWidth * ((accBTot/100)/desiredAcc) sigmaProposalWidth <- sigmaProposalWidth * ((accS2Tot/100)/desiredAcc) accATot <- 0 accBTot <- 0 accS2Tot <- 0 } } res <- data.frame(paramsA,paramsB,s2) return(res) }
相关问答
更多-
如果y碰巧为0(并且每次迭代的概率为0.5,这将确定地发生),则alpha为0/0(因为x[i-1] == 0 )。 它给你NaN 。 条件something <= NaN提供NA 。 If y happens to be 0 (and with 0.5 probability for each iteration this will happen with certainty), then alpha is 0 / 0 (because x[i-1] == 0). It gives you NaN. Co ...
-
Sklearn - 线性回归(Sklearn - Linear regression)[2022-09-09]
您的代码在LinearRegression的构造函数中有错误。 代替: reg = LinearRegression(x,y) 做这个: reg = LinearRegression() 现在至于你所说的错误,这是因为你在X中只有一列。所以当前的形状是 (n_rows,) 所有scikit估计器都需要X的形状: (n_rows, n_columns) 所以,像这样重塑你的X: X = X.reshape(-1,1) 然后将它们传递给fit() Your code has error in the ... -
我只是从Spark MLlib开始,特别是线性回归,所以我只能讨论技术问题(不是为什么在机器学习中这样做的原因)。 这是我陷入困境的部分。 如何使用此模型进行预测? 模型是变换器(如VectorAssembler ),它提供了一个与变换运算符非常简单的接口。 transform(dataset:Dataset [_]):DataFrame转换输入数据集。 这就是您传递数据集并返回另一个带有prediction列的数据集的位置。 这就是训练和预测的一般方法。 以下内容将为您提供输入数据集中功能的预测。 val ...
-
这是一个快速设置,以帮助您入门 from statsmodels.tsa.stattools import ARMA import pandas as pd import numpy as np ts = pd.Series(np.random.randn(500), index=pd.date_range('2010-01-01', periods=500)) p, q = 1, 1 arma = ARMA(endog=ts, order=(p, q)).fit() print arma.summ ...
-
它有点笨重,但可以将模型导出到PMML文件,重新读入然后使用XPath解析它。 这个链接有一个使用决策树的例子。 It's mildly clunky but it is possible to export the model to a PMML file, read it back in and then parse it using XPath. There's an example using decision trees at this link.
-
Python:线性回归,重塑用于模型的numpy数组(Python: Linear Regression, reshaping numpy arrays for use in model)[2023-04-19]
改变这些行 X = np.reshape(X,(5)) Y = np.reshape(Y,(5)) 或者只是删除它们 Change these lines X = np.reshape(X,(5)) Y = np.reshape(Y,(5)) or just removed them both -
线性回归模型(Linear regression model)[2022-05-28]
您正在进行随机梯度下降,评估每个数据点的拟合度。 所以最后的m和c给出了拟合关系的参数。 您绘制的线是拟合线的“演变”。 以下是我绘制它的方式,以及对代码的一些其他调整,因为我弄清楚你在做什么: import numpy as np import matplotlib.pyplot as plt X = np.array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) Y = np.array([ 3, 5, 9, 9, 11, 13, 16, 17, 19, 2 ... -
您可以使用bquote对语言进行一些简单的计算来构建您的呼叫。 temp_fm_list = lapply(temp_formula_list, function(x) { lmc <- bquote( lm(data = mtcars, formula = .(x))) eval(lmc) }) temp_fm_list # Call: # lm(formula = hp ~ 1, ...
-
拦截部分有一个错误(paramsA)。 其他一切都很好。 我已经实施了Alexey在评论中提出的建议。 这是解决方案: pow <- function(x,y) { return(x^y) } #### posterior distribution posteriorDistribution <- function(x, y, a, b,s2,N) { sumSqError <- 0.0 for(i in 1:N) { sumSqError <- sumSqError + pow(y[i] - ...
-
你有3个问题: 1)您似乎想要模拟二项分布,因此随机游走应该在1:n范围内的整数而不是[0,1]范围内的实数。 2)你在alpha的计算中切换了分子和分母 3)你有一个错误的X[t] < X[t - 1] 。 修复这些并清理你的代码(包括使用dbinom函数@ZheyuanLi建议)产生: metropolisAlgorithm <- function(n, p, T){ # implementation of the algorithm # @n,p binomial parameters # ...