expand_dims 中的困惑

2018-01-21 22:35:24 +08:00
 panins

买了一《 tensorflow 机器学习实战指南》,自己学到了第六章,神经网络算法。

但是有个简单例子小弟一时半会卡在这里,大家是否可以抽空指导一下。

谢谢

6.5 用 TENSORFLOW 实现神经网络常见层

我的问题是定义卷积层函数的时候,书上说把 1D 扩展到 4D。

为什么 input_4d = tf.expand_dims(input_3d, 3)第二个参数用了 3,而 input_1-3d 都是 0

import tensorflow as tf
import numpy as np
sess = tf.Session()
data_size = 25
data_1d = np.random.normal(size=data_size)
x_input_1d = tf.placeholder(dtype=tf.float32, shape=[data_size])


def conv_layer_1d(input_1d, my_filter):
    # make 1d input into 4d
    input_2d = tf.expand_dims(input_1d, 0)
    input_3d = tf.expand_dims(input_2d, 0)
    input_4d = tf.expand_dims(input_3d, 3)
    # perform convolution
    convolution_output = tf.nn.conv2d(input_4d, filter=my_filter,
                                      strides=[1,1,1,1], padding="VALID")
    conv_output_1d = tf.squeeze(convolution_output)
    return(conv_output_1d)

my_filter = tf.Variable(tf.random_normal(shape=[1,5,1,1]))
my_convolution_output = conv_layer_1d(x_input_1d, my_filter)
......
3588 次点击
所在节点    TensorFlow
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/424758

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX