Python 线性回归与自回归
假设$F\in\mathbb{R}^{S\times F}$是特征矩阵,我想使用逻辑回归和autograd[1]对它们进行分类。我使用的代码与下面示例[2]中的代码类似 我唯一想改变的是,我有一个额外的权重矩阵$W$,在$\mathbb{R}^{F\times L}$中,我想应用于每个特性。因此,每个特征乘以$W$,然后输入逻辑回归 是否有可能同时使用autograd训练$W$和逻辑回归的权重 我尝试了下面的代码,不幸的是权重保持为0Python 线性回归与自回归,python,numpy,machine-learning,mathematical-optimization,Python,Numpy,Machine Learning,Mathematical Optimization,假设$F\in\mathbb{R}^{S\times F}$是特征矩阵,我想使用逻辑回归和autograd[1]对它们进行分类。我使用的代码与下面示例[2]中的代码类似 我唯一想改变的是,我有一个额外的权重矩阵$W$,在$\mathbb{R}^{F\times L}$中,我想应用于每个特性。因此,每个特征乘以$W$,然后输入逻辑回归 是否有可能同时使用autograd训练$W$和逻辑回归的权重 我尝试了下面的代码,不幸的是权重保持为0 import autograd.numpy as np fr
import autograd.numpy as np
from autograd import grad
global inputs
def sigmoid(x):
return 0.5 * (np.tanh(x) + 1)
def logistic_predictions(weights, inputs):
# Outputs probability of a label being true according to logistic model.
return sigmoid(np.dot(inputs, weights))
def training_loss(weights):
global inputs
# Training loss is the negative log-likelihood of the training labels.
feature_weights = weights[3:]
feature_weights = np.reshape(feature_weights, (3, 3))
inputs = np.dot(inputs, feature_weights)
preds = logistic_predictions(weights[0:3], inputs)
label_probabilities = preds * targets + (1 - preds) * (1 - targets)
return -np.sum(np.log(label_probabilities))
# Build a toy dataset.
inputs = np.array([[0.52, 1.12, 0.77],
[0.88, -1.08, 0.15],
[0.52, 0.06, -1.30],
[0.74, -2.49, 1.39]])
targets = np.array([True, True, False, True])
# Define a function that returns gradients of training loss using autograd.
training_gradient_fun = grad(training_loss)
# Optimize weights using gradient descent.
weights = np.zeros([3 + 3 * 3])
print "Initial loss:", training_loss(weights)
for i in xrange(100):
print(i)
print(weights)
weights -= training_gradient_fun(weights) * 0.01
print "Trained loss:", training_loss(weights)
[1]
[2] 典型的做法是将所有“矢量化”参数连接到决策变量向量中 如果通过以下方式更新
logistic_预测
以包括W
矩阵
def logistic_predictions(weights_and_W, inputs):
'''
Here, :arg weights_and_W: is an array of the form [weights W.ravel()]
'''
# Outputs probability of a label being true according to logistic model.
weights = weights_and_W[:inputs.shape[1]]
W_raveled = weights_and_W[inputs.shape[1]:]
n_W = len(W_raveled)
W = W_raveled.reshape(inputs.shape[1], n_W/inputs.shape[1])
return sigmoid(np.dot(np.dot(inputs, W), weights))
然后只需将traning\u loss
更改为(从原始源代码示例)
典型的做法是将所有“矢量化”参数连接到决策变量向量中 如果通过以下方式更新
logistic_预测
以包括W
矩阵
def logistic_predictions(weights_and_W, inputs):
'''
Here, :arg weights_and_W: is an array of the form [weights W.ravel()]
'''
# Outputs probability of a label being true according to logistic model.
weights = weights_and_W[:inputs.shape[1]]
W_raveled = weights_and_W[inputs.shape[1]:]
n_W = len(W_raveled)
W = W_raveled.reshape(inputs.shape[1], n_W/inputs.shape[1])
return sigmoid(np.dot(np.dot(inputs, W), weights))
然后只需将traning\u loss
更改为(从原始源代码示例)
如果像DOS这样支持内联方程,至少对于numpy问题,那将非常好。如果像DOS这样支持内联方程,至少对于numpy问题,那将非常好。