6

I am trying to introduce keras.initializers into my net, following this link:

import keras
from keras.optimizers import RMSprop, Adam
from keras.layers import Input, Embedding, LSTM, Dense, merge, Activation
from keras.models import Model, Sequential

model = Sequential()
model.add(Dense(100, init='lecun_uniform', input_shape=(6,)))
model.add(Activation('relu'))
model.add(Dense(27, init='lecun_uniform'))
model.add(Activation('linear'))

rms = RMSprop(lr = 0.01)
keras.initializers.RandomUniform(minval=-0.05, maxval=0.05, seed=None)
model.compile(loss='mse', optimizer=rms)

And it fails with the following error:

keras.initializers.RandomUniform(minval=-0.05, maxval=0.05, seed=None)

AttributeError: module 'keras' has no attribute 'initializers'

Any ideas as to why it happens?

Massyanya
  • 2,844
  • 8
  • 28
  • 37
  • Try either `import keras.initializers` or `from keras.initializers import RandomUniform`. – Marcin Możejko Apr 06 '17 at 15:13
  • Which version of keras are you using? – Nassim Ben Apr 06 '17 at 15:17
  • When I do `import keras.initializers` I get `ImportError: No module named 'keras.initializers'` error. – Massyanya Apr 06 '17 at 22:09
  • How do I figure out my version of keras if `python -c "import keras; print keras.__version__"` doesn't work? – Massyanya Apr 06 '17 at 22:16
  • Any luck? I'm also stuck with this...! – Karnivaurus Apr 12 '17 at 16:52
  • No good solution yet, I am afraid. I semi-solved the problem of weight initialisation with `model.get_weights()` and `model.set_weights()` but it's not a proper solution unfortunately. – Massyanya Apr 18 '17 at 17:17
  • If keras.__version__ doesn't work, check out waterproof's answer here: https://stackoverflow.com/questions/20180543/how-to-check-version-of-python-modules Change their line: `if pkg.key in ['setuptools', 'statlib', 'construct']` To `if pkg.key in ['keras']` – StatsSorceress May 24 '17 at 17:58

1 Answers1

3

You have to check version of Keras being used. Probable mistake is that you have 1.x.x and trying to use initializers from Keras 2.x.x

Maciej Osowski
  • 115
  • 1
  • 2
  • 7