RBF_neural_network_python icon indicating copy to clipboard operation
RBF_neural_network_python copied to clipboard

RBF code

Open mearcla opened this issue 3 years ago • 5 comments

Could you help me to understand why you used here: rbflayer = RBFLayer(34, initializer=InitCentersKMeans(X_train), betas=3.0,input_shape=(568,)):

34 as output_dim ? I am trying to use this code on my dataset but I have problem. any help please?

mearcla avatar Aug 12 '22 11:08 mearcla

Hello, thank your for reaching out yes the first parameters is your output_dim for my case its 34 i forgot to change it in the readme, tell me where did you find a problem? im happy to help.

raaaouf avatar Aug 12 '22 12:08 raaaouf

Thank you so much for your reply, I am trying to adapt your code on my dataset, my dataset it look like this (consist of 7 columns and 1000 rows)

            Class
0.25 0 0 0 0 0.25 1
0.625 0 0 0 0 0.625 4
1 0 0 0 0 1 2
0.5 0 0 0 0 0.5 1
0.75 0 0 0 0 0.75 1
0.375 0 0 0 0 0.375 3
1 0 0 0 0 1 2
1 0 0 0 0 1 2
1 0 0 0 0 1 2

So I updated on your code, I used

X = data.iloc[:, 0:6].values
y = data.iloc[:, 6].values
and reshape(-1, 1)
ohe = OneHotEncoder()
y = (ohe.fit_transform(y.reshape(-1, 1)).toarray())

instead of :

###X=data.iloc[2:570,:].values
###y = data.iloc[0:1,:].values

and then commented this

###X=np.transpose(X)
###y=np.transpose(y)

My problem I can not run the code in correct way yet, I got on this error: self.centers = self.add_weight(name='centers', File "c:\Users\RBF_neural_network_python-master\RBF_neuralNetwork .py", line 118, in call assert shape[1] == self.X.shape[1] AssertionError

Please i need your appreciated help.

mearcla avatar Aug 12 '22 12:08 mearcla

This is all the code:

from keras import backend as K

###from keras.engine.topology import Layer original

from keras.layers import Layer

from keras.initializers import RandomUniform, Initializer, Constant
import numpy as np


class InitCentersRandom(Initializer):
    """ Initializer for initialization of centers of RBF network
        as random samples from the given data set.
    # Arguments
        X: matrix, dataset to choose the centers from (random rows
          are taken as centers)
    """

    def __init__(self, X):
        self.X = X

    def __call__(self, shape, dtype=None):
        assert shape[1] == self.X.shape[1]
        idx = np.random.randint(self.X.shape[0], size=shape[0])
        return self.X[idx, :]


class RBFLayer(Layer):
    """ Layer of Gaussian RBF units.
    # Example
    ```python
        model = Sequential()
        model.add(RBFLayer(10,
                           initializer=InitCentersRandom(X),
                           betas=1.0,
                           input_shape=(1,)))
        model.add(Dense(1))
    ```
    # Arguments
        output_dim: number of hidden units (i.e. number of outputs of the
                    layer)
        initializer: instance of initiliazer to initialize centers
        betas: float, initial value for betas
    """

    def __init__(self, output_dim, initializer=None, betas=1.0, **kwargs):
        self.output_dim = output_dim
        self.init_betas = betas
        if not initializer:
            self.initializer = RandomUniform(0.0, 1.0)
        else:
            self.initializer = initializer
        super(RBFLayer, self).__init__(**kwargs)

    def build(self, input_shape):

        self.centers = self.add_weight(name='centers',
                                       shape=(self.output_dim, input_shape[1]),
                                       initializer=self.initializer,
                                       trainable=True)
        self.betas = self.add_weight(name='betas',
                                     shape=(self.output_dim,),
                                     initializer=Constant(
                                         value=self.init_betas),
                                     # initializer='ones',
                                     trainable=True)

        super(RBFLayer, self).build(input_shape)

    def call(self, x):

        C = K.expand_dims(self.centers)
        H = K.transpose(C-K.transpose(x))
        return K.exp(-self.betas * K.sum(H**2, axis=1))

        # C = self.centers[np.newaxis, :, :]
        # X = x[:, np.newaxis, :]

        # diffnorm = K.sum((C-X)**2, axis=-1)
        # ret = K.exp( - self.betas * diffnorm)
        # return ret

    def compute_output_shape(self, input_shape):
        return (input_shape[0], self.output_dim)

    def get_config(self):
        # have to define get_config to be able to use model_from_json
        config = {
            'output_dim': self.output_dim
        }
        base_config = super(RBFLayer, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))

