最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

TensorFlow中如何確定張量的形狀實例

 更新時間:2020年06月23日 14:08:46   作者:信道者  
這篇文章主要介紹了TensorFlow中如何確定張量的形狀實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

我們可以使用tf.shape()獲取某張量的形狀張量。

import tensorflow as tf
x = tf.reshape(tf.range(1000), [10, 10, 10])
sess = tf.Session()
sess.run(tf.shape(x))
 
Out[1]: array([10, 10, 10])

我們可以使用tf.shape()在計算圖中確定改變張量的形狀。

high = tf.shape(x)[0] // 2
width = tf.shape(x)[1] * 2
x_reshape = tf.reshape(x, [high, width, -1])
sess.run(tf.shape(x_reshape))
 
Out: array([ 5, 20, 10])

我們可以使用tf.shape_n()在計算圖中得到若干個張量的形狀。

y = tf.reshape(tf.range(504), [7,8,9])
sess.run(tf.shape_n([x, y]))
 
Out: [array([10, 10, 10]), array([7, 8, 9])]

我們可以使用tf.size()獲取張量的元素個數(shù)。

sess.run([tf.size(x), tf.size(y)])

Out: [1000, 504]

tensor.get_shape()或者tensor.shape是無法在計算圖中用于確定張量的形狀。

In [20]: x.get_shape()
Out[20]: TensorShape([Dimension(10), Dimension(10), Dimension(10)])
 
In [21]: x.get_shape()[0]
Out[21]: Dimension(10)
 
In [22]: type(x.get_shape()[0])
Out[22]: tensorflow.python.framework.tensor_shape.Dimension
 
In [23]: x.get_shape()
Out[23]: TensorShape([Dimension(10), Dimension(10), Dimension(10)])
 
In [24]: sess.run(x.get_shape())
---------------------------------------------------------------------------
TypeError     Traceback (most recent call last)
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in __init__(self, fetches, contraction_fn)
 299  self._unique_fetches.append(ops.get_default_graph().as_graph_element(
--> 300  fetch, allow_tensor=True, allow_operation=True))
 301 except TypeError as e:
 
~\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in as_graph_element(self, obj, allow_tensor, allow_operation)
 3477 with self._lock:
-> 3478 return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
 3479
 
~\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in _as_graph_element_locked(self, obj, allow_tensor, allow_operation)
 3566 raise TypeError("Can not convert a %s into a %s." % (type(obj).__name__,
-> 3567        types_str))
 3568
 
TypeError: Can not convert a TensorShapeV1 into a Tensor or Operation.
 
During handling of the above exception, another exception occurred:
 
TypeError     Traceback (most recent call last)
<ipython-input-24-de007c69e003> in <module>
----> 1 sess.run(x.get_shape())
 
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
 927 try:
 928 result = self._run(None, fetches, feed_dict, options_ptr,
--> 929    run_metadata_ptr)
 930 if run_metadata:
 931  proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
 
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
 1135 # Create a fetch handler to take care of the structure of fetches.
 1136 fetch_handler = _FetchHandler(
-> 1137  self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
 1138
 1139 # Run request and get response.
 
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in __init__(self, graph, fetches, feeds, feed_handles)
 469 """
 470 with graph.as_default():
--> 471 self._fetch_mapper = _FetchMapper.for_fetch(fetches)
 472 self._fetches = []
 473 self._targets = []
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in for_fetch(fetch)
 269  if isinstance(fetch, tensor_type):
 270  fetches, contraction_fn = fetch_fn(fetch)
--> 271  return _ElementFetchMapper(fetches, contraction_fn)
 272 # Did not find anything.
 273 raise TypeError('Fetch argument %r has invalid type %r' % (fetch,
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in __init__(self, fetches, contraction_fn)
 302  raise TypeError('Fetch argument %r has invalid type %r, '
 303    'must be a string or Tensor. (%s)' %
--> 304    (fetch, type(fetch), str(e)))
 305 except ValueError as e:
 306  raise ValueError('Fetch argument %r cannot be interpreted as a '
TypeError: Fetch argument TensorShape([Dimension(10), Dimension(10), Dimension(10)]) has invalid type <class 'tensorflow.python.framework.tensor_shape.TensorShapeV1'>, must be a string or Tensor. (Can not convert a TensorShapeV1 into a Tensor or Operation.)

我們可以使用tf.rank()來確定張量的秩。tf.rank()會返回一個代表張量秩的張量,可直接在計算圖中使用。

In [25]: tf.rank(x)
Out[25]: <tf.Tensor 'Rank:0' shape=() dtype=int32>
 
In [26]: sess.run(tf.rank(x))
Out[26]: 3

補(bǔ)充知識:tensorflow循環(huán)改變tensor的值

使用tf.concat()實現(xiàn)4維tensor的循環(huán)賦值

alist=[[[[1,1,1],[2,2,2],[3,3,3]],[[4,4,4],[5,5,5],[6,6,6]]],[[[7,7,7],[8,8,8],[9,9,9]],[[10,10,10],[11,11,11],[12,12,12]]]] #2,2,3,3-n,c,h,w
kenel=(np.asarray(alist)*2).tolist()
print(kenel)
inputs=tf.constant(alist,dtype=tf.float32)
kenel=tf.constant(kenel,dtype=tf.float32)
inputs=tf.transpose(inputs,[0,2,3,1]) #n,h,w,c
kenel=tf.transpose(kenel,[0,2,3,1]) #n,h,w,c
uints=inputs.get_shape()
h=int(uints[1])
w=int(uints[2])
encoder_output=[]
for b in range(int(uints[0])):
 encoder_output_c=[]
 for c in range(int(uints[-1])):
  one_channel_in = inputs[b, :, :, c]
  one_channel_in = tf.reshape(one_channel_in, [1, h, w, 1])
  one_channel_kernel = kenel[b, :, :, c]
  one_channel_kernel = tf.reshape(one_channel_kernel, [h, w, 1, 1])
  encoder_output_cc = tf.nn.conv2d(input=one_channel_in, filter=one_channel_kernel, strides=[1, 1, 1, 1], padding="SAME")
  if c==0:
   encoder_output_c=encoder_output_cc
  else:
   encoder_output_c=tf.concat([encoder_output_c,encoder_output_cc],axis=3)

 if b==0:
  encoder_output=encoder_output_c
 else:
  encoder_output = tf.concat([encoder_output, encoder_output_c], axis=0)

with tf.Session() as sess:
 print(sess.run(tf.transpose(encoder_output,[0,3,1,2])))
 print(encoder_output.get_shape())

輸出:

[[[[ 32. 48. 32.]
 [ 56. 84. 56.]
 [ 32. 48. 32.]]

 [[ 200. 300. 200.]
 [ 308. 462. 308.]
 [ 200. 300. 200.]]]


 [[[ 512. 768. 512.]
 [ 776. 1164. 776.]
 [ 512. 768. 512.]]

 [[ 968. 1452. 968.]
 [1460. 2190. 1460.]
 [ 968. 1452. 968.]]]]
(2, 3, 3, 2)

以上這篇TensorFlow中如何確定張量的形狀實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 快速一鍵生成Python爬蟲請求頭

    快速一鍵生成Python爬蟲請求頭

    這篇文章主要介紹了如何快速一鍵生成Python爬蟲請求頭,幫助大家更好的理解和學(xué)習(xí)使用python爬蟲,感興趣的朋友可以了解下
    2021-03-03
  • python聊天室(雖然很簡潔,但是可以用)

    python聊天室(雖然很簡潔,但是可以用)

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)多人聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Python使用pandas模塊實現(xiàn)表之間的關(guān)聯(lián)

    Python使用pandas模塊實現(xiàn)表之間的關(guān)聯(lián)

    在數(shù)據(jù)分析和處理中,表之間的關(guān)聯(lián)是非常常見的操作,本文為大家介紹了pandas中實現(xiàn)表之間的關(guān)聯(lián)有四種方式,感興趣的小伙伴可以了解一下
    2023-07-07
  • Django多對多ManyToManyField字段的使用

    Django多對多ManyToManyField字段的使用

    Django是一個支持多對多關(guān)系的Web框架,可以在模型中定義多對多關(guān)系,本文主要介紹了Django多對多ManyToManyField字段的使用,感興趣的可以了解一下
    2023-12-12
  • python隨機(jī)生成大小寫字母數(shù)字混合密碼(僅20行代碼)

    python隨機(jī)生成大小寫字母數(shù)字混合密碼(僅20行代碼)

    這篇文章主要介紹了python隨機(jī)生成大小寫字母數(shù)字混合密碼,主要是利用random模塊隨機(jī)生成數(shù)字,大小寫字母,通過循環(huán)次數(shù)來實現(xiàn)此功能,需要的朋友可以參考下
    2020-02-02
  • Python中高階函數(shù)的小實踐分享

    Python中高階函數(shù)的小實踐分享

    這篇文章主要為大家介紹了一些Python中高階函數(shù)的小實踐,文中的示例代碼講解詳細(xì),對我們深入學(xué)習(xí)Python有一定的幫助,需要的可以參考下
    2023-05-05
  • python使用Pybind11擴(kuò)展c++的實現(xiàn)

    python使用Pybind11擴(kuò)展c++的實現(xiàn)

    Pybind11是一個輕量級的C++庫,旨在無縫地將C++代碼綁定到Python,本文主要介紹了python使用Pybind11擴(kuò)展c++的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2025-04-04
  • Python圖像處理之圖像融合與ROI區(qū)域繪制詳解

    Python圖像處理之圖像融合與ROI區(qū)域繪制詳解

    這篇文章將詳細(xì)講解圖像融合處理和ROI區(qū)域繪制,同時補(bǔ)充圖像屬性、通道和類型轉(zhuǎn)換。文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-01-01
  • 用Python自動下載網(wǎng)站所有文件

    用Python自動下載網(wǎng)站所有文件

    這篇文章主要介紹了如何用Python自動下載網(wǎng)站所有文件,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-03-03
  • 淺析Python中g(shù)lobal和nonlocal關(guān)鍵字的妙用

    淺析Python中g(shù)lobal和nonlocal關(guān)鍵字的妙用

    這篇文章主要來和大家一起深入探討Python中關(guān)鍵詞global和nonlocal的用法,包括詳細(xì)的示例代碼和實際應(yīng)用場景,感興趣的可以了解下
    2024-04-04

最新評論

伊吾县| 崇明县| 福安市| 盖州市| 荥阳市| 潮安县| 高阳县| 永顺县| 涪陵区| 固始县| 富锦市| 呼和浩特市| 禹城市| 濮阳市| 海阳市| 政和县| 磐石市| 托克逊县| 屏东县| 鄂州市| 镇赉县| 赤水市| 浏阳市| 正宁县| 潜山县| 工布江达县| 当雄县| 达拉特旗| 长沙县| 韩城市| 黄山市| 博白县| 和硕县| 五原县| 浮梁县| 临邑县| 雅安市| 余庆县| 深水埗区| 化德县| 离岛区|