본문 바로가기
Artificial Intelligence/Keras

[Tensorflow 2][Keras] Week 2 - Programming Assignment: Creating a custom loss function

by 개발자J의일상 2021. 3. 25.
반응형

본 포스팅은 다음 과정을 정리 한 글입니다.

 

Custom Models, Layers, and Loss Functions with TensorFlow

www.coursera.org/specializations/tensorflow-advanced-techniques

 

지난 시간 리뷰

2021.03.21 - [Artificial Intelligence/Keras] - [Tensorflow 2][Keras] Week 2 - Contrastive Loss

 

[Tensorflow 2][Keras] Week 2 - Contrastive Loss

본 포스팅은 다음 과정을 정리 한 글입니다. Custom Models, Layers, and Loss Functions with TensorFlow www.coursera.org/specializations/tensorflow-advanced-techniques 지난 시간 리뷰 2021.03.20 - [Artif..

mypark.tistory.com

# Please uncomment all lines in this cell and replace those marked with `# YOUR CODE HERE`.
# You can select all lines in this code cell with Ctrl+A (Windows/Linux) or Cmd+A (Mac), then press Ctrl+/ (Windows/Linux) or Cmd+/ (Mac) to uncomment.



def my_rmse(y_true, y_pred):
    error = y_true - y_pred;
    sqr_error = K.square(error)
    mean_sqr_error = K.mean(sqr_error)
    sqrt_mean_sqr_error = K.sqrt(mean_sqr_error)
    return sqrt_mean_sqr_error

# Please uncomment all lines in this cell and replace those marked with `# YOUR CODE HERE`.
# You can select all lines in this code cell with Ctrl+A (Windows/Linux) or Cmd+A (Mac), then press Ctrl+/ (Windows/Linux) or Cmd+/ (Mac) to uncomment.



# define the model architecture
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])

# use the function you just coded as the loss
model.compile(optimizer='sgd', loss=my_rmse)
              
# train the model 
model.fit(xs, ys, epochs=500,verbose=0)
              
# test with a sample input
print(model.predict([10.0]))
300x250

댓글