计算Tensorflow与Pyrotch中的梯度

2024-04-26 14:21:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图计算一个简单线性模型的损失梯度。然而,我面临的问题是,当使用TensorFlow时,梯度计算为“无”。为什么会发生这种情况,以及如何使用TensorFlow计算梯度

import numpy as np
import tensorflow as tf

inputs = np.array([[73, 67, 43], 
                   [91, 88, 64], 
                   [87, 134, 58], 
                   [102, 43, 37], 
                   [69, 96, 70]], dtype='float32')

targets = np.array([[56, 70], 
                    [81, 101], 
                    [119, 133], 
                    [22, 37], 
                    [103, 119]], dtype='float32')

inputs = tf.convert_to_tensor(inputs)
targets = tf.convert_to_tensor(targets)

w = tf.random.normal(shape=(2, 3))
b = tf.random.normal(shape=(2,))
print(w, b)

def model(x):
  return tf.matmul(x, w, transpose_b = True) + b

def mse(t1, t2):
  diff = t1-t2
  return tf.reduce_sum(diff * diff) / tf.cast(tf.size(diff), 'float32')

with tf.GradientTape() as tape:
  pred = model(inputs)
  loss = mse(pred, targets)

print(tape.gradient(loss, [w, b]))

下面是使用PyTorch的工作代码。按预期计算坡度

import torch

inputs = np.array([[73, 67, 43], 
                   [91, 88, 64], 
                   [87, 134, 58], 
                   [102, 43, 37], 
                   [69, 96, 70]], dtype='float32')

targets = np.array([[56, 70], 
                    [81, 101], 
                    [119, 133], 
                    [22, 37], 
                    [103, 119]], dtype='float32')

inputs = torch.from_numpy(inputs)
targets = torch.from_numpy(targets)

w = torch.randn(2, 3, requires_grad = True)
b = torch.randn(2, requires_grad = True)

def model(x):
  return x @ w.t() + b

def mse(t1, t2):
  diff = t1 - t2
  return torch.sum(diff * diff) / diff.numel()

pred = model(inputs)
loss = mse(pred, targets)
loss.backward()

print(w.grad)
print(b.grad)

Tags: modelreturntfdefnpdifftorcharray
1条回答
网友
1楼 · 发布于 2024-04-26 14:21:12

您的代码不起作用,因为在tensorflow中,只为tf.Variable计算梯度。创建图层时,TF会自动将其权重和偏差标记为变量(除非指定trainable=False

因此,为了使代码正常工作,您只需将wb包装为tf.Variable

w = tf.Variable(tf.random.normal(shape=(2, 3)), name='w')
b = tf.Variable(tf.random.normal(shape=(2,)), name='b')

使用这些行定义权重和偏差,您将在最终打印中获得实际值

相关问题 更多 >