日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

First Steps with TensorFlow代码解析

發(fā)布時(shí)間:2023/12/10 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 First Steps with TensorFlow代码解析 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

注:本文的內(nèi)容基本上都摘自tensorflow的官網(wǎng),只不過官網(wǎng)中的這部分內(nèi)容在國(guó)內(nèi)訪問不了,所以我只是當(dāng)做一個(gè)知識(shí)的搬運(yùn)工,同時(shí)梳理了一遍,方便大家查看。本文相關(guān)內(nèi)容地址如下:

https://developers.google.cn/machine-learning/crash-course/ml-intro

?

一、first_steps_with_tensor_flow可運(yùn)行代碼

以下是tensorflow官網(wǎng)中first_steps_with_tensor_flow.py的完整可運(yùn)行代碼,直接復(fù)制編譯就可以出結(jié)果。代碼如下:

import math from IPython import display from matplotlib import cm from matplotlib import gridspec from matplotlib import pyplot as plt import numpy as np import pandas as pd from sklearn import metrics import tensorflow as tf from tensorflow.python.data import Datasetdef my_input_fn(features, targets, batch_size = 1, shuffle=True, num_epochs = None):# Convert pandas data into a dict of np arrays.features = {key:np.array(value) for key,value in dict(features).items()} # Construct a dataset, and configure batching/repeatingds = Dataset.from_tensor_slices((features,targets)) # warning: 2GB limitds = ds.batch(batch_size).repeat(num_epochs)# Shuffle the data, if specifiedif shuffle:ds = ds.shuffle(buffer_size=10000)# Return the next batch of datafeatures, labels = ds.make_one_shot_iterator().get_next()return features, labelsdef train_model(learning_rate, steps, batch_size, input_feature="total_rooms"):periods = 10steps_per_period = steps / periods# 第 1 步:定義特征并配置特征列my_feature = input_featuremy_feature_data = california_housing_dataframe[[my_feature]]# Create feature columnsfeature_columns = [tf.feature_column.numeric_column(my_feature)]# 第 2 步:定義目標(biāo)my_label = "median_house_value"targets = california_housing_dataframe[my_label]# 第 4 步:定義輸入函數(shù)# Create input functionstraining_input_fn = lambda:my_input_fn(my_feature_data, targets, batch_size=batch_size)prediction_input_fn = lambda: my_input_fn(my_feature_data, targets, num_epochs=1, shuffle=False)# 第 3 步:配置 LinearRegressor 線性回歸器# Create a linear regressor object.my_optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0)linear_regressor = tf.estimator.LinearRegressor(feature_columns=feature_columns,optimizer=my_optimizer)# Set up to plot the state of our model's line each period.plt.figure(figsize=(15, 6))plt.subplot(1, 2, 1)plt.title("Learned Line by Period")plt.ylabel(my_label)plt.xlabel(my_feature)sample = california_housing_dataframe.sample(n=300)plt.scatter(sample[my_feature], sample[my_label])colors = [cm.coolwarm(x) for x in np.linspace(-1, 1, periods)]# Train the model, but do so inside a loop so that we can periodically assess# loss metrics.print("Training model...")print("RMSE (on training data):")root_mean_squared_errors = []for period in range (0, periods):# 第 5 步:訓(xùn)練模型# Train the model, starting from the prior state. linear_regressor.train(input_fn=training_input_fn,steps=steps_per_period)# 第 6 步:評(píng)估模型# Take a break and compute predictions.predictions = linear_regressor.predict(input_fn=prediction_input_fn)predictions = np.array([item['predictions'][0] for item in predictions])# Compute loss.root_mean_squared_error = math.sqrt(metrics.mean_squared_error(predictions, targets))# Occasionally print the current loss.print(" period %02d : %0.2f" % (period, root_mean_squared_error))# Add the loss metrics from this period to our list. root_mean_squared_errors.append(root_mean_squared_error)# Finally, track the weights and biases over time.# Apply some math to ensure that the data and line are plotted neatly.y_extents = np.array([0, sample[my_label].max()])weight = linear_regressor.get_variable_value('linear/linear_model/%s/weights' % input_feature)[0]bias = linear_regressor.get_variable_value('linear/linear_model/bias_weights')x_extents = (y_extents - bias) / weightx_extents = np.maximum(np.minimum(x_extents,sample[my_feature].max()),sample[my_feature].min())y_extents = weight * x_extents + biasplt.plot(x_extents, y_extents, color = colors[period])print("Model training finished.")# Output a graph of loss metrics over periods.plt.subplot(1, 2, 2)plt.ylabel('RMSE')plt.xlabel('Periods')plt.title("Root Mean Squared Error vs. Periods")plt.tight_layout()plt.plot(root_mean_squared_errors)plt.show()# Output a table with calibration data.calibration_data = pd.DataFrame()calibration_data["predictions"] = pd.Series(predictions)calibration_data["targets"] = pd.Series(targets)display.display(calibration_data.describe())print("Final RMSE (on training data): %0.2f" % root_mean_squared_error)tf.logging.set_verbosity(tf.logging.ERROR) pd.options.display.max_rows = 10 pd.options.display.float_format = '{:.1f}'.formatcalifornia_housing_dataframe = pd.read_csv("E://woodsfile//machine learning//Tensorflow實(shí)戰(zhàn)代碼//tensorflow使用步驟//california_housing_train.csv",sep=",")## 對(duì)數(shù)據(jù)進(jìn)行隨機(jī)化處理 california_housing_dataframe = california_housing_dataframe.reindex(np.random.permutation(california_housing_dataframe.index))california_housing_dataframe["median_house_value"] /= 1000.0# 輸出關(guān)于各列的一些實(shí)用統(tǒng)計(jì)信息快速摘要:樣本數(shù)、均值、標(biāo)準(zhǔn)偏差、最大值、最小值和各種分位數(shù) california_housing_dataframe.describe()train_model(learning_rate=0.00002,steps=500,batch_size=5 )

