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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

argmax函数_1.4 TensorFlow2.1常用函数

發布時間:2023/12/1 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 argmax函数_1.4 TensorFlow2.1常用函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.4 TF常用函數

tf.cast(tensor,dtype=datatype)可以進行強制類型轉換。

tf.reduce_min(tensor)和tf.reduce_max(tensor)將計算出張量中所有元素的最大值和最小值。

import?tensorflow?as?tfx1 = tf.constant([1., 2., 3.], dtype=tf.float64)print("x1:", x1)x2 = tf.cast(x1, tf.int32)print("x2", x2)print("minimum of x2:", tf.reduce_min(x2))print("maxmum of x2:", tf.reduce_max(x2))

axis代表軸。以矩陣為例axis=0表示列而axis=1 表示行。tf.reduce_mean(tensor, axis=operating axis)被用來計算在axis方向上的隨機數,但如果采用默認的axis,它將計算所有元素的均值。tf.reduce_sum(tensor, axis)計算合也是同理

import?tensorflow?as?tfx = tf.constant([[1, 2, 3], [2, 2, 3]])print("x:", x)print("mean of x:", tf.reduce_mean(x))print("sum of x:", tf.reduce_sum(x, axis=1))

tf.Variable()可以將張量標記為可訓練的。在回傳環節被標記的張量可以記錄梯度的信息。

import?tensorflow?as?tfa = tf.ones([1, 3])b = tf.fill([1, 3], 3.)print("a:", a)print("b:", b)print("a+b:", tf.add(a, b))print("a-b:", tf.subtract(a, b))print("a*b:", tf.multiply(a, b))print("b/a:",?tf.divide(b,?a))

四則運算: tf.add, tf.subtract, tf.multiply, tf.divede(tensor1,tensor2),張量的維度必須相等

import?tensorflow?as?tfa = tf.ones([1, 3])b = tf.fill([1, 3], 3.)print("a:", a)print("b:", b)print("a+b:", tf.add(a, b))print("a-b:", tf.subtract(a, b))print("a*b:", tf.multiply(a, b))print("b/a:",?tf.divide(b,?a))

乘方運算: tf.square(tensor), tf.pow(tensor,n), tf.sqrt(tensor)

import?tensorflow?as?tfa = tf.fill([1, 2], 3.)print("a:", a)print("a的立方:", tf.pow(a, 3))print("a的平方:", tf.square(a))print("a的開方:", tf.sqrt(a))

矩陣乘法: tf.matmul(tensor1,tensor2)需要符合乘法規則

import?tensorflow?as?tfa = tf.ones([3, 2])b = tf.fill([2, 3], 3.)print("a:", a)print("b:", b)print("a*b:",?tf.matmul(a,?b))

Tensorflow提供給我們一個函數把特征和標簽配對。把第一位張量的第一維分隔開。常用以下語句使用

dataset. data =tf.data.Dataset.from_tensor_slices((tensor1,tensor2))

import tensorflow as tffeatures = tf.constant([12, 23, 10, 17])labels = tf.constant([0, 1, 1, 0])dataset = tf.data.Dataset.from_tensor_slices((features, labels))for element in dataset: print(element)

我們可以在with函數中使用tf.GradientTape實現對函數某個參數的求導運算

with tf.GradientTape() as tape:若干計算過程grad=tape.gradient(函數,對誰求導)import tensorflow as tfwith tf.GradientTape() as tape: x = tf.Variable(tf.constant(3.0)) y = tf.pow(x, 2)grad = tape.gradient(y, x)print(grad)

python內置函數enumerate可以枚舉每一個元素并在元素前配上對應的索引號

seq = ['one', 'two', 'three']for i, element in enumerate(seq):????print(i,?element)

在實現分類問題時常用獨熱碼表示標簽比如標簽為1,獨熱碼表示為(0,1,0),tensorflow中提供了函數tf.one_hot(帶轉換的數據,depth=幾分類)轉換為獨熱碼形式。

import tensorflow as tfclasses = 3labels = tf.constant([1, 0, 0, 2, 1]) output = tf.one_hot(labels, depth=classes)print("result of labels1:", output)print("")

上圖中我們得到的結果就是每種鳶尾花的可能性大小,但是概率不可能大于1也不可能小于0,所有上式計算出的并不是概率,所以我們使用softmax函數把輸入的數據映射為0~1之間的實數并且歸一化保證和為1。max理解為取最大值,是二元對立非黑即白的,而soft則是緩和了max的對立,為依照概率取值。

e^y0=2.75,p0=0.256

e^y1=7.46,p1=0.695

e^y2=0.52,p2=0.048

import tensorflow as tfy = tf.constant([1.01, 2.01, -0.66])y_pro = tf.nn.softmax(y)print("After softmax, y_pro is:", y_pro)print("The sum of y_pro:", tf.reduce_sum(y_pro))]

assign_sub函數可以自減參數的值并且沒有返回值,在調用assign_sub前需要先定義變量為可訓練的。

import?tensorflow?as?tfx = tf.Variable(4)x.assign_sub(1)print("x:",?x)

tf.argmax(張量名,axis=操作軸)返回沿指定維度最大值的索引。

import numpy as npimport tensorflow as tftest = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]])test = tf.convert_to_tensor(test)print("test:", test)print("每一列的最大值的索引:", tf.argmax(test, axis=0))print("每一行的最大值的索引", tf.argmax(test, axis=1))

微信搜索:做夢當院士的李子哥

總結

以上是生活随笔為你收集整理的argmax函数_1.4 TensorFlow2.1常用函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。