Skip to content

Instantly share code, notes, and snippets.

@jef5ez
Created May 17, 2017 19:40
Show Gist options
  • Save jef5ez/fbe9e181c7132078d4f3a0855801be80 to your computer and use it in GitHub Desktop.
Save jef5ez/fbe9e181c7132078d4f3a0855801be80 to your computer and use it in GitHub Desktop.
keras -> dl4j
val model = KerasModelImport.importKerasSequentialModelAndWeights(
"keras4j.json",
"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)
// Output is [0.23, 0.69, 0.08]
groupSize = 8
vectorDim = 2
trackLength = 30
model = Sequential()
model.add(Convolution2D(32, 10, 2, border_mode='same', dim_ordering='tf', input_shape=(trackLength, vectorDim, groupSize)))
model.add(Activation('relu'))
model.add(Convolution2D(16, 5, 2, border_mode='same', dim_ordering='tf'))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dropout(0.2))
model.add(Dense(128))
model.add(Dropout(0.2))
model.add(Dense(3))
model.add(Activation('softmax'))
optim = RMSprop()
model.compile(loss='categorical_crossentropy', optimizer=optim)
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")
model.predict_proba(np.ones((1, trackLength, vectorDim, groupSize)))
# output is [ 0.51176351, 0.43943113, 0.04880537]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment