tensorflow中常用的計算操作

2021-08-29 16:20:37 字數 4041 閱讀 9610

從tensor的維度上面計算元素之和

tf.reduce_sum(

input_tensor, # 輸入

axis=none, # 表示在哪個維度進行sum操作。

keepdims=none, # 表示是否保留原始資料的維度,false相當於執行完後原始資料就會少乙個維度。

name=none,

reduction_indices=none,

keep_dims=none

)

import tensorflow as tf

x = tf.constant([[

1,1,

1],[

1,1,

1]])

a = tf.reduce_sum(x)

# 修改這裡

b = tf.reduce_sum(x, axis=0)

c = tf.reduce_sum(x, axis=1)

d = tf.reduce_sum(x, keep_dims=

true

)e = tf.reduce_sum(x, keep_dims=

false

)f = tf.reduce_sum(x, axis=[0

,1])

tensors =

[a, b, c, d, e, f]

with tf.session(

)as sess:

for tensor in tensors:

y = sess.run(tensor)

print

(y)# 6

# [2 2 2]

# [3 3]

# [[6]]

# 6# 6

數乘

tf.multiply(

x,y,

name=none # a name for the operation (optional).

)

import tensorflow as tf

a = tf.constant([[

1,2]

,[3,

4]])

b = tf.constant([[

1,3]

,[2,

1]])

y = tf.multiply(a, b)

with tf.session(

)as sess:

res = sess.run(y)

print

(res)

# [[1 6]

# [6 4]]

import tensorflow as tf

x = tf.constant([[1.0, 2.0],

[3.0, 4.0]])

a = 0.5 * x

b = tf.multiply(0.5, x)

tensors = [a, b]

with tf.session() as sess:

for tensor in tensors:

y = sess.run(tensor)

print(y)

# [[0.5 1. ]

# [1.5 2. ]]

# [[0.5 1. ]

# [1.5 2. ]]

矩陣點乘

tf.matmul(

a,b,

transpose_a=false,

transpose_b=false,

adjoint_a=false,

adjoint_b=false,

a_is_sparse=false,

b_is_sparse=false,

name=none

)

import tensorflow as tf

a = tf.constant([[

1,2]

,[3,

4]])

b = tf.constant([[

1,3]

,[2,

1]])

y = tf.matmul(a, b)

with tf.session(

)as sess:

res = sess.run(y)

print

(res)

# [[ 5 5]

# [11 13]]

import tensorflow as tf

x = tf.constant([[1, 2],

[3, 4]])

a = 1 + x

b = tf.add(1, x)

tensors = [a, b]

with tf.session() as sess:

for tensor in tensors:

y = sess.run(tensor)

print(y)

# [[2 3]

# [4 5]]

# [[2 3]

# [4 5]]

import tensorflow as tf

x = tf.constant([[1.8, 2.2],

[3.0, 4.1]])

y = tf.cast(x, tf.int32)

with tf.session() as sess:

res = sess.run(y)

print(res)

# [[1 2]

# [3 4]]

import tensorflow as tf

x = tf.constant([[1.8, 2.2],

[3.0, 4.1]])

y = tf.cast(x, tf.int32)

with tf.session() as sess:

res = sess.run(y)

print(res)

# [[1 2]

# [3 4]]

import tensorflow as tf

x = [1, 5, 3]

y = [2, 4, 6]

z = tf.maximum(x, y)

with tf.session() as sess:

res = sess.run(z)

print(res)

# [2 5 6]

import tensorflow as tf

x = [1, 5, 3]

y = [2, 4, 6]

z = tf.minimum(x, y)

with tf.session() as sess:

res = sess.run(z)

print(res)

# [1 4 3] 每個維度返回乙個最小值

# import tensorflow as tf

a = tf.argmax([[1,3,4,5,6]], axis=1)

b = tf.argmax([[1,3,4], [2,4,1]], axis=1)

c = tf.argmin([[1,3,4,5,6]], axis=1)

d = tf.argmin([[1,3,4], [2,4,1]], axis=1)

tensors = [a ,b, c, d]

with tf.session() as sess:

for tensor in tensors:

y = sess.run(tensor)

print(y)

# [4]

# [2 1]

# [0]

# [0 2]

tensorflow中常用的函式

正文 一,tensorflow中有一類在tensor的某一維度上求值的函式。如 求最大值 tf.reduce max input tensor,reduction indices none,keep dims false,name none 求平均值tf.reduce mean input tens...

Tensorflow中常用函式記載

最近在學習tensorflow,函式眾多,因此將一些常用的用於構建神經網路的函式記載如下。1 匯入tensorflow庫 import tensorflow as tf 匯入tensorflow庫2 與計算圖相關 tf.get default graph 獲取當前預設的計算圖 tensor a a....

mysql中常用的語句 mysql中常用的語句整理

mysql中常用的語句 1 建立帶自增長的主鍵的表 drop table if exists user login create table user login user id int unsigned not null auto increment,user name varchar 50 de...