【发布时间】:2020-12-13 23:45:20
【问题描述】:
我是 Keras 的新手,正在尝试自定义我在 Keras 中的训练步骤。
问题:
- 自定义训练循环时如何在 Keras 中使用
weights_right=weights- (lr+alpha)*gradients创建新变量 weights_right? - 如何以权重作为形式参数前馈 NN?我可以像下面的代码那样自定义 Keras 中的转发功能吗?
背景:
在随机梯度下降算法中,在前馈一个小批量数据并获得这个小批量数据的梯度后,我想扰动权重并创建一个名为 weights_right weights_righ t= weights-(lr+alpha)*gradients 的新变量(alpha 是一个常量) 然后用 weights_right 前馈 NN 以获得新的损失。
python中的部分代码如下:
class Network(object):
def __init__(self, sizes):
self.num_layers = len(sizes)
self.sizes = sizes
self.weights = [np.random.randn(y,1) for y in sizes[1:]]
self.biases = [np.random.randn(y,x) for x, y in zip(sizes[:-1], sizes[1:])]
def feedforward(self, a, weights=None, biases=None):
"""Return the output of the network if ``a`` is input."""
if weights is None:
weights=self.weights
if biases is None:
biases=self.biases
#!!! Note the output layer has no activation for regression.
for b, w in zip(biases[:-1], weights[:-1]):
a = sigmoid(np.dot(w, a)+b)
a=np.dot(weights[-1],a)+biases[-1]
return a
#-----------------------------------
# The following is the important one.
#-----------------------------------
def customSGD():
for epoch in range(epochs):
random.shuffle(training_data)
mini_batches= [training_data[k:k+mini_batch_size] for k in range(0, len(training_data), mini_batch_size)]
for mini_batch in mini_batches:
gradients_on_mini_batch = get_gradients(mini_batch)
#---------------------------------------
# The following two steps are what
# I would like to archive in Keras
#---------------------------------------
# Creat new variable called weights_right
weights_right = weights-(lr+alpha)*gradients_on_mini_batch
# feed the NN with weights_right, note that the params
#in current NN are still weights, not weights_right.
pred_right = feedforward(training_data, weights_right)
loss_right = loss_func(pred_right, training_labels)
......
# update weights
weights = weights-lr*gradients_on_mini_batch
以上代码主要来自网书Michael Nielsen。
任何帮助将不胜感激。非常感谢!
【问题讨论】:
-
@Andrey。嗨,安德烈,非常感谢您的回复。我会重写我的问题。其实我找了很久,也没有很清楚如何在 Keras 中执行。但我知道如何使用 numpy 在 Python 中编写代码。无论如何,非常感谢!
标签: python tensorflow keras neural-network