본문 바로가기

TIL

TIL 051623

import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam, SGD

x_data = np.array([[1], [2], [3]])
y_data = np.array([[10], [20], [30]])

model = Sequential([
  Dense(1)
])

model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.1))

model.fit(x_data, y_data, epochs=100) # epochs 복수형으로 쓰기!

실행 시 에러

WARNING:absl:`lr` is deprecated in Keras optimizer, please use `learning_rate` or use the legacy optimizer, e.g.,tf.keras.optimizers.legacy.SGD.
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-b916cb00e6f5> in <cell line: 15>()
     13 model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.1))
     14 
---> 15 model.fit(x_data, y_data, epochs=100) # epochs 복수형으로 쓰기!


5 frames
/usr/local/lib/python3.10/dist-packages/keras/engine/training_v1.py in _make_train_function(self)
   2282                 with backend.name_scope("training"):
   2283                     # Training updates
-> 2284                     updates = self.optimizer.get_updates(
   2285                         params=self._collected_trainable_weights,
   2286                         loss=self.total_loss,

AttributeError: 'SGD' object has no attribute 'get_updates'

 

- lr을 learning_rate로 바꿔서 해봤다.

AttributeError                            Traceback (most recent call last)
<ipython-input-6-c3fcce5a10ea> in <cell line: 15>()
     13 model.compile(loss='mean_squared_error', optimizer=SGD(learning_rate=0.1))
     14 
---> 15 model.fit(x_data, y_data, epochs=100) # epochs 복수형으로 쓰기!


5 frames
/usr/local/lib/python3.10/dist-packages/keras/engine/training_v1.py in _make_train_function(self)
   2282                 with backend.name_scope("training"):
   2283                     # Training updates
-> 2284                     updates = self.optimizer.get_updates(
   2285                         params=self._collected_trainable_weights,
   2286                         loss=self.total_loss,

AttributeError: 'SGD' object has no attribute 'get_updates'

- get_updates를 검색해봤다.

https://stackoverflow.com/questions/75356826/attributeerror-adam-object-has-no-attribute-get-updates

 

AttributeError: 'Adam' object has no attribute 'get_updates'

I'm training a VAE with TensorFlow Keras backend and I'm using Adam as the optimizer. the code I used is attached below. def compile(self, learning_rate=0.0001): optimizer = tf.keras.optimi...

stackoverflow.com

 

optimizer = tf.keras.optimizers.legacy.Adam(learning_rate=learning_rate)에서 Adam을 SGD로 바꿔서 했더니 됐다.

 

맨 처음 오류 메시지에 please use 'learning_rate' or ...까지만 보고 lr을 learning_rate로 바꿨는데 그 or 뒤쪽에 해결법이 있었다.

or use the legacy optimizer, e.g., tf.keras.optimizers.legacy.SGD.

'TIL' 카테고리의 다른 글

TIL 051823  (4) 2023.05.18
TIL 051723  (0) 2023.05.16
TIL 051523  (0) 2023.05.16
TIL 051223  (0) 2023.05.12
TIL 051123  (0) 2023.05.11