開始使用免費開始

調整機率尺度

在這個練習中,你會比較使用不同 temperature 值來縮放機率分佈時,所產生句子的差異。

函式 generate_phrase() 是你先前建立之函式的調整版,已經載入環境。它會接收參數:model(已訓練好的模型)、initial_text(作為語境的文字),以及用來縮放 softmax()temperature 值。

本練習屬於課程

使用 Keras 建立語言模型的循環神經網路(RNN)

檢視課程

練習說明

  • 將多個溫度值的清單存入變數 temperatures
  • 以變數 temperature 迴圈遍歷 temperatures 清單。
  • 使用已預先載入的函式 generate_phrase() 產生一句話。
  • 列印出 temperature 與產生的句子。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Define the initial text
initial_text = "Spock and me "

# Define a vector with temperature values
____ = [0.2, 0.8, 1.0, 3.0, 10.0]

# Loop over temperatures and generate phrases
for ____ in temperatures:
	# Generate a phrase
	phrase = ____(model, initial_text, temperature)
    
	# Print the phrase
	print('Temperature {0}: {1}'.format(____, ____))
編輯並執行程式碼