小编Mil*_*los的帖子

在python中使用Abs查找函数的派生

我想y=Abs(0.5-0.5*sqrt(1-4*x))使用python 计算0.1的导数。这是我的代码:

x=Symbol('x')
y=Abs(0.5-0.5*sqrt(1-4*x))
deriv=y.diff(x)
d=lambdify(x,deriv,'numpy')
print d(0.1)
Run Code Online (Sandbox Code Playgroud)

这是我得到的:

Traceback (most recent call last):
  File "/home/milossimic/g4/s1/.../optimize.py", line 100, in <module>
    print d(0.1)
  File "<string>", line 1, in <lambda>
NameError: global name 'Derivative' is not defined
Run Code Online (Sandbox Code Playgroud)

我是一个新手,sympy而且numpy,所以我想我使用了错误的方法来确定衍生物。

编辑:我打印了派生,这就是我得到的:

在此处输入图片说明

阅读此http://docs.sympy.org/dev/modules/functions/elementary.html之后,我尝试了fdiff()

x=Symbol('x')
y=Abs(0.5-0.5*sqrt(1-4*x))
deriv=y.fdiff()
d=lambdify(x,deriv,'numpy')
print d(0)
Run Code Online (Sandbox Code Playgroud)

但是,在尝试了其他值以计算导数之后,我发现结果deriv实际上是-1、0或1 sign(-0.5*sqrt(-4*x + 1) + 0.5)

我该怎么办?

numpy和sympy都被导入:

from sympy import *
import numpy as np
Run Code Online (Sandbox Code Playgroud)

如果我尝试查找不在Abs下的函数的派生,则没有问题。

numpy sympy derivative python-2.7

5
推荐指数
2
解决办法
1632
查看次数

为什么 drop1 忽略混合模型的线性项?

我有六个固定因子:A, B, C, D, EandF和一个随机因子R。我想使用语言 R 测试线性项、纯二次项和双向交互。因此,我构建了完整的线性混合模型并尝试使用以下方法测试其项drop1

full.model <- lmer(Z ~ A + B + C + D + E + F
                     + I(A^2) + I(B^2) + I(C^2) + I(D^2) + I(E^2) + I(F^2)
                     + A:B + A:C + A:D + A:E + A:F
                           + B:C + B:D + B:E + B:F
                                 + C:D + C:E + C:F 
                                       + D:E + D:F
                                             + E:F
                     + (1 | R), data=mydata, REML=FALSE)
drop1(full.model, test="Chisq") …
Run Code Online (Sandbox Code Playgroud)

r linear-regression mixed-models

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

如何使用张量流更正此自定义损失函数?

我想编写一个自定义损失函数,该函数将权衡正目标值的低估。它将像均方误差一样工作,唯一的区别在于,在这种情况下,均方误差将乘以大于1的权重。

我这样写:

def wmse(ground_truth, predictions):
    square_errors = np.square(np.subtract(ground_truth, predictions))
    weights = np.ones_like(square_errors)
    weights[np.logical_and(predictions < ground_truth, np.sign(ground_truth) > 0)] =  100
    weighted_mse = np.mean(np.multiply(square_errors, weights))
    return weighted_mse
Run Code Online (Sandbox Code Playgroud)

然而,当我把它提供给我的顺序模型kerastensorflow作为后端:

model.compile(loss=wmse,optimizer='rmsprop')
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

 raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. 
TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to …
Run Code Online (Sandbox Code Playgroud)

machine-learning keras tensorflow loss-function

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