PetraVidnerova/rbf_keras

Is there a working example of RBFLayer with InitCentersKMeans activation?

agcala opened this issue · 6 comments

I am having a hard time making this code to work

ds=pd.read_csv(Drv+'validation.csv')

X=ds.values

X=X/255  # X is a 28 x 435 test matrix
rbflayer = RBFLayer(56,
                initializer=InitCentersKMeans(X), # As a test I was using n-centers=2
                betas=0.5)
 
print( rbflayer.get_config() )
print( rbflayer.compute_output_shape((inputdim,)) )

sgd=SGD(lr=0.01, momentum=0.9, decay=0.0, nesterov=False)

model = Sequential()
model.add(InputLayer(input_shape=(inputdim,)))  # Had to use an InputLayer cause RBFLayer would not accept input_shape as input parameter
model.add(rbflayer)                             # Here is where the code chokes
model.add(Dense(dim2,activation='sigmoid'))
model.summary()
model.compile(loss = 'binary_crossentropy',optimizer=sgd,metrics=['binary_accuracy'])

Output:

{'name': 'rbf_layer_1', 'trainable': True, 'dtype': 'float32', 'output_dim': 56}
(435, 56)

Traceback (most recent call last):
  File "programs/Keras.RBF.py", line 75, in <module>
    model.add(rbflayer)
..................................
File "J:\Anaconda\envs\gpu\lib\site-packages\keras\initializers.py", line 523,
 in get  str(identifier))
ValueError: Could not interpret initializer identifier: 
[[ 7.76108189e-01  0.00000000e+00  2.93012772e-02  1.50262960e-02
  2.40420736e-02  3.00525920e-02  2.40420736e-02  1.65289256e-02
  1.05184072e-02  6.01051841e-03  2.25394440e-03  7.51314801e-04
  1.50262960e-03  6.61363325e-18  3.30681663e-18  3.73403456e-01
  1.59075441e-01  5.18893357e-02  5.54262312e-04  2.57916354e-01
  2.45347004e-01  2.20633167e-01  2.56599048e-01  3.06607151e-01
  3.09099748e-01  2.67526996e-02  1.90509863e-02  1.86738557e-02]
  [1.81700329e-02  4.80841473e-02  0.00000000e+00  5.70999249e-02
   4.58302029e-02  4.43275733e-02  5.33433509e-02  4.58302029e-02
   3.60631104e-02  1.20210368e-02  1.20210368e-02  3.75657400e-03
   3.00525920e-03  1.40946282e-17 -1.38777878e-17  7.42678488e-18
   3.90683696e-01  1.58418409e-01  4.93086431e-02  5.47026709e-04
   2.59538015e-01  2.46463664e-01  2.20892444e-01  2.55650329e-01
   3.08301292e-01  3.11374317e-01  2.64197640e-02  1.88565283e-02]]

Please.

This works for me:

import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense
from keras.optimizers import RMSprop
from rbflayer import RBFLayer
from kmeans_initializer import InitCentersKMeans
import matplotlib.pyplot as plt


def load_data():

    data = np.loadtxt("data/data.txt")
    X = data[:, :-1]
    y = data[:, -1:]
    return X, y


if __name__ == "__main__":

    X, y = load_data()

    model = Sequential()
    rbflayer = RBFLayer(10,
                        initializer=InitCentersKMeans(X),
                        betas=2.0,
                        input_shape=(1,))
    model.add(rbflayer)
    model.add(Dense(1))

    model.compile(loss='mean_squared_error',
                  optimizer=RMSprop())

    model.fit(X, y,
              batch_size=50,
              epochs=2000,
              verbose=1)

Could you please send me the whole code?

Thank you so much for your quick reply.

Did you check the code I posted? That's the code I am having trouble with. There is no more code other than the import statements.

Anyway I tried to run the code you say "This work for me" but it doesn't work for me either:

File "J:\Programs\untitled0.py", line 33, in <module>
   input_shape=(1,))
File "J:\Anaconda\envs\gpu\lib\site-packages\keras\engine\base_layer.py", line 147, in __init__
raise TypeError('Keyword argument not understood:', kwarg)

TypeError: ('Keyword argument not understood:', 'initializer')

I am using Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)]
Again thank you very much.

well, I checked it with python3.6 and it seems that the problem is in the version of Keras
do you know which version you are using?

you can also check the repo rbf_for_tf2 that is the same RBFLayer but should work with tensorflow 2

Version is Keras 2.3.1
Tensorflow 2.0.0
Tensorflow-gpu 2.0.0

please look at
https://github.com/PetraVidnerova/rbf_for_tf2
there are fixes for Tensorflow 2.0

Looks like it is working!
Thank you so much.