Created
December 2, 2018 16:21
-
-
Save hadifar/97799edde61701ccf3de3b728c93d1cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import tensorflow as tf | |
| # create dummy data | |
| X_raw = np.array([2013, 2014, 2015, 2016, 2017], dtype=np.float32) | |
| y_raw = np.array([12000, 14000, 15000, 16500, 17500], dtype=np.float32) | |
| X_data = (X_raw - X_raw.min()) / (X_raw.max() - X_raw.min()) | |
| Y_data = (y_raw - y_raw.min()) / (y_raw.max() - y_raw.min()) | |
| X_data = np.expand_dims(X_data, axis=1) | |
| Y_data = np.expand_dims(Y_data, axis=1) | |
| x = tf.placeholder(dtype=tf.float32, shape=(1, 1), name='x') | |
| y = tf.placeholder(dtype=tf.float32, shape=(1, 1), name='y') | |
| # our linear regression model | |
| dense = tf.layers.Dense(units=1)(x) | |
| # our optimizer and loss | |
| loss = tf.losses.mean_squared_error(labels=y, predictions=dense) | |
| train_step = tf.train.GradientDescentOptimizer(1e-3).minimize(loss) | |
| with tf.Session() as sess: | |
| sess.run(tf.global_variables_initializer()) | |
| # our training loop | |
| for index in range(10000): | |
| batch_x, batch_y = X_data[index % 5], Y_data[index % 5] | |
| _, mae = sess.run([train_step, loss], feed_dict={x: [batch_x], y: [batch_y]}) | |
| if (index + 1) % 100 == 0: | |
| print('loss at step {}: {:5.1f}'.format(index, mae)) | |
| # finally save the model with simple_save API | |
| tf.saved_model.simple_save(session=sess, | |
| export_dir='./test/', | |
| inputs={'x': x}, | |
| outputs={'y': dense}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment