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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tensorflow入门_TensorFlow法律和统计入门

發(fā)布時間:2023/11/29 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tensorflow入门_TensorFlow法律和统计入门 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

tensorflow入門

by Daniel Deutsch

由Daniel Deutsch

TensorFlow法律和統(tǒng)計入門 (Get started with TensorFlow on law and statistics)

  • What this is about

    這是關(guān)于什么的

  • What we will use

    我們將使用什么

  • Get started

    開始吧

  • Shell commands for installing everything you need

    用于安裝所需內(nèi)容的Shell命令

  • Get data and draw a plot

    獲取數(shù)據(jù)并繪制圖

  • Import everything you need

    導入您需要的一切

  • Create and plot some numbers

    創(chuàng)建并繪制一些數(shù)字

  • Build a TensorFlow model

    建立一個TensorFlow模型

  • Prepare data

    準備數(shù)據(jù)

  • Set up variables and operations for TensorFlow

    為TensorFlow設(shè)置變量和操作

  • Start the calculations with a TensorFlow session

    從TensorFlow會話開始計算

  • Visualize the result and process

    可視化結(jié)果和過程

這是關(guān)于什么的 (What this is about)

As I am exploring TensorFlow, I wanted build a beginner example and document it. This is a very basic example that uses a gradient descent optimization to train parameters with TensorFlow. The key variables are evidence and convictions. It will illustrate:

在探索TensorFlow時,我想構(gòu)建一個初學者示例并記錄下來。 這是一個非常基本的示例,該示例使用梯度下降優(yōu)化來使用TensorFlow訓練參數(shù)。 關(guān)鍵變量是證據(jù)信念 。 它將說明:

  • how the number of convictions depend upon the number of pieces of evidence

    定罪的數(shù)量如何取決于證據(jù)的數(shù)量
  • how to predict the number of convictions using a regression model

    如何使用回歸模型預測定罪人數(shù)

The Python file is in my repository on GitHub.

Python文件位于我在GitHub上的存儲庫中 。

See the article in better formatting on GitHub.

在GitHub上以更好的格式查看文章。

我們將使用什么 (What we will use)

1. TensorFlow(as tf) (1. TensorFlow (as tf))

Tensors

張量

  • tf.placeholders

    tf.placeholders
  • tf.Variables

    tf。變量

Helper function

輔助功能

  • tf.global_variables_initializer

    tf.global_variables_initializer

Math Operations

數(shù)學運算

  • tf.add

    tf.add
  • tf.multiply

    tf.multiply
  • tf.reduce_sum

    tf.reduce_sum
  • tf.pow

    tf.pow

Building a graph

建立圖

  • tf.train.GradientDescentOptimizer

    tf.train.GradientDescentOptimizer

Session

屆會

  • tf.Session

    會話

2.脾氣暴躁(as np) (2. Numpy (as np))

  • np.random.seed

    np.random.seed
  • np.random.zeros

    np.random.zeros
  • np.random.randint

    np.random.randint
  • np.random.randn

    np.random.randn
  • np.random.asanyarray

    np.random.asanyarray

3. Matplotlib (3. Matplotlib)

4.數(shù)學 (4. Math)

入門 (Getting started)

Install TensorFlow with virtualenv. See the guide on the TF website.

使用virtualenv安裝TensorFlow。 請參閱TF網(wǎng)站上的指南 。

用于安裝所需內(nèi)容的Shell命令 (Shell commands for installing everything you need)

sudo easy_install pippip3 install --upgrade virtualenvvirtualenv --system-site-packages <targetDirectory>cd <targetDirectory>source ./bin/activateeasy_install -U pip3pip3 install tensorflowpip3 install matplotlib

獲取數(shù)據(jù)并繪制圖 (Get data and draw a plot)

導入您需要的一切 (Import everything you need)

import tensorflow as tfimport numpy as npimport mathimport matplotlibmatplotlib.use('TkAgg')import matplotlib.pyplot as pltimport matplotlib.animation as animation

As you can see I am using the “TkAgg” backend from matplotlib. This allows me to debug with my vsCode and macOS setup without any further complicated installments.

如您所見,我正在使用matplotlib中的“ TkAgg”后端。 這使我可以使用vsCode和macOS設(shè)置進行調(diào)試,而無需進行任何其他復雜的安裝。

創(chuàng)建并繪制一些數(shù)字 (Create and plot some numbers)