from keras.initializers import Initializer
from sklearn.cluster import KMeans


class InitCentersKMeans(Initializer):
    """ Initializer for initialization of centers of RBF network
        by clustering the given data set.
    # Arguments
        X: matrix, dataset
    """

    def __init__(self, X, max_iter=100):
        self.X = X
        self.max_iter = max_iter

    def __call__(self, shape, dtype=None):
        assert shape[1] == self.X.shape[1]

        n_centers = shape[0]
        km = KMeans(n_clusters=n_centers, max_iter=self.max_iter, verbose=0)
        km.fit(self.X)
        return km.cluster_centers_

# Commented out IPython magic to ensure Python compatibility.
import numpy as np, pandas as pd
from keras.models import Sequential 
from keras.layers.core import Dense
from keras.layers import Activation
from keras.optimizers import RMSprop


import matplotlib.pyplot as plt

data = pd.read_csv('C:/Users/RBF_neural_network_python-master/train3.csv',header=None)
data.head(10) #Return 10 rows of data

datatrans=np.transpose(data)
print(datatrans[0].value_counts())
datatrans[0].value_counts()[:].plot(kind='bar', alpha=0.5)
plt.xlabel('\n Figure 1: Répartition selon classes \n', fontsize='17', horizontalalignment='center')
plt.tick_params(axis='x',  direction='out', length=10, width=3)

plt.show() #2300

#data spliting
###X=data.iloc[2:570,:].values
###y = data.iloc[0:1,:].values

X = data.iloc[:, 0:6].values
y = data.iloc[:, 6].values

#data rotation
###X=np.transpose(X)
###y=np.transpose(y)
print('rotation ')
###print(X)
###print(y)
#standarizing
from sklearn.preprocessing import MinMaxScaler
X = MinMaxScaler().fit_transform(X)

from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder()
y = (ohe.fit_transform(y.reshape(-1, 1)).toarray())
print('resulats de scalling')
print(X,y)


from sklearn.model_selection import train_test_split
from keras.optimizers import SGD
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2,random_state=0)#80% train et 20% test

model = Sequential()
rbflayer = RBFLayer(34,
                        initializer=InitCentersKMeans(X_train),
                        betas=3.0,
                        input_shape=(568,))
model.add(rbflayer)
model.add(Dense(4))
model.add(Activation('linear'))
model.compile(loss='mean_squared_error',
                  optimizer=RMSprop(), metrics=['accuracy'])

print(model.summary())

history1 = model.fit(X_train, y_train, epochs=1000, batch_size=32)

import matplotlib.pyplot as plt
plt.plot(history1.history['accuracy'])
plt.plot(history1.history['loss'])
plt.title('train accuracy and loss')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['accuracy', 'loss'], loc='upper left')

plt.show()

# saving to and loading from file
z_model = "C:/Users/RBF_neural_network_python-master/my_file.h5"
print("Save model to file {} ... ".format(z_model), end="")
model.save(z_model)
print("OK")

#model already saved in file
from tensorflow.python.keras.models import  load_model
newmodel1= load_model("C:/Users/RBF_neural_network_python-master/my_file.h5",custom_objects={'RBFLayer': RBFLayer})
print("OK")

# Evaluate the model on the test data using `evaluate`
print("Evaluate on test data")
results = newmodel1.evaluate(X_test, y_test, batch_size=32)
print("test loss:", results[0])
print("test accuracy:",results[1]*100,'%')

# y_pred = newmodel1.predict(X_test)
# #Converting predictions to label
# pred = list()
# for i in range(len(y_pred)):
#     pred.append(np.argmax(y_pred[i]))
# #Converting one hot encoded test label to label
# test = list()
# for i in range(len(y_test)):
#     test.append(np.argmax(y_test[i]))

# from sklearn.metrics import accuracy_score
# a = accuracy_score(pred,test)
# print('Test Accuracy is:', a*100)

mearcla avatar Aug 12 '22 13:08 mearcla

There is no answer!! :(

mearcla avatar Aug 13 '22 08:08 mearcla

I am also having the same issue :( Did you end up fixing it in the end?

cvol9999 avatar Oct 30 '23 03:10 cvol9999