เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

ปัญหา Exploding Gradient

ในวิดีโอ คุณได้เรียนรู้เกี่ยวกับปัญหาสองประการที่อาจเกิดขึ้นเมื่อทำงานกับโมเดล RNN ได้แก่ ปัญหา Vanishing Gradient และ Exploding Gradient

แบบฝึกหัดนี้จะสำรวจปัญหา Exploding Gradient โดยแสดงให้เห็นว่าอนุพันธ์ของฟังก์ชันสามารถเพิ่มขึ้นแบบเอกซ์โพเนนเชียลได้อย่างไร และวิธีแก้ปัญหาด้วยเทคนิคที่เรียบง่าย

ข้อมูลถูกโหลดไว้ในสภาพแวดล้อมแล้วในชื่อ X_train, X_test, y_train และ y_test

จะใช้ optimizer แบบ Stochastic Gradient Descent (SGD) และ Mean Squared Error (MSE) เป็น loss function

ในขั้นตอนแรก จะสังเกตการณ์ Gradient Explosion โดยคำนวณ MSE บนชุดข้อมูล train และ test ในขั้นตอนที่ 2 จะเปลี่ยน optimizer โดยใช้พารามิเตอร์ clipvalue เพื่อแก้ปัญหา

Stochastic Gradient Descent ใน Keras ถูกโหลดไว้แล้วในชื่อ SGD

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Recurrent Neural Networks (RNNs) สำหรับ Language Modeling ด้วย Keras

ดูคอร์ส

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Create a Keras model with one hidden Dense layer
model = Sequential()
model.add(Dense(25, input_dim=20, activation='relu', kernel_initializer=he_uniform(seed=42)))
model.add(Dense(1, activation='linear'))

# Compile and fit the model
model.compile(loss='mean_squared_error', optimizer=____(learning_rate=0.01, momentum=0.9))
history = model.fit(X_train, y_train, validation_data=(____, ____), epochs=100, verbose=0)

# See Mean Square Error for train and test data
train_mse = model.____(X_train, y_train, verbose=0)
test_mse = model.evaluate(X_test, y_test, verbose=0)

# Print the values of MSE
print('Train: %.3f, Test: %.3f' % (____, ____))
แก้ไขและรันโค้ด