我创建了一个使用Sigmoid函数实现Logistic回归的类。 在下面的定义中查找以供参考。
class model():
def __init__(self):
self.loss = 0.0
self.accuracy = 0.0
@tf.function
def Model(self,X):
return tf.sigmoid(tf.multiply(X,self.weights[0])+self.weights[1])
def fit(self,x_train,y_train,learning_rate=0.001, epochs=10):
self.x_train = x_train
self.y_train = y_train
self.weights = tf.Variable([0., 0.], dtype=x_train.dtype)
loss = lambda: tf.reduce_mean((tf.abs(tf.subtract(y_train,self.Model(self.x_train)))))
for epoch in range(epochs):
_ = tf.keras.optimizers.SGD(learning_rate=learning_rate).minimize(loss, var_list=[self.weights])
print(self.weights)
self.loss = loss().numpy()
def predict(self, x_test):
self.y_pred = self.Model(x_test)
return np.round(self.y_pred.numpy())
def evaluate(self,x_test,y_test):
return (tf.constant(1, dtype=tf.float64) - tf.reduce_mean(tf.abs(tf.subtract(y_test,self.Model(x_test))))).numpy()
然后,我将创建此类的对象用于训练和预测,如下所示:
m = model()
m.fit(x_train,y_train,epochs=100,learning_rate=0.1)
m.evaluate(x_test,y_test)
这在第一次迭代中运行良好。但是,当我第二次运行m.fit而不重新初始化该对象时,它将引发以下提到的异常:
ValueError Traceback (most recent call last)
<ipython-input-268-39014f3ec7dd> in <module>
----> 1 m.fit(x_train,y_train,epochs=100,learning_rate=0.1)
<ipython-input-265-4d5e4e8ae4e1> in fit(self, x_train, y_train, learning_rate, epochs)
14 loss = lambda: tf.reduce_mean((tf.abs(tf.subtract(y_train,self.Model(self.x_train)))))
15 for epoch in range(epochs):
---> 16 _ = tf.keras.optimizers.SGD(learning_rate=learning_rate).minimize(loss, var_list=[self.weights])
17 print(self.weights)
18 self.loss = loss().numpy()
/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/optimizer_v2/optimizer_v2.py in minimize(self, loss, var_list, grad_loss, name)
317 loss, var_list=var_list, grad_loss=grad_loss)
318
--> 319 return self.apply_gradients(grads_and_vars, name=name)
320
321 def _compute_gradients(self, loss, var_list, grad_loss=None):
/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/optimizer_v2/optimizer_v2.py in apply_gradients(self, grads_and_vars, name)
425 ValueError: If none of the variables have gradients.
426 """
--> 427 grads_and_vars = _filter_grads(grads_and_vars)
428 var_list = [v for (_, v) in grads_and_vars]
429
/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/optimizer_v2/optimizer_v2.py in _filter_grads(grads_and_vars)
1023 if not filtered:
1024 raise ValueError("No gradients provided for any variable: %s." %
-> 1025 ([v.name for _, v in grads_and_vars],))
1026 if vars_with_empty_grads:
1027 logging.warning(
ValueError: No gradients provided for any variable: ['Variable:0'].```