1. 程式人生 > >google機器學習框架tensorflow學習筆記(七)

google機器學習框架tensorflow學習筆記(七)

使用Tensorflow的基本步驟

第五步: 訓練模型 現在,我們可以在 linear_regressor 上呼叫 train() 來訓練模型。我們會將 my_input_fn 封裝在 lambda 中,以便可以將 my_feature  target 作為引數傳入,首先,我們會訓練 100 步。 _ = linear_regressor.train(
    input_fn = lambda:my_input_fn(my_feature, targets),
    steps=100
)
第六步: 評估模型

我們基於該訓練資料做一次預測,看看我們的模型在訓練期間與這些資料的擬合情況。

注意:訓練誤差可以衡量您的模型與訓練資料的擬合情況,但並 不能衡量模型 泛化到新資料的效果。在後面的練習中,您將探索如何拆分資料以評估模型的泛化能力。
# Create an input function for predictions.
# Note: Since we're making just one prediction for each example, we don't 
# need to repeat or shuffle the data here.
prediction_input_fn =lambda: my_input_fn(my_feature, targets, num_epochs=1, shuffle=False)

# Call predict() on the linear_regressor to make predictions.
predictions = linear_regressor.predict(input_fn=prediction_input_fn)

# Format predictions as a NumPy array, so we can calculate error metrics.
predictions = np.array([item['predictions'][0] for item in predictions])

# Print Mean Squared Error and Root Mean Squared Error.
mean_squared_error = metrics.mean_squared_error(predictions, targets)
root_mean_squared_error = math.sqrt(mean_squared_error)
print "Mean Squared Error (on training data): %0.3f" % mean_squared_error
print "Root Mean Squared Error (on training data): %0.3f" % root_mean_squared_error

執行以上程式可以得到: Mean Squared Error (on training data): 56367.025
Root Mean Squared Error (on training data): 237.417

這是出色的模型嗎?您如何判斷誤差有多大?

由於均方誤差 (MSE) 很難解讀,因此我們經常檢視的是均方根誤差 (RMSE)。RMSE 的一個很好的特性是,它可以在與原目標相同的規模下解讀。

我們來比較一下 RMSE 與目標最大值和最小值的差值:

min_house_value = california_housing_dataframe["median_house_value"].min()
max_house_value = california_housing_dataframe["median_house_value"].max()
min_max_difference = max_house_value - min_house_value

print "Min. Median House Value: %0.3f" % min_house_value
print "Max. Median House Value: %0.3f" % max_house_value
print "Difference between Min. and Max.: %0.3f" % min_max_difference
print "Root Mean Squared Error: %0.3f" % root_mean_squared_error

執行以上程式可以得到: Min. Median House Value: 14.999
Max. Median House Value: 500.001
Difference between Min. and Max.: 485.002
Root Mean Squared Error: 237.417

我們的誤差跨越目標值的近一半範圍,可以進一步縮小誤差嗎?

這是每個模型開發者都會煩惱的問題。我們來制定一些基本策略,以降低模型誤差。

首先,我們可以瞭解一下根據總體摘要統計資訊,預測和目標的符合情況。

calibration_data = pd.DataFrame()
calibration_data["predictions"] = pd.Series(predictions)
calibration_data["targets"] = pd.Series(targets)
calibration_data.describe()

好的,此資訊也許有幫助。平均值與模型的 RMSE 相比情況如何?各種分位數呢?

我們還可以將資料和學到的線視覺化。我們已經知道,單個特徵的線性迴歸可繪製成一條將輸入 x 對映到輸出 y 的線。

首先,我們將獲得均勻分佈的隨機資料樣本,以便繪製可辨的散點圖。


sample = california_housing_dataframe.sample(n=300) 然後,我們根據模型的偏差項和特徵權重繪製學到的線,並繪製散點圖。該線會以紅色顯示。
# Get the min and max total_rooms values.
x_0 = sample["total_rooms"].min()
x_1 = sample["total_rooms"].max()

# Retrieve the final weight and bias generated during training.
weight = linear_regressor.get_variable_value('linear/linear_model/total_rooms/weights')[0]
bias = linear_regressor.get_variable_value('linear/linear_model/bias_weights')

# Get the predicted median_house_values for the min and max total_rooms values.
y_0 = weight * x_0 + bias 
y_1 = weight * x_1 + bias

# Plot our regression line from (x_0, y_0) to (x_1, y_1).
plt.plot([x_0, x_1], [y_0, y_1], c='r')

# Label the graph axes.
plt.ylabel("median_house_value")
plt.xlabel("total_rooms")

# Plot a scatter plot from our data sample.
plt.scatter(sample["total_rooms"], sample["median_house_value"])

# Display graph.
plt.show()


這條初始線看起來與目標相差很大。看看您能否回想起摘要統計資訊,並看到其中蘊含的相同資訊。

綜上所述,這些初始健全性檢查提示我們也許可以找到更好的線。