def train_linreg(X_train, y_train, eta, epochs):
costs = []
eta0 = T.fscalar('eta0')
y = T.fvector(name='y')
X = T.fmatrix(name='X')
w = theano.shared(
np.zeros(shape=(X_train.shape[1] + 1), dtype=theano.config.floatX),
name='w',
)
net_input = T.dot(X, w[1:]) + w[0]
errors = y - net_input
cost = T.sum(T.pow(errors, 2))
gradient = T.grad(cost, wrt=w)
update = [(w, w - (eta0 * gradient))]
train = theano.function(
inputs=[eta0],
outputs=cost,
updates=update,
givens={X: X_train, y: y_train},
)
for _ in range(epochs):
costs.append(train(eta))
return costs, w
chapter_13.py 文件源码
python
阅读 53
收藏 0
点赞 0
评论 0
评论列表
文章目录