ValueError:通过张量丢失时需要磁带



如https://www.tensorflow.org/api_docs/python/tf/keras/optimizers/Optimizer?hl=en#minimize所述,minimize的第一个参数应满足要求,

张量或可调用的。如果是可调用的,loss应该不带参数并返回要最小化的值。如果是张量,则必须传递tape参数

第一段代码以张量作为minimize()的输入,它需要梯度带,但我不知道怎么用。

第二段代码将可调用函数作为minimize()的输入,这很简单
import numpy as np
import tensorflow as tf
from tensorflow import keras
x_train = [1, 2, 3]
y_train = [1, 2, 3]
W = tf.Variable(tf.random.normal([1]), name='weight')
b = tf.Variable(tf.random.normal([1]), name='bias')
hypothesis = W * x_train + b

@tf.function
def cost():
y_model = W * x_train + b
error = tf.reduce_mean(tf.square(y_train - y_model))
return error

optimizer = tf.optimizers.SGD(learning_rate=0.01)
cost_value = cost()
train = tf.keras.optimizers.Adam().minimize(cost_value, var_list=[W, b])
tf.print(W)
tf.print(b)

如何添加渐变磁带,我知道下面的代码当然可以工作。

import numpy as np
import tensorflow as tf
from tensorflow import keras
x_train = [1, 2, 3]
y_train = [1, 2, 3]
W = tf.Variable(tf.random.normal([1]), name='weight')
b = tf.Variable(tf.random.normal([1]), name='bias')
hypothesis = W * x_train + b

@tf.function
def cost():
y_model = W * x_train + b
error = tf.reduce_mean(tf.square(y_train - y_model))
return error

optimizer = tf.optimizers.SGD(learning_rate=0.01)
cost_value = cost()
train = tf.keras.optimizers.Adam().minimize(cost, var_list=[W, b])
tf.print(W)
tf.print(b)

请帮我修改第一段代码,让它运行,谢谢!

这是一个迟来的答案(Hakan基本上为你找到了),但我写这篇文章是希望它能帮助那些在未来被困在谷歌上搜索这个确切问题的人(就像我一样)。这也是直接使用tf.GradientTape()的另一种实现。

import numpy as np
import tensorflow as tf
from tensorflow import keras
x_train = [1, 2, 3]
y_train = [1, 2, 3]
W = tf.Variable(tf.random.normal([1]), trainable = True, name='weight')
b = tf.Variable(tf.random.normal([1]), trainable = True, name='bias')
@tf.function
def cost(W, b):
y_model = W * x_train + b
error = tf.reduce_mean(tf.square(y_train - y_model))
return error

optimizer = tf.optimizers.SGD(learning_rate=0.01)
trainable_vars = [W,b]
epochs = 100 #(or however many iterations you want it to run)
for _ in range(epochs):
with tf.GradientTape() as tp:
#your loss/cost function must always be contained within the gradient tape instantiation
cost_fn = cost(W, b)
gradients = tp.gradient(cost_fn, trainable_vars)
optimizer.apply_gradients(zip(gradients, trainable_vars))
tf.print(W)
tf.print(b)

这应该给你的权重和偏差的值后,你运行的epoch的数量。

必须在每次调用新的梯度磁带时计算损失函数。然后你得到损失函数的梯度,然后调用优化器。apply_gradient根据tensorflow文档在这里所说的做最小化:https://www.tensorflow.org/api_docs/python/tf/keras/optimizers/Optimizer#apply_gradients.

这是因为.minimize()需要一个函数。而cost_value&cost()是tf。张量对象,代价是一个tf函数。你应该直接把你的损失函数作为tf.keras.optimizers.Adam().minimize(cost, var_list=[W, b])传递给最小值。

改变了渐变部分:

train = tf.keras.optimizers.Adam().minimize(cost(), var_list=[W, b],tape=tf.GradientTape())

优化过程需要不断迭代才能获得更好的结果。将计算包在GradientTape中以实现自动区分。

with tf.GradientTape() as g:
pred = W * X + b # Linear regression.
loss = tf.reduce_mean(tf.square(pred - Y))

计算梯度。

gradients = g.gradient(loss, [W, b])

根据梯度更新W和b。

optimizer.apply_gradients(zip(gradients, [W, b]))

最新更新