'TF.2 model(training=True) prediction is changed when apply_gradient is not applied
I have a question about TF.2 trainable variables..
When I checked the model weights in loops, I found that loss is changed despite of not applying apply_gradient function.
So I stored the variables and loss but I didn't find the weights are changed in loops. Can you explain that for me..?
(That means.. same input/weight -> predictions are different.... T.T )
### model ###
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
#self.flatten = Flatten(input_shape=(32, 28, 28)) # flatten, input
#self.input_layer = Input( shape=(32, 784) )
self.hidden_256_1 = Dense(256, activation='relu', name='hidden1')
self.hidden_256_2 = Dense(256, activation='relu', name='hidden2')
self.drop_out = Dropout(0.5)
self.dense_final = Dense(10)
def call(self, x):
# x = self.flatten(x)
# input_x = self.input_layer(x)
x = self.hidden_256_1(x)
x = self.drop_out(x)
x = self.hidden_256_2(x)
x = self.drop_out(x)
return self.dense_final(x)
model = MyModel()
### print loss from same input/weight ###
vars_list = []
for i in range(5):
predictions = model(x_train[:10].reshape(10,-1), training=True)
loss = loss_object(y_train[:10], predictions)
vars = model.trainable_weights
vars_list.append( vars )
print(loss)
tf.Tensor(2.3669665, shape=(), dtype=float32)
tf.Tensor(2.4489748, shape=(), dtype=float32)
tf.Tensor(2.4756782, shape=(), dtype=float32)
tf.Tensor(2.3325222, shape=(), dtype=float32)
tf.Tensor(2.341004, shape=(), dtype=float32)
# print weights of loop 1
print(vars_list[0][4])
<tf.Variable 'my_model/dense_3/kernel:0' shape=(256, 10) dtype=float32, numpy=
array([[-0.09301923, 0.03969355, -0.01272634, ..., -0.0475869 ,
0.00238203, 0.0469147 ],
[ 0.03952365, -0.08654938, 0.10042584, ..., -0.0594193 ,
-0.01550625, -0.14284368],
[-0.06716147, 0.07387908, 0.07724436, ..., 0.08974642,
-0.11946932, -0.0196999 ],
...,
[-0.12894998, 0.10549605, -0.04042872, ..., -0.07101952,
0.14050862, -0.13851884],
[ 0.05723909, -0.00989152, 0.02192681, ..., 0.11859939,
0.06149144, 0.1294086 ],
[ 0.0371833 , 0.09864074, 0.04617405, ..., -0.01336585,
0.10135004, -0.07977842]], dtype=float32)>
### print weights of loop 2
print(vars_list[1][4])
<tf.Variable 'my_model/dense_3/kernel:0' shape=(256, 10) dtype=float32, numpy=
array([[-0.09301923, 0.03969355, -0.01272634, ..., -0.0475869 ,
0.00238203, 0.0469147 ],
[ 0.03952365, -0.08654938, 0.10042584, ..., -0.0594193 ,
-0.01550625, -0.14284368],
[-0.06716147, 0.07387908, 0.07724436, ..., 0.08974642,
-0.11946932, -0.0196999 ],
...,
[-0.12894998, 0.10549605, -0.04042872, ..., -0.07101952,
0.14050862, -0.13851884],
[ 0.05723909, -0.00989152, 0.02192681, ..., 0.11859939,
0.06149144, 0.1294086 ],
[ 0.0371833 , 0.09864074, 0.04617405, ..., -0.01336585,
0.10135004, -0.07977842]], dtype=float32)>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
