Last active
January 9, 2020 14:30
-
-
Save Breta01/d61e526a6d1969085faa17eea8f02bb4 to your computer and use it in GitHub Desktop.
Code for creating, training and saving TensorFlow model.
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 tensorflow as tf | |
### Linear Regression ### | |
# Input placeholders | |
x = tf.placeholder(tf.float32, name='x') | |
y = tf.placeholder(tf.float32, name='y') | |
# Model parameters | |
W1 = tf.Variable([0.1], tf.float32) | |
W2 = tf.Variable([0.1], tf.float32) | |
W3 = tf.Variable([0.1], tf.float32) | |
b = tf.Variable([0.1], tf.float32) | |
# Output | |
linear_model = tf.identity(W1 * x + W2 * x**2 + W3 * x**3 + b, | |
name='activation_opt') | |
# Loss | |
loss = tf.reduce_sum(tf.square(linear_model - y), name='loss') | |
# Optimizer and training step | |
optimizer = tf.train.AdamOptimizer(0.001) | |
train = optimizer.minimize(loss, name='train_step') | |
# Remember output operation for later application | |
# Adding it to a collections for easy acces | |
# This is not required if you NAME your output operation | |
tf.add_to_collection("activation", linear_model) | |
## Start the session ## | |
sess = tf.Session() | |
sess.run(tf.global_variables_initializer()) | |
# CREATE SAVER | |
saver = tf.train.Saver() | |
# Training loop | |
for i in range(10000): | |
sess.run(train, {x: data, y: expected}) | |
if i % 1000 == 0: | |
# You can also save checkpoints using global_step variable | |
saver.save(sess, "models/model_name", global_step=i) | |
# SAVE TensorFlow graph into path models/model_name | |
saver.save(sess, "models/model_name") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is a typo in line 22: aplication -> application