国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

TensorFlow用expand_dim()來增加維度的方法

2020-02-15 22:32:07
字體:
來源:轉載
供稿:網友

TensorFlow中,想要維度增加一維,可以使用tf.expand_dims(input, dim, name=None)函數。當然,我們常用tf.reshape(input, shape=[])也可以達到相同效果,但是有些時候在構建圖的過程中,placeholder沒有被feed具體的值,這時就會包下面的錯誤:TypeError: Expected binary or unicode string, got 1

在這種情況下,我們就可以考慮使用expand_dims來將維度加1。比如我自己代碼中遇到的情況,在對圖像維度降到二維做特定操作后,要還原成四維[batch, height, width, channels],前后各增加一維。如果用reshape,則因為上述原因報錯

one_img2 = tf.reshape(one_img, shape=[1, one_img.get_shape()[0].value, one_img.get_shape()[1].value, 1])

用下面的方法可以實現:

one_img = tf.expand_dims(one_img, 0)one_img = tf.expand_dims(one_img, -1) #-1表示最后一維

在最后,給出官方的例子和說明

# 't' is a tensor of shape [2]shape(expand_dims(t, 0)) ==> [1, 2]shape(expand_dims(t, 1)) ==> [2, 1]shape(expand_dims(t, -1)) ==> [2, 1]# 't2' is a tensor of shape [2, 3, 5]shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]

Args:

input: A Tensor.
dim: A Tensor. Must be one of the following types: int32, int64. 0-D (scalar). Specifies the dimension index at which to expand the shape of input.
name: A name for the operation (optional).

Returns:

A Tensor. Has the same type as input. Contains the same data as input, but its shape has an additional dimension of size 1 added.

以上這篇TensorFlow用expand_dim()來增加維度的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巫山县| 唐河县| 云林县| 固阳县| 和田市| 乐昌市| 朝阳县| 乳山市| 塔城市| 贺州市| 突泉县| 望都县| 佳木斯市| 娄底市| 溧水县| 长宁区| 新乐市| 孟连| 景泰县| 杂多县| 丹寨县| 汝南县| 双桥区| 射阳县| 长宁县| 方山县| 平江县| 青海省| 锡林浩特市| 上蔡县| 宁津县| 耒阳市| 醴陵市| 中牟县| 安国市| 万山特区| 汤原县| 贵州省| 二连浩特市| 夏邑县| 沅陵县|