fit根据loss函数算出梯度,sample_weight对每一个sample的梯度乘以对应的weight
import tensorflow as tf
from tensorflow import keras
from keras.layers import *
import numpy as np
tf.random.set_seed(1)
np.random.seed(1)
x_input = Input(shape=(None, 1))
y_input = Input(shape=(None, 1))
ki = keras.initializers.Constant(value=2)
bi = keras.initializers.Constant(value=-1)
y = Dense(1, kernel_initializer=ki, bias_initializer=bi)(x_input)
def myloss(yt, yp):
return yt - yp
opt = tf.compat.v1.train.GradientDescentOptimizer(1)
m = keras.models.Model(inputs=[x_input, y_input], outputs=y)
m.compile(loss=myloss, optimizer=opt)
x_pure = np.random.randint(1, 4, 1).astype(np.float).reshape(-1, 1)
x_train = x_pure
y_train = 3 * x_pure + 2
print('data:', x_train, y_train)
print(m.trainable_weights, '>>>')
# y-wx-b >> dw=-x,db=-1
with tf.GradientTape() as tape:
loss = myloss(y_train, m([x_train, y_train]))
grads = tape.gradient(loss, m.trainable_weights)
print('grads', grads)
# (w,b)=(2,-1) grads=(-2,-1)
# 没有sample_weight时,(w,b)=(w,b)-grads=(4,0)
# m.fit(x=[x_train, y_train], y=y_train, verbose=1, epochs=1, shuffle=False)
# sample_weight=(3)时,(w,b)=(w,b)-grads*(3)=(8,-2)
m.fit(x=[x_train, y_train], y=y_train, verbose=1, epochs=1,shuffle=False,sample_weight=[np.array([3])])
# trainable_weights返回tensor的list, get_weights返回np.array的list
print(m.trainable_weights, '>>>')
print(m.get_weights(), '>>>')
testdata = np.arange(3).reshape(-1, 1)
print(m.predict([testdata, testdata]))