gpt4 book ai didi

python - 你如何在tensorflow中创建一个初始模块

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:50 24 4
gpt4 key购买 nike

查看 tensorflow 页面:https://github.com/tensorflow/models/tree/master/inception

他们展示了一张带有架构的图像,特别是并行包含的“初始”模块:

  • 1x1 的转换层
  • 3x3 的转换层
  • 5x5 的转换层
  • 平均池化 + 1x1 转换

后跟一个“concat”层。

enter image description here

如何在 tensorflow 中创建它?

我想我可以按照这个思路做一些事情来创建并行操作:

start_layer = input_data

filter = tf.Variable(tf.truncated_normal([1,1,channels,filter_count], stddev=0.1)
one_by_one = tf.nn.conv2d(start_layer, filter, strides=[1,1,1,1], padding='SAME')

filter = tf.Variable(tf.truncated_normal([3,3,channels,filter_count], stddev=0.1)
three_by_three = tf.nn.conv2d(start_layer, filter, strides=[1,1,1,1], padding='SAME')

filter = tf.Variable(tf.truncated_normal([5,5,channels,filter_count], stddev=0.1)
five_by_five = tf.nn.conv2d(start_layer, filter, strides=[1,1,1,1], padding='SAME')

filter = tf.Variable(tf.truncated_normal([1,1,channels,filter_count], stddev=0.1)
pooling = tf.nn.avg_pool(start_layer, filter, strides=[1,2,2,1], padding='SAME')

filter = tf.Variable(tf.truncated_normal([1,1,channels,filter_count], stddev=0.1)
pooling = tf.nn.conv2d(pooling, filter, strides=[1,1,1,1], padding='SAME')

#connect one_by_one, three_by_three, five_by_five, pooling into an concat layer

但是我如何将这 4 个操作组合到一个 concat 层中呢?

最佳答案

我做了一些与您所做的非常相似的事情,然后用 tf.concat() 完成了它。请注意 axis=3,它匹配我的 4d 张量并连接到第 4 维(索引 3)。它的文档是 here .

我的最终代码是这样的:

def inception2d(x, in_channels, filter_count):
# bias dimension = 3*filter_count and then the extra in_channels for the avg pooling
bias = tf.Variable(tf.truncated_normal([3*filter_count + in_channels], mu, sigma)),

# 1x1
one_filter = tf.Variable(tf.truncated_normal([1, 1, in_channels, filter_count], mu, sigma))
one_by_one = tf.nn.conv2d(x, one_filter, strides=[1, 1, 1, 1], padding='SAME')

# 3x3
three_filter = tf.Variable(tf.truncated_normal([3, 3, in_channels, filter_count], mu, sigma))
three_by_three = tf.nn.conv2d(x, three_filter, strides=[1, 1, 1, 1], padding='SAME')

# 5x5
five_filter = tf.Variable(tf.truncated_normal([5, 5, in_channels, filter_count], mu, sigma))
five_by_five = tf.nn.conv2d(x, five_filter, strides=[1, 1, 1, 1], padding='SAME')

# avg pooling
pooling = tf.nn.avg_pool(x, ksize=[1, 3, 3, 1], strides=[1, 1, 1, 1], padding='SAME')

x = tf.concat([one_by_one, three_by_three, five_by_five, pooling], axis=3) # Concat in the 4th dim to stack
x = tf.nn.bias_add(x, bias)
return tf.nn.relu(x)

关于python - 你如何在tensorflow中创建一个初始模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41108684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com