Skip to content

Instantly share code, notes, and snippets.

@jef5ez
Last active May 18, 2017 17:23
Show Gist options
  • Save jef5ez/f17f3d98fb2c52e8b336b074fe0c55ce to your computer and use it in GitHub Desktop.
Save jef5ez/f17f3d98fb2c52e8b336b074fe0c55ce to your computer and use it in GitHub Desktop.
keras th ordering
val model = KerasModelImport.importKerasSequentialModelAndWeights(
"/opt/devel/src/hannibal/hannibal-python/ecoa/keras4j.json",
"/opt/devel/src/hannibal/hannibal-python/ecoa/keras4j.h5")
val (trackLength, vectorDim, groupSize) = (30, 2, 8)
val o = Array.fill(trackLength * vectorDim * groupSize)(1.0)
val ones = Nd4j.create(o, Array(1, groupSize, vectorDim, trackLength))
val oneOut = model.output(ones)
// [0.41, 0.40, 0.19]
In dl4j:
scala> println(res8.getRow(0))
[[[-0.01, -0.06, 0.01, 0.07, -0.01, 0.09, 0.08, 0.05, -0.02, -0.00],
[-0.07, 0.05, 0.02, -0.07, 0.07, 0.00, 0.01, 0.07, -0.03, 0.08]],
[[0.05, 0.07, -0.00, 0.01, 0.01, -0.04, -0.04, 0.08, 0.07, -0.07],
[-0.03, -0.01, 0.07, 0.08, -0.01, 0.04, -0.00, 0.02, -0.05, 0.05]],
[[0.01, 0.02, -0.05, 0.02, -0.07, -0.07, -0.06, -0.00, 0.03, 0.05],
[-0.06, 0.06, -0.00, 0.02, -0.04, -0.00, 0.07, -0.03, -0.03, -0.06]],
[[0.08, -0.08, -0.08, -0.01, -0.02, 0.02, 0.01, 0.02, 0.00, -0.06],
[0.08, -0.03, -0.08, -0.03, 0.08, 0.02, 0.02, 0.04, 0.07, -0.02]],
[[0.06, -0.07, 0.04, 0.01, 0.06, -0.05, 0.07, 0.06, 0.02, -0.05],
[-0.01, 0.03, -0.02, 0.05, -0.02, -0.05, -0.07, 0.03, 0.08, -0.05]],
[[-0.02, 0.07, 0.05, 0.04, -0.08, -0.05, -0.03, 0.02, -0.04, -0.08],
[-0.03, -0.02, 0.01, -0.03, 0.03, -0.01, 0.05, 0.03, 0.00, -0.05]],
[[-0.07, 0.01, -0.06, -0.05, -0.00, 0.04, -0.02, -0.03, -0.07, 0.01],
[-0.04, 0.07, -0.04, 0.06, 0.08, -0.03, -0.04, -0.02, 0.04, -0.02]],
[[0.08, 0.04, -0.08, -0.04, 0.03, 0.07, 0.07, -0.04, -0.03, 0.07],
[-0.05, -0.01, 0.04, -0.08, 0.06, 0.07, -0.02, -0.02, -0.05, -0.01]]]
In Python:
print model.get_weights()[0][0]
[[[ 0.07676533 -0.03117377 0.06891505 0.00826494 0.00290956 0.07187851
-0.07164567 0.0169886 0.05354632 -0.06551674]
[-0.00348628 -0.01874308 0.05135714 0.08106387 0.08658282 -0.0125316
0.07233612 0.00676258 -0.0583616 -0.00871787]]
[[ 0.05433306 -0.05441435 0.01570591 -0.00498693 0.04137021 -0.00683805
0.08082487 0.07156506 -0.00727309 -0.03086448]
[-0.07242365 0.06931256 0.07738896 -0.03929505 -0.03519952 0.01477922
0.00650515 -0.00138164 0.06599145 0.0548986 ]]
[[-0.06366576 -0.02515704 -0.03476964 0.0700172 -0.00052914 -0.03933529
0.02269914 -0.00237731 0.06332166 -0.05861359]
[ 0.05349948 0.02544831 -0.00453325 -0.05656359 -0.06786122 -0.07119821
0.02357902 -0.04714607 0.01953493 0.00579332]]
[[-0.01521546 0.07284176 0.04143168 0.02168714 0.02437495 0.08211306
-0.02770139 -0.08141261 -0.02879204 0.07727191]
[-0.06016762 0.00331838 0.01770175 0.01325131 0.01809193 -0.02122333
-0.01375933 -0.07907184 -0.07925276 0.08340394]]
[[-0.04790336 0.08006929 0.03018796 -0.07247118 -0.04933546 -0.02051881
0.04873452 -0.01753233 0.03327365 -0.01034204]
[-0.04579724 0.01649789 0.05555908 0.0725324 -0.04999334 0.06409916
0.01181215 0.04371393 -0.06629309 0.05552076]]
[[-0.05160485 0.00228648 0.02856116 0.04772033 -0.01463272 0.02620524
-0.02530901 0.01186486 -0.01923612 -0.03276508]
[-0.07548475 -0.04069661 0.02406996 -0.0337285 -0.05363468 -0.07712238
0.03820712 0.04714371 0.06748833 -0.02 ]]
[[-0.01733848 0.03871317 -0.01713949 -0.03550589 -0.02539484 0.08202767
0.06316736 -0.03533741 0.06995845 -0.04261048]
[ 0.00951921 -0.06653412 -0.03266657 -0.01623273 0.03671551 -0.00367464
-0.04966186 -0.05797498 0.00909566 -0.06771586]]
[[-0.00626809 -0.05195602 -0.01752971 -0.02099497 0.0686373 0.06118456
-0.07832465 0.03741774 -0.01404193 -0.04903556]
[ 0.06766865 -0.02603962 -0.03748363 0.07432315 0.06585176 0.03124297
-0.03805119 -0.0758684 0.04481697 0.08070454]]]
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, Convolution3D
from keras.optimizers import RMSprop, Adadelta
groupSize = 8
vectorDim = 2
trackLength = 30
model = Sequential()
model.add(Convolution2D(32, 2, 10, border_mode='same', dim_ordering='th', input_shape=(groupSize, vectorDim, trackLength)))
model.add(Activation('relu'))
model.add(Convolution2D(16, 2, 5, border_mode='same', dim_ordering='th'))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(128))
model.add(Dense(3))
model.add(Activation('softmax'))
optim = RMSprop()
# optim = Adadelta()
model.compile(loss='categorical_crossentropy', optimizer=optim)
model.predict_proba(np.ones((1, groupSize, vectorDim, trackLength)))
#array([[ 0.45079258, 0.24883677, 0.30037063]], dtype=float32)
model_json = model.to_json()
with open("keras4j.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("keras4j.h5")
print("Saved model to disk")
In dl4j:
Map(W -> [[0.14, 0.12, -0.16],
[-0.04, 0.15, 0.02],
[-0.16, -0.06, -0.07],
[-0.13, -0.14, -0.07],
[0.19, 0.09, 0.09],
[-0.11, -0.13, 0.12],
[-0.00, 0.05, -0.06],
[-0.19, 0.17, -0.11],
...
In Python:
[[ 1.38035819e-01 1.24384061e-01 -1.55227304e-01]
[ -3.71695161e-02 1.51023373e-01 2.41503268e-02]
[ -1.59801364e-01 -5.85760027e-02 -6.58198148e-02]
[ -1.26131564e-01 -1.43218338e-01 -7.05740899e-02]
[ 1.93560138e-01 8.63352865e-02 9.10301656e-02]
17/05/18 16:56:22 WARN reflections.Reflections: could not create Vfs.Dir from url. ignoring the exception and continuing
org.reflections.ReflectionsException: could not create Vfs.Dir from url, no matching UrlType was found [file:/opt/devel/src/expeditionary-warfare/exw-app/target/test-classes]
either use fromURL(final URL url, final List<UrlType> urlTypes) or use the static setDefaultURLTypes(final List<UrlType> urlTypes) or addDefaultURLTypes(UrlType urlType) with your specialized UrlType.
at org.reflections.vfs.Vfs.fromURL(Vfs.java:109)
at org.reflections.vfs.Vfs.fromURL(Vfs.java:91)
at org.reflections.Reflections.scan(Reflections.java:237)
at org.reflections.Reflections.scan(Reflections.java:204)
at org.reflections.Reflections.<init>(Reflections.java:129)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration.registerSubtypes(NeuralNetConfiguration.java:431)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration.configureMapper(NeuralNetConfiguration.java:386)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration.initMapper(NeuralNetConfiguration.java:376)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration.<clinit>(NeuralNetConfiguration.java:123)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration$Builder.build(NeuralNetConfiguration.java:1019)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration$ListBuilder.build(NeuralNetConfiguration.java:269)
at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.getMultiLayerConfiguration(KerasSequentialModel.java:203)
at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.getMultiLayerNetwork(KerasSequentialModel.java:224)
at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.getMultiLayerNetwork(KerasSequentialModel.java:213)
at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasSequentialModelAndWeights(KerasModelImport.java:236)
at $line12.$read$$iw$$iw$$iw$$iw$.<init>(<console>:21)
at $line12.$read$$iw$$iw$$iw$$iw$.<clinit>(<console>)
at $line12.$eval$.$print$lzycompute(<console>:7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment