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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

tensorflow随笔-求平均值的函数

發(fā)布時間:2025/3/12 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tensorflow随笔-求平均值的函数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

求平均值的函數(shù)
reduce_mean
axis為1表示求行

axis為0表示求列

>>> xxx=tf.constant([[1., 10.],[3.,30.]]) >>> sess.run(xxx) array([[ 1., 10.],[ 3., 30.]], dtype=float32) >>> mymean=tf.reduce_mean(xxx,0) >>> sess.run(mymean) array([ 2., 20.], dtype=float32) >>> mymean=tf.reduce_mean(xxx,1) >>> sess.run(mymean) array([ 5.5, 16.5], dtype=float32) >>>

keep_dims表示是否保持維度。

>>> mymean=tf.reduce_mean(xxx,axis=0,keep_dims=True) >>> sess.run(mymean) array([[ 2., 20.]], dtype=float32) >>> mymean=tf.reduce_mean(xxx,axis=0,keep_dims=False) >>> sess.run(mymean) array([ 2., 20.], dtype=float32) >>> mymean=tf.reduce_mean(xxx,keep_dims=False) >>> sess.run(mymean) 11.0 >>> mymean=tf.reduce_mean(xxx,keep_dims=True) >>> sess.run(mymean) array([[ 11.]], dtype=float32) >>> mymean=tf.reduce_mean(xxx) >>> sess.run(mymean) 11.0

tf.reduce_mean

reduce_mean(
input_tensor,
axis=None,
keep_dims=False,
name=None,
reduction_indices=None
)

Defined in tensorflow/python/ops/math_ops.py.

See the guide: Math > Reduction

Computes the mean of elements across dimensions of a tensor.

Reduces input_tensor along the dimensions given in axis. Unless keep_dims is true, the rank of the tensor is reduced by 1 for each entry in axis. If keep_dims is true, the reduced dimensions are retained with length 1.

If axis has no entries, all dimensions are reduced, and a tensor with a single element is returned.

For example:

'x' is [[1., 1.][2., 2.]]tf.reduce_mean(x) ==> 1.5 tf.reduce_mean(x, 0) ==> [1.5, 1.5] tf.reduce_mean(x, 1) ==> [1., 2.]

Args:

input_tensor: The tensor to reduce. Should have numeric type.
axis: The dimensions to reduce. If None (the default), reduces all dimensions.
keep_dims: If true, retains reduced dimensions with length 1.
name: A name for the operation (optional).
reduction_indices: The old (deprecated) name for axis.
tf.pow

pow(x,y,name=None )

Defined in tensorflow/python/ops/math_ops.py.

See the guide: Math > Basic Math Functions

Computes the power of one value to another.

Given a tensor x and a tensor y, this operation computes (x^y) for corresponding elements in x and y. For example:

tensor 'x' is [[2, 2], [3, 3]]tensor 'y' is [[8, 16], [2, 3]]tf.pow(x, y) ==> [[256, 65536], [9, 27]]class tf.train.AdamOptimizerinit(learning_rate=0.001, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False, name='Adam')

線性分類源碼:

#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Mon Jul 10 09:35:04 2017 @author: myhaspl@myhaspl.com,http://blog.csdn.net/myhaspl""" import tensorflow as tf import numpy as npbatch_size=10 w1=tf.Variable(tf.random_normal([2,3],stddev=1,seed=1)) w2=tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))x=tf.placeholder(tf.float32,shape=(None,2),name="x") y=tf.placeholder(tf.float32,shape=(None,1),name="y")h=tf.matmul(x,w1) yo=tf.matmul(h,w2)#損失函數(shù)計算差異平均值 cross_entropy=tf.reduce_mean(tf.abs(y-yo)) #反向傳播 train_step=tf.train.AdamOptimizer().minimize(cross_entropy)#生成200個隨機(jī)樣本 DATASIZE=200 x_=np.random.rand(DATASIZE,2) y_=[[int((x1+x2)>2.5)] for (x1,x2) in x_]with tf.Session() as sess:#初始化變量init_op=tf.global_variables_initializer()sess.run(init_op)print sess.run(w1)print sess.run(w2)#設(shè)定訓(xùn)練輪數(shù)TRAINCOUNT=10000for i in range(TRAINCOUNT):#每次遞進(jìn)選擇一組start=(i*batch_size) % DATASIZEend=min(start+batch_size,DATASIZE)#開始訓(xùn)練sess.run(train_step,feed_dict={x:x_[start:end],y:y_[start:end]})if i%1000==0:total_cross_entropy=sess.run(cross_entropy,feed_dict={x:x_[start:end],y:y_[start:end]})print("%d 次訓(xùn)練之后,損失:%g"%(i+1,total_cross_entropy))print(sess.run(w1))print(sess.run(w2)) [[-0.81131822 1.48459876 0.06532937 -2.4427042 0.0992484 0.59122431]

[ 0.59282297 -2.12292957 -0.72289723 -0.05627038 0.64354479 -0.26432407]]
[[-0.81131822]
[ 1.48459876]
[ 0.06532937]
[-2.4427042 ]
[ 0.0992484 ]
[ 0.59122431]]
1 次訓(xùn)練之后,損失:2.37311
1001 次訓(xùn)練之后,損失:0.587702
2001 次訓(xùn)練之后,損失:0.00187977
3001 次訓(xùn)練之后,損失:0.000224713
4001 次訓(xùn)練之后,損失:0.000245593
5001 次訓(xùn)練之后,損失:0.000837345
6001 次訓(xùn)練之后,損失:0.000561878
7001 次訓(xùn)練之后,損失:0.000521504
8001 次訓(xùn)練之后,損失:0.000369141
9001 次訓(xùn)練之后,損失:2.88023e-05
[[-0.40749896 0.74481744 -1.35231423 -1.57555723 1.5161525 0.38725093]
[ 0.84865922 -2.07912779 -0.41053897 -0.21082011 -0.0567192 -0.69210052]]
[[ 0.36143586]
[ 0.34388798]
[ 0.79891819]
[-1.57640576]
[-0.86542428]
[-0.51558757]]

tf.nn.relu

relu(features,name=None )

Defined in tensorflow/python/ops/gen_nn_ops.py.

See the guides: Layers (contrib) > Higher level ops for building neural network layers, Neural Network > Activation Functions

Computes rectified linear: max(features, 0)

總結(jié)

以上是生活随笔為你收集整理的tensorflow随笔-求平均值的函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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