# Generate evidence numbers between 10 and 20# Generate a number of convictions from the evidence with a random noise addednp.random.seed(42)sampleSize = 200numEvid = np.random.randint(low=10, high=50, size=sampleSize)numConvict = numEvid * 10 + np.random.randint(low=200, high=400, size=sampleSize)# Plot the data to get a feelingplt.title("Number of convictions based on evidence")plt.plot(numEvid, numConvict, "bx")plt.xlabel("Number of Evidence")plt.ylabel("Number of Convictions")plt.show(block=False) # Use the keyword 'block' to override the blocking behavior

I am creating random values for the evidence. The number of convictions depends on the amount (number) of evidence, with random noise. Of course those numbers are made up, but they are just used to prove a point.

我正在為證據(jù)創(chuàng)建隨機值。 定罪的數(shù)量取決于證據(jù)的數(shù)量(數(shù)量)以及隨機噪聲。 當然,這些數(shù)字是虛構(gòu)的,但是它們只是用來證明這一點。

建立一個TensorFlow模型 (Build a TensorFlow model)

To build a basic machine learning model, we need to prepare the data. Then we make predictions, measure the loss, and optimize by minimizing the loss.

要構(gòu)建基本的機器學習模型,我們需要準備數(shù)據(jù)。 然后我們進行預測,測量損失,并通過最小化損失進行優(yōu)化。

準備數(shù)據(jù) (Prepare data)

# create a function for normalizing values# use 70% of the data for training (the remaining 30% shall be used for testing)def normalize(array): return (array - array.mean()) / array.std()numTrain = math.floor(sampleSize * 0.7)# convert list to an array and normalize arraystrainEvid = np.asanyarray(numEvid[:numTrain])trainConvict = np.asanyarray(numConvict[:numTrain])trainEvidNorm = normalize(trainEvid)trainConvictdNorm = normalize(trainConvict)testEvid = np.asanyarray(numEvid[numTrain:])testConvict = np.asanyarray(numConvict[numTrain:])testEvidNorm = normalize(testEvid)testConvictdNorm = normalize(testConvict)

We are splitting the data into training and testing portions. Afterwards, we normalize the values, as this is necessary for machine learning projects. (See also “feature scaling”.)

我們將數(shù)據(jù)分為訓練和測試部分。 之后,我們將值標準化,因為這對于機器學習項目是必需的。 (另請參閱“ 功能縮放 ”。)

為TensorFlow設(shè)置變量和操作 (Set up variables and operations for TensorFlow)

# define placeholders and variablestfEvid = tf.placeholder(tf.float32, name="Evid")tfConvict = tf.placeholder(tf.float32, name="Convict")tfEvidFactor = tf.Variable(np.random.randn(), name="EvidFactor")tfConvictOffset = tf.Variable(np.random.randn(), name="ConvictOffset")# define the operation for predicting the conviction based on evidence by adding both values# define a loss function (mean squared error)tfPredict = tf.add(tf.multiply(tfEvidFactor, tfEvid), tfConvictOffset)tfCost = tf.reduce_sum(tf.pow(tfPredict - tfConvict, 2)) / (2 * numTrain)# set a learning rate and a gradient descent optimizerlearningRate = 0.1gradDesc = tf.train.GradientDescentOptimizer(learningRate).minimize(tfCost)

The pragmatic differences between tf.placeholder and tf.Variable are:

tf.placeholder和tf.Variable之間的實用差異是:

  • placeholders are allocated storage for data, and initial values are not required

    占位符被分配用于數(shù)據(jù)存儲,并且不需要初始值
  • variables are used for parameters to learn, and initial values are required. The values can be derived from training.

    變量用于學習參數(shù),并且需要初始值。 這些值可以從訓練中得出。

I use the TensorFlow operators precisely as tf.add(…), because it is pretty clear what library is used for the calculation. This is instead of using the + operator.

我將TensorFlow運算符精確地用作tf.add(…) ,因為很清楚使用哪個庫進行計算。 這不是使用+運算符。

從TensorFlow會話開始計算 (Start the calculations with a TensorFlow session)

