def tearDown(self): # test for disable eager test ops.disable_eager_execution() self.assertFalse(context.executing_eagerly()) # Calling disable eager execution a second time should not cause an error. ops.disable_eager_execution() self.assertFalse(context.executing_eagerly())
def test_get_variable_reuse_error(self): ops.disable_eager_execution() with self.session( config=default_config, graph=ops.Graph(), use_gpu=test_util.is_gpu_available(), ): with variable_scope.variable_scope("embedding", reuse=False): _ = de.get_variable("t900", initializer=-1, dim=2) with self.assertRaisesRegexp(ValueError, "Variable embedding/t900 already exists"): _ = de.get_variable("t900", initializer=-1, dim=2)
def test_loader_v1(self): read_count = metrics.GetRead() ops.disable_eager_execution() save_dir = self._create_save_v1_model() loader = loader_impl.SavedModelLoader(save_dir) with self.session(graph=ops.Graph()) as sess: loader.load(sess, ["foo"]) ops.enable_eager_execution() self.assertEqual( metrics.GetReadApi(loader_impl._LOADER_LABEL, write_version="1"), 1) self.assertEqual(metrics.GetRead(), read_count + 1)
def _prepare_signature(layers: dict, model_keys): if tf_version.split(".")[0] == "2": disable_eager_execution() signature = {} for key, value in model_keys.items(): if value in layers.keys(): x = array_ops.placeholder( dtype=type_mapping[layers[value].precision], shape=layers[value].shape, name=value) x_tensor_info = build_tensor_info(x) signature[key] = x_tensor_info return signature
def disable_v2_behavior(): """Disables TensorFlow 2.x behaviors. This function can be called at the beginning of the program (before `Tensors`, `Graphs` or other structures have been created, and before devices have been initialized. It switches all global behaviors that are different between TensorFlow 1.x and 2.x to behave as intended for 1.x. User can call this function to disable 2.x behavior during complex migrations. """ tf2.disable() # Switches TensorArrayV2 and control flow V2 ops.disable_eager_execution() tensor_shape.disable_v2_tensorshape() # Also switched by tf2 variable_scope.disable_resource_variables()
def main(argv): if len(argv) > 1: raise app.UsageError("Too many command-line arguments.") os.environ["TF_TRT_ALLOW_ENGINE_NATIVE_SEGMENT_EXECUTION"] = "False" if FLAGS.use_tf2: logging.info("Running in TF2 mode. Eager execution is enabled.") framework_ops.enable_eager_execution() else: logging.info("Running in TF1 mode. Eager execution is disabled.") framework_ops.disable_eager_execution() run_all_tests()
def disable_v2_behavior(): """Enables TensorFlow 2.x behaviors. This function can be called at the beginning of the program (before `Tensors`, `Graphs` or other structures have been created, and before devices have been initialized. It switches all global behaviors that are different between TensorFlow 1.x and 2.x to behave as intended for 1.x. User can call this function to disable 2.x behavior during complex migrations. """ tf2.disable() # Switches TensorArrayV2 and control flow V2 ops.disable_eager_execution() tensor_shape.disable_v2_tensorshape() # Also switched by tf2 variable_scope.disable_resource_variables()
def disable_v2_behavior(): """Disables TensorFlow 2.x behaviors. This function can be called at the beginning of the program (before `Tensors`, `Graphs` or other structures have been created, and before devices have been initialized. It switches all global behaviors that are different between TensorFlow 1.x and 2.x to behave as intended for 1.x. User can call this function to disable 2.x behavior during complex migrations. """ # _v2_behavior_usage_gauge.get_cell("disable").set(True) tf2.disable() ops.disable_eager_execution() tensor_shape.disable_v2_tensorshape() # Also switched by tf2 # FIXME[bug]: # Warning: disable_resource_variables (from # tensorflow.python.ops.variable_scope) is deprecated and will be # removed in a future version. # Instructions for updating: # non-resource variables are not supported in the long term # # The function tf.compat.v1.disable_resource_variables() is # depreciated instead you can mention use_resource=False in # tf.get_variable() which will be forced to true when eager excecution # is enabled by default in Tensorflow 2.x. # variable_scope.disable_resource_variables() ops.disable_tensor_equality() # Disables TensorArrayV2 and control flow V2. control_flow_v2_toggles.disable_control_flow_v2() # Make sure internal uses of tf.data symbols map to V1 versions. dataset_ops.Dataset = dataset_ops.DatasetV1 readers.FixedLengthRecordDataset = readers.FixedLengthRecordDatasetV1 readers.TFRecordDataset = readers.TFRecordDatasetV1 readers.TextLineDataset = readers.TextLineDatasetV1 counter.Counter = counter.CounterV1 interleave_ops.choose_from_datasets = \ interleave_ops.choose_from_datasets_v1 interleave_ops.sample_from_datasets = \ interleave_ops.sample_from_datasets_v1 random_ops.RandomDataset = random_ops.RandomDatasetV1 exp_readers.CsvDataset = exp_readers.CsvDatasetV1 exp_readers.SqlDataset = exp_readers.SqlDatasetV1 exp_readers.make_batched_features_dataset = ( exp_readers.make_batched_features_dataset_v1) exp_readers.make_csv_dataset = exp_readers.make_csv_dataset_v1
def setup_tf(): disable_eager_execution() gpus = tf.config.experimental.list_physical_devices('GPU') if gpus: try: # Currently, memory growth needs to be the same across GPUs for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) logical_gpus = tf.config.experimental.list_logical_devices('GPU') print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs") except RuntimeError as e: # Memory growth must be set before GPUs have been initialized print(e)
def main(argv): if len(argv) > 1: raise app.UsageError("Too many command-line arguments.") os.environ["TF_TRT_ALLOW_ENGINE_NATIVE_SEGMENT_EXECUTION"] = "False" if FLAGS.use_tf2: logging.info("Running in TF2 mode. Eager execution is enabled.") framework_ops.enable_eager_execution() else: logging.info("Running in TF1 mode. Eager execution is disabled.") framework_ops.disable_eager_execution() if FLAGS.use_int8: logging.info("Will try converting with INT8 precision.") else: logging.info("Will not try converting with INT8 precision.") if FLAGS.gpu_memory_limit_mb: set_up_gpu_memory_limit(FLAGS.gpu_memory_limit_mb) analyzer = result_analyzer.ResultAnalyzer( use_cpu_latency_baseline=FLAGS.latency_baseline == "CPU", use_cpu_numerics_baseline=FLAGS.numerics_baseline == "CPU", checkers=[ functools.partial(result_analyzer.check_column, name="speedup", fn=lambda x: x > FLAGS.speedup_tolerance), functools.partial(result_analyzer.check_column, name="rel_diff_mean", fn=lambda x: all(v < FLAGS.diff_tolerance for v in x.values())) ]) runner = SampleRunner( saved_model_dir=FLAGS.saved_model_dir, saved_model_tags=FLAGS.saved_model_tags, saved_model_signature_key=FLAGS.saved_model_signature_key, batch_size=FLAGS.batch_size, output_dir=FLAGS.output_dir, output_format=FLAGS.output_format, use_tf2=FLAGS.use_tf2, use_int8=FLAGS.use_int8, analyzer=analyzer) runner.run_all_tests()
def main(argv): if len(argv) > 1: raise app.UsageError("Too many command-line arguments.") os.environ["TF_TRT_ALLOW_ENGINE_NATIVE_SEGMENT_EXECUTION"] = "False" if FLAGS.use_tf2: logging.info("Running in TF2 mode. Eager execution is enabled.") framework_ops.enable_eager_execution() else: logging.info("Running in TF1 mode. Eager execution is disabled.") framework_ops.disable_eager_execution() SampleRunner(saved_model_dir=FLAGS.saved_model_dir, saved_model_tags=FLAGS.saved_model_tags, saved_model_signature_key=FLAGS.saved_model_signature_key, batch_size=FLAGS.batch_size, use_tf2=FLAGS.use_tf2).run_all_tests()
def initiate_agent(self, env): """initiate a deep Q agent""" disable_eager_execution() self.env = env nb_actions = self.env.action_space.n print(env) print("---") print(env.observation_space) print("---") print(nb_actions) print("---") self.model = Sequential() self.model.add( Dense(512, activation='relu', input_shape=env.observation_space)) self.model.add(Dropout(0.2)) self.model.add(Dense(512, activation='relu')) self.model.add(Dropout(0.2)) self.model.add(Dense(512, activation='relu')) self.model.add(Dropout(0.2)) self.model.add(Dense(nb_actions, activation='linear')) # Finally, we configure and compile our agent. You can use every built-in Keras optimizer and # even the metrics! self.memory = SequentialMemory(limit=memory_limit, window_length=window_length) policy = TrumpPolicy() nb_actions = env.action_space.n self.dqn = DQNAgent(model=self.model, nb_actions=nb_actions, memory=self.memory, nb_steps_warmup=nb_steps_warmup, target_model_update=1e-2, policy=policy, processor=CustomProcessor(), batch_size=batch_size, train_interval=train_interval, enable_double_dqn=enable_double_dqn) self.dqn.compile(Adam(lr=1e-3), metrics=['mae'])
def testClusterSpecPropagationIsolation(self): """Test that two sessions using ClusterSpec propagation are isolated.""" disable_eager_execution() server = server_lib.Server.create_local_server() init_value = array_ops.placeholder(dtypes.int32, shape=[]) v = variables.Variable(init_value) cluster_def = cluster_pb2.ClusterDef() job = cluster_def.job.add() job.name = 'worker' job.tasks[0] = server.target[len('grpc://'):] config = config_pb2.ConfigProto(cluster_def=cluster_def) sess1 = session.Session(server.target, config=config) sess2 = session.Session(server.target, config=config) # Initially, the variable is uninitialized in both sessions. with self.assertRaises(errors.FailedPreconditionError): sess1.run(v) with self.assertRaises(errors.FailedPreconditionError): sess2.run(v) # An update in sess1 should be visible in sess1 only. sess1.run(v.initializer, feed_dict={init_value: 37}) self.assertEqual(37, sess1.run(v)) with self.assertRaises(errors.FailedPreconditionError): sess2.run(v) # An update in sess2 should be visible in sess2 only. sess2.run(v.initializer, feed_dict={init_value: 86}) self.assertEqual(37, sess1.run(v)) self.assertEqual(86, sess2.run(v)) # Closing sess2 has no effect on the state of sess1. sess2.close() self.assertEqual(37, sess1.run(v)) # Subsequent sessions will not see the state of existing sessions. sess3 = session.Session(server.target, config=config) self.assertEqual(37, sess1.run(v)) with self.assertRaises(errors.FailedPreconditionError): sess3.run(v)
def disable_v2_behavior(): """Disables TensorFlow 2.x behaviors. This function can be called at the beginning of the program (before `Tensors`, `Graphs` or other structures have been created, and before devices have been initialized. It switches all global behaviors that are different between TensorFlow 1.x and 2.x to behave as intended for 1.x. User can call this function to disable 2.x behavior during complex migrations. @compatibility(TF2) Using this function indicates that your software is not compatible with eager execution and `tf.function` in TF2. To migrate to TF2, rewrite your code to be compatible with eager execution. Please refer to the [migration guide] (https://www.tensorflow.org/guide/migrate) for additional resource on the topic. @end_compatibility """ _v2_behavior_usage_gauge.get_cell("disable").set(True) tf2.disable() ops.disable_eager_execution() tensor_shape.disable_v2_tensorshape() # Also switched by tf2 variable_scope.disable_resource_variables() ops.disable_tensor_equality() # Disables TensorArrayV2 and control flow V2. control_flow_v2_toggles.disable_control_flow_v2() # Make sure internal uses of tf.data symbols map to V1 versions. dataset_ops.Dataset = dataset_ops.DatasetV1 readers.FixedLengthRecordDataset = readers.FixedLengthRecordDatasetV1 readers.TFRecordDataset = readers.TFRecordDatasetV1 readers.TextLineDataset = readers.TextLineDatasetV1 counter.Counter = counter.CounterV1 interleave_ops.choose_from_datasets = interleave_ops.choose_from_datasets_v1 interleave_ops.sample_from_datasets = interleave_ops.sample_from_datasets_v1 random_ops.RandomDataset = random_ops.RandomDatasetV1 exp_readers.CsvDataset = exp_readers.CsvDatasetV1 exp_readers.SqlDataset = exp_readers.SqlDatasetV1 exp_readers.make_batched_features_dataset = ( exp_readers.make_batched_features_dataset_v1) exp_readers.make_csv_dataset = exp_readers.make_csv_dataset_v1
def testV2BehaviorLogging(self): with self.assertLogs(level='INFO') as logs: try: ops.enable_eager_execution() # Ignore this exception to test log output successfully except ValueError as e: if 'must be called at program startup' not in str(e): raise e self.assertIn('Enabling eager execution', ''.join(logs.output)) with self.assertLogs(level='INFO') as logs: ops.disable_eager_execution() self.assertIn('Disabling eager execution', ''.join(logs.output)) with self.assertLogs(level='INFO') as logs: tensor_shape.enable_v2_tensorshape() self.assertIn('Enabling v2 tensorshape', ''.join(logs.output)) with self.assertLogs(level='INFO') as logs: tensor_shape.disable_v2_tensorshape() self.assertIn('Disabling v2 tensorshape', ''.join(logs.output)) with self.assertLogs(level='INFO') as logs: variable_scope.enable_resource_variables() self.assertIn('Enabling resource variables', ''.join(logs.output)) with self.assertLogs(level='INFO') as logs: variable_scope.disable_resource_variables() self.assertIn('Disabling resource variables', ''.join(logs.output)) with self.assertLogs(level='INFO') as logs: ops.enable_tensor_equality() self.assertIn('Enabling tensor equality', ''.join(logs.output)) with self.assertLogs(level='INFO') as logs: ops.disable_tensor_equality() self.assertIn('Disabling tensor equality', ''.join(logs.output)) with self.assertLogs(level='INFO') as logs: control_flow_v2_toggles.enable_control_flow_v2() self.assertIn('Enabling control flow v2', ''.join(logs.output)) with self.assertLogs(level='INFO') as logs: control_flow_v2_toggles.disable_control_flow_v2() self.assertIn('Disabling control flow v2', ''.join(logs.output))
def testLSTM(self): """Freezes a Keras LSTM.""" ops.disable_eager_execution() model = keras.models.Sequential( [keras.layers.LSTM(units=10, input_shape=(10, 10))]) tensor_names = [tensor.name for tensor in model.inputs + model.outputs] # Freeze the model. sess = keras.backend.get_session() variable_graph_def = sess.graph_def variable_graph_def = self._inline_functions(variable_graph_def, tensor_names) output_tensor = self._get_tensor_names(model.outputs) constant_graph_def = graph_util.convert_variables_to_constants( sess, variable_graph_def, output_tensor) # Validate converted graph. input_data = np.array(np.random.random_sample([10, 10, 10]), dtype=np.int32) self._ensure_no_variables_in_graph(constant_graph_def) self._test_converted_keras_model(model, constant_graph_def, input_data)
def test_sharing_between_multi_sessions(self): ops.disable_eager_execution() # Start a server to store the table state server = server_lib.Server({"local0": ["localhost:0"]}, protocol="grpc", start=True) # Create two sessions sharing the same state session1 = session.Session(server.target, config=default_config) session2 = session.Session(server.target, config=default_config) table = de.get_variable("tx100", dtypes.int64, dtypes.int32, initializer=0, dim=1) # Populate the table in the first session with session1: with ops.device(_get_devices()[0]): self.evaluate(variables.global_variables_initializer()) self.evaluate(variables.local_variables_initializer()) self.assertAllEqual(0, table.size().eval()) keys = constant_op.constant([11, 12], dtypes.int64) values = constant_op.constant([[11], [12]], dtypes.int32) table.upsert(keys, values).run() self.assertAllEqual(2, table.size().eval()) output = table.lookup( constant_op.constant([11, 12, 13], dtypes.int64)) self.assertAllEqual([[11], [12], [0]], output.eval()) # Verify that we can access the shared data from the second session with session2: with ops.device(_get_devices()[0]): self.assertAllEqual(2, table.size().eval()) output = table.lookup( constant_op.constant([10, 11, 12], dtypes.int64)) self.assertAllEqual([[0], [11], [12]], output.eval())
def test2(): import numpy as np import tensorflow as tf import tensorflow.compat.v1 as tf1 from tensorflow.python.framework.ops import disable_eager_execution disable_eager_execution() x = tf1.placeholder(tf.float32,[None,3]) y = tf1.placeholder(tf.float32) z = tf.keras.layers.Dense(units=1)(x) # 또는 z = tf1.layers.dense(x, units=1) print(z) sess = tf1.Session() sess.run(tf1.global_variables_initializer()) print(sess.run(z, feed_dict={x: np.random.randn(2,3)}))
def __init__( self, env, *args, epsilon=1.0, gamma=0.99, batch_size=64, epsilon_min=0.01, lr=0.001, epsilon_decay=0.996, memory_length=int(1e6), seed=None, **kwars ): disable_eager_execution() self.env = env self.epsilon = epsilon self.gamma = gamma self.batch_size = batch_size self.epsilon_min = epsilon_min self.lr = lr self.epsilon_decay = epsilon_decay self.memory = deque(maxlen=memory_length) self.observation_space = None self.action_space = None self.obs_size = 0 self.action_size = 0 self.policy = None if self.env is not None: self.observation_space = env.observation_space self.action_space = env.action_space self.obs_size = self.observation_space.shape[0] self.action_size = self.action_space.n self.policy = self._build_policy() self.seed(seed)
def main(): from tensorflow.python.framework.ops import disable_eager_execution disable_eager_execution() parser = argparse.ArgumentParser() parser.add_argument('infile') parser.add_argument('outfile', nargs='?') parser.add_argument('-r', '--resize', type=int, default=0) args = me.args = parser.parse_args() with tf.Session(get_target()) as sess: image_data, width, height, image = _process_image(args.infile) image_out = sess.run( tf.io.encode_jpeg( sess.run(random_crop(image_data, resize=args.resize)))) if not args.outfile: args.outfile = os.path.basename(args.infile) if args.outfile == args.infile: print( 'Implicitly overwriting infile not supported; pass `%s %s` to confirm' % (args.outfile, args.outfile)) sys.exit(1) with open(args.outfile, 'wb') as f: f.write(image_out)
dim_nums = xla_data_pb2.DotDimensionNumbers() dim_nums.lhs_contracting_dimensions.append(2) dim_nums.rhs_contracting_dimensions.append(3) dim_nums.lhs_batch_dimensions.append(0) dim_nums.rhs_batch_dimensions.append(0) with self.assertRaisesRegex(ValueError, 'Batch dimension sizes do not match. ' 'Got: 2 and 4'): xla.dot_general(a, b, dim_nums) def testDotShapeInference(self): a = array_ops.placeholder(np.float32, shape=(1, 2, 3, 4)) b = array_ops.placeholder(np.float32, shape=(4, 3, 2, 1)) dim_nums = xla_data_pb2.DotDimensionNumbers() dim_nums.lhs_contracting_dimensions.append(1) dim_nums.rhs_contracting_dimensions.append(2) dim_nums.lhs_batch_dimensions.append(3) dim_nums.rhs_batch_dimensions.append(0) c = xla.dot_general(a, b, dim_nums) self.assertEqual(c.shape, tensor_shape.TensorShape([4, 1, 3, 3, 1])) if __name__ == '__main__': # This test is using Tensorflow sessions which are not compatible with eager # mode. ops.disable_eager_execution() googletest.main()
from keras.layers import BatchNormalization from keras.models import Model from keras.losses import binary_crossentropy from keras import backend as K import numpy as np import matplotlib.pyplot as plt from graph import ECG_model from keras.callbacks import EarlyStopping, ModelCheckpoint, TensorBoard, ReduceLROnPlateau, LearningRateScheduler from utils import * from config import get_config from tensorflow.python.framework.ops import disable_eager_execution disable_eager_execution() def Conv1DTranspose(input_tensor, filters, kernel_size, strides=2, padding='same', activation='relu', name='conv12d'): """ input_tensor: tensor, with the shape (batch_size, time_steps, dims) filters: int, output dimension, i.e. the output tensor will have the shape of (batch_size, time_steps, filters) kernel_size: int, size of the convolution kernel strides: int, convolution step size padding: 'same' | 'valid'
def testConnectToClusterInGraphModeWillFail(self): ops.disable_eager_execution() with self.assertRaises(ValueError): remote.connect_to_cluster(self._cluster_resolver) ops.enable_eager_execution()
import stupid #stupid.imports(globals()) import tensorflow.compat.v1 as tf from tensorflow.python.framework.ops import disable_eager_execution disable_eager_execution() # sonst gibt es keinen graph folder = "relu4_1" # standard objekt inspect def inspect(obj): for key in dir(obj): if(not key.startswith("_")): value = getattr(obj,key) print(f"{key}: {type(value)}") if(value.__doc__ != None): print("\t"+value.__doc__.split("\n")[0]) from google.protobuf import text_format # lade pbtxt datei (untested) def load_pb(file): f = open(file, "r") graph_protobuf = text_format.Parse(f.read(), tf.GraphDef()) # Import the graph protobuf into our new graph. g = tf.Graph() with g.as_default():
def get_simple_session(): ops.disable_eager_execution() sess = session_lib.Session() variables.Variable(1.) sess.run(variables.global_variables_initializer()) return sess
def __init__(self): if not FLAGS.run_eagerly: ops.disable_eager_execution() self.use_tf_function = FLAGS.use_tf_function self.load_input_data(FLAGS.batch_size)
# Copyright (c) Microsoft Corporation. # Licensed under the MIT license. from __future__ import absolute_import, division, print_function, \ unicode_literals import numpy as np import tensorflow as tf from tensorflow.python.framework.ops import disable_eager_execution disable_eager_execution() # turn eager execution off from modules.TensorflowCommon.utils import to_tf_tensor, flatten from shared.ITest import ITest from shared.BAData import BAInput, BAOutput from shared.BASparseMat import BASparseMat from modules.TensorflowCommon.ba_objective import compute_reproj_err, compute_w_err class TensorflowGraphBA(ITest): '''Test class for BA diferentiation by Tensorflow using computational graphs.''' def prepare(self, input): '''Prepares calculating. This function must be run before any others.''' self.p = len(input.obs) self.cams = input.cams self.x = input.x self.w = input.w self.obs = input.obs
def setUp(self): super(CustomAggregatorTest, self).setUp() ops.disable_eager_execution()
def test_training_save_restore(self): # embedding_lookup does not work in eager mode when num_shards is more than 1. ops.disable_eager_execution() keys_type_list = [dtypes.int64] if test_util.is_gpu_available() else [ dtypes.int64, dtypes.string ] for run_id, num_shards, key_dtype, value_dtype, _, dim, run_step \ in _next_run_step_config(keys_type_list): save_dir = os.path.join(self.get_temp_dir(), "save_restore") save_path = os.path.join(tempfile.mkdtemp(prefix=save_dir), "hash") ids = script_ops.py_func( _create_dynamic_shape_tensor(dtype=_type_converter(key_dtype)), inp=[], Tout=key_dtype, stateful=True) params = deo.get_variable( name="params-test-0915-" + str(run_id), key_dtype=key_dtype, value_dtype=value_dtype, devices=_get_devices() * num_shards, initializer=init_ops.random_normal_initializer(0.0, 0.01), dim=dim) _, var0 = deo.embedding_lookup(params, ids, return_trainable=True) loss = lambda: var0 * var0 params_keys, params_vals = params.export() opt = adam.AdamOptimizer(0.3) mini = opt.minimize(loss, var_list=[var0]) opt_slots = [opt.get_slot(var0, _s) for _s in opt.get_slot_names()] _saver = saver_lib.Saver([params] + [_s.params for _s in opt_slots]) with self.session(config=default_config, use_gpu=test_util.is_gpu_available()) as sess: self.evaluate(variables.global_variables_initializer()) for _i in range(run_step): self.evaluate([mini]) size_before_saved = self.evaluate(params.size()) np_params_keys_before_saved = self.evaluate(params_keys) np_params_vals_before_saved = self.evaluate(params_vals) opt_slots_kv_pairs = [_s.params.export() for _s in opt_slots] np_slots_kv_pairs_before_saved = [ self.evaluate(_kv) for _kv in opt_slots_kv_pairs ] _saver.save(sess, save_path) with self.session(config=default_config, use_gpu=test_util.is_gpu_available()) as sess: self.evaluate(variables.global_variables_initializer()) self.assertAllEqual(0, self.evaluate(params.size())) _saver.restore(sess, save_path) params_keys_restored, params_vals_restored = params.export() size_after_restored = self.evaluate(params.size()) np_params_keys_after_restored = self.evaluate( params_keys_restored) np_params_vals_after_restored = self.evaluate( params_vals_restored) opt_slots_kv_pairs_restored = [ _s.params.export() for _s in opt_slots ] np_slots_kv_pairs_after_restored = [ self.evaluate(_kv) for _kv in opt_slots_kv_pairs_restored ] self.assertAllEqual(size_before_saved, size_after_restored) self.assertAllEqual(np.sort(np_params_keys_before_saved), np.sort(np_params_keys_after_restored)) self.assertAllEqual( np.sort(np_params_vals_before_saved, axis=0), np.sort(np_params_vals_after_restored, axis=0)) for pairs_before, pairs_after in zip( np_slots_kv_pairs_before_saved, np_slots_kv_pairs_after_restored): self.assertAllEqual(np.sort(pairs_before[0], axis=0), np.sort(pairs_after[0], axis=0)) self.assertAllEqual(np.sort(pairs_before[1], axis=0), np.sort(pairs_after[1], axis=0)) if test_util.is_gpu_available(): self.assertTrue( _check_device(params.tables[0].resource_handle, "GPU"))
import pandas as pd from deepctr.feature_column import SparseFeat, VarLenSparseFeat from preprocess import gen_data_set, gen_model_input from sklearn.preprocessing import LabelEncoder from tensorflow.python.keras.models import Model from deepmatch.models import * import tensorflow.keras.backend as K import tensorflow as tf from tensorflow.python.framework.ops import disable_eager_execution disable_eager_execution() # using for custom loss def dual_augmented_loss(p_u, p_v, a_u, a_v): def loss(y_true, y_pred): y_ = K.cast(y_true, tf.float32) loss_p = K.mean(K.square(y_ - y_pred)) loss_u = K.mean(K.square(y_ * a_u + (1 - y_) * p_v - p_v)) loss_v = K.mean(K.square(y_ * a_v + (1 - y_) * p_u - p_u)) return loss_p + 0.5 * loss_u + 0.5 * loss_v return loss if __name__ == "__main__": data = pd.read_csvdata = pd.read_csv("./movielens_sample.txt") sparse_features = [ "movie_id", "user_id",
def setUp(self): super(AudioFeatureGenerationTest, self).setUp() ops.disable_eager_execution()
def dca( adata, mode='denoise', ae_type='nb-conddisp', normalize_per_cell=True, scale=True, log1p=True, hidden_size=(64, 32, 64), # network args hidden_dropout=0., batchnorm=True, activation='relu', init='glorot_uniform', network_kwds={}, epochs=300, # training args reduce_lr=10, early_stop=15, batch_size=32, optimizer='RMSprop', learning_rate=None, random_state=0, threads=None, verbose=False, training_kwds={}, return_model=False, return_info=False, copy=False, check_counts=True, ): """Deep count autoencoder(DCA) API. Fits a count autoencoder to the count data given in the anndata object in order to denoise the data and capture hidden representation of cells in low dimensions. Type of the autoencoder and return values are determined by the parameters. Parameters ---------- adata : :class:`~scanpy.api.AnnData` An anndata file with `.raw` attribute representing raw counts. mode : `str`, optional. `denoise`(default), or `latent`. `denoise` overwrites `adata.X` with denoised expression values. In `latent` mode DCA adds `adata.obsm['X_dca']` to given adata object. This matrix represent latent representation of cells via DCA. ae_type : `str`, optional. `nb-conddisp`(default), `zinb`, `nb-conddisp` or `nb`. Type of the autoencoder. Return values and the architecture is determined by the type e.g. `nb` does not provide dropout probabilities. normalize_per_cell : `bool`, optional. Default: `True`. If true, library size normalization is performed using the `sc.pp.normalize_per_cell` function in Scanpy and saved into adata object. Mean layer is re-introduces library size differences by scaling the mean value of each cell in the output layer. See the manuscript for more details. scale : `bool`, optional. Default: `True`. If true, the input of the autoencoder is centered using `sc.pp.scale` function of Scanpy. Note that the output is kept as raw counts as loss functions are designed for the count data. log1p : `bool`, optional. Default: `True`. If true, the input of the autoencoder is log transformed with a pseudocount of one using `sc.pp.log1p` function of Scanpy. hidden_size : `tuple` or `list`, optional. Default: (64, 32, 64). Width of hidden layers. hidden_dropout : `float`, `tuple` or `list`, optional. Default: 0.0. Probability of weight dropout in the autoencoder (per layer if list or tuple). batchnorm : `bool`, optional. Default: `True`. If true, batch normalization is performed. activation : `str`, optional. Default: `relu`. Activation function of hidden layers. init : `str`, optional. Default: `glorot_uniform`. Initialization method used to initialize weights. network_kwds : `dict`, optional. Additional keyword arguments for the autoencoder. epochs : `int`, optional. Default: 300. Number of total epochs in training. reduce_lr : `int`, optional. Default: 10. Reduces learning rate if validation loss does not improve in given number of epochs. early_stop : `int`, optional. Default: 15. Stops training if validation loss does not improve in given number of epochs. batch_size : `int`, optional. Default: 32. Number of samples in the batch used for SGD. learning_rate : `float`, optional. Default: None. Learning rate to use in the training. optimizer : `str`, optional. Default: "RMSprop". Type of optimization method used for training. random_state : `int`, optional. Default: 0. Seed for python, numpy and tensorflow. threads : `int` or None, optional. Default: None Number of threads to use in training. All cores are used by default. verbose : `bool`, optional. Default: `False`. If true, prints additional information about training and architecture. training_kwds : `dict`, optional. Additional keyword arguments for the training process. return_model : `bool`, optional. Default: `False`. If true, trained autoencoder object is returned. See "Returns". return_info : `bool`, optional. Default: `False`. If true, all additional parameters of DCA are stored in `adata.obsm` such as dropout probabilities (obsm['X_dca_dropout']) and estimated dispersion values (obsm['X_dca_dispersion']), in case that autoencoder is of type zinb or zinb-conddisp. copy : `bool`, optional. Default: `False`. If true, a copy of anndata is returned. check_counts : `bool`. Default `True`. Check if the counts are unnormalized (raw) counts. Returns ------- If `copy` is true and `return_model` is false, AnnData object is returned. In "denoise" mode, `adata.X` is overwritten with the denoised values. In "latent" mode, latent low dimensional representation of cells are stored in `adata.obsm['X_dca']` and `adata.X` is not modified. Note that these values are not corrected for library size effects. If `return_info` is true, all estimated distribution parameters are stored in AnnData such as: - `.obsm["X_dca_dropout"]` which is the mixture coefficient (pi) of the zero component in ZINB, i.e. dropout probability. (Only if ae_type is zinb or zinb-conddisp) - `.obsm["X_dca_dispersion"]` which is the dispersion parameter of NB. - `.uns["dca_loss_history"]` which stores the loss history of the training. Finally, the raw counts are stored as `.raw`. If `return_model` is given, trained model is returned. When both `copy` and `return_model` are true, a tuple of anndata and model is returned in that order. """ assert isinstance(adata, anndata.AnnData), 'adata must be an AnnData instance' assert mode in ('denoise', 'latent'), '%s is not a valid mode.' % mode # set seed for reproducibility random.seed(random_state) np.random.seed(random_state) tf.random.set_seed(random_state) os.environ['PYTHONHASHSEED'] = '0' # this creates adata.raw with raw counts and copies adata if copy==True adata = read_dataset(adata, transpose=False, test_split=False, copy=copy, check_counts=check_counts) # check for zero genes nonzero_genes, _ = sc.pp.filter_genes(adata.X, min_counts=1) assert nonzero_genes.all( ), 'Please remove all-zero genes before using DCA.' adata = normalize( adata, filter_min_counts=False, # no filtering, keep cell and gene idxs same size_factors=normalize_per_cell, normalize_input=scale, logtrans_input=log1p) network_kwds = { **network_kwds, 'hidden_size': hidden_size, 'hidden_dropout': hidden_dropout, 'batchnorm': batchnorm, 'activation': activation, 'init': init } from tensorflow.python.framework.ops import disable_eager_execution disable_eager_execution() input_size = output_size = adata.n_vars net = AE_types[ae_type](input_size=input_size, output_size=output_size, **network_kwds) net.save() net.build() training_kwds = { **training_kwds, 'epochs': epochs, 'reduce_lr': reduce_lr, 'early_stop': early_stop, 'batch_size': batch_size, 'optimizer': optimizer, 'verbose': verbose, 'threads': threads, 'learning_rate': learning_rate } hist = train(adata[adata.obs.dca_split == 'train'], net, **training_kwds) res = net.predict(adata, mode, return_info, copy) adata = res if copy else adata if return_info: adata.uns['dca_loss_history'] = hist.history if return_model: return (adata, net) if copy else net else: return adata if copy else None