Соответствующий выпуск с полным кодом;выдержка:
var_delta = m_t / (K.sqrt(v_t) + epsilon_t)
var_update = state_ops.assign_sub(var, lr_t * var_delta, use_locking=self._use_locking)
Выше не удается обновить var
, но использование math_ops.sqrt
исправляет это. Чтобы дифференцировать, ниже приведены некоторые print
заявления и их результаты. Код также включал демонстрацию ограничения самоанализа.
Похоже, что K.sqrt
дает Keras Tensor
, тогда как math_ops.sqrt
возвращает tf.Tensor
, причем первый не показывает его значение, даже если включен eager. Однако при вызове K.eval
на K.sqrt
отображается одно и то же значение, поэтому оно не теряется и не отличается.
Все рассмотрено: почему state_ops.assign_sub
работает с EagerTensor
, но не работает с Tensor
? Насколько они разные? - (TF2, Keras 2.3.0)
X1 = math_ops.sqrt(v_t)
X2 = K.sqrt(v_t)
Y1 = m_t / (math_ops.sqrt(v_t) + epsilon_t)
Y2 = m_t / (K.sqrt(v_t) + epsilon_t)
print(X1); print(X2); print()
print(Y1); print(Y2); print()
print(type(X1)); print(type(X2)); print()
print(type(Y1)); print(type(Y2)); print()
print("type(ref) =", type(var))
tf.Tensor(
[[0.0002048 0.00189643]
[0.00204984 0.00426161]
[0.0013686 0.0048186 ]
[0.00296201 0.00318883]], shape=(4, 2), dtype=float32)
Tensor("Sqrt:0", shape=(4, 2), dtype=float32, device=/job:localhost/
replica:0/task:0/device:CPU:0)
tf.Tensor(
[[ 3.1607556 -3.1621318]
[-3.162145 -3.1622243]
[ 3.1620677 3.162233 ]
[-3.1621926 -3.1621997]], shape=(4, 2), dtype=float32)
Tensor("truediv:0", shape=(4, 2), dtype=float32, device=/job:localhost/
replica:0/task:0/device:CPU:0)
<class 'tensorflow.python.framework.ops.EagerTensor'>
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'tensorflow.python.framework.ops.EagerTensor'>
<class 'tensorflow.python.framework.ops.Tensor'>
type(ref) = <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>
Ограничение самоанализа :
Изучая информацию о типе, я спотыкаюсьпри следующем:
from tensorflow.python.framework.ops import Tensor, EagerTensor
print(Tensor.__doc__) # OK
print(EagerTensor.__doc__) # returns None
from inspect import getsource
print(getsource(EagerTensor)) # OSError: could not find class definition
Рассматривая родительский модуль, по-видимому, нужно заглянуть в C API, чтобы узнать:
EagerTensor = c_api.TFE_Py_InitEagerTensor(_EagerTensorBase)