Skip to content

Instantly share code, notes, and snippets.

View hadifar's full-sized avatar
:octocat:

Amir Hadifar hadifar

:octocat:
View GitHub Profile
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
}
@hadifar
hadifar / tf_checkpoint_8.py
Last active December 2, 2018 17:10
simple usage of simple_save API in tensorflow
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)
tf.saved_model.simple_save(session,
export_dir,
inputs={"x": x, "y": y},
outputs={"z": z})
import tensorflow as tf
# Let's load a previously saved meta graph in the default graph
# This function returns a Saver
saver = tf.train.import_meta_graph('results/model.ckpt-1000.meta')
# We can now access the default graph where all our metadata has been loaded
graph = tf.get_default_graph()
# Finally we can retrieve tensors, operations, collections, etc.
import tensorflow as tf
v1 = tf.Variable(80., name="v1")
v2 = tf.Variable(10., name="v2")
a = tf.add(v1, v2)
saver0 = tf.train.Saver(var_list=[v1, v2])
saver1 = tf.train.Saver(var_list={'myVar1': v1, 'myVar2': v2})
saver2 = tf.train.Saver(var_list={'myVar1': v1})
saver3 = tf.train.Saver(var_list=tf.trainable_variables())
import tensorflow as tf
v1 = tf.Variable(9., name="v1")
v2 = tf.Variable(10., name="v2")
a = tf.add(v1, v2)
ckpnt = tf.train.Checkpoint()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
ckpnt.save('./test/myVar', sess)
tf.train.Checkpoint(myOptimizer=optimizer, myModel=model, myVar=var1)
import tensorflow as tf
v1 = tf.Variable(80., name="v1")
v2 = tf.Variable(10., name="v2")
a = tf.add(v1, v2)
ckpnt = tf.train.Checkpoint(firstVar=v1, secondVar=v2)
status = ckpnt.restore(tf.train.latest_checkpoint('./test/'))
status.assert_consumed()
import tensorflow as tf
v1 = tf.Variable(9., name="v1")
v2 = tf.Variable(2., name="v2")
a = tf.add(v1, v2)
#create checkpoint object... We not save anything yet!
ckpnt = tf.train.Checkpoint(firstVar=v1, secondVar=v2)
with tf.Session() as sess:
import random
from collections import deque
import gym
import numpy as np
import tensorflow as tf
tf.enable_eager_execution()
num_episodes = 3000