tf.expand_dims 来增加维度
主要是因为tflearn官方的例子总是有embeding层,去掉的话要conv1d正常工作,需要加上expand_dims
network = input_data(shape=[None, 100], name='input')
network = tf.expand_dims(network, 2)
branch1 = conv_1d(network, 128, 3, padding='valid', activation='relu', regularizer="L2")
ref:https://stackoverflow.com/questions/49592771/tflearn-classification-with-cnn-conv-1d
from: https://blog.csdn.net/jasonzzj/article/details/60811035
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.
最新文章
- 几种常见的Shell
- Python高级特性(1):Iterators、Generators和itertools(参考)
- Solr Dataimporthandler 导入MySQL 内存溢出。
- Java Hour 11
- Oracle角色
- Dtrace for Linux 2016
- 深入分析C++引用
- 小乌龟git
- 解决HighChart开发遇到的2个问题
- Redis_简单使用
- 微信公众号JSAPI支付-多公众号向同一商户号支付的问题解决
- 以添加评论组件为例看angular2请求数据的处理
- vim中delete(backspace)键不能向左删除
- Android启动过程
- Linux系统运维笔记(四),CentOS 6.4安装 MongoDB
- GitHub 在使用命令行 git push 时报错:The requested URL returned error: 403
- 【备档】客户端自动化(主Android Appium + python
- react-native 常规操作
- JPA Spring Data 概述
- Dubbo实践(十二)Refer
热门文章
- activity 的跳转
- JVM(一)
- Kali Linux 更新源 操作完整版教程
- L1-044. 稳赢
- gc调优我们到底在调整什么
- vuex 源码:深入 vuex 之辅助函数 mapState
- P493 brass
- Codeforces 600 E - Lomsat gelral
- 日常英语---十四、Dolce &; Gabbana cancels China show amid 'racist' ad controversy(adj.温柔的,prep.在其中)
- windows/browser ---->; cmd命令/powershell命令/chrome插件vimuim命令