運(yùn)行結(jié)果如下:

?

此程序?qū)崿F(xiàn)輸入房間總數(shù)(total_rooms),預(yù)測(cè)得到房子的價(jià)格(median_house_vaule)。

左邊f(xié)igure,每訓(xùn)練一次,就畫一條預(yù)測(cè)函直線,共預(yù)測(cè)了十次。右邊f(xié)igure表示了每次預(yù)測(cè)的RMSE的值。

?

上述代碼中關(guān)于california房屋統(tǒng)計(jì)情況的數(shù)據(jù)集請(qǐng)?jiān)谖业陌俣染W(wǎng)盤上下載,地址如下:

https://pan.baidu.com/s/1A-EbKXRIP7q_5uraRmbXxw

(注:下面一小節(jié)的代碼是第一部分的詳細(xì)步驟,代碼不完全一致,基本步驟是一樣的。)

?

二、使用tensorflow的基本步驟

使用tensorflow有以下幾個(gè)基本步驟:

  • 定義特征并配置特征列
  • 定義目標(biāo)(label)
  • 配置線性回歸模型
  • 定義輸入函數(shù)
  • 訓(xùn)練模型
  • 評(píng)估模型
  • 1、定義特征并配置特征列

    本例中只采用了數(shù)據(jù)列表中“total_rooms”這一個(gè)特征,并將這些數(shù)據(jù)轉(zhuǎn)化為數(shù)值。

    以下代碼從california_housing_dataframe提取total_rooms數(shù)據(jù),并使用numeric_column將數(shù)據(jù)指定為數(shù)值。

    1 # Define the input feature: total_rooms. 2 # 只提取一個(gè)特征值:total_rooms 3 my_feature = california_housing_dataframe[["total_rooms"]] 4 5 # 將total_rooms列中的數(shù)據(jù)轉(zhuǎn)化為數(shù)值,為一維數(shù)組 6 feature_columns = [tf.feature_column.numeric_column("total_rooms")]

    ?

    2、定義目標(biāo)(label)

    接下來定義目標(biāo)即median_house_value,同樣從california_housing_dataframe提取。

    1 # Define the label. 2 targets = california_housing_dataframe["median_house_value"]

    ?

    3、配置線性回歸模型

    接下來我們使用線性回歸模型(LinearRegressor),并使用隨機(jī)梯度下降法(SGD)訓(xùn)練該模型。learning_rate參數(shù)用來控制梯度補(bǔ)償大小。為了安全起見,程序中通過clip_gradients_by_norm將梯度裁剪應(yīng)用到我們的優(yōu)化器。梯度裁剪可確保梯度大小在訓(xùn)練期間不會(huì)變得過大,梯度過大將會(huì)導(dǎo)致梯度下降算法失敗。

    # Use gradient descent as the optimizer for training the model. my_optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.0000001)#這一步使用了梯度裁剪,保證梯度大小在訓(xùn)練期間不會(huì)變得過大,梯度過大導(dǎo)致梯度下降法失敗 my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0) # Configure the linear regression model with our feature columns and optimizer. # Set a learning rate of 0.0000001 for Gradient Descent. linear_regressor = tf.estimator.LinearRegressor(feature_columns = feature_columns,optimizer = my_optimizer )

    ?

    4、定義輸入函數(shù)

    要將california住房數(shù)據(jù)導(dǎo)入LinearRegressor,我們需要定義一個(gè)輸入函數(shù)。這個(gè)函數(shù)的作用是告訴TensorFlow如何對(duì)數(shù)據(jù)進(jìn)行預(yù)處理,以及在模型訓(xùn)練期間如何批處理、隨機(jī)處理和重復(fù)數(shù)據(jù)。

    程序中,首先將Pandas特征數(shù)據(jù)轉(zhuǎn)換成Numpy數(shù)組字典。然后,我們可以使用TensorFlow Dataset API根據(jù)我們的數(shù)據(jù)構(gòu)建Dataset對(duì)象,并將數(shù)據(jù)拆分成大小為batch_size的多批數(shù)據(jù),以按照指定周期數(shù)(num_epochs)進(jìn)行重復(fù)。最后,輸入函數(shù)會(huì)為該數(shù)據(jù)集構(gòu)建一個(gè)迭代器,并向LinearRegressor返回下一批數(shù)據(jù)。

    注意:

    • 如果將默認(rèn)值num_epochs=None傳遞到repeat(),輸入數(shù)據(jù)會(huì)無限期循環(huán)。
    • 如果shuffle這只為true,則我們會(huì)對(duì)數(shù)據(jù)進(jìn)行隨機(jī)處理,以便數(shù)據(jù)在訓(xùn)練期間以隨機(jī)方式傳遞到模型。buffer_size參數(shù)會(huì)指定shuffle隨機(jī)抽樣的數(shù)據(jù)集大小。
    def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):
    # Convert pandas data into a dict of np arrays.features = {key:np.array(value) for key,value in dict(features).items()} # Construct a dataset, and configure batching/repeatingds = Dataset.from_tensor_slices((features,targets)) # warning: 2GB limitds = ds.batch(batch_size).repeat(num_epochs)# Shuffle the data, if specifiedif shuffle:ds = ds.shuffle(buffer_size=10000)# Return the next batch of datafeatures, labels = ds.make_one_shot_iterator().get_next()return features, labels

    ?

    5、訓(xùn)練模型

    接下來在linear_regressor上調(diào)用train()來訓(xùn)練模型。我們會(huì)將my_inout_fn封裝在lambda中,以便可以將my_feature和target作為參數(shù)傳入。

    # 訓(xùn)練steps = 100步 _ = linear_regressor.train(input_fn = lambda:my_input_fn(my_feature, targets),steps=100)

    ?

    6、評(píng)估模型

    我們基于該訓(xùn)練數(shù)據(jù)做一側(cè)預(yù)測(cè),看看我們的模型在訓(xùn)練期間與這些數(shù)據(jù)的擬合情況。(對(duì)代碼有疑問的話,代碼上方的注釋都給出了很明確的說明。注釋都很小并且都是英文,往往容易被我們忽視,別這樣做。)

    # 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)

    由于均方誤差(MSE)很難解讀,我們經(jīng)常查看的是均方根誤差(RMSE)。RMSE是一個(gè)很好的特性可以在與原目標(biāo)相同的規(guī)模下解讀。

    那上面的預(yù)測(cè)效果到底如何呢?我們可以通過查看總體摘要的統(tǒng)計(jì)信息進(jìn)行查看,代碼如下:

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

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/woods-bagnzhu/p/9642124.html

    總結(jié)

    以上是生活随笔為你收集整理的First Steps with TensorFlow代码解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。