本文關鍵給大家介紹了python深度神經網絡tensorflow卷積層范例實例教程,感興趣的小伙伴可以參考借鑒一下,希望可以有一定的幫助,祝愿大家多多的發展,盡早漲薪。
一、舊版本(1.0以下)的卷積函數:tf.nn.conv2d
在tf1.0中,對卷積層重新進行了封裝,比原來版本的卷積層有了很大的簡化。
conv2d( input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None )
該函數定義在tensorflow/python/ops/gen_nn_ops.py。
參數:
input:一個4維Tensor(N,H,W,C).類型必須是以下幾種類型之一:half,float32,float64.
filter:卷積核.類型和input必須相同,
4維tensor,[filter_height,filter_width,in_channels,out_channels],如[5,5,3,32]
strides:在input上切片采樣時,每個方向上的滑窗步長,必須和format指定的維度同階,如[1,2,2,1]
padding:指定邊緣填充類型:"SAME","VALID".SAME表示卷積后圖片保持不變,VALID則會縮小。
use_cudnn_on_gpu:可選項,bool型。表示是否在GPU上用cudnn進行加速,默認為True.
data_format:可選項,指定輸入數據的格式:"NHWC"或"NCHW",默認為"NHWC"。
NHWC格式指[batch,in_height,in_width,in_channels]NCHW格式指[batch,in_channels,in_height,in_width]
name:操作名,可選.
示例
conv1=tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
二、1.0版本中的卷積函數:tf.layers.conv2d
conv2d( inputs, filters, kernel_size, strides=(1,1), padding='valid', data_format='channels_last', dilation_rate=(1,1), activation=None, use_bias=True, kernel_initializer=None, bias_initializer=tf.zeros_initializer(), kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, trainable=True, name=None, reuse=None ) 定義 #Copyright 2015 The TensorFlow Authors.All Rights Reserved. # #Licensed under the Apache License,Version 2.0(the"License"); #you may not use this file except in compliance with the License. #You may obtain a copy of the License at # #http://www.apache.org/licenses/LICENSE-2.0 # #Unless required by applicable law or agreed to in writing,software #distributed under the License is distributed on an"AS IS"BASIS, #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied. #See the License for the specific language governing permissions and #limitations under the License. #============================================================================= #pylint:disable=unused-import,g-bad-import-order """Contains the convolutional layer classes and their functional aliases. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import six from six.moves import xrange#pylint:disable=redefined-builtin import numpy as np from tensorflow.python.framework import ops from tensorflow.python.ops import array_ops from tensorflow.python.ops import control_flow_ops from tensorflow.python.ops import nn from tensorflow.python.ops import math_ops from tensorflow.python.ops import init_ops from tensorflow.python.ops import standard_ops from tensorflow.python.ops import variable_scope as vs from tensorflow.python.layers import base from tensorflow.python.layers import utils class _Conv(base._Layer):#pylint:disable=protected-access """Abstract nD convolution layer(private,used as implementation base). This layer creates a convolution kernel that is convolved (actually cross-correlated)with the layer input to produce a tensor of outputs.If`use_bias`is True(and a`bias_initializer`is provided), a bias vector is created and added to the outputs.Finally,if `activation`is not`None`,it is applied to the outputs as well. Arguments: rank:An integer,the rank of the convolution,e.g."2"for 2D convolution. filters:Integer,the dimensionality of the output space(i.e.the number of filters in the convolution). kernel_size:An integer or tuple/list of n integers,specifying the length of the convolution window. strides:An integer or tuple/list of n integers, specifying the stride length of the convolution. Specifying any stride value!=1 is incompatible with specifying any`dilation_rate`value!=1. padding:One of`"valid"`or`"same"`(case-insensitive). data_format:A string,one of`channels_last`(default)or`channels_first`. The ordering of the dimensions in the inputs. `channels_last`corresponds to inputs with shape `(batch,...,channels)`while`channels_first`corresponds to inputs with shape`(batch,channels,...)`. dilation_rate:An integer or tuple/list of n integers,specifying the dilation rate to use for dilated convolution. Currently,specifying any`dilation_rate`value!=1 is incompatible with specifying any`strides`value!=1. activation:Activation function.Set it to None to maintain a linear activation. use_bias:Boolean,whether the layer uses a bias. kernel_initializer:An initializer for the convolution kernel. bias_initializer:An initializer for the bias vector.If None,no bias will be applied. kernel_regularizer:Optional regularizer for the convolution kernel. bias_regularizer:Optional regularizer for the bias vector. activity_regularizer:Regularizer function for the output. trainable:Boolean,if`True`also add variables to the graph collection `GraphKeys.TRAINABLE_VARIABLES`(see`tf.Variable`). name:A string,the name of the layer. """ def __init__(self,rank, filters, kernel_size, strides=1, padding='valid', data_format='channels_last', dilation_rate=1, activation=None, use_bias=True, kernel_initializer=None, bias_initializer=init_ops.zeros_initializer(), kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, trainable=True, name=None, **kwargs): super(_Conv,self).__init__(trainable=trainable, name=name,**kwargs) self.rank=rank self.filters=filters self.kernel_size=utils.normalize_tuple(kernel_size,rank,'kernel_size') self.strides=utils.normalize_tuple(strides,rank,'strides') self.padding=utils.normalize_padding(padding) self.data_format=utils.normalize_data_format(data_format) self.dilation_rate=utils.normalize_tuple( dilation_rate,rank,'dilation_rate') self.activation=activation self.use_bias=use_bias self.kernel_initializer=kernel_initializer self.bias_initializer=bias_initializer self.kernel_regularizer=kernel_regularizer self.bias_regularizer=bias_regularizer self.activity_regularizer=activity_regularizer def build(self,input_shape): if len(input_shape)!=self.rank+2: raise ValueError('Inputs should have rank'+ str(self.rank+2)+ 'Received input shape:',str(input_shape)) if self.data_format=='channels_first': channel_axis=1 else: channel_axis=-1 if input_shape[channel_axis]is None: raise ValueError('The channel dimension of the inputs' 'should be defined.Found`None`.') input_dim=input_shape[channel_axis] kernel_shape=self.kernel_size+(input_dim,self.filters) self.kernel=vs.get_variable('kernel', shape=kernel_shape, initializer=self.kernel_initializer, regularizer=self.kernel_regularizer, trainable=True, dtype=self.dtype) if self.use_bias: self.bias=vs.get_variable('bias', shape=(self.filters,), initializer=self.bias_initializer, regularizer=self.bias_regularizer, trainable=True, dtype=self.dtype) else: self.bias=None def call(self,inputs): outputs=nn.convolution( input=inputs, filter=self.kernel, dilation_rate=self.dilation_rate, strides=self.strides, padding=self.padding.upper(), data_format=utils.convert_data_format(self.data_format,self.rank+2)) if self.bias is not None: if self.rank!=2 and self.data_format=='channels_first': #bias_add does not support channels_first for non-4D inputs. if self.rank==1: bias=array_ops.reshape(self.bias,(1,self.filters,1)) if self.rank==3: bias=array_ops.reshape(self.bias,(1,self.filters,1,1)) outputs+=bias else: outputs=nn.bias_add( outputs, self.bias, data_format=utils.convert_data_format(self.data_format,4)) #Note that we passed rank=4 because bias_add will only accept #NHWC and NCWH even if the rank of the inputs is 3 or 5. if self.activation is not None: return self.activation(outputs) return outputs class Conv1D(_Conv): """1D convolution layer(e.g.temporal convolution). This layer creates a convolution kernel that is convolved (actually cross-correlated)with the layer input to produce a tensor of outputs.If`use_bias`is True(and a`bias_initializer`is provided), a bias vector is created and added to the outputs.Finally,if `activation`is not`None`,it is applied to the outputs as well. Arguments: filters:Integer,the dimensionality of the output space(i.e.the number of filters in the convolution). kernel_size:An integer or tuple/list of a single integer,specifying the length of the 1D convolution window. strides:An integer or tuple/list of a single integer, specifying the stride length of the convolution. Specifying any stride value!=1 is incompatible with specifying any`dilation_rate`value!=1. padding:One of`"valid"`or`"same"`(case-insensitive). data_format:A string,one of`channels_last`(default)or`channels_first`. The ordering of the dimensions in the inputs. `channels_last`corresponds to inputs with shape `(batch,length,channels)`while`channels_first`corresponds to inputs with shape`(batch,channels,length)`. dilation_rate:An integer or tuple/list of a single integer,specifying the dilation rate to use for dilated convolution. Currently,specifying any`dilation_rate`value!=1 is incompatible with specifying any`strides`value!=1. activation:Activation function.Set it to None to maintain a linear activation. use_bias:Boolean,whether the layer uses a bias. kernel_initializer:An initializer for the convolution kernel. bias_initializer:An initializer for the bias vector.If None,no bias will be applied. kernel_regularizer:Optional regularizer for the convolution kernel. bias_regularizer:Optional regularizer for the bias vector. activity_regularizer:Regularizer function for the output. trainable:Boolean,if`True`also add variables to the graph collection `GraphKeys.TRAINABLE_VARIABLES`(see`tf.Variable`). name:A string,the name of the layer. """ def __init__(self,filters, kernel_size, strides=1, padding='valid', data_format='channels_last', dilation_rate=1, activation=None, use_bias=True, kernel_initializer=None, bias_initializer=init_ops.zeros_initializer(), kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, trainable=True, name=None, **kwargs): super(Convolution1D,self).__init__( rank=1, filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, dilation_rate=dilation_rate, activation=activation, use_bias=use_bias, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer, activity_regularizer=activity_regularizer, trainable=trainable, name=name,**kwargs) def conv1d(inputs, filters, kernel_size, strides=1, padding='valid', data_format='channels_last', dilation_rate=1, activation=None, use_bias=True, kernel_initializer=None, bias_initializer=init_ops.zeros_initializer(), kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, trainable=True, name=None, reuse=None): """Functional interface for 1D convolution layer(e.g.temporal convolution). This layer creates a convolution kernel that is convolved (actually cross-correlated)with the layer input to produce a tensor of outputs.If`use_bias`is True(and a`bias_initializer`is provided), a bias vector is created and added to the outputs.Finally,if `activation`is not`None`,it is applied to the outputs as well. Arguments: inputs:Tensor input. filters:Integer,the dimensionality of the output space(i.e.the number of filters in the convolution). kernel_size:An integer or tuple/list of a single integer,specifying the length of the 1D convolution window. strides:An integer or tuple/list of a single integer, specifying the stride length of the convolution. Specifying any stride value!=1 is incompatible with specifying any`dilation_rate`value!=1. padding:One of`"valid"`or`"same"`(case-insensitive). data_format:A string,one of`channels_last`(default)or`channels_first`. The ordering of the dimensions in the inputs. `channels_last`corresponds to inputs with shape `(batch,length,channels)`while`channels_first`corresponds to inputs with shape`(batch,channels,length)`. dilation_rate:An integer or tuple/list of a single integer,specifying the dilation rate to use for dilated convolution. Currently,specifying any`dilation_rate`value!=1 is incompatible with specifying any`strides`value!=1. activation:Activation function.Set it to None to maintain a linear activation. use_bias:Boolean,whether the layer uses a bias. kernel_initializer:An initializer for the convolution kernel. bias_initializer:An initializer for the bias vector.If None,no bias will be applied. kernel_regularizer:Optional regularizer for the convolution kernel. bias_regularizer:Optional regularizer for the bias vector. activity_regularizer:Regularizer function for the output. trainable:Boolean,if`True`also add variables to the graph collection `GraphKeys.TRAINABLE_VARIABLES`(see`tf.Variable`). name:A string,the name of the layer. reuse:Boolean,whether to reuse the weights of a previous layer by the same name. Returns: Output tensor. """ layer=Conv1D( filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, dilation_rate=dilation_rate, activation=activation, use_bias=use_bias, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer, activity_regularizer=activity_regularizer, trainable=trainable, name=name, _reuse=reuse, _scope=name) return layer.apply(inputs) class Conv2D(_Conv): """2D convolution layer(e.g.spatial convolution over images). This layer creates a convolution kernel that is convolved (actually cross-correlated)with the layer input to produce a tensor of outputs.If`use_bias`is True(and a`bias_initializer`is provided), a bias vector is created and added to the outputs.Finally,if `activation`is not`None`,it is applied to the outputs as well. Arguments: filters:Integer,the dimensionality of the output space(i.e.the number of filters in the convolution). kernel_size:An integer or tuple/list of 2 integers,specifying the width and height of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions. strides:An integer or tuple/list of 2 integers, specifying the strides of the convolution along the height and width. Can be a single integer to specify the same value for all spatial dimensions. Specifying any stride value!=1 is incompatible with specifying any`dilation_rate`value!=1. padding:One of`"valid"`or`"same"`(case-insensitive). data_format:A string,one of`channels_last`(default)or`channels_first`. The ordering of the dimensions in the inputs. `channels_last`corresponds to inputs with shape `(batch,height,width,channels)`while`channels_first`corresponds to inputs with shape`(batch,channels,height,width)`. dilation_rate:An integer or tuple/list of 2 integers,specifying the dilation rate to use for dilated convolution. Can be a single integer to specify the same value for all spatial dimensions. Currently,specifying any`dilation_rate`value!=1 is incompatible with specifying any stride value!=1. activation:Activation function.Set it to None to maintain a linear activation. use_bias:Boolean,whether the layer uses a bias. kernel_initializer:An initializer for the convolution kernel. bias_initializer:An initializer for the bias vector.If None,no bias will be applied. kernel_regularizer:Optional regularizer for the convolution kernel. bias_regularizer:Optional regularizer for the bias vector. activity_regularizer:Regularizer function for the output. trainable:Boolean,if`True`also add variables to the graph collection `GraphKeys.TRAINABLE_VARIABLES`(see`tf.Variable`). name:A string,the name of the layer. """ def __init__(self,filters, kernel_size, strides=(1,1), padding='valid', data_format='channels_last', dilation_rate=(1,1), activation=None, use_bias=True, kernel_initializer=None, bias_initializer=init_ops.zeros_initializer(), kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, trainable=True, name=None, **kwargs): super(Conv2D,self).__init__( rank=2, filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, dilation_rate=dilation_rate, activation=activation, use_bias=use_bias, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer, activity_regularizer=activity_regularizer, trainable=trainable, name=name,**kwargs) def conv2d(inputs, filters, kernel_size, strides=(1,1), padding='valid', data_format='channels_last', dilation_rate=(1,1), activation=None, use_bias=True, kernel_initializer=None, bias_initializer=init_ops.zeros_initializer(), kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, trainable=True, name=None, reuse=None): """Functional interface for the 2D convolution layer. This layer creates a convolution kernel that is convolved (actually cross-correlated)with the layer input to produce a tensor of outputs.If`use_bias`is True(and a`bias_initializer`is provided), a bias vector is created and added to the outputs.Finally,if `activation`is not`None`,it is applied to the outputs as well. Arguments: inputs:Tensor input. filters:Integer,the dimensionality of the output space(i.e.the number of filters in the convolution). kernel_size:An integer or tuple/list of 2 integers,specifying the width and height of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions. strides:An integer or tuple/list of 2 integers, specifying the strides of the convolution along the height and width. Can be a single integer to specify the same value for all spatial dimensions. Specifying any stride value!=1 is incompatible with specifying any`dilation_rate`value!=1. padding:One of`"valid"`or`"same"`(case-insensitive). data_format:A string,one of`channels_last`(default)or`channels_first`. The ordering of the dimensions in the inputs. `channels_last`corresponds to inputs with shape `(batch,height,width,channels)`while`channels_first`corresponds to inputs with shape`(batch,channels,height,width)`. dilation_rate:An integer or tuple/list of 2 integers,specifying the dilation rate to use for dilated convolution. Can be a single integer to specify the same value for all spatial dimensions. Currently,specifying any`dilation_rate`value!=1 is incompatible with specifying any stride value!=1. activation:Activation function.Set it to None to maintain a linear activation. use_bias:Boolean,whether the layer uses a bias. kernel_initializer:An initializer for the convolution kernel. bias_initializer:An initializer for the bias vector.If None,no bias will be applied. kernel_regularizer:Optional regularizer for the convolution kernel. bias_regularizer:Optional regularizer for the bias vector. activity_regularizer:Regularizer function for the output. trainable:Boolean,if`True`also add variables to the graph collection `GraphKeys.TRAINABLE_VARIABLES`(see`tf.Variable`). name:A string,the name of the layer. reuse:Boolean,whether to reuse the weights of a previous layer by the same name. Returns: Output tensor. """ layer=Conv2D( filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, dilation_rate=dilation_rate, activation=activation, use_bias=use_bias, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer, activity_regularizer=activity_regularizer, trainable=trainable, name=name, _reuse=reuse, _scope=name) return layer.apply(inputs) class Conv3D(_Conv): """3D convolution layer(e.g.spatial convolution over volumes). This layer creates a convolution kernel that is convolved (actually cross-correlated)with the layer input to produce a tensor of outputs.If`use_bias`is True(and a`bias_initializer`is provided), a bias vector is created and added to the outputs.Finally,if `activation`is not`None`,it is applied to the outputs as well. Arguments: filters:Integer,the dimensionality of the output space(i.e.the number of filters in the convolution). kernel_size:An integer or tuple/list of 3 integers,specifying the depth,height and width of the 3D convolution window. Can be a single integer to specify the same value for all spatial dimensions. strides:An integer or tuple/list of 3 integers, specifying the strides of the convolution along the depth, height and width. Can be a single integer to specify the same value for all spatial dimensions. Specifying any stride value!=1 is incompatible with specifying any`dilation_rate`value!=1. padding:One of`"valid"`or`"same"`(case-insensitive). data_format:A string,one of`channels_last`(default)or`channels_first`. The ordering of the dimensions in the inputs. `channels_last`corresponds to inputs with shape `(batch,depth,height,width,channels)`while`channels_first` corresponds to inputs with shape `(batch,channels,depth,height,width)`. dilation_rate:An integer or tuple/list of 3 integers,specifying the dilation rate to use for dilated convolution. Can be a single integer to specify the same value for all spatial dimensions. Currently,specifying any`dilation_rate`value!=1 is incompatible with specifying any stride value!=1. activation:Activation function.Set it to None to maintain a linear activation. use_bias:Boolean,whether the layer uses a bias. kernel_initializer:An initializer for the convolution kernel. bias_initializer:An initializer for the bias vector.If None,no bias will be applied. kernel_regularizer:Optional regularizer for the convolution kernel. bias_regularizer:Optional regularizer for the bias vector. activity_regularizer:Regularizer function for the output. trainable:Boolean,if`True`also add variables to the graph collection `GraphKeys.TRAINABLE_VARIABLES`(see`tf.Variable`). name:A string,the name of the layer. """ def __init__(self,filters, kernel_size, strides=(1,1,1), padding='valid', data_format='channels_last', dilation_rate=(1,1,1), activation=None, use_bias=True, kernel_initializer=None, bias_initializer=init_ops.zeros_initializer(), kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, trainable=True, name=None, **kwargs): super(Conv3D,self).__init__( rank=3, filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, dilation_rate=dilation_rate, activation=activation, use_bias=use_bias, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer, activity_regularizer=activity_regularizer, trainable=trainable, name=name,**kwargs) def conv3d(inputs, filters, kernel_size, strides=(1,1,1), padding='valid', data_format='channels_last', dilation_rate=(1,1,1), activation=None, use_bias=True, kernel_initializer=None, bias_initializer=init_ops.zeros_initializer(), kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, trainable=True, name=None, reuse=None): """Functional interface for the 3D convolution layer. This layer creates a convolution kernel that is convolved (actually cross-correlated)with the layer input to produce a tensor of outputs.If`use_bias`is True(and a`bias_initializer`is provided), a bias vector is created and added to the outputs.Finally,if `activation`is not`None`,it is applied to the outputs as well. Arguments: inputs:Tensor input. filters:Integer,the dimensionality of the output space(i.e.the number of filters in the convolution). kernel_size:An integer or tuple/list of 3 integers,specifying the depth,height and width of the 3D convolution window. Can be a single integer to specify the same value for all spatial dimensions. strides:An integer or tuple/list of 3 integers, specifying the strides of the convolution along the depth, height and width. Can be a single integer to specify the same value for all spatial dimensions. Specifying any stride value!=1 is incompatible with specifying any`dilation_rate`value!=1. padding:One of`"valid"`or`"same"`(case-insensitive). data_format:A string,one of`channels_last`(default)or`channels_first`. The ordering of the dimensions in the inputs. `channels_last`corresponds to inputs with shape `(batch,depth,height,width,channels)`while`channels_first` corresponds to inputs with shape `(batch,channels,depth,height,width)`. dilation_rate:An integer or tuple/list of 3 integers,specifying the dilation rate to use for dilated convolution. Can be a single integer to specify the same value for all spatial dimensions. Currently,specifying any`dilation_rate`value!=1 is incompatible with specifying any stride value!=1. activation:Activation function.Set it to None to maintain a linear activation. use_bias:Boolean,whether the layer uses a bias. kernel_initializer:An initializer for the convolution kernel. bias_initializer:An initializer for the bias vector.If None,no bias will be applied. kernel_regularizer:Optional regularizer for the convolution kernel. bias_regularizer:Optional regularizer for the bias vector. activity_regularizer:Regularizer function for the output. trainable:Boolean,if`True`also add variables to the graph collection `GraphKeys.TRAINABLE_VARIABLES`(see`tf.Variable`). name:A string,the name of the layer. reuse:Boolean,whether to reuse the weights of a previous layer by the same name. Returns: Output tensor. """ layer=Conv3D( filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, dilation_rate=dilation_rate, activation=activation, use_bias=use_bias, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer, activity_regularizer=activity_regularizer, trainable=trainable, name=name, _reuse=reuse, _scope=name) return layer.apply(inputs) class SeparableConv2D(Conv2D): """Depthwise separable 2D convolution. This layer performs a depthwise convolution that acts separately on channels,followed by a pointwise convolution that mixes channels. If`use_bias`is True and a bias initializer is provided, it adds a bias vector to the output. It then optionally applies an activation function to produce the final output. Arguments: filters:Integer,the dimensionality of the output space(i.e.the number of filters in the convolution). kernel_size:A tuple or list of 2 integers specifying the spatial dimensions of of the filters.Can be a single integer to specify the same value for all spatial dimensions. strides:A tuple or list of 2 positive integers specifying the strides of the convolution.Can be a single integer to specify the same value for all spatial dimensions. Specifying any`stride`value!=1 is incompatible with specifying any`dilation_rate`value!=1. padding:One of`"valid"`or`"same"`(case-insensitive). data_format:A string,one of`channels_last`(default)or`channels_first`. The ordering of the dimensions in the inputs. `channels_last`corresponds to inputs with shape `(batch,height,width,channels)`while`channels_first`corresponds to inputs with shape`(batch,channels,height,width)`. dilation_rate:An integer or tuple/list of 2 integers,specifying the dilation rate to use for dilated convolution. Can be a single integer to specify the same value for all spatial dimensions. Currently,specifying any`dilation_rate`value!=1 is incompatible with specifying any stride value!=1. depth_multiplier:The number of depthwise convolution output channels for each input channel.The total number of depthwise convolution output channels will be equal to`num_filters_in*depth_multiplier`. activation:Activation function.Set it to None to maintain a linear activation. use_bias:Boolean,whether the layer uses a bias. depthwise_initializer:An initializer for the depthwise convolution kernel. pointwise_initializer:An initializer for the pointwise convolution kernel. bias_initializer:An initializer for the bias vector.If None,no bias will be applied. depthwise_regularizer:Optional regularizer for the depthwise convolution kernel. pointwise_regularizer:Optional regularizer for the pointwise convolution kernel. bias_regularizer:Optional regularizer for the bias vector. activity_regularizer:Regularizer function for the output. trainable:Boolean,if`True`also add variables to the graph collection `GraphKeys.TRAINABLE_VARIABLES`(see`tf.Variable`). name:A string,the name of the layer. """ def __init__(self,filters, kernel_size, strides=(1,1), padding='valid', data_format='channels_last', dilation_rate=(1,1), depth_multiplier=1, activation=None, use_bias=True, depthwise_initializer=None, pointwise_initializer=None, bias_initializer=init_ops.zeros_initializer(), depthwise_regularizer=None, pointwise_regularizer=None, bias_regularizer=None, activity_regularizer=None, trainable=True, name=None, **kwargs): super(SeparableConv2D,self).__init__( filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, dilation_rate=dilation_rate, activation=activation, use_bias=use_bias, bias_regularizer=bias_regularizer, activity_regularizer=activity_regularizer, trainable=trainable, name=name, **kwargs) self.depth_multiplier=depth_multiplier self.depthwise_initializer=depthwise_initializer self.pointwise_initializer=pointwise_initializer self.depthwise_regularizer=depthwise_regularizer self.pointwise_regularizer=pointwise_regularizer def build(self,input_shape): if len(input_shape)<4: raise ValueError('Inputs to`SeparableConv2D`should have rank 4.' 'Received input shape:',str(input_shape)) if self.data_format=='channels_first': channel_axis=1 else: channel_axis=3 if input_shape[channel_axis]is None: raise ValueError('The channel dimension of the inputs to' '`SeparableConv2D`' 'should be defined.Found`None`.') input_dim=int(input_shape[channel_axis]) depthwise_kernel_shape=(self.kernel_size[0], self.kernel_size[1], input_dim, self.depth_multiplier) pointwise_kernel_shape=(1,1, self.depth_multiplier*input_dim, self.filters) self.depthwise_kernel=vs.get_variable( 'depthwise_kernel', shape=depthwise_kernel_shape, initializer=self.depthwise_initializer, regularizer=self.depthwise_regularizer, trainable=True, dtype=self.dtype) self.pointwise_kernel=vs.get_variable( 'pointwise_kernel', shape=pointwise_kernel_shape, initializer=self.pointwise_initializer, regularizer=self.pointwise_regularizer, trainable=True, dtype=self.dtype) if self.use_bias: self.bias=vs.get_variable('bias', shape=(self.filters,), initializer=self.bias_initializer, regularizer=self.bias_regularizer, trainable=True, dtype=self.dtype) else: self.bias=None def call(self,inputs): if self.data_format=='channels_first': #Reshape to channels last inputs=array_ops.transpose(inputs,(0,2,3,1)) #Apply the actual ops. outputs=nn.separable_conv2d( inputs, self.depthwise_kernel, self.pointwise_kernel, strides=(1,)+self.strides+(1,), padding=self.padding.upper(), rate=self.dilation_rate) if self.data_format=='channels_first': #Reshape to channels first outputs=array_ops.transpose(outputs,(0,3,1,2)) if self.bias is not None: outputs=nn.bias_add( outputs, self.bias, data_format=utils.convert_data_format(self.data_format,ndim=4)) if self.activation is not None: return self.activation(outputs) return outputs def separable_conv2d(inputs, filters, kernel_size, strides=(1,1), padding='valid', data_format='channels_last', dilation_rate=(1,1), depth_multiplier=1, activation=None, use_bias=True, depthwise_initializer=None, pointwise_initializer=None, bias_initializer=init_ops.zeros_initializer(), depthwise_regularizer=None, pointwise_regularizer=None, bias_regularizer=None, activity_regularizer=None, trainable=True, name=None, reuse=None): """Functional interface for the depthwise separable 2D convolution layer. This layer performs a depthwise convolution that acts separately on channels,followed by a pointwise convolution that mixes channels. If`use_bias`is True and a bias initializer is provided, it adds a bias vector to the output. It then optionally applies an activation function to produce the final output. Arguments: inputs:Input tensor. filters:Integer,the dimensionality of the output space(i.e.the number of filters in the convolution). kernel_size:A tuple or list of 2 integers specifying the spatial dimensions of of the filters.Can be a single integer to specify the same value for all spatial dimensions. strides:A tuple or list of 2 positive integers specifying the strides of the convolution.Can be a single integer to specify the same value for all spatial dimensions. Specifying any`stride`value!=1 is incompatible with specifying any`dilation_rate`value!=1. padding:One of`"valid"`or`"same"`(case-insensitive). data_format:A string,one of`channels_last`(default)or`channels_first`. The ordering of the dimensions in the inputs. `channels_last`corresponds to inputs with shape `(batch,height,width,channels)`while`channels_first`corresponds to inputs with shape`(batch,channels,height,width)`. dilation_rate:An integer or tuple/list of 2 integers,specifying the dilation rate to use for dilated convolution. Can be a single integer to specify the same value for all spatial dimensions. Currently,specifying any`dilation_rate`value!=1 is incompatible with specifying any stride value!=1. depth_multiplier:The number of depthwise convolution output channels for each input channel.The total number of depthwise convolution output channels will be equal to`num_filters_in*depth_multiplier`. activation:Activation function.Set it to None to maintain a linear activation. use_bias:Boolean,whether the layer uses a bias. depthwise_initializer:An initializer for the depthwise convolution kernel. pointwise_initializer:An initializer for the pointwise convolution kernel. bias_initializer:An initializer for the bias vector.If None,no bias will be applied. depthwise_regularizer:Optional regularizer for the depthwise convolution kernel. pointwise_regularizer:Optional regularizer for the pointwise convolution kernel. bias_regularizer:Optional regularizer for the bias vector. activity_regularizer:Regularizer function for the output. trainable:Boolean,if`True`also add variables to the graph collection `GraphKeys.TRAINABLE_VARIABLES`(see`tf.Variable`). name:A string,the name of the layer. reuse:Boolean,whether to reuse the weights of a previous layer by the same name. Returns: Output tensor. """ layer=SeparableConv2D( filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, dilation_rate=dilation_rate, depth_multiplier=depth_multiplier, activation=activation, use_bias=use_bias, depthwise_initializer=depthwise_initializer, pointwise_initializer=pointwise_initializer, bias_initializer=bias_initializer, depthwise_regularizer=depthwise_regularizer, pointwise_regularizer=pointwise_regularizer, bias_regularizer=bias_regularizer, activity_regularizer=activity_regularizer, trainable=trainable, name=name, _reuse=reuse, _scope=name) return layer.apply(inputs) class Conv2DTranspose(Conv2D): """Transposed convolution layer(sometimes called Deconvolution). The need for transposed convolutions generally arises from the desire to use a transformation going in the opposite direction of a normal convolution,i.e.,from something that has the shape of the output of some convolution to something that has the shape of its input while maintaining a connectivity pattern that is compatible with said convolution. Arguments: filters:Integer,the dimensionality of the output space(i.e.the number of filters in the convolution). kernel_size:A tuple or list of 2 positive integers specifying the spatial dimensions of of the filters.Can be a single integer to specify the same value for all spatial dimensions. strides:A tuple or list of 2 positive integers specifying the strides of the convolution.Can be a single integer to specify the same value for all spatial dimensions. padding:one of`"valid"`or`"same"`(case-insensitive). data_format:A string,one of`channels_last`(default)or`channels_first`. The ordering of the dimensions in the inputs. `channels_last`corresponds to inputs with shape `(batch,height,width,channels)`while`channels_first`corresponds to inputs with&
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/128751.html
摘要:目前,是成長最快的一種深度學習框架。這將是對社區發展的一個巨大的推動作用。以下代碼是如何開始導入和構建序列模型。現在,我們來構建一個簡單的線性回歸模型。 作者:chen_h微信號 & QQ:862251340微信公眾號:coderpai簡書地址:https://www.jianshu.com/p/205... Keras 是提供一些高可用的 Python API ,能幫助你快速的構建...
摘要:貢獻者飛龍版本最近總是有人問我,把這些資料看完一遍要用多長時間,如果你一本書一本書看的話,的確要用很長時間。為了方便大家,我就把每本書的章節拆開,再按照知識點合并,手動整理了這個知識樹。 Special Sponsors showImg(https://segmentfault.com/img/remote/1460000018907426?w=1760&h=200); 貢獻者:飛龍版...
本文主要是給大家介紹了python深度神經網絡tensorflow1.0主要參數和svm算法,感興趣的小伙伴可以參考借鑒一下,希望可以有一定的幫助,祝愿大家盡可能不斷進步,盡早漲薪 tf.trainable_variables()獲取練習主要參數 在tf中,參加鍛煉的主要參數可用tf.trainable_variables()分離出來,如: #取出所有參與訓練的參數 params=t...
摘要:在第輪的時候,竟然跑出了的正確率綜上,借助和機器學習工具,我們只有幾十行代碼,就解決了手寫識別這樣級別的問題,而且準確度可以達到如此程度。 摘要: Tensorflow入門教程1 去年買了幾本講tensorflow的書,結果今年看的時候發現有些樣例代碼所用的API已經過時了。看來自己維護一個保持更新的Tensorflow的教程還是有意義的。這是寫這一系列的初心。快餐教程系列希望能夠盡可...
閱讀 911·2023-01-14 11:38
閱讀 878·2023-01-14 11:04
閱讀 740·2023-01-14 10:48
閱讀 1982·2023-01-14 10:34
閱讀 942·2023-01-14 10:24
閱讀 819·2023-01-14 10:18
閱讀 499·2023-01-14 10:09
閱讀 572·2023-01-14 10:02