ELU implemented

This commit is contained in:
Francesco Gatti
2017-06-28 11:56:43 +00:00
parent 0767df43a2
commit a86df107be
10 changed files with 123 additions and 24 deletions
+44
View File
@@ -0,0 +1,44 @@
import keras
import numpy as np
import pickle
from keras.models import Sequential
from keras.layers import Input, Dense, Activation, Flatten, Dropout, ELU
from keras.layers.convolutional import Convolution2D, Convolution3D
from keras.layers.pooling import MaxPooling2D, MaxPooling3D
from keras.models import Sequential, Model
from keras.layers import Cropping2D
import keras.backend.tensorflow_backend as KTF
def dense_model(inp, out):
model = Sequential()
model.add(Dense(out, input_shape=(1, inp)))
model.add(ELU())
sgd = keras.optimizers.Adam(lr=1e-4, decay=1e-8)
model.compile(optimizer=sgd, loss="mse")
return model
if __name__ == '__main__':
model = dense_model(8, 2)
wg = model.get_weights()
w = np.squeeze(wg[0])
w = np.array([ i[0] for i in w ] + [ i[1] for i in w ], dtype=np.float32)
b = np.squeeze(wg[1])
print "weigths: ", w
print "bias: ", b
w.tofile("dense.bin", format="f")
b.tofile("dense.bias.bin", format="f")
X = np.array([[[0,1,2,3,4,5,6,7]]], dtype=np.float32)
i = np.squeeze(X[0][0])
print "input: ", i
i.tofile("input.bin", format="f")
r = model.predict( X, batch_size=1)
print "Result: ", r
print "Result shape: ", np.shape(r)
+15 -3
View File
@@ -3,10 +3,22 @@
int main() {
tkDNN::dataDim_t dim(1, 1, 10, 10);
dim.print();
tkDNN::Network net;
tkDNN::Dense d(&net, dim, 2, "ci", "lol");
tkDNN::dataDim_t dim(1, 8, 1, 1);
tkDNN::Dense d(&net, dim, 2, "../tests/dense.bin", "../tests/dense.bias.bin");
tkDNN::Activation a(&net, d.output_dim, tkDNN::ACTIVATION_ELU);
value_type *data;
value_type *input_h;
readBinaryFile("../tests/input.bin", 8, &input_h, &data);
dim.print();
data = d.infer(dim, data);
dim.print();
data = a.infer(dim, data);
dim.print();
printDeviceVector(dim.tot(), data);
return 0;
}