我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用tensorflow.python.ops.init_ops.random_uniform_initializer()。
def __call__(self, inputs, state, scope=None): with vs.variable_scope(scope or "eunn_cell"): state = _eunn_loop(state, self._capacity, self.diag_vec, self.off_vec, self.diag, self._fft) input_matrix_init = init_ops.random_uniform_initializer(-0.01, 0.01) if self._comp: input_matrix_re = vs.get_variable("U_re", [inputs.get_shape()[-1], self._hidden_size], initializer=input_matrix_init) input_matrix_im = vs.get_variable("U_im", [inputs.get_shape()[-1], self._hidden_size], initializer=input_matrix_init) inputs_re = math_ops.matmul(inputs, input_matrix_re) inputs_im = math_ops.matmul(inputs, input_matrix_im) inputs = math_ops.complex(inputs_re, inputs_im) else: input_matrix = vs.get_variable("U", [inputs.get_shape()[-1], self._hidden_size], initializer=input_matrix_init) inputs = math_ops.matmul(inputs, input_matrix) bias = vs.get_variable("modReLUBias", [self._hidden_size], initializer=init_ops.constant_initializer()) output = self._activation((inputs + state), bias, self._comp) return output, output
def __call__(self, inputs, state, scope=None): """Run the cell on embedded inputs.""" with vs.variable_scope(scope or type(self).__name__): # "EmbeddingWrapper" with ops.device("/cpu:0"): if self._embedding: embedding = self._embedding else: if self._initializer: initializer = self._initializer elif vs.get_variable_scope().initializer: initializer = vs.get_variable_scope().initializer else: # Default initializer for embeddings should have variance=1. sqrt3 = math.sqrt(3) # Uniform(-sqrt(3), sqrt(3)) has variance=1. initializer = init_ops.random_uniform_initializer(-sqrt3, sqrt3) embedding = vs.get_variable("embedding", [self._embedding_classes, self._cell.input_size], initializer=initializer) embedded = embedding_ops.embedding_lookup( embedding, array_ops.reshape(inputs, [-1])) return self._cell(embedded, state)
def __call__(self, inputs, state, scope=None): """Run the cell on embedded inputs.""" with vs.variable_scope(scope or type(self).__name__): # "EmbeddingWrapper" with ops.device("/cpu:0"): if self._initializer: initializer = self._initializer elif vs.get_variable_scope().initializer: initializer = vs.get_variable_scope().initializer else: # Default initializer for embeddings should have variance=1. sqrt3 = math.sqrt(3) # Uniform(-sqrt(3), sqrt(3)) has variance=1. initializer = init_ops.random_uniform_initializer(-sqrt3, sqrt3) if type(state) is tuple: data_type = state[0].dtype else: data_type = state.dtype embedding = vs.get_variable( "embedding", [self._embedding_classes, self._embedding_size], initializer=initializer, dtype=data_type) embedded = embedding_ops.embedding_lookup( embedding, array_ops.reshape(inputs, [-1])) return self._cell(embedded, state)
def testJITVariableSeed(self): """Test that the stateful initializer is not marked for compilation. XLA does not currently support seeded initialization and XLA initializers therefore return different values than non-XLA counterparts. Here we ensure that if we can disable JIT compilation for the initializers and get the same variable values as if no JIT compilation happened. """ def create_ops(): with variable_scope.variable_scope( "root", initializer=init_ops.random_uniform_initializer( -0.1, 0.1, seed=2)): inputs = variable_scope.get_variable("var", (1,)) return inputs _, v_false_1 = self.compute(False, create_ops) _, v_false_2 = self.compute(False, create_ops) _, v_true_1 = self.compute(enable_jit_nonstateful, create_ops) _, v_true_2 = self.compute(enable_jit_nonstateful, create_ops) self.assertAllClose(v_false_1, v_false_2) self.assertAllClose(v_true_1, v_true_2) self.assertAllClose(v_false_1, v_true_1)
def random_uniform_variable(shape, low, high, dtype=None, name=None, seed=None): """Instantiates a variable with values drawn from a uniform distribution. Arguments: shape: Tuple of integers, shape of returned Keras variable. low: Float, lower boundary of the output interval. high: Float, upper boundary of the output interval. dtype: String, dtype of returned Keras variable. name: String, name of returned Keras variable. seed: Integer, random seed. Returns: A Keras variable, filled with drawn samples. Example: ```python # TensorFlow example >>> kvar = K.random_uniform_variable((2,3), 0, 1) >>> kvar <tensorflow.python.ops.variables.Variable object at 0x10ab40b10> >>> K.eval(kvar) array([[ 0.10940075, 0.10047495, 0.476143 ], [ 0.66137183, 0.00869417, 0.89220798]], dtype=float32)
""" if dtype is None: dtype = floatx() shape = tuple(map(int, shape)) tf_dtype = _convert_string_dtype(dtype) if seed is None: # ensure that randomness is conditioned by the Numpy RNG seed = np.random.randint(10e8) value = init_ops.random_uniform_initializer( low, high, dtype=tf_dtype, seed=seed)(shape) return variable(value, dtype=dtype, name=name)
```
def __call__(self, inputs, state, scope=None): with vs.variable_scope(scope or "goru_cell"): U_init = init_ops.random_uniform_initializer(-0.01, 0.01) b_init = init_ops.constant_initializer(2.) mod_b_init = init_ops.constant_initializer(0.01) U = vs.get_variable("U", [inputs.get_shape()[-1], self._hidden_size * 3], dtype=tf.float32, initializer = U_init) Ux = math_ops.matmul(inputs, U) U_cx, U_rx, U_gx = array_ops.split(Ux, 3, axis=1) W_r = vs.get_variable("W_r", [self._hidden_size, self._hidden_size], dtype=tf.float32, initializer = U_init) W_g = vs.get_variable("W_g", [self._hidden_size, self._hidden_size], dtype=tf.float32, initializer = U_init) W_rh = math_ops.matmul(state, W_r) W_gh = math_ops.matmul(state, W_g) bias_r = vs.get_variable("bias_r", [self._hidden_size], dtype=tf.float32, initializer = b_init) bias_g = vs.get_variable("bias_g", [self._hidden_size], dtype=tf.float32) bias_c = vs.get_variable("bias_c", [self._hidden_size], dtype=tf.float32, initializer = mod_b_init) r_tmp = U_rx + W_rh + bias_r g_tmp = U_gx + W_gh + bias_g r = math_ops.sigmoid(r_tmp) g = math_ops.sigmoid(g_tmp) Unitaryh = _eunn_loop(state, self._capacity, self.diag_vec, self.off_vec, self.diag, self._fft) c = modrelu(math_ops.multiply(r, Unitaryh) + U_cx, bias_c, False) new_state = math_ops.multiply(g, state) + math_ops.multiply(1 - g, c) return new_state, new_state
def testJITCreateOpsLambda(self): """Test several ways of customizing the compilation attribute.""" def create_ops(): with variable_scope.variable_scope( "root", initializer=init_ops.random_uniform_initializer( -0.1, 0.1, seed=2)): inputs = random_ops.random_uniform((1,), seed=1) return inputs v_false_1_t, v_false_1 = self.compute(False, create_ops) _, v_false_2 = self.compute(False, create_ops) v_true_1_t, v_true_1 = self.compute(enable_jit_nonstateful, create_ops) _, v_true_2 = self.compute(enable_jit_nonstateful, create_ops) v_all_true_t, _ = self.compute(True, create_ops) self.assertEqual(False, v_false_1_t.op.get_attr("_XlaCompile")) v_true_1_t_sampler_op = v_true_1_t.graph.get_operation_by_name( "root/random_uniform/RandomUniform") v_all_true_t_sampler_op = v_all_true_t.graph.get_operation_by_name( "root/random_uniform/RandomUniform") self.assertEqual(False, v_true_1_t_sampler_op.get_attr("_XlaCompile")) self.assertEqual(True, v_all_true_t_sampler_op.get_attr("_XlaCompile")) self.assertEqual(True, v_true_1_t.op.get_attr("_XlaCompile")) self.assertEqual(True, v_all_true_t.op.get_attr("_XlaCompile")) # Additionally ensure that where no JIT compilation happens on the # random_uniform op, the output values are identical to the case # where no JIT compilation happens anywhere. self.assertAllClose(v_false_1, v_false_2) self.assertAllClose(v_true_1, v_true_2) self.assertAllClose(v_false_1, v_true_1)
def benchmarkTfRNNLSTMTraining(self): test_configs = self._GetTestConfig() for config_name, config in test_configs.items(): num_layers = config["num_layers"] num_units = config["num_units"] batch_size = config["batch_size"] seq_length = config["seq_length"] with ops.Graph().as_default(), ops.device("/gpu:0"): inputs = seq_length * [ array_ops.zeros([batch_size, num_units], dtypes.float32) ] initializer = init_ops.random_uniform_initializer(-0.01, 0.01, seed=127) cell = core_rnn_cell_impl.LSTMCell( num_units=num_units, initializer=initializer, state_is_tuple=True) multi_cell = core_rnn_cell_impl.MultiRNNCell( [cell() for _ in range(num_layers)]) outputs, final_state = core_rnn.static_rnn( multi_cell, inputs, dtype=dtypes.float32) trainable_variables = ops.get_collection( ops.GraphKeys.TRAINABLE_VARIABLES) gradients = gradients_impl.gradients([outputs, final_state], trainable_variables) training_op = control_flow_ops.group(*gradients) self._BenchmarkOp(training_op, "tf_rnn_lstm %s %s" % (config_name, self._GetConfigDesc(config)))