pythonoptimizationtensorflowkerasadam

How does the ADAM optimizer output a specific value for v_t in keras?


I want to look at the results of each iteration of the ADAM algorithm that computes v_t, so I added the following code to the Adam class in the keras\optimizers.py file:

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

I want to get every numeric value in this shape, similar to the form of a matrix. But the output below is not what I expected:

v_t: Tensor("training/Adam/add_2:0", shape=(8, 512), dtype=float32)

v_t: Tensor("training/Adam/add_5:0", shape=(128, 512), dtype=float32)

This line of code is part of the Adam class itself, and the next line adds its own:

v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g)
print('v_t:',v_t)# This is my added code

I made the following attempts but would report the error as follows: InvalidArgumentError (see above for traceback): Shape [-1,1,8] has negative dimensions [[Node: lstm_1_input = Placeholderdtype=DT_FLOAT, shape=[?,1,8], _device="/job:localhost/replica:0/task:0/cpu:0"]]

This is my model:

model = Sequential()

model.add(LSTM(128, input_shape=(train_X.shape[1], train_X.shape[2])))

model.add(Dense(1))

model.compile(loss='mae', optimizer='adam',metrics=['mae'])

I just want to output every value in this shape.


Solution

  • If your backend is Tensorflow you can use tf.Print() to print the value of a tensor.