小编spa*_*ath的帖子

如何在tf.keras自定义损失函数中触发python函数?

在我的自定义损失函数中,我需要调用一个纯python函数,以传递计算出的TD错误和一些索引。该函数不需要返回任何值或进行区分。这是我要调用的函数:

def update_priorities(self, traces_idxs, td_errors):
    """Updates the priorities of the traces with specified indexes."""
    self.priorities[traces_idxs] = td_errors + eps
Run Code Online (Sandbox Code Playgroud)

我试过使用tf.py_function调用包装函数,但是只有在将其嵌入到图形中(即,它具有输入和输出且使用了输出)时,才会调用该函数。因此,我尝试通过某些张量而不对它们执行任何操作,然后调用该函数。这是我的整个自定义损失函数:

def masked_q_loss(data, y_pred):
    """Computes the MSE between the Q-values of the actions that were taken and the cumulative
    discounted rewards obtained after taking those actions. Updates trace priorities.
    """
    action_batch, target_qvals, traces_idxs = data[:,0], data[:,1], data[:,2]
    seq = tf.cast(tf.range(0, tf.shape(action_batch)[0]), tf.int32)
    action_idxs = tf.transpose(tf.stack([seq, tf.cast(action_batch, tf.int32)]))
    qvals = tf.gather_nd(y_pred, action_idxs)

    def update_priorities(_qvals, _target_qvals, _traces_idxs):
        """Computes the TD error …
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow tf.keras tensorflow2.0

6
推荐指数
1
解决办法
116
查看次数

LinearRegression() 和 Ridge(alpha=0) 的区别

当 alpha 参数接近零时,Tikhonov(岭)成本变得等同于最小二乘成本。在一切有关的主题scikit学习文档表示相同。因此我期待

sklearn.linear_model.Ridge(alpha=1e-100).fit(data, target)
Run Code Online (Sandbox Code Playgroud)

相当于

sklearn.linear_model.LinearRegression().fit(data, target)
Run Code Online (Sandbox Code Playgroud)

但事实并非如此。为什么?

更新代码:

import pandas as pd
from sklearn.linear_model import Ridge, LinearRegression
from sklearn.preprocessing import PolynomialFeatures
import matplotlib.pyplot as plt
%matplotlib inline

dataset = pd.read_csv('house_price_data.csv')

X = dataset['sqft_living'].reshape(-1, 1)
Y = dataset['price'].reshape(-1, 1)

polyX = PolynomialFeatures(degree=15).fit_transform(X)

model1 = LinearRegression().fit(polyX, Y)
model2 = Ridge(alpha=1e-100).fit(polyX, Y)

plt.plot(X, Y,'.',
         X, model1.predict(polyX),'g-',
         X, model2.predict(polyX),'r-')
Run Code Online (Sandbox Code Playgroud)

注意:情节看起来相同alpha=1e-8alpha=1e-100

在此处输入图片说明

python regression machine-learning linear-regression scikit-learn

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