```

2020-07-13  本文已影响0人  __method__
import numpy as np
class CNN_layer():
    def init(self,cores,pedding_number=1,strided_number=1):#输入的cores为列表
        self.input_number = None
        self.output_number = len(cores)
        self.core = cores
        self.core[:][:, 0] = -1
        self.core[:][:, 2] = 1
        self.pedding = pedding_number
        self.strided = strided_number
        self.output =None
    def fit(self,input):#input是三维数组
        self.input_number = input.shape[0]
        ilength=input.shape[2]
        iwidth=input.shape[1]
        clength=self.core[0].shape[2]
        output = np.ones((self.output_number, int((iwidth - clength) / self.strided + 1),
                           int((ilength - clength) / self.strided + 1)))
        for i in range(len(self.core)):#对于每一层
            for j in range(int((iwidth - clength) / self.strided + 1)):#对于每一列
                for k in range(int((ilength - clength) / self.strided + 1)):#对于每一行
                    cnn_result=input[:,:j*self.strided,:k*self.strided]*self.core[i]
                    cnn_sum=0
                    for layer in range (cnn_result.shape[0]):#求和以下
                        for width in range(cnn_result.shape[1]):
                            for length in range(cnn_result.shape[2]):
                                cnn_sum+=cnn_result[layer,width,length]
                    output[i][j][k]=cnn_sum
        self.output=output
        return self



import numpy as np
class Pooling():
def init(self,cores_size,pedding_number=1,strided_number=1):#输入的cores_size为池化核大小
self.input_number = None
self.core = cores_size
self.pedding = pedding_number
self.strided = strided_number
self.output =None
def fit_max(self,input):#input是三维数组
self.input_number = input.shape[0]#输入的层数
ilength=input.shape[2]
iwidth=input.shape[1]
clength=self.core[0].shape[2]
output = np.ones((self.input_number, int((iwidth - clength) / self.strided + 1),
int((ilength - clength) / self.strided + 1)))
for i in range(self.input_number):#对于每一层
for j in range(int((iwidth - clength) / self.strided + 1)):#对于每一列
for k in range(int((ilength - clength) / self.strided + 1)):#对于每一行
output[i][j][k]=np.max(input[i,:jself.strided,:kself.strided])
self.output=output
return self
def fit_mean(self,input):#input是三维数组
self.input_number = input.shape[0]#输入的层数
ilength=input.shape[2]
iwidth=input.shape[1]
clength=self.core[0].shape[2]
output = np.ones((self.input_number, int((iwidth - clength) / self.strided + 1),
int((ilength - clength) / self.strided + 1)))
for i in range(self.input_number):#对于每一层
for j in range(int((iwidth - clength) / self.strided + 1)):#对于每一列
for k in range(int((ilength - clength) / self.strided + 1)):#对于每一行
output[i][j][k]=np.mean(input[i,:jself.strided,:kself.strided])
self.output=output
return self


上一篇下一篇

猜你喜欢

热点阅读