def _OLSReg(self, ModifiedWeights):
        """
        :param : 
        :return: Keras Models, objective function value of state
        """
        UnflattenedWeights = self._UnflattenWeights(
            WeightsStrucure=self.WeightsStrucure,
            ModifiedWeights=ModifiedWeights)

        #OLS Regression
        #obtain the output of an intermediate layer
        #https://keras.io/getting-started/faq/?fbclid=IwAR3Zv35V-vmEy85anudOrlxCExXYwyG6cRL1UR0AaLPU6sZEoBjsbX-8LXQ#how-can-i-obtain-the-output-of-an-intermediate-layer
        self.KerasModels.set_weights(UnflattenedWeights)
        layer_name = 'IntermediateLayer'
        intermediate_layer_model = keras_models_Model(
            inputs=self.KerasModels.input,
            outputs=self.KerasModels.get_layer(layer_name).output)
        intermediate_output = intermediate_layer_model.predict(self.X_train)

        #fit LM
        lm = LogisticRegression(random_state=0, solver='liblinear').fit(
            intermediate_output, self.y_train)

        #lm =  LinearRegression().fit(intermediate_output, self.y_train)
        # 印出係數, 截距 print(lm.coef_, lm.intercept_)

        #score
        #score = log_loss(y_pred = lm.predict(intermediate_output), y_true= self.y_train)

        #get OutLayerWeights
        OutLayerWeights = [
            np.array(lm.coef_).reshape(self.WeightsStrucure[-2]),
            np.array(lm.intercept_).reshape(self.WeightsStrucure[-1])
        ]

        #update ES-optimized weights
        UnflattenedWeights[-2:] = OutLayerWeights

        #self.KerasModels.set_weights(UnflattenedWeights)
        #test_on_batch = self.KerasModels.test_on_batch(self.X_train, self.y_train, sample_weight=None) # return ['loss', 'acc']

        #print( 'score',score, 'test_on_batch',test_on_batch)
        _, OLS_Optimized_Weight = self._FlattenWeights(UnflattenedWeights)
        return OLS_Optimized_Weight
Exemple #2
0
MyES = ES(model, InitialSigma = 0.1, ParentsSize = 15, ChildSize = 100, tao = 0.5)   
ES_Optimized_Weights, ES_Optimized_ObjVal  = MyES.run(weights, max_steps=5, verbose = 1)
#%% run2
# gradient-based optimize
model.set_weights(ES_Optimized_Weights)
model.fit(X_train, y_train, epochs=5, batch_size=32)
weights = model.get_weights() 

# ES
ES_Optimized_Weights, ES_Optimized_ObjVal  = MyES.run(weights, max_steps=10, verbose = 1)

#%% obtain the output of an intermediate layer
#https://keras.io/getting-started/faq/?fbclid=IwAR3Zv35V-vmEy85anudOrlxCExXYwyG6cRL1UR0AaLPU6sZEoBjsbX-8LXQ#how-can-i-obtain-the-output-of-an-intermediate-layer

layer_name = 'IntermediateLayer'
intermediate_layer_model = keras_models_Model(inputs=model.input,
                                 outputs=model.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(data_x)

"""
How can I obtain the output of an intermediate layer?
One simple way is to create a new Model that will output the layers that you are interested in:

from keras.models import Model

model = ...  # create the original model

layer_name = 'my_layer'
intermediate_layer_model = Model(inputs=model.input,
                                 outputs=model.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(data)
Alternatively, you can build a Keras function that will return the output of a certain layer given a certain input, for example: