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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

tensorflow随笔-条件循环控制(1)

發布時間:2025/3/12 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tensorflow随笔-条件循环控制(1) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

TensorFlow 提供了一些操作和類,您可以使用它們來控制操作的執行,并向圖表添加條件依賴項。

tf.identity
tf.tuple
tf.group
tf.no_op
tf.count_up_to
tf.cond
tf.case
tf.while_loop

tf.identity

tf.identity(
input,
name=None
)

返回和輸入大小與內容一致的tensor

參數:

input: 一個Tensor.
name: 操作名字(可選)
這個方法的源碼:

def identity(input, name=None): # pylint: disable=redefined-builtinr"""Return a tensor with the same shape and contents as input.Args:input: A `Tensor`.name: A name for the operation (optional).Returns:A `Tensor`. Has the same type as `input`."""if context.executing_eagerly():input = ops.convert_to_tensor(input)in_device = input.device# TODO(ashankar): Does 'identity' need to invoke execution callbacks?context_device = context.context().device_nameif not context_device:context_device = "/job:localhost/replica:0/task:0/device:CPU:0"if context_device != in_device:return input._copy() # pylint: disable=protected-accessreturn inputelse:return gen_array_ops.identity(input, name=name)

從源碼可看出,input的device會與context.context()相比較,如果相同,則直接返回input,如果不相同則返回disable=protected-access權限的input的copy。

#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Mon Aug 27 11:16:32 2018"""import tensorflow as tf x1 = tf.constant(2.) x2 = [1,2,3] y1=tf.identity(x1,name="my_y1") y2=tf.identity(x2,name="my_y") sess=tf.Session() with sess: print sess.run(y1)print sess.run(y2)

2.0
[1 2 3]

tf.tuple(元組)

tf.tuple(
tensors,
name=None,
control_inputs=None
)

將多個tensor合并組。

這創建了一個張量元組,其值與多張量參數相同,只是每個張量的值只有在計算完所有張量的值之后才返回。

control_inputs包含額外的OPS,在OP完成之前必須完成,但其輸出不返回。

這可以被用作并行計算的“連接”機制:所有的參數張量可以并行計算,但是在所有并行計算完成之后,元組返回的任何張量的值都是可用的。

.

參數:
tensors: 多tensor的列表或IndexedSlices, 有些條目可能是None。
name: (可選)用作操作的 name_scope的名稱。
control_inputs: 在返回之前完成附加操作表
返回:

像tensors一樣

Raises:

ValueError:如果tensors沒有包括任何tensor或IndexedSlices。
TypeError: 如果control_inputs不是一個Operation或Tensor 對象的列表。

#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Mon Aug 27 11:16:32 2018 @author: myhaspl """import tensorflow as tf x1 = tf.random_normal([1000,1000],mean=100) x2 = tf.random_normal([1000,1000],mean=100) x3 = tf.random_normal([1000,1000],mean=100)y1 = tf.sqrt(x1) y2 = tf.sqrt(x2) y3 = tf.sqrt(x3) z = tf.tuple([y1,y2,y3])sess=tf.Session() with sess: res=sess.run(z)print res[0]

[[ 9.989998 9.980425 9.897268 … 10.010141 10.008263 10.005144]
[ 9.970587 9.988404 10.011098 … 10.027785 9.956984 10.110886]
[ 9.982615 9.952952 10.033136 … 9.999784 10.009718 9.915539]

[ 9.934934 9.926054 10.045075 … 10.0579 10.020126 9.959899]
[10.031994 10.059689 10.05442 … 10.009988 9.977903 10.036525]
[ 9.945853 9.955557 10.047363 … 10.067215 9.942139 10.06124 ]]

總結

以上是生活随笔為你收集整理的tensorflow随笔-条件循环控制(1)的全部內容,希望文章能夠幫你解決所遇到的問題。

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