gpt4 book ai didi

python - TensorFlow 2.0 GradientTape 返回 None 作为手动模型的梯度

转载 作者:行者123 更新时间:2023-11-30 09:58:42 33 4
gpt4 key购买 nike

我正在尝试手动创建逻辑回归模型,但 GradientTape 返回 NoneType 渐变

class LogisticRegressionTF:
def __init__(self,dim):
#dim = X_train.shape[0]
tf.random.set_seed(1)
weight_init = tf.initializers.VarianceScaling(scale=1.0, mode="fan_avg", distribution="uniform", seed=1)
zeros_init = tf.zeros_initializer()
self.W = tf.Variable(zeros_init([dim,1]), trainable=True, name="W")
self.b = tf.Variable(zeros_init([1]), trainable=True, name="b")

def sigmoid(self,z):
x = tf.Variable(z, trainable=True,dtype=tf.float32, name='x')
sigmoid = tf.sigmoid(x)
result = sigmoid
return result

def predict(self, x):
x = tf.cast(x, dtype=tf.float32)
h = tf.sigmoid(tf.add(tf.matmul(tf.transpose(self.W), x), self.b))
return h

def loss(self,logits, labels):
z = tf.Variable(logits, trainable=False,dtype=tf.float32, name='z')
y = tf.Variable(labels, trainable=False,dtype=tf.float32, name='y')
m = tf.cast(tf.size(z), dtype=tf.float32)
cost = tf.divide(tf.reduce_sum(y*tf.math.log(z) + (1-y)*tf.math.log(1-z)),-m)
return cost

def fit(self,X_train, Y_train, lr_rate = 0.01, epochs = 1000):
costs=[]
optimizer = tf.optimizers.SGD(learning_rate=lr_rate)

for i in range(epochs):
current_loss = self.loss(self.predict(X_train), Y_train)
print(current_loss)
with tf.GradientTape() as t:
t.watch([self.W, self.b])
currt_loss = self.loss(self.predict(X_train), Y_train)
print(currt_loss)
grads = t.gradient(currt_loss, [self.W, self.b])
print(grads)
#optimizer.apply_gradients(zip(grads,[self.W, self.b]))
self.W.assign_sub(lr_rate * grads[0])
self.b.assign_sub(lr_rate * grads[1])
if(i %100 == 0):
print('Epoch %2d: , loss=%2.5f' %(i, current_loss))
costs.append(current_loss)

plt.plot(costs)
plt.ylim(0,50)
plt.ylabel('Cost J')
plt.xlabel('Iterations')

log_reg = LogisticRegressionTF(train_set_x.shape[0])
log_reg.fit(train_set_x, train_set_y)

这会产生一个 TypeError,这是由于梯度返回 None

tf.Tensor(0.6931474, shape=(), dtype=float32)
tf.Tensor(0.6931474, shape=(), dtype=float32)
[None, None]

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-192-024668d532b0> in <module>()
1 log_reg = LogisticRegressionTF(train_set_x.shape[0])
----> 2 log_reg.fit(train_set_x, train_set_y)

<ipython-input-191-4fef932eb231> in fit(self, X_train, Y_train, lr_rate, epochs)
40 print(grads)
41 #optimizer.apply_gradients(zip(grads,[self.W, self.b]))
---> 42 self.W.assign_sub(lr_rate * grads[0])
43 self.b.assign_sub(lr_rate * grads[1])
44 if(i %100 == 0):

TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

我的假设函数是 tf.sigmoid(tf.add(tf.matmul(tf.transpose(self.W), x), self.b))

我已手动将成本函数定义为 tf.divide(tf.reduce_sum(y*tf.math.log(z) + (1-y)*tf.math.log(1-z)),-m ),其中 m 是训练样本的数量

验证它是否将损失返回为 tf.Tensor(0.6931474, shape=(), dtype=float32)

我也做了一个 t.watch() 但什么也没发生,它仍然返回 [None, None]

train_set_y.dtype 是 dtype('int64')

train_set_x.dtype 是 dtype('float64')

train_set_x.shape 为 (12288, 209)

train_set_y.shape 为 (1, 209)

类型(train_set_x)是numpy.ndarray

我哪里出错了?

谢谢

最佳答案

在我的环境中,Tensorflow 正在Eagerly 运行,即它处于 Eager Execution 状态。我们可以使用 tf.executing_eagerly() 来检查这一点,以便启用急切执行,它返回 True

问题出在 loss(self,logits, labels): 函数

Logits 不应该是 `tf.Variable(...)'

它应该更改为 z = logits ,并且 logits 应被视为 Tensor 对象而不是 tf.Variable 对象。

我还将 tf.divide 更改为 Eager 模式(尽管不是必需的)

之前:

    def loss(self,logits, labels):
z = tf.Variable(logits, trainable=False,dtype=tf.float32, name='z')
y = tf.Variable(labels, trainable=False,dtype=tf.float32, name='y')
m = tf.cast(tf.size(z), dtype=tf.float32)
cost = tf.divide(tf.reduce_sum(y*tf.math.log(z) + (1-y)*tf.math.log(1-z)),-m)
return cost

之后:

    def loss(self,logits, labels):
z = logits
y = tf.constant(labels,dtype=tf.float32, name='y')
m = tf.cast(tf.size(z), dtype=tf.float32)
cost = (-1/m)*tf.reduce_sum(y*tf.math.log(z) + (1-y)*tf.math.log(1-z))
return cost

关于python - TensorFlow 2.0 GradientTape 返回 None 作为手动模型的梯度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59957233/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com