def test_main3():
'''
Integration test - training then prediction: attention model
'''
import tempfile
wfn = "tmp_weights.tfl"
if os.path.exists(wfn):
os.unlink(wfn)
arglist = "-e 2 -o tmp_weights.tfl -v -v -v -v -m embedding_attention train 5000"
arglist = arglist.split(' ')
tf.reset_default_graph()
ts2s = CommandLine(arglist=arglist)
assert os.path.exists(wfn)
arglist = "-i tmp_weights.tfl -v -v -v -v -m embedding_attention predict 1 2 3 4 5 6 7 8 9 0"
arglist = arglist.split(' ')
tf.reset_default_graph()
ts2s = CommandLine(arglist=arglist)
assert len(ts2s.prediction_results[0][0])==10
#-----------------------------------------------------------------------------
python类reset_default_graph()的实例源码
mnist_2conv_2dense.py 文件源码
项目:probabilistic_line_search
作者: ProbabilisticNumerics
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def set_up_model():
tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape=[None, 784])
y = tf.placeholder(tf.float32, shape=[None, 10])
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
X_image = tf.reshape(X, [-1,28,28,1])
h_conv1 = tf.nn.relu(conv2d(X_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
h_fc2 = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)
losses = -tf.reduce_sum(y*tf.log(h_fc2), reduction_indices=[1])
return losses, [X, y], [W_conv1, b_conv1, W_conv2, b_conv2, W_fc1, b_fc1, W_fc2, b_fc2]
test_gradient_moment.py 文件源码
项目:probabilistic_line_search
作者: ProbabilisticNumerics
项目源码
文件源码
阅读 62
收藏 0
点赞 0
评论 0
def setUp(self):
# Set up model
tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape=[None, 784])
y = tf.placeholder(tf.float32, shape=[None, 10])
W_fc1 = weight_variable([784, 1024])
b_fc1 = bias_variable([1024])
h_fc1 = tf.nn.relu(tf.matmul(X, W_fc1) + b_fc1)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
h_fc2 = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)
losses = -tf.reduce_sum(y*tf.log(h_fc2), reduction_indices=[1])
self.loss = tf.reduce_mean(losses)
self.batch_size = tf.cast(tf.gather(tf.shape(losses), 0), tf.float32)
self.var_list = [W_fc1, b_fc1, W_fc2, b_fc2]
self.X = X
self.y = y
self.sess = tf.Session()
self.sess.run(tf.initialize_all_variables())
self.mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
def testModuleInfo_multiple_subgraph(self):
# pylint: disable=not-callable
tf.reset_default_graph()
dumb = DumbModule(name="dumb_a")
ph_0 = tf.placeholder(dtype=tf.float32, shape=(1, 10,))
dumb(ph_0)
with tf.name_scope("foo"):
dumb(ph_0)
def check():
sonnet_collection = tf.get_default_graph().get_collection(
base_info.SONNET_COLLECTION_NAME)
self.assertEqual(len(sonnet_collection), 1)
self.assertEqual(len(sonnet_collection[0].connected_subgraphs), 2)
connected_subgraph_0 = sonnet_collection[0].connected_subgraphs[0]
connected_subgraph_1 = sonnet_collection[0].connected_subgraphs[1]
self.assertEqual(connected_subgraph_0.name_scope, "dumb_a")
self.assertEqual(connected_subgraph_1.name_scope, "foo/dumb_a")
check()
_copy_default_graph()
check()
def testModuleInfo_sparsetensor(self):
# pylint: disable=not-callable
tf.reset_default_graph()
dumb = DumbModule(name="dumb_a")
sparse_tensor = tf.SparseTensor(
indices=tf.placeholder(dtype=tf.int64, shape=(10, 2,)),
values=tf.placeholder(dtype=tf.float32, shape=(10,)),
dense_shape=tf.placeholder(dtype=tf.int64, shape=(2,)))
dumb(sparse_tensor)
def check():
sonnet_collection = tf.get_default_graph().get_collection(
base_info.SONNET_COLLECTION_NAME)
connected_subgraph = sonnet_collection[0].connected_subgraphs[0]
self.assertIsInstance(
connected_subgraph.inputs["inputs"], tf.SparseTensor)
self.assertIsInstance(connected_subgraph.outputs, tf.SparseTensor)
check()
_copy_default_graph()
check()
def testModuleInfo_namedtuple(self):
# pylint: disable=not-callable
tf.reset_default_graph()
dumb = DumbModule(name="dumb_a")
ph_0 = tf.placeholder(dtype=tf.float32, shape=(1, 10,))
ph_1 = tf.placeholder(dtype=tf.float32, shape=(1, 10,))
dumb(DumbNamedTuple(ph_0, ph_1))
def check():
sonnet_collection = tf.get_default_graph().get_collection(
base_info.SONNET_COLLECTION_NAME)
connected_subgraph = sonnet_collection[0].connected_subgraphs[0]
self.assertTrue(
base_info._is_namedtuple(connected_subgraph.inputs["inputs"]))
self.assertTrue(base_info._is_namedtuple(connected_subgraph.outputs))
check()
_copy_default_graph()
check()
def testModuleInfo_dict(self):
# pylint: disable=not-callable
tf.reset_default_graph()
dumb = DumbModule(name="dumb_a")
ph_0 = tf.placeholder(dtype=tf.float32, shape=(1, 10,))
ph_1 = tf.placeholder(dtype=tf.float32, shape=(1, 10,))
dumb({"ph_0": ph_0, "ph_1": ph_1})
def check():
sonnet_collection = tf.get_default_graph().get_collection(
base_info.SONNET_COLLECTION_NAME)
connected_subgraph = sonnet_collection[0].connected_subgraphs[0]
self.assertIsInstance(connected_subgraph.inputs["inputs"], dict)
self.assertIsInstance(connected_subgraph.outputs, dict)
check()
_copy_default_graph()
check()
def testModuleInfo_recursion(self):
# pylint: disable=not-callable
tf.reset_default_graph()
dumb = DumbModule(name="dumb_a", no_nest=True)
ph_0 = tf.placeholder(dtype=tf.float32, shape=(1, 10,))
val = {"one": ph_0, "self": None}
val["self"] = val
dumb(val)
def check(check_type):
sonnet_collection = tf.get_default_graph().get_collection(
base_info.SONNET_COLLECTION_NAME)
connected_subgraph = sonnet_collection[0].connected_subgraphs[0]
self.assertIsInstance(connected_subgraph.inputs["inputs"]["one"],
tf.Tensor)
self.assertIsInstance(
connected_subgraph.inputs["inputs"]["self"], check_type)
self.assertIsInstance(connected_subgraph.outputs["one"], tf.Tensor)
self.assertIsInstance(connected_subgraph.outputs["self"], check_type)
check(dict)
_copy_default_graph()
check(base_info._UnserializableObject)
def testUnknownImageShape(self):
tf.reset_default_graph()
batch_size = 2
height, width = 224, 224
num_classes = 1000
input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
with self.test_session() as sess:
inputs = tf.placeholder(tf.float32, shape=(batch_size, None, None, 3))
logits, end_points = inception.inception_v2(inputs, num_classes)
self.assertTrue(logits.op.name.startswith('InceptionV2/Logits'))
self.assertListEqual(logits.get_shape().as_list(),
[batch_size, num_classes])
pre_pool = end_points['Mixed_5c']
feed_dict = {inputs: input_np}
tf.global_variables_initializer().run()
pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
self.assertListEqual(list(pre_pool_out.shape), [batch_size, 7, 7, 1024])
def testUnknownImageShape(self):
tf.reset_default_graph()
batch_size = 2
height, width = 299, 299
num_classes = 1000
input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
with self.test_session() as sess:
inputs = tf.placeholder(tf.float32, shape=(batch_size, None, None, 3))
logits, end_points = inception.inception_v3(inputs, num_classes)
self.assertListEqual(logits.get_shape().as_list(),
[batch_size, num_classes])
pre_pool = end_points['Mixed_7c']
feed_dict = {inputs: input_np}
tf.global_variables_initializer().run()
pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
self.assertListEqual(list(pre_pool_out.shape), [batch_size, 8, 8, 2048])
def testUnknownImageShape(self):
tf.reset_default_graph()
batch_size = 2
height, width = 224, 224
num_classes = 1000
input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
with self.test_session() as sess:
inputs = tf.placeholder(tf.float32, shape=(batch_size, None, None, 3))
logits, end_points = inception.inception_v1(inputs, num_classes)
self.assertTrue(logits.op.name.startswith('InceptionV1/Logits'))
self.assertListEqual(logits.get_shape().as_list(),
[batch_size, num_classes])
pre_pool = end_points['Mixed_5c']
feed_dict = {inputs: input_np}
tf.global_variables_initializer().run()
pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
self.assertListEqual(list(pre_pool_out.shape), [batch_size, 7, 7, 1024])
def __init__(self, dataset_name, model_name, net_constructor):
# Initialize all defaults
self.dataset_name = dataset_name
self.model_name = model_name
self.num_iterations = 200
self.iterations_per_test = 5
self.display_iter = 5
self.snapshot_iter = 1000000
self.train_batch_size = 0
self.test_batch_size = 0
self.crop_if_possible = True
self.debug = False
self.starter_learning_rate = 0.1
self.learning_rate_exp = 0.1
self.learning_rate_step = 1000
self.reports = {}
self.silent = False
self.optimizer = 'momentum'
self.net_constructor = net_constructor
self.net = GraphCNNNetwork()
self.net_desc = GraphCNNNetworkDescription()
tf.reset_default_graph()
# print_ext can be disabled through the silent flag
def _check_adam():
for _mode in HO_MODES[:2]:
for _model in IMPLEMENTED_MODEL_TYPES[1:2]:
_model_kwargs = {'dims': [None, 300, 300, None]}
tf.reset_default_graph()
# set random seeds!!!!
np.random.seed(1)
tf.set_random_seed(1)
experiment('test_with_model_' + _model,
collect_data=False, hyper_iterations=3, mode=_mode, epochs=3,
optimizer=rf.AdamOptimizer,
optimizer_kwargs={'lr': tf.Variable(.001, name='eta_adam')},
model=_model,
model_kwargs=_model_kwargs,
set_T=100,
)
def _check_forward():
w_100 = []
for i in range(1):
for _mode in HO_MODES[0:1]:
for _model in IMPLEMENTED_MODEL_TYPES[0:2]:
_model_kwargs = {} # {'dims': [None, 300, 300, None]}
tf.reset_default_graph()
# set random seeds!!!!
np.random.seed(1)
tf.set_random_seed(1)
results = experiment('test_with_model_' + _model, collect_data=False, hyper_iterations=10, mode=_mode,
epochs=None,
model=_model,
model_kwargs=_model_kwargs,
set_T=1000,
synthetic_hypers=None,
hyper_batch_size=100
# optimizer=rf.GradientDescentOptimizer,
# optimizer_kwargs={'lr': tf.Variable(.01, name='eta')}
)
w_100.append(results[0]['weights'])
# rf.save_obj(w_100, 'check_forward')
return w_100
def _check_all_methods():
for _mode in HO_MODES[:]:
for _model in IMPLEMENTED_MODEL_TYPES:
# _model_kwargs = {'dims': [None, 300, 300, None]}
tf.reset_default_graph()
# set random seeds!!!!
np.random.seed(1)
tf.set_random_seed(1)
experiment('test_with_model_' + _model, collect_data=False, hyper_iterations=3, mode=_mode,
# epochs=3,
model=_model,
# model_kwargs=_model_kwargs,
set_T=100,
synthetic_hypers=None,
hyper_batch_size=100
# optimizer=rf.GradientDescentOptimizer,
# optimizer_kwargs={'lr': tf.Variable(.01, name='eta')}
)
def _check_cnn():
print('END')
for _mode in HO_MODES[2:3]:
for _model in IMPLEMENTED_MODEL_TYPES[2:3]:
tf.reset_default_graph()
np.random.seed(1)
tf.set_random_seed(1)
_model_kwargs = {'conv_dims': [[5, 5, 1, 2], [5, 5, 2, 4], [5, 5, 4, 8]],
'ffnn_dims': [128, 10]}
# noinspection PyTypeChecker
experiment('test_with_model_' + _model, collect_data=False, hyper_iterations=3, mode=_mode,
epochs=2,
model=_model,
model_kwargs=_model_kwargs,
set_T=100,
synthetic_hypers=None,
hyper_batch_size=100,
l1=None,
l2=None
# optimizer=rf.GradientDescentOptimizer,
# optimizer_kwargs={'lr': tf.Variable(.01, name='eta')}
)
def load_neural_network(self):
meanStdInput = pd.read_csv(self.meanStdInputPath, sep = ',').set_index('Unnamed: 0').as_matrix()
self.meanInput = np.array(meanStdInput[0])
self.stdInput = np.array(meanStdInput[1])
meanStdOutput = pd.read_csv(self.meanStdOutputPath, sep = ',').set_index('Unnamed: 0').as_matrix()
self.meanOutput = np.array(meanStdOutput[0])
self.stdOutput = np.array(meanStdOutput[1])
tf.reset_default_graph()
with tf.Graph().as_default(), tf.Session() as self.sess:
self.x = tf.placeholder('float32', [None, self.inputSize]) # Input Tensor
self.y_ = tf.placeholder('float32', [None, self.outputSize]) # Output Tensor
self.create_NN()
self.sess.run(tf.global_variables_initializer())
self.sess = tf.Session(config = tf.ConfigProto(log_device_placement = True))
saver = tf.train.Saver()
saver = tf.train.import_meta_graph(self.ANNPath + '.meta')
saver.restore(self.sess, self.ANNPath)
print('Artificial Neural Network from: ' + self.saveFolder + ' loaded !')
precompute_probs.py 文件源码
项目:instacart-basket-prediction
作者: colinmorris
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def precompute_probs_for_tag(tag, userfold):
hps = hypers.hps_for_tag(tag, mode=hypers.Mode.inference)
tf.logging.info('Creating model')
dat = BasketDataset(hps, userfold)
model = rnnmodel.RNNModel(hps, dat)
sess = tf.InteractiveSession()
# Load pretrained weights
tf.logging.info('Loading weights')
utils.load_checkpoint_for_tag(tag, sess)
# TODO: deal with 'test mode'
tf.logging.info('Calculating probabilities')
probmap = get_probmap(model, sess)
# Hack because of silly reasons.
if userfold == 'validation_full':
userfold = 'validation'
common.save_pdict_for_tag(tag, probmap, userfold)
sess.close()
tf.reset_default_graph()
return probmap
svnh_semi_supervised_model_train.py 文件源码
项目:tf_serving_example
作者: Vetal1977
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def main():
# preparations
create_checkpoints_dir()
utils.download_train_and_test_data()
trainset, testset = utils.load_data_sets()
# create real input for the GAN model (its dicriminator) and
# GAN model itself
real_size = (32, 32, 3)
z_size = 100
learning_rate = 0.0003
tf.reset_default_graph()
input_real = tf.placeholder(tf.float32, (None, *real_size), name='input_real')
net = GAN(input_real, z_size, learning_rate)
# craete dataset
dataset = Dataset(trainset, testset)
# train the model
batch_size = 128
epochs = 25
_, _, _ = train(net, dataset, epochs, batch_size, z_size)
def close(self):
tf.reset_default_graph()
self.sess.close()