Skip to content

Commit

Permalink
basic affine model
Browse files Browse the repository at this point in the history
  • Loading branch information
laitassou committed Jun 13, 2020
1 parent 2427dc3 commit 06e4572
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
60 changes: 60 additions & 0 deletions service/examples/affine/model_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#-----------------------------------------------------------------------------------
# Basic exmple showing affine function f(x) = ax + b
#
#-----------------------------------------------------------------------------------

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt


x = np.array([1, 5, 8, 9 ,10, 15,13, 3,-2],np.float32)
y = np.array([-2,-5, -7, -12 ,-15, -5, -12,-10,-5],np.float32)


BATCH_SIZE = 4
num_epochs = 1000

dataset = tf.data.Dataset.from_tensor_slices(( x , y ))


dataset = tf.data.Dataset.from_tensor_slices(( x , y ))
dataset = dataset.shuffle(9).batch(BATCH_SIZE, drop_remainder=True).repeat(num_epochs)


model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(units=1, activation='linear',input_shape=(1,)))

model.layers[0].set_weights([np.array([[7.3]],np.float32),np.array([5.5],np.float32)])

model.compile(optimizer='sgd', loss='mse')

model.fit(dataset, epochs=num_epochs, steps_per_epoch=tf.data.experimental.cardinality(dataset).numpy() // BATCH_SIZE -1, verbose=0)

print(model.layers[0].get_weights())



# Save model to SavedModel format
tf.saved_model.save(model, "./affine/")

yy= [model((np.array([[xi]],np.float32))).numpy()[0,0] for xi in x]

plt.figure(1)

plt.scatter(x, y, c='b', label='Donnees')

plt.plot(x, yy, c='r', label='model')

plt.show()

#result example y= -0.38 x - 5.9

x_in = [0,1]
y_out = model.predict(x_in)

print(y_out)


delta =(y_out[0] - y_out[1]) / ( x_in[0] - x_in[1])
print(delta)
2 changes: 1 addition & 1 deletion service/examples/load_model/create_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def example_1():
i = tf.initializers.global_variables()

# Write the model definition
with open('models/load_model.pb', 'wb') as f:
with open('models/simple_model.pb', 'wb') as f:
f.write(tf.get_default_graph().as_graph_def().SerializeToString())


Expand Down
2 changes: 1 addition & 1 deletion service/examples/load_model/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <iomanip>

int main() {
Model model("../model.pb");
Model model("../simle_model.pb");
model.init();

Tensor input_a{model, "input_a"};
Expand Down

0 comments on commit 06e4572

Please sign in to comment.