# initialize variablesinit = tf.global_variables_initializer()with tf.Session() as sess: sess.run(init)# set up iteration parameters displayEvery = 2 numTrainingSteps = 50# Calculate the number of lines to animation # define variables for updating during animation numPlotsAnim = math.floor(numTrainingSteps / displayEvery) evidFactorAnim = np.zeros(numPlotsAnim) convictOffsetAnim = np.zeros(numPlotsAnim) plotIndex = 0# iterate through the training data for i in range(numTrainingSteps):# ======== Start training by running the session and feeding the gradDesc for (x, y) in zip(trainEvidNorm, trainConvictdNorm): sess.run(gradDesc, feed_dict={tfEvid: x, tfConvict: y})# Print status of learning if (i + 1) % displayEvery == 0: cost = sess.run( tfCost, feed_dict={tfEvid: trainEvidNorm, tfConvict: trainConvictdNorm} ) print( "iteration #:", "%04d" % (i + 1), "cost=", "{:.9f}".format(cost), "evidFactor=", sess.run(tfEvidFactor), "convictOffset=", sess.run(tfConvictOffset), )# store the result of each step in the animation variables evidFactorAnim[plotIndex] = sess.run(tfEvidFactor) convictOffsetAnim[plotIndex] = sess.run(tfConvictOffset) plotIndex += 1# log the optimized result print("Optimized!") trainingCost = sess.run( tfCost, feed_dict={tfEvid: trainEvidNorm, tfConvict: trainConvictdNorm} ) print( "Trained cost=", trainingCost, "evidFactor=", sess.run(tfEvidFactor), "convictOffset=", sess.run(tfConvictOffset), "\n", )

Now we come to the actual training and the most interesting part.

現(xiàn)在我們來進行實際的培訓和最有趣的部分。

The graph is now executed in a tf.Session. I am using "feeding" as it lets you inject data into any Tensor in a computation graph. You can see more on reading data here.

該圖現(xiàn)在在tf.Session執(zhí)行。 我正在使用“饋送”,因為它可以讓您將數(shù)據(jù)注入計算圖中的任何張量。 您可以在此處查看有關(guān)讀取數(shù)據(jù)的更多信息。

tf.Session() is used to create a session that is automatically closed on exiting the context. The session also closes when an uncaught exception is raised.

tf.Session()用于創(chuàng)建一個會話,該會話在退出上下文時自動關(guān)閉。 當引發(fā)未捕獲的異常時,會話也會關(guān)閉。

The tf.Session.run method is the main mechanism for running a tf.Operation or evaluating a tf.Tensor. You can pass one or more tf.Operation or tf.Tensor objects to tf.Session.run, and TensorFlow will execute the operations that are needed to compute the result.

tf.Session.run方法是運行tf.Operation或評估tf.Tensor的主要機制。 您可以將一個或多個tf.Operation或tf.Tensor對象傳遞給tf.Session.run ,TensorFlow將執(zhí)行計算結(jié)果所需的操作。

First, we are running the gradient descent training while feeding it the normalized training data. After that, we are calculating the the loss.

首先,我們在進行梯度下降訓練的同時將其歸一化訓練數(shù)據(jù)。 之后,我們正在計算損失。

We are repeating this process until the improvements per step are very small. Keep in mind that the tf.Variables (the parameters) have been adapted throughout and now reflect an optimum.

我們將重復此過程,直到每個步驟的改進很小為止。 請記住,已經(jīng)對tf.Variables (參數(shù))進行了全面調(diào)整,現(xiàn)在反映了最優(yōu)值。

可視化結(jié)果和過程 (Visualize the result and process)

# de-normalize variables to be plotable again trainEvidMean = trainEvid.mean() trainEvidStd = trainEvid.std() trainConvictMean = trainConvict.mean() trainConvictStd = trainConvict.std() xNorm = trainEvidNorm * trainEvidStd + trainEvidMean yNorm = ( sess.run(tfEvidFactor) * trainEvidNorm + sess.run(tfConvictOffset) ) * trainConvictStd + trainConvictMean# Plot the result graph plt.figure()plt.xlabel("Number of Evidence") plt.ylabel("Number of Convictions")plt.plot(trainEvid, trainConvict, "go", label="Training data") plt.plot(testEvid, testConvict, "mo", label="Testing data") plt.plot(xNorm, yNorm, label="Learned Regression") plt.legend(loc="upper left")plt.show()# Plot an animated graph that shows the process of optimization fig, ax = plt.subplots() line, = ax.plot(numEvid, numConvict)plt.rcParams["figure.figsize"] = (10, 8) # adding fixed size parameters to keep animation in scale plt.title("Gradient Descent Fitting Regression Line") plt.xlabel("Number of Evidence") plt.ylabel("Number of Convictions") plt.plot(trainEvid, trainConvict, "go", label="Training data") plt.plot(testEvid, testConvict, "mo", label="Testing data")# define an animation function that changes the ydata def animate(i): line.set_xdata(xNorm) line.set_ydata( (evidFactorAnim[i] * trainEvidNorm + convictOffsetAnim[i]) * trainConvictStd + trainConvictMean ) return (line,)# Initialize the animation with zeros for y def initAnim(): line.set_ydata(np.zeros(shape=numConvict.shape[0])) return (line,)# call the animation ani = animation.FuncAnimation( fig, animate, frames=np.arange(0, plotIndex), init_func=initAnim, interval=200, blit=True, )plt.show()

To visualize the process, it is helpful to plot the result and maybe even the optimization process.

為了使過程可視化,對結(jié)果甚至優(yōu)化過程進行繪圖很有幫助。

Check out this Pluralsight course which helped me a lot to get started. :)

查看此Pluralsight課程 ,該課程對我有很多幫助。 :)

Thanks for reading my article! Feel free to leave any feedback!

感謝您閱讀我的文章! 隨時留下任何反饋!

Daniel is a LL.M. student in business law, working as a software engineer and organizer of tech related events in Vienna. His current personal learning efforts focus on machine learning.

丹尼爾(Daniel)是法學碩士。 商業(yè)法專業(yè)的學生,??在維也納擔任軟件工程師和技術(shù)相關(guān)活動的組織者。 他目前的個人學習重點是機器學習。

Connect on:

連接:

  • LinkedIn

    領(lǐng)英

  • Github

    Github

  • Medium

  • Twitter

    推特

  • Steemit

    Steemit

  • Hashnode

    哈希節(jié)點

翻譯自: https://www.freecodecamp.org/news/tensorflow-starter-on-law-and-statistics-646072b93b5a/

tensorflow入門

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的tensorflow入门_TensorFlow法律和统计入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 在线视频区 | 欧美精品久久久久久久久老牛影院 | 国产色影院 | 亚洲欧洲在线视频 | 日本少妇性生活 | 成人免费毛片男人用品 | 91精品91久久久中77777 | 26uuu精品一区二区在线观看 | 特级淫片aaaaaaa级附近的 | 日日夜夜草 | 激情网站在线观看 | tube极品少妇videos | 欧美日本韩国一区二区 | 一级少妇精品久久久久久久 | 免费在线黄 | 国产免费一区二区视频 | 色七七桃花综合影院 | 亚洲精品影院 | 久久久精品网 | 国产一区二区三区视频免费在线观看 | 小珊的性放荡羞辱日记 | 日韩乱淫| 青青精品| 亚洲成人免费av | 成人永久免费视频 | 麻豆tube| 午夜激情福利在线 | 8x8ⅹ国产精品一区二区二区 | 亚洲精品无码不卡在线播he | 成人在线视频一区 | 一本无码aⅴ久久久国产 | 高清av在线| 国产麻豆91| 国产精品视频自拍 | 天天舔天天射 | 色一情一乱一伦 | 激情视频亚洲 | 日韩精品视频免费 | 亚洲精品人妻av | 外国黄色网 | 精品人妻一区二区三区日产 | 久操国产 | 国产精品无码毛片 | 17c精品麻豆一区二区免费 | av一二三 | 亚洲a成人| 公侵犯人妻一区二区三区 | 国产午夜三级一区二区三 | 激情久久婷婷 | 自拍亚洲欧美 | 国产做a视频 | 69天堂| 国产精品国产 | www.五月激情 | 狠狠操操| 69精品人人| 日韩精品少妇 | 成人特级毛片69免费观看 | 国产另类综合 | 中文字幕高清在线观看 | 欧美日韩激情在线 | 色呦呦网站 | 国产精品夜夜爽张柏芝 | 97香蕉久久夜色精品国产 | 久久激情视频 | 国产老熟女伦老熟妇露脸 | 一级片中文 | 日本精品三级 | 国产欧美一区二区三区视频在线观看 | 在线天堂www在线国语对白 | 一区二区三区精品国产 | 日韩欧美大片在线观看 | 欧美老熟妇喷水 | 97av在线| 50部乳奶水在线播放 | 中文字幕91视频 | 久久久三级视频 | 久久77 | 69亚洲精品久久久蜜桃小说 | 8x8x国产精品一区二区 | 伊人网站在线观看 | 久久999| 开心黄色网 | 国产亚洲第一页 | 香蕉私人影院 | 亚洲图片欧美在线 | 国内自拍第一页 | 中文幕无线码中文字夫妻 | 国产资源第一页 | a毛片视频| 亚洲天堂一区在线 | 欧美激情日韩 | 性色av无码久久一区二区三区 | 美国黄色一级大片 | 五月天看片| 91精品久久久久久久 | 精品偷拍网 | 午夜亚洲| 成年人看的黄色片 |