Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 向Keras中的输出层添加新节点_Python 3.x_Neural Network_Deep Learning_Keras_Keras Layer - Fatal编程技术网

Python 3.x 向Keras中的输出层添加新节点

Python 3.x 向Keras中的输出层添加新节点,python-3.x,neural-network,deep-learning,keras,keras-layer,Python 3.x,Neural Network,Deep Learning,Keras,Keras Layer,我想向输出层添加新节点,以便稍后对其进行训练,我正在执行以下操作: def add_outputs(self, n_new_outputs): out = self.model.get_layer('fc8').output last_layer = self.model.get_layer('fc7').output out2 = Dense(n_new_outputs, activation='softmax', name='fc9')(last_layer)

我想向输出层添加新节点,以便稍后对其进行训练,我正在执行以下操作:

def add_outputs(self, n_new_outputs):
    out = self.model.get_layer('fc8').output
    last_layer = self.model.get_layer('fc7').output
    out2 = Dense(n_new_outputs, activation='softmax', name='fc9')(last_layer)
    output = merge([out, out2], mode='concat')
    self.model = Model(input=self.model.input, output=output)
其中,
'fc7'
是输出层
'fc8'
之前的完全连接层。我只希望有最后一层
out=self.model.get_layer('fc8')。output
,但输出都是模型。 有没有办法只从网络中获取一层? 也许还有其他更简单的方法


谢谢

最后我找到了一个解决方案:

1) 从最后一层获取权重

2) 将零添加到权重并随机初始化其连接

3) 弹出输出层并创建一个新层

4) 为新层设置新权重

代码如下:

 def add_outputs(self, n_new_outputs):
        #Increment the number of outputs
        self.n_outputs += n_new_outputs
        weights = self.model.get_layer('fc8').get_weights()
        #Adding new weights, weights will be 0 and the connections random
        shape = weights[0].shape[0]
        weights[1] = np.concatenate((weights[1], np.zeros(n_new_outputs)), axis=0)
        weights[0] = np.concatenate((weights[0], -0.0001 * np.random.random_sample((shape, n_new_outputs)) + 0.0001), axis=1)
        #Deleting the old output layer
        self.model.layers.pop()
        last_layer = self.model.get_layer('batchnormalization_1').output
        #New output layer
        out = Dense(self.n_outputs, activation='softmax', name='fc8')(last_layer)
        self.model = Model(input=self.model.input, output=out)
        #set weights to the layer
        self.model.get_layer('fc8').set_weights(weights)
        print(weights[0])