Tensorflow 4D张量

时间:2018-07-16 13:30:40

标签: python tensorflow

以下代码包含用于在Tensorflow中的CNN中创建卷积层,权重和偏差的函数。我的问题在下面一行的create_convolutional_layer()中。这条线创建权重的张量。这是4D张量吗?实际上看起来像这张图片

weights = create_weights(shape=[conv_filter_size, conv_filter_size, num_input_channels, num_filters])

代码:

def create_weights(shape):
    return tf.Variable(tf.truncated_normal(shape, stddev=0.05))

def create_biases(size):
    return tf.Variable(tf.constant(0.05, shape=[size]))

#This function creates the convolution NN

def create_convolutional_layer(input,
               num_input_channels, 
               conv_filter_size,        
               num_filters):  

    ## We shall define the weights that will be trained using create_weights function.
    weights = create_weights(shape=[conv_filter_size, conv_filter_size, num_input_channels, num_filters])
    ## We create biases using the create_biases function. These are also trained.
    biases = create_biases(num_filters)

    ## Creating the convolutional layer
    layer = tf.nn.conv2d(input=input,
                     filter=weights,
                     strides=[1, 1, 1, 1],
                     padding='SAME')

    print("layer name is", str(layer.name))

    layer += biases

    ## We shall be using max-pooling.  
    layer = tf.nn.max_pool(value=layer,
                            ksize=[1, 2, 2, 1],
                            strides=[1, 2, 2, 1],
                            padding='SAME')
    ## Output of pooling is fed to Relu which is the activation function for us.
    layer = tf.nn.relu(layer)

    return layer 

0 个答案:

没有答案